A platform for community discussion. Free, open, simple.
DEV: Refactor reports index into service (#31667)
Martin Brennan38920724a09b
diff --git a/app/assets/javascripts/admin/addon/services/admin-search-data-source.js b/app/assets/javascripts/admin/addon/services/admin-search-data-source.jsindex 37e4adab73d..8aba3f0862b 100644--- a/app/assets/javascripts/admin/addon/services/admin-search-data-source.js+++ b/app/assets/javascripts/admin/addon/services/admin-search-data-source.js@@ -65,11 +65,7 @@ export default class AdminSearchDataSource extends Service {const allItems = await ajax("/admin/search/all.json");this.#processSettings(allItems.settings);this.#processThemesAndComponents(allItems.themes_and_components);-- // TODO (martin) Move this to all.json after refactoring reports controller- // into a service.- const reportItems = await ajax("/admin/reports.json");- this.#processReports(reportItems.reports);+ this.#processReports(allItems.reports);await Promise.resolve();this._mapCached = true;diff --git a/app/controllers/admin/reports_controller.rb b/app/controllers/admin/reports_controller.rbindex c22e2697739..dcd100c2499 100644--- a/app/controllers/admin/reports_controller.rb+++ b/app/controllers/admin/reports_controller.rb@@ -3,70 +3,8 @@class Admin::ReportsController < Admin::StaffControllerREPORTS_LIMIT = 50- HIDDEN_PAGEVIEW_REPORTS = %w[site_traffic page_view_legacy_total_reqs]-- HIDDEN_LEGACY_PAGEVIEW_REPORTS = %w[- consolidated_page_views_browser_detection- page_view_anon_reqs- page_view_logged_in_reqs- ]-def index- page_view_req_report_methods =- ["page_view_total_reqs"] +- ApplicationRequest- .req_types- .keys- .select { |r| r =~ /\Apage_view_/ && r !~ /mobile/ }- .map { |r| r + "_reqs" }-- if !SiteSetting.use_legacy_pageviews- page_view_req_report_methods << "page_view_legacy_total_reqs"- end-- reports_methods =- page_view_req_report_methods +- Report.singleton_methods.grep(/\Areport_(?!about|storage_stats)/)-- reports =- reports_methods- .reduce([]) do |reports_acc, name|- type = name.to_s.gsub("report_", "")- description = I18n.t("reports.#{type}.description", default: "")- description_link = I18n.t("reports.#{type}.description_link", default: "")-- if SiteSetting.use_legacy_pageviews- next reports_acc if HIDDEN_PAGEVIEW_REPORTS.include?(type)- else- next reports_acc if HIDDEN_LEGACY_PAGEVIEW_REPORTS.include?(type)- end-- report_data = {- type: type,- title: I18n.t("reports.#{type}.title"),- description: description.presence ? description : nil,- description_link: description_link.presence ? description_link : nil,- }-- # HACK: We need to show a different label and description for this- # old report while people are still relying on it, that lets us- # point toward the new 'Site traffic' report as well. Not ideal,- # but apart from duplicating the report there's not a nicer way to do this.- if SiteSetting.use_legacy_pageviews- if type == "consolidated_page_views" ||- type === "consolidated_page_views_browser_detection"- report_data[:title] = I18n.t("reports.#{type}.title_legacy")- report_data[:description] = I18n.t("reports.#{type}.description_legacy")- end- end-- reports_acc << report_data-- reports_acc- end- .sort_by { |report| report[:title] }-- render_json_dump(reports: reports)+ render_json_dump(reports: Reports::ListQuery.call)enddef bulk@@ -80,12 +18,12 @@ class Admin::ReportsController < Admin::StaffControllerreport = Report.find_cached(report_type, args) if (report_params[:cache])if SiteSetting.use_legacy_pageviews- if HIDDEN_PAGEVIEW_REPORTS.include?(report_type)+ if Report::HIDDEN_PAGEVIEW_REPORTS.include?(report_type)report = Report._get(report_type, args)report.error = :not_foundendelse- if HIDDEN_LEGACY_PAGEVIEW_REPORTS.include?(report_type)+ if Report::HIDDEN_LEGACY_PAGEVIEW_REPORTS.include?(report_type)report = Report._get(report_type, args)report.error = :not_foundend@@ -117,9 +55,9 @@ class Admin::ReportsController < Admin::StaffControllerraise Discourse::NotFound unless report_type =~ /\A[a-z0-9\_]+\z/if SiteSetting.use_legacy_pageviews- raise Discourse::NotFound if HIDDEN_PAGEVIEW_REPORTS.include?(report_type)+ raise Discourse::NotFound if Report::HIDDEN_PAGEVIEW_REPORTS.include?(report_type)else- raise Discourse::NotFound if HIDDEN_LEGACY_PAGEVIEW_REPORTS.include?(report_type)+ raise Discourse::NotFound if Report::HIDDEN_LEGACY_PAGEVIEW_REPORTS.include?(report_type)endargs = parse_params(params)diff --git a/app/controllers/admin/search_controller.rb b/app/controllers/admin/search_controller.rbindex 7e18f892b8a..dd267c5dc71 100644--- a/app/controllers/admin/search_controller.rb+++ b/app/controllers/admin/search_controller.rb@@ -4,8 +4,6 @@ class Admin::SearchController < Admin::AdminControllerRESULT_TYPES = %w[page setting theme component report].freezedef index- # TODO (martin) Include reports here too, need to refact- # the reports controller into a reusable lookup service first.respond_to do |format|format.json dorender_json_dump(@@ -20,6 +18,7 @@ class Admin::SearchController < Admin::AdminController),themes_and_components:serialize_data(Theme.include_relations.order(:name), BasicThemeSerializer),+ reports: Reports::ListQuery.call,)enddiff --git a/app/models/report.rb b/app/models/report.rbindex 57848046227..f766cee2e86 100644--- a/app/models/report.rb+++ b/app/models/report.rb@@ -27,6 +27,14 @@ class Reportstorage_stats: :storage_stats,}+ HIDDEN_PAGEVIEW_REPORTS = %w[site_traffic page_view_legacy_total_reqs]++ HIDDEN_LEGACY_PAGEVIEW_REPORTS = %w[+ consolidated_page_views_browser_detection+ page_view_anon_reqs+ page_view_logged_in_reqs+ ]+include Reports::Bookmarksinclude Reports::ConsolidatedApiRequestsinclude Reports::ConsolidatedPageViewsdiff --git a/app/queries/reports/list_query.rb b/app/queries/reports/list_query.rbnew file mode 100644index 00000000000..981983fc490--- /dev/null+++ b/app/queries/reports/list_query.rb@@ -0,0 +1,72 @@+# frozen_string_literal: true++module Reports+ class ListQuery+ class FormattedReport+ attr_reader :type, :name++ def initialize(name)+ @name = name+ @type = name.to_s.gsub("report_", "")+ end++ def to_h+ return if skip_report?+ {+ type:,+ title:,+ description:,+ description_link: I18n.t("reports.#{type}.description_link", default: "").presence,+ }+ end++ private++ def skip_report?+ (SiteSetting.use_legacy_pageviews && type.in?(Report::HIDDEN_PAGEVIEW_REPORTS)) ||+ (!SiteSetting.use_legacy_pageviews && type.in?(Report::HIDDEN_LEGACY_PAGEVIEW_REPORTS))+ end++ # HACK: We need to show a different label and description for some+ # old reports while people are still relying on them, that lets us+ # point toward the new 'Site traffic' report as well. Not ideal,+ # but apart from duplicating the report there's not a nicer way to do this.+ def title+ return I18n.t("reports.#{type}.title_legacy") if legacy?+ I18n.t("reports.#{type}.title")+ end++ def description+ return I18n.t("reports.#{type}.description_legacy") if legacy?+ I18n.t("reports.#{type}.description", default: "").presence+ end++ def legacy?+ SiteSetting.use_legacy_pageviews &&+ type.in?(%w[consolidated_page_views consolidated_page_views_browser_detection])+ end+ end++ def self.call+ page_view_req_report_methods =+ ["page_view_total_reqs"] ++ ApplicationRequest+ .req_types+ .keys+ .select { |r| r =~ /\Apage_view_/ && r !~ /mobile/ }+ .map { |r| r + "_reqs" }++ if !SiteSetting.use_legacy_pageviews+ page_view_req_report_methods << "page_view_legacy_total_reqs"+ end++ reports_methods =+ page_view_req_report_methods ++ Report.singleton_methods.grep(/\Areport_(?!about|storage_stats)/)++ reports_methods+ .filter_map { |report_name| Reports::ListQuery::FormattedReport.new(report_name).to_h }+ .sort_by { |report| report[:title] }+ end+ end+enddiff --git a/spec/queries/reports/list_query_spec.rb b/spec/queries/reports/list_query_spec.rbnew file mode 100644index 00000000000..42ff1159841--- /dev/null+++ b/spec/queries/reports/list_query_spec.rb@@ -0,0 +1,87 @@+# frozen_string_literal: true++RSpec.describe Reports::ListQuery do+ describe ".call" do+ subject(:result) { described_class.call }++ fab!(:current_user) { Fabricate(:admin) }++ let(:result_page_view_report_types) do+ result.filter { |r| r[:type].starts_with?("page_view") }.map { |r| r[:type] }+ end++ it "does not include the about or storage_stats reports" do+ expect(result.map { |r| r[:type] }).not_to include("about", "storage_stats")+ end++ it "does not include any mobile versions of page_view reports" do+ expect(result_page_view_report_types).not_to include(+ "page_view_logged_in_mobile_reqs",+ "page_view_anon_mobile_reqs",+ "page_view_anon_browser_mobile_reqs",+ "page_view_logged_in_browser_mobile_reqs",+ )+ end++ it "sorts reports by title" do+ expect(result.map { |r| r[:title] }[0..4]).to eq(+ [+ I18n.t("reports.staff_logins.title"),+ I18n.t("reports.page_view_anon_browser_reqs.title"),+ I18n.t("reports.bookmarks.title"),+ I18n.t("reports.consolidated_api_requests.title"),+ I18n.t("reports.dau_by_mau.title"),+ ],+ )+ end++ context "when using legacy pageviews" do+ before { SiteSetting.use_legacy_pageviews = true }++ it "includes all of the correct page view reports in the result" do+ expect(result_page_view_report_types).to match_array(+ %w[+ page_view_total_reqs+ page_view_crawler_reqs+ page_view_logged_in_reqs+ page_view_anon_reqs+ page_view_anon_browser_reqs+ page_view_logged_in_browser_reqs+ ],+ )+ end++ it "changes the title and description for consolidated_page_views and consolidated_page_views_browser_detection reports" do+ consolidated_page_views = result.find { |r| r[:type] == "consolidated_page_views" }+ consolidated_page_views_browser_detection =+ result.find { |r| r[:type] == "consolidated_page_views_browser_detection" }++ expect(consolidated_page_views).to include(+ title: I18n.t("reports.consolidated_page_views.title_legacy"),+ description: I18n.t("reports.consolidated_page_views.description_legacy"),+ )+ expect(consolidated_page_views_browser_detection).to include(+ title: I18n.t("reports.consolidated_page_views_browser_detection.title_legacy"),+ description:+ I18n.t("reports.consolidated_page_views_browser_detection.description_legacy"),+ )+ end+ end++ context "when not using legacy pageviews" do+ before { SiteSetting.use_legacy_pageviews = false }++ it "includes all of the correct page view reports in the result" do+ expect(result_page_view_report_types).to match_array(+ %w[+ page_view_total_reqs+ page_view_crawler_reqs+ page_view_anon_browser_reqs+ page_view_logged_in_browser_reqs+ page_view_legacy_total_reqs+ ],+ )+ end+ end+ end+enddiff --git a/spec/requests/admin/reports_controller_spec.rb b/spec/requests/admin/reports_controller_spec.rbindex e0965382057..ec0cd44e17e 100644--- a/spec/requests/admin/reports_controller_spec.rb+++ b/spec/requests/admin/reports_controller_spec.rb@@ -33,7 +33,7 @@ RSpec.describe Admin::ReportsController doget "/admin/reports.json"expect(response.parsed_body["reports"].map { |r| r[:type] }).not_to include("site_traffic")expect(response.parsed_body["reports"].map { |r| r[:type] }).to include(- *Admin::ReportsController::HIDDEN_LEGACY_PAGEVIEW_REPORTS,+ *Report::HIDDEN_LEGACY_PAGEVIEW_REPORTS,)endend@@ -45,7 +45,7 @@ RSpec.describe Admin::ReportsController doget "/admin/reports.json"expect(response.parsed_body["reports"].map { |r| r[:type] }).to include("site_traffic")expect(response.parsed_body["reports"].map { |r| r[:type] }).not_to include(- *Admin::ReportsController::HIDDEN_LEGACY_PAGEVIEW_REPORTS,+ *Report::HIDDEN_LEGACY_PAGEVIEW_REPORTS,)endend@@ -385,7 +385,7 @@ RSpec.describe Admin::ReportsController doendit "does not allow running site_traffic report" do- Admin::ReportsController::HIDDEN_PAGEVIEW_REPORTS.each do |report_type|+ Report::HIDDEN_PAGEVIEW_REPORTS.each do |report_type|get "/admin/reports/#{report_type}.json"expect(response.status).to eq(404)expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))@@ -405,7 +405,7 @@ RSpec.describe Admin::ReportsController doendit "does not allow running legacy pageview reports" do- Admin::ReportsController::HIDDEN_LEGACY_PAGEVIEW_REPORTS.each do |report_type|+ Report::HIDDEN_LEGACY_PAGEVIEW_REPORTS.each do |report_type|get "/admin/reports/#{report_type}.json"expect(response.status).to eq(404)expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))