0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 15:18:34 +08:00
discourse/app/models/browser_pageview_country_daily_rollup.rb
Krzysztof Kotlarek 820ebddd65
FIX: Split browser pageview rollup source at the cutover date (#42013)
Previously, the pageview rollups picked a single event source globally:
beacon when the `dashboard_improvements` upcoming change was enabled,
piggyback otherwise — so enabling the change silently rebuilt historical
days from sparse beacon data, and disabling it discarded beacon-era
days.

This change replaces `BrowserPageviewEvent.rollup_source` with a
per-date condition based on `beacon_cutover_date`: days before the
cutover are aggregated from piggyback events and days from the cutover
onwards from beacon events, matching how the dashboard traffic chart
already splits its counters.
2026-07-24 14:56:36 +08:00

40 lines
1.3 KiB
Ruby
Vendored

# frozen_string_literal: true
class BrowserPageviewCountryDailyRollup < ActiveRecord::Base
def self.aggregate(start_date:, end_date:)
start_date = start_date.to_date
end_date = end_date.to_date + 1
DB.exec(<<~SQL, start_date:, end_date:)
INSERT INTO browser_pageview_country_daily_rollups (date, country_code, count, logged_in_count)
SELECT
created_at::date AS date,
country_code,
COUNT(*) AS count,
COUNT(*) FILTER (WHERE user_id IS NOT NULL) AS logged_in_count
FROM browser_pageview_events
WHERE created_at >= :start_date
AND created_at < :end_date
AND #{BrowserPageviewEvent.rollup_source_condition}
GROUP BY date, country_code
ON CONFLICT (date, country_code) DO UPDATE
SET count = EXCLUDED.count,
logged_in_count = EXCLUDED.logged_in_count
SQL
end
end
# == Schema Information
#
# Table name: browser_pageview_country_daily_rollups
#
# id :bigint not null, primary key
# count :bigint not null
# country_code :string(2)
# date :date not null
# logged_in_count :bigint not null
#
# Indexes
#
# idx_bpcd_rollups_date_country_unique (date,country_code) UNIQUE NULLS NOT DISTINCT
#