A platform for community discussion. Free, open, simple.
FIX: Clamp other traffic to zero in site traffic reports (#43507)
Krzysztof Kotlarekbcb661e9f02b
diff --git a/app/models/concerns/reports/site_traffic.rb b/app/models/concerns/reports/site_traffic.rbindex 30519dc9078..f6c39483e70 100644--- a/app/models/concerns/reports/site_traffic.rb+++ b/app/models/concerns/reports/site_traffic.rb@@ -57,13 +57,16 @@ module Reports::SiteTraffic(COALESCE(MAX(lc.logged_in), 0) + COALESCE(MAX(lc.anonymous), 0)) AS page_view_likely_crawler,SUM(CASE WHEN ar.req_type = :page_view_crawler THEN ar.count ELSE 0 END) AS page_view_crawler,SUM(CASE WHEN ar.req_type = :page_view_embed THEN ar.count ELSE 0 END) AS page_view_embed,- SUM(- CASE WHEN ar.req_type = :page_view_anon THEN ar.count- WHEN ar.req_type = :page_view_logged_in THEN ar.count- WHEN ar.req_type = :page_view_anon_browser THEN -ar.count- WHEN ar.req_type = :page_view_logged_in_browser THEN -ar.count- ELSE 0- END+ GREATEST(+ 0,+ SUM(+ CASE WHEN ar.req_type = :page_view_anon THEN ar.count+ WHEN ar.req_type = :page_view_logged_in THEN ar.count+ WHEN ar.req_type = :page_view_anon_browser THEN -ar.count+ WHEN ar.req_type = :page_view_logged_in_browser THEN -ar.count+ ELSE 0+ END+ )) AS page_view_otherFROM application_requests arLEFT JOIN likely_crawlers lc ON lc.date = ar.datediff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rbindex 64a16673a1b..b7d2f5d347a 100644--- a/spec/models/report_spec.rb+++ b/spec/models/report_spec.rb@@ -1810,6 +1810,32 @@ RSpec.describe Report doCachedCounting.disableend+ it "clamps daily other traffic to zero while preserving browser counts" do+ freeze_time Time.utc(2024, 1, 3)+ dates = [2.days.ago.to_date, 1.day.ago.to_date, Time.zone.today]++ dates+ .zip([6, 4, 3])+ .each do |date, logged_in_count|+ ApplicationRequest.write_cache!(:page_view_anon, 2, date)+ ApplicationRequest.write_cache!(:page_view_logged_in, logged_in_count, date)+ ApplicationRequest.write_cache!(:page_view_anon_browser, 5, date)+ ApplicationRequest.write_cache!(:page_view_logged_in_browser, 1, date)+ end++ series = reports.data.index_by { |report| report[:req] }++ expect(series["page_view_other"][:data]).to eq(+ dates.zip([2, 0, 0]).map { |date, count| { x: date, y: count } },+ )+ expect(series["page_view_anon_browser"][:data]).to eq(+ dates.map { |date| { x: date, y: 5 } },+ )+ expect(series["page_view_logged_in_browser"][:data]).to eq(+ dates.map { |date| { x: date, y: 1 } },+ )+ end+it "exposes embedded pageviews as their own series without polluting other series" doFabricate(:embeddable_host)