A platform for community discussion. Free, open, simple.
FIX: Correct visit counter calculations for stacked report data (#38362)
Krzysztof Kotlareke4020c8ee5da
diff --git a/app/models/user_visit.rb b/app/models/user_visit.rbindex 6b21f4b71e2..f66a975001c 100644--- a/app/models/user_visit.rb+++ b/app/models/user_visit.rb@@ -1,6 +1,8 @@# frozen_string_literal: trueclass UserVisit < ActiveRecord::Base+ belongs_to :user+def self.counts_by_day_query(start_date, end_date, group_id = nil)result = where("visited_at >= ? and visited_at <= ?", start_date.to_date, end_date.to_date)diff --git a/frontend/discourse/admin/components/admin-report-new.gjs b/frontend/discourse/admin/components/admin-report-new.gjsindex f971b86b805..a1ec8b379ad 100644--- a/frontend/discourse/admin/components/admin-report-new.gjs+++ b/frontend/discourse/admin/components/admin-report-new.gjs@@ -19,15 +19,17 @@ import { i18n } from "discourse-i18n";>{{#unless @report.isHidden}}<ConditionalLoadingSection @isLoading={{@report.isLoading}}>- {{#if- (and @report.siteSettings.reporting_improvements @report.model.legacy)- }}- <div class="alert alert-info">- {{icon "triangle-exclamation"}}- <span>{{i18n "admin.reports.legacy_warning"}}</span>- </div>- {{/if}}{{#if @report.showHeader}}+ {{#if+ (and+ @report.siteSettings.reporting_improvements @report.model.legacy+ )+ }}+ <div class="alert alert-info">+ {{icon "triangle-exclamation"}}+ <span>{{i18n "admin.reports.legacy_warning"}}</span>+ </div>+ {{/if}}<div class="header">{{#unless @report.showNotFoundError}}<DPageSubheaderdiff --git a/frontend/discourse/admin/models/report.js b/frontend/discourse/admin/models/report.jsindex 3c9ce5c29ca..bb5ad4a2667 100644--- a/frontend/discourse/admin/models/report.js+++ b/frontend/discourse/admin/models/report.js@@ -1,5 +1,6 @@import EmberObject, { computed } from "@ember/object";import { isEmpty } from "@ember/utils";+import { REPORT_MODES } from "discourse/admin/lib/constants";import { renderAvatar } from "discourse/helpers/user-avatar";import { ajax } from "discourse/lib/ajax";import { durationTiny, number } from "discourse/lib/formatter";@@ -221,13 +222,28 @@ export default class Report extends EmberObject {);}+ get combinedData() {+ if (this.modes?.includes(REPORT_MODES.stacked_chart)) {+ return Object.values(+ this.data+ .flatMap((series) => series.data)+ .reduce((acc, { x, y }) => {+ acc[x] ??= { x, y: 0 };+ acc[x].y += y;+ return acc;+ }, {})+ );+ }+ return this.data;+ }+valueAt(numDaysAgo) {- if (this.data) {+ if (this.combinedData) {const wantedDate = moment().subtract(numDaysAgo, "days").locale("en").format("YYYY-MM-DD");- const item = this.data.find((d) => d.x === wantedDate);+ const item = this.combinedData.find((d) => d.x === wantedDate);if (item) {return item.y;}@@ -236,13 +252,13 @@ export default class Report extends EmberObject {}valueFor(startDaysAgo, endDaysAgo) {- if (this.data) {+ if (this.combinedData) {const earliestDate = moment().subtract(endDaysAgo, "days").startOf("day");const latestDate = moment().subtract(startDaysAgo, "days").startOf("day");let d,sum = 0,count = 0;- this.data.forEach((datum) => {+ this.combinedData.forEach((datum) => {d = moment(datum.x);if (d >= earliestDate && d <= latestDate) {sum += datum.y;diff --git a/spec/fabricators/user_visit_fabricator.rb b/spec/fabricators/user_visit_fabricator.rbnew file mode 100644index 00000000000..7e3a0f9e3bf--- /dev/null+++ b/spec/fabricators/user_visit_fabricator.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Fabricator(:user_visit) do+ user+ visited_at Date.today+ mobile false+enddiff --git a/spec/system/admin_dashboard_general_spec.rb b/spec/system/admin_dashboard_general_spec.rbnew file mode 100644index 00000000000..eca34b7bf75--- /dev/null+++ b/spec/system/admin_dashboard_general_spec.rb@@ -0,0 +1,37 @@+# frozen_string_literal: true++describe "Admin Dashboard General Tab", type: :system do+ fab!(:admin)+ fab!(:user)+ fab!(:user_visit) { Fabricate(:user_visit, user: admin) }+ fab!(:user_visit_2) { Fabricate(:user_visit, user: user, mobile: true) }+ fab!(:user_visit_3) { Fabricate(:user_visit, user: user, visited_at: 1.day.ago) }++ before { sign_in(admin) }++ context "when reporting_improvements is enabled" do+ before { SiteSetting.reporting_improvements = true }++ it "displays correct visit counters combining desktop and mobile visits" do+ visit("/admin")++ within ".admin-report.visits .admin-report-counters" do+ expect(page).to have_css(".today-count", text: "2")+ expect(page).to have_css(".yesterday-count", text: "1")+ end+ end+ end++ context "when reporting_improvements is disabled" do+ before { SiteSetting.reporting_improvements = false }++ it "displays correct visit counters" do+ visit("/admin")++ within ".admin-report.visits .admin-report-counters" do+ expect(page).to have_css(".today-count", text: "2")+ expect(page).to have_css(".yesterday-count", text: "1")+ end+ end+ end+end