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:
parent
9865c18d32
commit
9ee8ed0d10
@ -44,7 +44,7 @@ ActiveAdmin.register_page "Ahoy Dashboard" do
|
||||
table_for City.least_popular_active do
|
||||
column("ID") { |city| city.id }
|
||||
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| city.country.name+"/"+city.country.region.name }
|
||||
column("图像个数") { |city| city.weather_arts.count }
|
||||
@ -66,7 +66,7 @@ ActiveAdmin.register_page "Ahoy Dashboard" do
|
||||
table_for City.most_popular_inactive do
|
||||
column("ID") { |city| city.id }
|
||||
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| city.country.name+"/"+city.country.region.name }
|
||||
column("图像个数") { |city| city.weather_arts.count }
|
||||
|
@ -122,11 +122,33 @@ class City < ApplicationRecord
|
||||
weather_arts.order(weather_date: :desc).first
|
||||
end
|
||||
|
||||
def view_count
|
||||
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
|
||||
def view_count(period = :day)
|
||||
start_time = case period
|
||||
when :day
|
||||
1.day.ago
|
||||
when :week
|
||||
7.days.ago
|
||||
when :month
|
||||
1.month.ago
|
||||
when :year
|
||||
1.year.ago
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user