Skip to content

fix(topics): read the overview over a date range, fast enough not to time out - #1106

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feature/topics-date-range
Oct 17, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feature/topics-date-range

Conversation

@cihan-gyk

@cihan-gyk cihan-gyk commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

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):

  • getTopicsOverview returns a 500 after ~9.3 s, and the page reads "Failed to load topics".
  • Its overview, topics_overview_aggregates (00057, 00098), scans 30 days of answers as the caller under RLS. It runs past the authenticated role's 8 s statement_timeout.

What changes

  • Migration 00156_topics_overview_window.sql adds topics_overview_window(p_brand_id, p_date_from, p_date_to).
    • A window under two days (the 24h default) reads its answers, and the previous window's, directly. Today's run may not be in the rollups yet, and a day of answers is a small read. Its competitor share comes from those answers' competitor_mentions.
    • A longer window reads whole UTC days from the rollups the Insights page reads: insights_prompt_daily (00130) and insights_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 index idx_prompt_results_brand_created_cover.
    • It follows 00152 / 00153's pattern:
      • plpgsql with force_custom_plan and work_mem;
      • security definer with the organization check made explicit;
      • nothing returned for a caller outside the organization;
      • granted to authenticated and service_role only.
    • The change compares the window with the one of the same length before it, instead of 7 days against 7.
    • The trend has a point per day of the window. There is none under two days, where the cell shows only the change.
  • getTopicsOverview(brandId, window?) (lib/actions/topic.ts)
    • Reads either function into one shape.
    • On PGRST202, for a web build deployed before the migration, it falls back to topics_overview_aggregates.
    • The prompt roster and the aggregate are read in parallel.
    • The summary gains trendDays. lib/topic-trend.ts holds the window helpers, because a 'use server' module may only export async functions.
  • The Topics page
    • Gets the Prompts page's DateRangeFilter (PROMPT_RANGE_PRESETS), default 24h.
    • Custom dates read as whole UTC days, so a range picked west of UTC doesn't shift a day.
    • A slower, older range's answer never overwrites a newer one's.
    • The CSV file name carries the range.
    • en.json drops "(last 30 days)", "(30d)" and "(7d)".

Next: #1107 moves this page into the Prompts area, as its Topics tab (/dashboard/prompts/topics). /dashboard/topics keeps working through a redirect.

Deploy and known limits

  • Apply 00156 with or before the web deploy. Until then the page falls back to the old function, which still times out on the largest brand.
  • Freshness: a day enters the rollups when its run completes, as for Insights. While a run is in progress, 7d and longer don't count it yet; 24h does.
  • Unchanged here:
    • a topic's detail page keeps its own fixed windows;
    • picking Custom with no dates leaves the previous range's figures until a date is entered.
  • topics_overview_aggregates stays 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 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

How to test

  1. Apply 00156, and open Topics. It opens on 24h. A topic answered in the last day shows its figures, with the Trend cell showing only the change; one that wasn't shows "—".
  2. Switch to 7d, 30d and 90d: the figures cover the window, and the Trend draws a point per day.
  3. Pick Custom with two dates: the table covers those calendar days.
  4. Switch ranges quickly: the table ends on the last one picked.
  5. On the largest brand, the page loads at every range.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/): no errors; the 7 warnings are in files this PR does not touch
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run:

  • web: yarn test (860) and yarn build pass. bash supabase/build-schema.sh regenerated schema.sql.

  • New tests

    • lib/actions/topic.test.ts:
      • the window read;
      • the change against the window before;
      • no trend at 24h;
      • the PGRST202 fallback;
      • the fixed overview without a window.
    • lib/topic-trend.test.ts:
      • trend days;
      • a rolling week staying at 7 days when the server reads it a moment later;
      • custom dates as UTC days.
  • Dev database:

    • Parity (rolled-back dry runs):

      • over 30 days the first version matched topics_overview_aggregates row for row;
      • the final version's 7 days from the rollups match the answers counted over the same UTC days for 16 of 16 topics;
      • a stranger gets nothing.
    • Timing: synthetic data, 60k answers, as authenticated, rolled back:

      old function, 30d 335–456 ms
      new, 24h ~100 ms
      new, 7d ~150 ms

      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):

    • 24h / 7d / 30d / 90d / custom;
    • quick range switches;
    • checked at 1440px and 1024px, in the light and dark themes.
  • Review: a code review raised these, now fixed:

    • stale responses;
    • custom ranges shifting a day west of UTC;
    • the sequential roster read;
    • the CSV name;
    • a helper's name.

    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

…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]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@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.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

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.

2 participants