0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 06:24:48 +08:00
discourse/app/models/browser_pageview_country_daily_rollup.rb
Krzysztof Kotlarek ae83498179
DEV: Track beacon browser pageviews behind dashboard_improvements (#40934)
Previously, browser pageviews were persisted through the MessageBus
piggyback path, which could miss navigation and unload events.

This change records piggyback and sendBeacon pageviews in
browser_pageview_events with a source, and uses the beacon transport for
dashboard_improvements because keepalive requests can complete after
page navigation starts.

---------

Co-authored-by: Natalie Tay <natalie.tay@gmail.com>
2026-06-23 10:21:39 +08:00

40 lines
1.3 KiB
Ruby
Vendored

# frozen_string_literal: true
class BrowserPageviewCountryDailyRollup < ActiveRecord::Base
def self.aggregate(start_date:, end_date:, source: BrowserPageviewEvent.rollup_source)
start_date = start_date.to_date
end_date = end_date.to_date + 1
DB.exec(<<~SQL, start_date:, end_date:, source:)
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 source = :source
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
#