diff --git a/app/admin/ahoy_dashboard.rb b/app/admin/ahoy_dashboard.rb index bb3242f..f843088 100644 --- a/app/admin/ahoy_dashboard.rb +++ b/app/admin/ahoy_dashboard.rb @@ -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 } diff --git a/app/models/city.rb b/app/models/city.rb index 3477578..00de946 100644 --- a/app/models/city.rb +++ b/app/models/city.rb @@ -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