Skip to content

feat(insights): serve the dashboard from daily rollups instead of rescanning history - #773

Merged
gkhngyk merged 1 commit into
mainfrom
feature/insights-daily-rollups
Aug 22, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/insights-daily-rollups

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Summary

The Insights aggregate RPCs recompute a brand's entire history on every page load, exploding competitor_mentions jsonb on the fly — 566,908 array elements for the largest brand. Under the authenticated role's 8s statement timeout, competitor_aggregates took 9.7s on that brand's all-time window, so the 30d/90d/all presets rendered an opaque production error. The cost grows with accumulated history, so rewriting the query only buys weeks: the second-largest brand is ~2 months behind on the same trajectory.

This makes the read cost scale with days in the window instead of accumulated results:

  • Migration 00066 adds four daily rollup tables and a refresh_insights_daily(brand, from, to) function (delete+insert, idempotent, service-role only), plus seven *_daily read functions that mirror the raw RPC payloads byte for byte. The raw RPCs are untouched — MCP, reports and pulse keep calling them under the service role.
  • Server refreshes a brand's days the moment its tracking run's ledger row is stamped (the same drain point Daily Pulse waits on), so a day enters the wide windows only once its run completed — never mid-stream. A daily every-brand sweep re-refreshes the trailing 3 days as backstop for unstamped runs and out-of-run writes. A restartable backfill script covers history.
  • Web: every preset except 24h now describes its window as whole UTC days and is answered from the rollups. 24h keeps its run-anchored raw window; topic-filtered calls stay raw (topic is a live prompt attribute — reassignment must move history). The competitor and SoV sections now degrade independently instead of taking the whole page down, and the production-redacted server error maps to an actionable toast.

Verification against the live database

  • Shadow diff before cutover: 36/36 byte-identical. All seven old/new function pairs compared on live data across all-time/30d/7d windows on the largest brand, plus five pairs on three more brands, plus a re-run after this morning's tracking run landed.
  • Measured on the largest brand's all-time window: competitor_aggregates 9,696ms → 474ms, ai_visibility_aggregates 5,766ms → 9ms, visibility_rate_trend → 562ms, insights_aggregates 124ms → 3.5ms. Everything is now far under the 8s ceiling with room that no longer shrinks as data accumulates.
  • Migration is applied to the hosted database and the backfill has run: 120/120 brands, 0 failures, all 56 brands with data covered (~30 MB of rollups against a 1.1 GB raw table). Generated types were refreshed from the live schema.
  • 10 new server unit tests (refresh windows, midnight-crossing runs, sweep resilience); server suite 382/382.

Semantics deliberately preserved

  • SoV counts deleted competitors (the raw RPC never joined competitors); the leaderboard and score paths filter to live competitors — each read function keeps its own rule.
  • A preset window now covers whole calendar days, so numbers advance when a run completes instead of drifting as the clock's trailing edge slides within the day. The skeleton/data behaviour of the page is unchanged.

Related issue

None filed — born from the 30d/90d/all outage on the largest brand.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

Testing

  • yarn build, yarn typecheck, yarn lint, yarn format:check — all pass from web/
  • yarn test, yarn lint, yarn format:check — all pass from server/ (382 tests)
  • Shadow verification and timing measurements above ran against the hosted database

How to test

  1. Open Insights on the largest brand and switch to 30d, 90d and All — each loads in well under the timeout (previously: opaque error).
  2. Numbers must match what the 7d/24h views imply — the shadow diff pins them to the old RPCs' exact output.
  3. After tonight's tracking run stamps, today's results appear in the wide windows without any manual step.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

…canning history

The Insights aggregate RPCs recomputed a brand's entire history on every
page load, exploding competitor_mentions jsonb on the fly — 566,908 array
elements for the largest brand. Under the authenticated role's 8s
statement timeout, competitor_aggregates took 9.7s on that brand's
all-time window, so the 30d/90d/all presets rendered an opaque server
error, and the cost grew with accumulated history.

Migration 00066 adds four daily rollup tables refreshed once per brand-day
— at the moment a tracking run's ledger row is stamped (the same drain
point Daily Pulse waits on), with a daily every-brand sweep as backstop —
plus seven read functions that mirror the raw RPC payloads byte for byte.
Every preset except 24h now describes its window as whole UTC days and is
answered from the rollups; 24h keeps its run-anchored raw window, and
topic/prompt-filtered calls stay raw because topic is a live prompt
attribute. Competitor and SoV sections now degrade independently instead
of taking the KPI header down, and the production-redacted server error is
mapped to an actionable toast.

Shadow-verified on the live database before cutover: 33 old-vs-new output
comparisons across four brands and all seven function pairs, every one
byte-identical. Measured on the largest brand's all-time window:
competitor_aggregates 9,696ms -> 474ms, ai_visibility_aggregates
5,766ms -> 9ms, insights_aggregates 124ms -> 3.5ms. Read cost now scales
with days in the window, never with results.

Closes nothing by number; born from the 30d/90d/all outage on the largest
brand.
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Aug 22, 2026
@gkhngyk
gkhngyk merged commit 91146ce into main Aug 22, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant