Repository navigation
fix(topics): read the overview over a date range, fast enough not to time out - #1106
Conversation
…time out The Topics page failed to load on the largest brand: its overview, topics_overview_aggregates (00057, 00098), scans 30 days of answers as the caller under RLS and ran past the authenticated role's 8 s statement timeout, so the request ended in a 500 after ~9 s and the page read "Failed to load topics". Migration 00156 adds topics_overview_window(brand, from, to): a window under two days reads its answers directly; a longer one reads whole UTC days from the rollups the Insights page reads (insights_prompt_daily, insights_competitor_prompt_daily), and each topic's last answer from the covering index. It is plpgsql with force_custom_plan and work_mem, and security definer with the organization check made explicit, as 00152 and 00153 are. The change compares the window with the one of the same length before it, and the trend has a point per day (none under two days, where the cell shows only the change). The page gets the Prompts page's date range (24h, 7d, 30d, 90d, custom), opening on 24 hours. Custom dates read as whole UTC days, a newer range's answer is never overwritten by an older one's, the roster and the aggregate are read in parallel, and the CSV names its range. A web build deployed before the migration falls back to the old function (PGRST202). Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@gkhngyk ready for review: the live Topics page fails to load on the largest brand (topics_overview_aggregates runs past the 8 s timeout, the request 500s after ~9 s). Migration 00156 adds a windowed overview (raw answers under two days, the Insights rollups beyond, security definer like 00152/00153), and the page gets a 24h / 7d / 30d / 90d / custom range opening on 24h. Please apply 00156 with or before the web deploy. |
…-range Co-Authored-By: Claude Opus 5.5 <[email protected]>
gkhngyk
left a comment
There was a problem hiding this comment.
Reviewed. Migration 00156 is applied to production. Measured as an authenticated member of the largest brand, warm: 24h 0.40 s, 7d 0.12 s, 30d 1.77 s, 90d 3.25 s, against the old function's ~9.3 s timeout. A user of another organization gets no rows. Merged main into the branch (schema.sql regenerated).
Summary
The Topics page fails to load on the largest brand. This makes its overview fast enough not to time out, and gives the page a date range: 24h, 7d, 30d, 90d or custom, opening on 24 hours.
The failure (live, read-only check):
getTopicsOverviewreturns a 500 after ~9.3 s, and the page reads "Failed to load topics".topics_overview_aggregates(00057, 00098), scans 30 days of answers as the caller under RLS. It runs past the authenticated role's 8 sstatement_timeout.What changes
00156_topics_overview_window.sqladdstopics_overview_window(p_brand_id, p_date_from, p_date_to).competitor_mentions.insights_prompt_daily(00130) andinsights_competitor_prompt_daily(00098). Days are counted back from the window's end, the rule 00098 used for the competitor share over 30 days. Each topic's last answer comes from the covering indexidx_prompt_results_brand_created_cover.force_custom_planandwork_mem;security definerwith the organization check made explicit;authenticatedandservice_roleonly.getTopicsOverview(brandId, window?)(lib/actions/topic.ts)PGRST202, for a web build deployed before the migration, it falls back totopics_overview_aggregates.trendDays.lib/topic-trend.tsholds the window helpers, because a'use server'module may only export async functions.DateRangeFilter(PROMPT_RANGE_PRESETS), default 24h.en.jsondrops "(last 30 days)", "(30d)" and "(7d)".Next: #1107 moves this page into the Prompts area, as its Topics tab (
/dashboard/prompts/topics)./dashboard/topicskeeps working through a redirect.Deploy and known limits
topics_overview_aggregatesstays for the fallback; drop it once no build calls it.Related issue
None. The live Topics page fails to load.
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 testsHow to test
Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/): no errors; the 7 warnings are in files this PR does not touchyarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)Also run:
web:yarn test(860) andyarn buildpass.bash supabase/build-schema.shregeneratedschema.sql.New tests
lib/actions/topic.test.ts:PGRST202fallback;lib/topic-trend.test.ts:Dev database:
Parity (rolled-back dry runs):
topics_overview_aggregatesrow for row;Timing: synthetic data, 60k answers, as
authenticated, rolled back:Each long-window read takes 40–100 ms. They are the reads
insights_topic_competitors_daily(00152) makes for the Insights heat map.Applied 00156, and refreshed the demo brand's rollups.
In the browser (demo data):
Review: a code review raised these, now fixed:
Rollup freshness, the deploy order and the detail page are noted above.
Merges cleanly with feat(insights): open on 24h, always compare same prompts, drop the trend's AC markers and Send biggest gap #1100, which also edits
lib/actions/topic.ts.🤖 Generated with Claude Code