From adb671e668cc0fb06e765493ba1058bb90d7d614 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Mon, 27 Jan 2025 00:48:07 +0800 Subject: [PATCH] feat: add least popular active cities panel - Introduce new panel in the admin dashboard for displaying the least popular active cities. - Implement a database scope `least_popular_active` to retrieve cities based on their view counts. - Ensure compatibility with both SQLite and PostgreSQL for fetching city view data. This addition enhances the admin dashboard by allowing administrators to easily identify and manage cities that are receiving less user interaction, thus aiding in strategic planning for engagement and promotion. --- app/admin/dashboard.rb | 10 ++++++++++ app/models/city.rb | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) 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