A platform for community discussion. Free, open, simple.
DEV: Let plugins exclude reports from the customisable dashboard (#40463)
Osama Sayegh2e8c44eb088c
diff --git a/app/models/report.rb b/app/models/report.rbindex df1b061a032..80f074d670f 100644--- a/app/models/report.rb+++ b/app/models/report.rb@@ -320,13 +320,21 @@ class Reportendend- def Report.add_report(name, &block)+ def Report.add_report(name, exclude_from_dashboard: false, &block)singleton_class.instance_eval { define_method("report_#{name}", &block) }+ dashboard_excluded_report_types << name.to_s if exclude_from_dashboardend# Only used for testing.def Report.remove_report(name)singleton_class.instance_eval { remove_method("report_#{name}") }+ dashboard_excluded_report_types.delete(name.to_s)+ end++ # Report types a plugin has marked as not mountable on the customisable+ # admin dashboard. They remain available on the regular reports page.+ def Report.dashboard_excluded_report_types+ @dashboard_excluded_report_types ||= Set.newenddef self._get(type, opts = nil)diff --git a/lib/admin_dashboard/reports/core_report_provider.rb b/lib/admin_dashboard/reports/core_report_provider.rbindex bdc5f031ac7..ffe56d09672 100644--- a/lib/admin_dashboard/reports/core_report_provider.rb+++ b/lib/admin_dashboard/reports/core_report_provider.rb@@ -20,10 +20,7 @@ module AdminDashboardreturn {} if guardian.nil?index =- ::Reports::ListQuery- .call(guardian: guardian)- .map { |entry| build_resolved(entry) }- .index_by(&:identifier)+ dashboard_entries(guardian).map { |entry| build_resolved(entry) }.index_by(&:identifier)identifiers.each_with_object({}) do |identifier, hash|key = identifier.to_sresolved = index[key]@@ -59,11 +56,18 @@ module AdminDashboardprivate_class_method :with_empty_flagdef self.list_all(search: nil, after: nil, limit: nil)- entries = ::Reports::ListQuery.call(guardian: Guardian.new(Discourse.system_user))+ entries = dashboard_entries(Guardian.new(Discourse.system_user))entries = filter_by_search(entries, search) if search.present?seek(entries.map { |entry| build_resolved(entry) }, after: after, limit: limit)end+ def self.dashboard_entries(guardian)+ ::Reports::ListQuery+ .call(guardian: guardian)+ .reject { |entry| ::Report.dashboard_excluded_report_types.include?(entry[:type]) }+ end+ private_class_method :dashboard_entries+def self.build_resolved(entry)AdminDashboard::Reports::ResolvedReport.new(source: SOURCE_NAME,diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rbindex 3ea87917a5a..3262a142ca7 100644--- a/lib/plugin/instance.rb+++ b/lib/plugin/instance.rb@@ -250,8 +250,8 @@ class Plugin::Instanceend# Applies to all sites in a multisite environment. Ignores plugin.enabled?- def add_report(name, &block)- reloadable_patch { |plugin| Report.add_report(name, &block) }+ def add_report(name, exclude_from_dashboard: false, &block)+ reloadable_patch { |plugin| Report.add_report(name, exclude_from_dashboard:, &block) }end# Applies to all sites in a multisite environment. Ignores plugin.enabled?diff --git a/plugins/discourse-ai/lib/sentiment/emotion_dashboard_report.rb b/plugins/discourse-ai/lib/sentiment/emotion_dashboard_report.rbindex 10bff1a6e65..1ec0e7b2158 100644--- a/plugins/discourse-ai/lib/sentiment/emotion_dashboard_report.rb+++ b/plugins/discourse-ai/lib/sentiment/emotion_dashboard_report.rb@@ -5,7 +5,7 @@ module DiscourseAiclass EmotionDashboardReportdef self.register!(plugin)Emotions::LIST.each do |emotion|- plugin.add_report("emotion_#{emotion}") do |report|+ plugin.add_report("emotion_#{emotion}", exclude_from_dashboard: true) do |report|query_results = DiscourseAi::Sentiment::EmotionDashboardReport.fetch_data(report)report.data = query_results.map { |row| { x: row.day, y: row.send(emotion) } }if report.facets.include?(:prev_period) && query_results.length > 30diff --git a/plugins/discourse-ai/spec/lib/modules/sentiment/entry_point_spec.rb b/plugins/discourse-ai/spec/lib/modules/sentiment/entry_point_spec.rbindex 628e353911b..f7204b84c41 100644--- a/plugins/discourse-ai/spec/lib/modules/sentiment/entry_point_spec.rb+++ b/plugins/discourse-ai/spec/lib/modules/sentiment/entry_point_spec.rb@@ -7,6 +7,19 @@ RSpec.describe DiscourseAi::Sentiment::EntryPoint dobefore { enable_current_plugin }+ describe "admin dashboard report registration" do+ it "excludes the per-emotion reports while keeping the sentiment summaries" do+ DiscourseAi::Sentiment::Emotions::LIST.each do |emotion|+ expect(Report.dashboard_excluded_report_types).to include("emotion_#{emotion}")+ end++ expect(Report.dashboard_excluded_report_types).not_to include(+ "overall_sentiment",+ "sentiment_analysis",+ )+ end+ end+describe "registering event callbacks" docontext "when editing a post" dofab!(:post) { Fabricate(:post, user: user) }diff --git a/spec/lib/admin_dashboard/reports/core_report_provider_spec.rb b/spec/lib/admin_dashboard/reports/core_report_provider_spec.rbindex 9c926f40674..ae038800e92 100644--- a/spec/lib/admin_dashboard/reports/core_report_provider_spec.rb+++ b/spec/lib/admin_dashboard/reports/core_report_provider_spec.rb@@ -82,6 +82,17 @@ RSpec.describe AdminDashboard::Reports::CoreReportProvider doendend+ describe "reports excluded from the dashboard" do+ after { Report.dashboard_excluded_report_types.delete("signups") }++ it "omits them from both list_all and resolve_many" do+ Report.dashboard_excluded_report_types << "signups"++ expect(described_class.list_all.map(&:identifier)).not_to include("signups")+ expect(described_class.resolve_many(%w[signups], guardian: guardian)).to be_empty+ end+ end+describe ".fetch_many" doit "returns report payloads keyed by identifier" doresult = described_class.fetch_many(%w[signups], guardian: guardian, filters: {})diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rbindex 150099a3f4f..b19ccd38caf 100644--- a/spec/models/report_spec.rb+++ b/spec/models/report_spec.rb@@ -1,6 +1,20 @@# frozen_string_literal: trueRSpec.describe Report do+ describe ".add_report" do+ after { Report.remove_report("my_custom_report") }++ it "records report types flagged as excluded from the dashboard" do+ Report.add_report("my_custom_report", exclude_from_dashboard: true) { |report| }+ expect(Report.dashboard_excluded_report_types).to include("my_custom_report")+ end++ it "does not record report types by default" do+ Report.add_report("my_custom_report") { |report| }+ expect(Report.dashboard_excluded_report_types).not_to include("my_custom_report")+ end+ end+let(:user) { Fabricate(:user) }let(:category_1) { Fabricate(:category, user: user) }let(:category_2) { Fabricate(:category, parent_category: category_1, user: user) } # id: 2