A platform for community discussion. Free, open, simple.
FEATURE: Switch to new methods of pageview measurement and reporting (#28729)
Martin Brennan14b436923c5b
diff --git a/app/assets/javascripts/admin/addon/components/admin-report-stacked-chart.gjs b/app/assets/javascripts/admin/addon/components/admin-report-stacked-chart.gjsindex 8a7e2df0973..fb04e15790d 100644--- a/app/assets/javascripts/admin/addon/components/admin-report-stacked-chart.gjs+++ b/app/assets/javascripts/admin/addon/components/admin-report-stacked-chart.gjs@@ -8,10 +8,14 @@ export default class AdminReportStackedChart extends Component {get chartConfig() {const { model } = this.args;+ const options = this.args.options || {};+ options.hiddenLabels ??= [];+const chartData = makeArray(model.chartData || model.data).map((cd) => ({label: cd.label,color: cd.color,data: Report.collapse(model, cd.data),+ req: cd.req,}));const data = {@@ -21,6 +25,7 @@ export default class AdminReportStackedChart extends Component {stack: "pageviews-stack",data: cd.data,backgroundColor: cd.color,+ hidden: options.hiddenLabels.includes(cd.req),})),};diff --git a/app/assets/javascripts/admin/addon/components/admin-report.hbs b/app/assets/javascripts/admin/addon/components/admin-report.hbsindex 65aea850595..f13e0c7d6a2 100644--- a/app/assets/javascripts/admin/addon/components/admin-report.hbs+++ b/app/assets/javascripts/admin/addon/components/admin-report.hbs@@ -7,7 +7,7 @@<ul class="breadcrumb">{{#if this.showAllReportsLink}}<li class="item all-reports">- <LinkTo @route="admin.dashboardReports" class="report-url">+ <LinkTo @route="adminReports.index" class="report-url">{{i18n "admin.dashboard.all_reports"}}</LinkTo></li>diff --git a/app/assets/javascripts/admin/addon/components/admin-report.js b/app/assets/javascripts/admin/addon/components/admin-report.jsindex 602f0cb6035..93175decfa6 100644--- a/app/assets/javascripts/admin/addon/components/admin-report.js+++ b/app/assets/javascripts/admin/addon/components/admin-report.js@@ -391,6 +391,8 @@ export default class AdminReport extends Component {Report.groupingForDatapoints(report.chartData.length),}));+ } else if (mode === "stacked-chart") {+ return this.get("reportOptions.stackedChart") || {};}}diff --git a/app/assets/javascripts/admin/addon/controllers/admin-dashboard-general.js b/app/assets/javascripts/admin/addon/controllers/admin-dashboard-general.jsindex f173af481a1..b4dff539625 100644--- a/app/assets/javascripts/admin/addon/controllers/admin-dashboard-general.js+++ b/app/assets/javascripts/admin/addon/controllers/admin-dashboard-general.js@@ -61,6 +61,7 @@ export default class AdminDashboardGeneralController extends AdminDashboardTabCoget isCommunityHealthVisible() {return ["consolidated_page_views",+ "site_traffic","signups","topics","posts",@@ -101,6 +102,13 @@ export default class AdminDashboardGeneralController extends AdminDashboardTabCo};}+ @discourseComputed+ siteTrafficOptions() {+ return {+ stackedChart: { hiddenLabels: ["page_view_other", "page_view_crawler"] },+ };+ }+@discourseComputedtopReferredTopicsFilters() {return {diff --git a/app/assets/javascripts/admin/addon/templates/dashboard_general.hbs b/app/assets/javascripts/admin/addon/templates/dashboard_general.hbsindex 702bed7c1c0..2a22ebbafff 100644--- a/app/assets/javascripts/admin/addon/templates/dashboard_general.hbs+++ b/app/assets/javascripts/admin/addon/templates/dashboard_general.hbs@@ -22,11 +22,20 @@<div class="section-body"><div class="charts">- <AdminReport- @dataSourceName="consolidated_page_views"- @forcedModes="stacked-chart"- @filters={{this.filters}}- />+ {{#if this.siteSettings.use_legacy_pageviews}}+ <AdminReport+ @dataSourceName="consolidated_page_views"+ @forcedModes="stacked-chart"+ @filters={{this.filters}}+ />+ {{else}}+ <AdminReport+ @dataSourceName="site_traffic"+ @forcedModes="stacked-chart"+ @reportOptions={{this.siteTrafficOptions}}+ @filters={{this.filters}}+ />+ {{/if}}<AdminReport@dataSourceName="signups"diff --git a/app/assets/stylesheets/common/admin/dashboard.scss b/app/assets/stylesheets/common/admin/dashboard.scssindex 2971d24bf25..f964644ed59 100644--- a/app/assets/stylesheets/common/admin/dashboard.scss+++ b/app/assets/stylesheets/common/admin/dashboard.scss@@ -145,7 +145,8 @@.admin-report {grid-column: span 4;- &.consolidated-page-views {+ &.consolidated-page-views,+ &.site-traffic {grid-column: span 12;}}diff --git a/app/controllers/admin/reports_controller.rb b/app/controllers/admin/reports_controller.rbindex ab49a29aa9d..ea3c0a6cd5b 100644--- a/app/controllers/admin/reports_controller.rb+++ b/app/controllers/admin/reports_controller.rb@@ -3,31 +3,53 @@class Admin::ReportsController < Admin::StaffControllerREPORTS_LIMIT = 50+ HIDDEN_PAGEVIEW_REPORTS = ["site_traffic"]++ HIDDEN_LEGACY_PAGEVIEW_REPORTS = %w[+ consolidated_page_views+ consolidated_page_views_browser_detection+ page_view_anon_reqs+ page_view_logged_in_reqs+ ]+def index- reports_methods =+ page_view_req_report_methods =["page_view_total_reqs"] +ApplicationRequest.req_types.keys.select { |r| r =~ /\Apage_view_/ && r !~ /mobile/ }- .map { |r| r + "_reqs" } ++ .map { |r| r + "_reqs" }++ reports_methods =+ page_view_req_report_methods +Report.singleton_methods.grep(/\Areport_(?!about|storage_stats)/)reports =- reports_methods.map do |name|- type = name.to_s.gsub("report_", "")- description = I18n.t("reports.#{type}.description", default: "")- description_link = I18n.t("reports.#{type}.description_link", default: "")-- {- type: type,- title: I18n.t("reports.#{type}.title"),- description: description.presence ? description : nil,- description_link: description_link.presence ? description_link : nil,- }- end+ 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++ reports_acc << {+ type: type,+ title: I18n.t("reports.#{type}.title"),+ description: description.presence ? description : nil,+ description_link: description_link.presence ? description_link : nil,+ }++ reports_acc+ end+ .sort_by { |report| report[:title] }- render_json_dump(reports: reports.sort_by { |report| report[:title] })+ render_json_dump(reports: reports)enddef bulk@@ -40,6 +62,18 @@ class Admin::ReportsController < Admin::StaffControllerreport = nilreport = Report.find_cached(report_type, args) if (report_params[:cache])+ if SiteSetting.use_legacy_pageviews+ if HIDDEN_PAGEVIEW_REPORTS.include?(report_type)+ report = Report._get(report_type, args)+ report.error = :not_found+ end+ else+ if HIDDEN_LEGACY_PAGEVIEW_REPORTS.include?(report_type)+ report = Report._get(report_type, args)+ report.error = :not_found+ end+ end+if reportreports << reportelse@@ -65,6 +99,12 @@ 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)+ else+ raise Discourse::NotFound if HIDDEN_LEGACY_PAGEVIEW_REPORTS.include?(report_type)+ end+args = parse_params(params)report = nildiff --git a/app/models/concerns/reports/consolidated_page_views.rb b/app/models/concerns/reports/consolidated_page_views.rbindex dcbd9d68607..d73f764c93a 100644--- a/app/models/concerns/reports/consolidated_page_views.rb+++ b/app/models/concerns/reports/consolidated_page_views.rb@@ -4,6 +4,8 @@ module Reports::ConsolidatedPageViewsextend ActiveSupport::Concernclass_methods do+ # NOTE: This report is deprecated, once use_legacy_pageviews is+ # always false or no longer needed we can delete this.def report_consolidated_page_views(report)filters = %w[page_view_logged_in page_view_anon page_view_crawler]diff --git a/app/models/concerns/reports/consolidated_page_views_browser_detection.rb b/app/models/concerns/reports/consolidated_page_views_browser_detection.rbindex 89e0a2e3cc8..96780fc9027 100644--- a/app/models/concerns/reports/consolidated_page_views_browser_detection.rb+++ b/app/models/concerns/reports/consolidated_page_views_browser_detection.rb@@ -4,72 +4,12 @@ module Reports::ConsolidatedPageViewsBrowserDetectionextend ActiveSupport::Concernclass_methods do+ # NOTE: This report is deprecated, once use_legacy_pageviews is+ # always false or no longer needed we can delete this.+ #+ # The new version of this report is site_traffic.def report_consolidated_page_views_browser_detection(report)- report.modes = [:stacked_chart]-- data =- DB.query(- <<~SQL,- SELECT- date,- SUM(CASE WHEN req_type = :page_view_logged_in_browser THEN count ELSE 0 END) AS page_view_logged_in_browser,- SUM(CASE WHEN req_type = :page_view_anon_browser THEN count ELSE 0 END) AS page_view_anon_browser,- SUM(CASE WHEN req_type = :page_view_crawler THEN count ELSE 0 END) AS page_view_crawler,- SUM(- CASE WHEN req_type = :page_view_anon THEN count- WHEN req_type = :page_view_logged_in THEN count- WHEN req_type = :page_view_anon_browser THEN -count- WHEN req_type = :page_view_logged_in_browser THEN -count- ELSE 0- END- ) AS page_view_other- FROM application_requests- WHERE date >= :start_date AND date <= :end_date- GROUP BY date- ORDER BY date ASC- SQL- start_date: report.start_date,- end_date: report.end_date,- page_view_anon: ApplicationRequest.req_types[:page_view_anon],- page_view_crawler: ApplicationRequest.req_types[:page_view_crawler],- page_view_logged_in: ApplicationRequest.req_types[:page_view_logged_in],- page_view_anon_browser: ApplicationRequest.req_types[:page_view_anon_browser],- page_view_logged_in_browser: ApplicationRequest.req_types[:page_view_logged_in_browser],- )-- report.data = [- {- req: "page_view_logged_in_browser",- label:- I18n.t(- "reports.consolidated_page_views_browser_detection.xaxis.page_view_logged_in_browser",- ),- color: report.colors[:turquoise],- data: data.map { |row| { x: row.date, y: row.page_view_logged_in_browser } },- },- {- req: "page_view_anon_browser",- label:- I18n.t(- "reports.consolidated_page_views_browser_detection.xaxis.page_view_anon_browser",- ),- color: report.colors[:lime],- data: data.map { |row| { x: row.date, y: row.page_view_anon_browser } },- },- {- req: "page_view_crawler",- label:- I18n.t("reports.consolidated_page_views_browser_detection.xaxis.page_view_crawler"),- color: report.colors[:purple],- data: data.map { |row| { x: row.date, y: row.page_view_crawler } },- },- {- req: "page_view_other",- label: I18n.t("reports.consolidated_page_views_browser_detection.xaxis.page_view_other"),- color: report.colors[:magenta],- data: data.map { |row| { x: row.date, y: row.page_view_other } },- },- ]+ Report.report_site_traffic(report)endendenddiff --git a/app/models/concerns/reports/site_traffic.rb b/app/models/concerns/reports/site_traffic.rbnew file mode 100644index 00000000000..b77c977f0a0--- /dev/null+++ b/app/models/concerns/reports/site_traffic.rb@@ -0,0 +1,80 @@+# frozen_string_literal: true++module Reports::SiteTraffic+ extend ActiveSupport::Concern++ class_methods do+ def report_site_traffic(report)+ report.modes = [:stacked_chart]++ first_browser_pageview_date =+ DB.query_single(+ <<~SQL,+ SELECT date FROM application_requests+ WHERE req_type = :page_view_logged_in_browser OR req_type = :page_view_anon_browser ORDER BY date LIMIT 1+ SQL+ page_view_logged_in_browser: ApplicationRequest.req_types[:page_view_logged_in_browser],+ page_view_anon_browser: ApplicationRequest.req_types[:page_view_anon_browser],+ ).first++ data =+ DB.query(+ <<~SQL,+ SELECT+ date,+ SUM(CASE WHEN req_type = :page_view_logged_in_browser THEN count ELSE 0 END) AS page_view_logged_in_browser,+ SUM(CASE WHEN req_type = :page_view_anon_browser THEN count ELSE 0 END) AS page_view_anon_browser,+ SUM(CASE WHEN req_type = :page_view_crawler THEN count ELSE 0 END) AS page_view_crawler,+ SUM(+ CASE WHEN req_type = :page_view_anon THEN count+ WHEN req_type = :page_view_logged_in THEN count+ WHEN req_type = :page_view_anon_browser THEN -count+ WHEN req_type = :page_view_logged_in_browser THEN -count+ ELSE 0+ END+ ) AS page_view_other+ FROM application_requests+ WHERE date >= :start_date AND date <= :end_date AND date >= :first_browser_pageview_date++ GROUP BY date+ ORDER BY date ASC+ SQL+ start_date: report.start_date,+ end_date: report.end_date,+ page_view_anon: ApplicationRequest.req_types[:page_view_anon],+ page_view_crawler: ApplicationRequest.req_types[:page_view_crawler],+ page_view_logged_in: ApplicationRequest.req_types[:page_view_logged_in],+ page_view_anon_browser: ApplicationRequest.req_types[:page_view_anon_browser],+ page_view_logged_in_browser: ApplicationRequest.req_types[:page_view_logged_in_browser],+ first_browser_pageview_date: first_browser_pageview_date,+ )++ report.data = [+ {+ req: "page_view_logged_in_browser",+ label: I18n.t("reports.site_traffic.xaxis.page_view_logged_in_browser"),+ color: report.colors[:turquoise],+ data: data.map { |row| { x: row.date, y: row.page_view_logged_in_browser } },+ },+ {+ req: "page_view_anon_browser",+ label: I18n.t("reports.site_traffic.xaxis.page_view_anon_browser"),+ color: report.colors[:lime],+ data: data.map { |row| { x: row.date, y: row.page_view_anon_browser } },+ },+ {+ req: "page_view_crawler",+ label: I18n.t("reports.site_traffic.xaxis.page_view_crawler"),+ color: report.colors[:purple],+ data: data.map { |row| { x: row.date, y: row.page_view_crawler } },+ },+ {+ req: "page_view_other",+ label: I18n.t("reports.site_traffic.xaxis.page_view_other"),+ color: report.colors[:magenta],+ data: data.map { |row| { x: row.date, y: row.page_view_other } },+ },+ ]+ end+ end+enddiff --git a/app/models/report.rb b/app/models/report.rbindex cde970a0b62..970b47c68de 100644--- a/app/models/report.rb+++ b/app/models/report.rb@@ -20,6 +20,7 @@ class Reportinclude Reports::ConsolidatedApiRequestsinclude Reports::ConsolidatedPageViewsinclude Reports::ConsolidatedPageViewsBrowserDetection+ include Reports::SiteTrafficinclude Reports::DailyEngagedUsersinclude Reports::DauByMauinclude Reports::Emails@@ -294,19 +295,35 @@ class Reportreportend+ # NOTE: Once use_legacy_pageviews is always false or no longer needed+ # we will no longer support the page_view_anon and page_view_logged_in reports,+ # they can be removed.def self.req_report(report, filter = nil)data =if filter == :page_view_total- # For this report we intentionally do not want to count mobile pageviews- # or "browser" pageviews. See `ConsolidatedPageViewsBrowserDetection` for- # browser pageviews.- ApplicationRequest.where(- req_type: [- ApplicationRequest.req_types[:page_view_crawler],- ApplicationRequest.req_types[:page_view_anon],- ApplicationRequest.req_types[:page_view_logged_in],- ].flatten,- )+ # For this report we intentionally do not want to count mobile pageviews.+ if SiteSetting.use_legacy_pageviews+ # We purposefully exclude "browser" pageviews. See+ # `ConsolidatedPageViewsBrowserDetection` for browser pageviews.+ ApplicationRequest.where(+ req_type: [+ ApplicationRequest.req_types[:page_view_crawler],+ ApplicationRequest.req_types[:page_view_anon],+ ApplicationRequest.req_types[:page_view_logged_in],+ ].flatten,+ )+ else+ # We purposefully exclude "crawler" pageviews here and by+ # only doing browser pageviews we are excluding "other" pageviews+ # too. This is to reflect what is shown in the "Site traffic" report+ # by default.+ ApplicationRequest.where(+ req_type: [+ ApplicationRequest.req_types[:page_view_anon_browser],+ ApplicationRequest.req_types[:page_view_logged_in_browser],+ ].flatten,+ )+ endelseApplicationRequest.where(req_type: ApplicationRequest.req_types[filter])enddiff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex c9c9b1c40b1..75876dbe3c3 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -1371,7 +1371,17 @@ en:page_view_crawler: "Known Crawler"page_view_other: "Other pageviews"yaxis: "Day"- description: "Pageviews for logged in users, anonymous users, known crawlers and other. This experimental report ensures logged-in/anon requests are coming from real browsers before counting them."+ description: "Pageviews for logged in users, anonymous users, known crawlers and other. This experimental report ensures logged-in/anon requests are coming from real browsers before counting them. Historical data for this report is unavailable, for historical data see the ‘Consolidated Pageviews' report."+ site_traffic:+ title: "Site traffic"+ xaxis:+ page_view_anon_browser: "Pageviews (anonymous)"+ page_view_logged_in_browser: "Pageviews (logged in)"+ page_view_crawler: "Known crawlers"+ page_view_other: "Other traffic"+ yaxis: "Day"+ description: "Pageviews for logged in users, anonymous users, known crawlers and other traffic."+ description_link: "https://meta.discourse.org/t/understanding-pageviews-and-the-site-traffic-report/324062"dau_by_mau:title: "DAU/MAU"xaxis: "Day"diff --git a/config/site_settings.yml b/config/site_settings.ymlindex af7203ebe8a..5ce59b0014c 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -429,6 +429,7 @@ basic:use_legacy_pageviews:default: falsehidden: true+ client: truelogin:invite_only:refresh: truediff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rbindex 79f0d6f98ef..1f6d188942e 100644--- a/spec/models/report_spec.rb+++ b/spec/models/report_spec.rb@@ -339,18 +339,40 @@ RSpec.describe Report doCachedCounting.disableend- it "works and does not count browser or mobile pageviews" do- 3.times { ApplicationRequest.increment!(:page_view_crawler) }- 8.times { ApplicationRequest.increment!(:page_view_logged_in) }- 6.times { ApplicationRequest.increment!(:page_view_logged_in_browser) }- 2.times { ApplicationRequest.increment!(:page_view_logged_in_mobile) }- 2.times { ApplicationRequest.increment!(:page_view_anon) }- 1.times { ApplicationRequest.increment!(:page_view_anon_browser) }- 4.times { ApplicationRequest.increment!(:page_view_anon_mobile) }+ context "when use_legacy_pageviews is true" do+ before { SiteSetting.use_legacy_pageviews = true }- CachedCounting.flush+ it "works and does not count browser or mobile pageviews" do+ 3.times { ApplicationRequest.increment!(:page_view_crawler) }+ 8.times { ApplicationRequest.increment!(:page_view_logged_in) }+ 6.times { ApplicationRequest.increment!(:page_view_logged_in_browser) }+ 2.times { ApplicationRequest.increment!(:page_view_logged_in_mobile) }+ 2.times { ApplicationRequest.increment!(:page_view_anon) }+ 1.times { ApplicationRequest.increment!(:page_view_anon_browser) }+ 4.times { ApplicationRequest.increment!(:page_view_anon_mobile) }- expect(report.data.sum { |r| r[:y] }).to eq(13)+ CachedCounting.flush++ expect(report.data.sum { |r| r[:y] }).to eq(13)+ end+ end++ context "when use_legacy_pageviews is false" do+ before { SiteSetting.use_legacy_pageviews = false }++ it "works and does not count mobile pageviews, and only counts browser pageviews" do+ 3.times { ApplicationRequest.increment!(:page_view_crawler) }+ 8.times { ApplicationRequest.increment!(:page_view_logged_in) }+ 6.times { ApplicationRequest.increment!(:page_view_logged_in_browser) }+ 2.times { ApplicationRequest.increment!(:page_view_logged_in_mobile) }+ 2.times { ApplicationRequest.increment!(:page_view_anon) }+ 1.times { ApplicationRequest.increment!(:page_view_anon_browser) }+ 4.times { ApplicationRequest.increment!(:page_view_anon_mobile) }++ CachedCounting.flush++ expect(report.data.sum { |r| r[:y] }).to eq(7)+ endendendend@@ -1348,6 +1370,7 @@ RSpec.describe Report doCachedCounting.resetCachedCounting.enableApplicationRequest.enable+ SiteSetting.use_legacy_pageviews = trueendafter do@@ -1392,6 +1415,51 @@ RSpec.describe Report doexpect(total_consolidated).to eq(total_page_views)end++ it "does not include any data before the first recorded browser page view (anon or logged in)" do+ freeze_time DateTime.parse("2024-02-10")++ 3.times { ApplicationRequest.increment!(:page_view_logged_in) }+ 2.times { ApplicationRequest.increment!(:page_view_anon) }++ CachedCounting.flush++ freeze_time DateTime.parse("2024-03-10")++ 3.times { ApplicationRequest.increment!(:page_view_logged_in) }+ 2.times { ApplicationRequest.increment!(:page_view_anon) }++ CachedCounting.flush++ freeze_time DateTime.parse("2024-04-10")++ 3.times { ApplicationRequest.increment!(:page_view_crawler) }+ 8.times { ApplicationRequest.increment!(:page_view_logged_in) }+ 6.times { ApplicationRequest.increment!(:page_view_logged_in_browser) }+ 2.times { ApplicationRequest.increment!(:page_view_anon) }+ 1.times { ApplicationRequest.increment!(:page_view_anon_browser) }++ CachedCounting.flush++ report_in_range =+ Report.find(+ "consolidated_page_views_browser_detection",+ start_date: DateTime.parse("2024-02-10").beginning_of_day,+ end_date: DateTime.parse("2024-04-11").beginning_of_day,+ )++ page_view_crawler_report = report_in_range.data.find { |r| r[:req] == "page_view_crawler" }+ page_view_logged_in_browser_report =+ report_in_range.data.find { |r| r[:req] == "page_view_logged_in_browser" }+ page_view_anon_browser_report =+ report_in_range.data.find { |r| r[:req] == "page_view_anon_browser" }+ page_view_other_report = report_in_range.data.find { |r| r[:req] == "page_view_other" }++ expect(page_view_crawler_report[:data].sum { |d| d[:y] }).to eql(3)+ expect(page_view_logged_in_browser_report[:data].sum { |d| d[:y] }).to eql(6)+ expect(page_view_anon_browser_report[:data].sum { |d| d[:y] }).to eql(1)+ expect(page_view_other_report[:data].sum { |d| d[:y] }).to eql(3)+ endendenddiff --git a/spec/requests/admin/reports_controller_spec.rb b/spec/requests/admin/reports_controller_spec.rbindex 57f06dc2183..0e2f53f38d5 100644--- a/spec/requests/admin/reports_controller_spec.rb+++ b/spec/requests/admin/reports_controller_spec.rb@@ -5,6 +5,52 @@ RSpec.describe Admin::ReportsController dofab!(:moderator)fab!(:user)+ describe "#index" do+ before { sign_in(admin) }++ it "excludes page view mobile reports" do+ get "/admin/reports.json"+ expect(response.parsed_body["reports"].map { |r| r[:type] }).not_to include(+ "page_view_anon_browser_mobile_reqs",+ "page_view_logged_in_browser_mobile_reqs",+ "page_view_anon_mobile_reqs",+ "page_view_logged_in_mobile_reqs",+ )+ end++ it "excludes about and storage stats reports" do+ get "/admin/reports.json"+ expect(response.parsed_body["reports"].map { |r| r[:type] }).not_to include(+ "report_about",+ "report_storage_stats",+ )+ end++ context "when use_legacy_pageviews is true" do+ before { SiteSetting.use_legacy_pageviews = true }++ it "excludes the site_traffic report and includes legacy pageview reports" do+ get "/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,+ )+ end+ end++ context "when use_legacy_pageviews is false" do+ before { SiteSetting.use_legacy_pageviews = false }++ it "includes the site_traffic report and excludes legacy pageview reports" do+ get "/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,+ )+ end+ end+ end+describe "#bulk" docontext "when logged in as an admin" dobefore { sign_in(admin) }@@ -128,6 +174,84 @@ RSpec.describe Admin::ReportsController doexpect(response.parsed_body["errors"]).to include(I18n.t("not_found"))endend++ context "when use_legacy_pageviews is true" do+ before do+ SiteSetting.use_legacy_pageviews = true+ sign_in(admin)+ end++ it "marks the site_traffic report as not_found and does not run it" do+ get "/admin/reports/bulk.json",+ params: {+ reports: {+ site_traffic: {+ limit: 10,+ },+ consolidated_page_views: {+ limit: 10,+ },+ consolidated_page_views_browser_detection: {+ limit: 10,+ },+ page_view_anon_reqs: {+ limit: 10,+ },+ page_view_logged_in_reqs: {+ limit: 10,+ },+ },+ }++ expect(response.status).to eq(200)+ expect(response.parsed_body["reports"].count).to eq(5)+ expect(response.parsed_body["reports"][0]).to include("error" => "not_found", "data" => nil)+ expect(response.parsed_body["reports"][1]["type"]).to eq("consolidated_page_views")+ expect(response.parsed_body["reports"][2]["type"]).to eq(+ "consolidated_page_views_browser_detection",+ )+ expect(response.parsed_body["reports"][3]["type"]).to eq("page_view_anon_reqs")+ expect(response.parsed_body["reports"][4]["type"]).to eq("page_view_logged_in_reqs")+ end+ end++ context "when use_legacy_pageviews is false" do+ before do+ SiteSetting.use_legacy_pageviews = false+ sign_in(admin)+ end++ it "marks the legacy pageview reports as not_found and does not run them" do+ get "/admin/reports/bulk.json",+ params: {+ reports: {+ site_traffic: {+ limit: 10,+ },+ consolidated_page_views: {+ limit: 10,+ },+ consolidated_page_views_browser_detection: {+ limit: 10,+ },+ page_view_anon_reqs: {+ limit: 10,+ },+ page_view_logged_in_reqs: {+ limit: 10,+ },+ },+ }++ expect(response.status).to eq(200)+ expect(response.parsed_body["reports"].count).to eq(5)+ expect(response.parsed_body["reports"][0]["type"]).to eq("site_traffic")+ expect(response.parsed_body["reports"][1]).to include("error" => "not_found", "data" => nil)+ expect(response.parsed_body["reports"][2]).to include("error" => "not_found", "data" => nil)+ expect(response.parsed_body["reports"][3]).to include("error" => "not_found", "data" => nil)+ expect(response.parsed_body["reports"][4]).to include("error" => "not_found", "data" => nil)+ end+ endenddescribe "#show" do@@ -228,5 +352,35 @@ RSpec.describe Admin::ReportsController doexpect(response.parsed_body["errors"]).to include(I18n.t("not_found"))endend++ context "when use_legacy_pageviews is true" do+ before do+ SiteSetting.use_legacy_pageviews = true+ sign_in(admin)+ end++ it "does not allow running site_traffic report" do+ Admin::ReportsController::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"))+ end+ end+ end++ context "when use_legacy_pageviews is false" do+ before do+ SiteSetting.use_legacy_pageviews = false+ sign_in(admin)+ end++ it "does not allow running legacy pageview reports" do+ Admin::ReportsController::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"))+ end+ end+ endendenddiff --git a/spec/system/admin_dashboard_community_health_spec.rb b/spec/system/admin_dashboard_community_health_spec.rbnew file mode 100644index 00000000000..827671e3a8e--- /dev/null+++ b/spec/system/admin_dashboard_community_health_spec.rb@@ -0,0 +1,33 @@+# frozen_string_literal: true++describe "Admin Dashboard Community Health", type: :system do+ fab!(:current_user) { Fabricate(:admin) }++ before { sign_in(current_user) }++ describe "Pageview Report" do+ context "when use_legacy_pageviews is true" do+ before { SiteSetting.use_legacy_pageviews = true }++ it "shows the 'Consolidated Pageviews' report" do+ visit("/admin")+ expect(page).to have_css(+ ".admin-report.consolidated-page-views",+ text: I18n.t("reports.consolidated_page_views.title"),+ )+ end+ end++ context "when use_legacy_pageviews is false" do+ before { SiteSetting.use_legacy_pageviews = false }++ it "shows the 'Site Traffic' report" do+ visit("/admin")+ expect(page).to have_css(+ ".admin-report.site-traffic",+ text: I18n.t("reports.site_traffic.title"),+ )+ end+ end+ end+end