feat: enhance view count method for city

- Update view_count method to accept different time periods
- Modify ahoy_dashboard to display views per day, week, and year

These changes provide a more granular view of city visits, allowing for
better analytics and insights on user engagement over time.
This commit is contained in:
songtianlun 2025-02-07 17:32:49 +08:00
parent 9865c18d32
commit 9ee8ed0d10
2 changed files with 29 additions and 7 deletions

View File

@ -44,7 +44,7 @@ ActiveAdmin.register_page "Ahoy Dashboard" do
table_for City.least_popular_active do table_for City.least_popular_active do
column("ID") { |city| city.id } column("ID") { |city| city.id }
column("城市") { |city| link_to(city.name, admin_city_path(city)) } column("城市") { |city| link_to(city.name, admin_city_path(city)) }
column("访问") { |city| city.view_count } column("访问(日/周/年)") { |city| "#{city.view_count(:day)}/#{city.view_count(:week)}/#{city.view_count(:year)}" }
column("状态") { |city| status_tag(city.active? ? "活跃" : "停用") } column("状态") { |city| status_tag(city.active? ? "活跃" : "停用") }
column("所属区域") { |city| city.country.name+"/"+city.country.region.name } column("所属区域") { |city| city.country.name+"/"+city.country.region.name }
column("图像个数") { |city| city.weather_arts.count } column("图像个数") { |city| city.weather_arts.count }
@ -66,7 +66,7 @@ ActiveAdmin.register_page "Ahoy Dashboard" do
table_for City.most_popular_inactive do table_for City.most_popular_inactive do
column("ID") { |city| city.id } column("ID") { |city| city.id }
column("城市") { |city| link_to(city.name, admin_city_path(city)) } column("城市") { |city| link_to(city.name, admin_city_path(city)) }
column("访问") { |city| city.view_count } column("访问(日/周/年)") { |city| "#{city.view_count(:day)}/#{city.view_count(:week)}/#{city.view_count(:year)}" }
column("状态") { |city| status_tag(city.active? ? "活跃" : "停用") } column("状态") { |city| status_tag(city.active? ? "活跃" : "停用") }
column("所属区域") { |city| city.country.name+"/"+city.country.region.name } column("所属区域") { |city| city.country.name+"/"+city.country.region.name }
column("图像个数") { |city| city.weather_arts.count } column("图像个数") { |city| city.weather_arts.count }

View File

@ -122,11 +122,33 @@ class City < ApplicationRecord
weather_arts.order(weather_date: :desc).first weather_arts.order(weather_date: :desc).first
end end
def view_count def view_count(period = :day)
if ActiveRecord::Base.connection.adapter_name.downcase == "sqlite" start_time = case period
Ahoy::Event.where("json_extract(properties, '$.event_type') = 'city_view' AND json_extract(properties, '$.city_id') = ?", self.id).count when :day
1.day.ago
when :week
7.days.ago
when :month
1.month.ago
when :year
1.year.ago
else else
Ahoy::Event.where("properties::jsonb->>'event_type' = 'city_view' AND (properties::jsonb->>'city_id')::integer = ?", self.id).count 1.year.ago
end
if ActiveRecord::Base.connection.adapter_name.downcase == "sqlite"
# Ahoy::Event.where("json_extract(properties, '$.event_type') = 'city_view' AND json_extract(properties, '$.city_id') = ?", self.id).count
Ahoy::Event
.where("time >= ?", start_time)
.where("json_extract(properties, '$.event_type') = 'city_view' AND json_extract(properties, '$.city_id') = ?",
self.id)
.count
else
# Ahoy::Event.where("properties::jsonb->>'event_type' = 'city_view' AND (properties::jsonb->>'city_id')::integer = ?", self.id).count
Ahoy::Event
.where("time >= ?", start_time)
.where("properties::jsonb->>'event_type' = 'city_view' AND (properties::jsonb->>'city_id')::integer = ?",
self.id)
.count
end end
end end
end end