diff --git a/app/admin/dashboard.rb b/app/admin/dashboard.rb index 7e8284e..7a6d65c 100644 --- a/app/admin/dashboard.rb +++ b/app/admin/dashboard.rb @@ -37,6 +37,16 @@ ActiveAdmin.register_page "Dashboard" do end end end + + column do + panel "冷门活跃城市" do + table_for City.least_popular_active.limit(10) do + column("城市") { |city| link_to(city.name, admin_city_path(city)) } + column("访问量") { |city| city.view_count } + # column("状态") { |city| status_tag(city.active? ? "活跃" : "停用") } + end + end + end end # 添加一个事件列表面板 diff --git a/app/models/city.rb b/app/models/city.rb index 6771ba4..6c1e6f2 100644 --- a/app/models/city.rb +++ b/app/models/city.rb @@ -37,6 +37,24 @@ class City < ApplicationRecord end } + scope :least_popular_active, -> { + if ActiveRecord::Base.connection.adapter_name.downcase == 'sqlite' + active + .joins("LEFT JOIN ahoy_events ON json_extract(ahoy_events.properties, '$.city_id') = cities.id + AND json_extract(ahoy_events.properties, '$.event_type') = 'city_view'") + .group("cities.id") + .select("cities.*, COUNT(ahoy_events.id) as visit_count") + .order("visit_count ASC, cities.name ASC") + else + active + .joins("LEFT JOIN ahoy_events ON (ahoy_events.properties->>'city_id')::integer = cities.id + AND ahoy_events.properties->>'event_type' = 'city_view'") + .group("cities.id") + .select("cities.*, COUNT(ahoy_events.id) as visit_count") + .order("visit_count ASC, cities.name ASC") + end + } + def to_s name end