Repository navigation
feat(insights): serve the dashboard from daily rollups instead of rescanning history - #773
Merged
Merged
Conversation
…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.
This was referenced Aug 23, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Insights aggregate RPCs recompute a brand's entire history on every page load, exploding
competitor_mentionsjsonb on the fly — 566,908 array elements for the largest brand. Under the authenticated role's 8s statement timeout,competitor_aggregatestook 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:
refresh_insights_daily(brand, from, to)function (delete+insert, idempotent, service-role only), plus seven*_dailyread 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.Verification against the live database
competitor_aggregates9,696ms → 474ms,ai_visibility_aggregates5,766ms → 9ms,visibility_rate_trend→ 562ms,insights_aggregates124ms → 3.5ms. Everything is now far under the 8s ceiling with room that no longer shrinks as data accumulates.Semantics deliberately preserved
competitors); the leaderboard and score paths filter to live competitors — each read function keeps its own rule.Related issue
None filed — born from the 30d/90d/all outage on the largest brand.
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsTesting
yarn build,yarn typecheck,yarn lint,yarn format:check— all pass fromweb/yarn test,yarn lint,yarn format:check— all pass fromserver/(382 tests)How to test
Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)