Skip to content

perf(topics): read a topic's latest runs lean on the topic page - #1150

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/topic-detail-recent-runs
Oct 18, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/topic-detail-recent-runs

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

The topic page reads its "Prompt performance" card lean: the last 50 runs' prompt and visibility only, with no count of every answer. The product owner reported that opening a topic from Topics is very slow.

Why

getTopicDetail runs six reads in parallel, and the page waits for the slowest.

  • Four aggregate reads: perf(insights): serve topic-filtered reads from prompt-grain rollups #867 (merged 2026-10-17) moved these to the rollups (00164). Per 00164's notes, on the largest topic (91 prompts, 35k answers) they took 6–18 s raw.
  • The sixth read (getPromptResults): feeds the best and worst prompts card, and was the one raw read over prompt_results left.
    • It read every column of the topic's last 50 runs: answers, citations, competitor mentions, search queries. All of it went to the browser.
    • It counted every answer the topic ever had (count: 'exact', under RLS) for a total the page never used.
    • It ran four queries: the topic's prompt ids, the page with its count, prompt texts, and topic names.
  • What the card actually uses: each run's prompt id, prompt text and visibility score.

Changes

  • getTopicRecentRuns(brandId, topicId, 50) (not exported, so it isn't a server action):
  • TopicDetailData.results is now TopicRecentRun[] (promptId, promptText, visibilityScore), and the page's state type follows.
  • getPromptResults is unchanged for its other callers.

The card shows the same as before: I compared it with main on the same topic.

Measured

On the dev database, demo topic "Espresso machines under $500" (83 runs), the same page from main and from this branch:

main this branch
getTopicDetail response 91.6 KB 13.7 KB
getTopicDetail server time 519–568 ms 432 ms

The demo answers average 134 characters. Production answers run to thousands of characters with citation lists, so the response gap there will be far larger, and the dropped count was over all of a topic's answers.

I could not measure production: the browser extension wasn't connected. I'll compare before and after once this deploys.

Deploy

No migration of its own. It assumes #867's 00164 and its backfill-insights-daily.js are already applied, as the other five reads do.

Related issue

None. Requested by the product owner.

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. With 00164 applied and the rollups backfilled, open Prompts → Topics and click a topic.
  2. The Prompt performance card lists the same top performers and prompts needing improvement as on main, with the same percentages and run counts.
  3. In the network tab, the getTopicDetail response no longer carries answers or citations.

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 (1332) and yarn build pass.
  • tracking.test.ts:
    • The test query builder now records each call.
    • The new test checks that getTopicDetail returns the runs with their prompt text.
    • It also checks that the one prompt_results query selects only prompt_id, visibility_score, with no count option, ordered by created_at desc and limited to 50.
  • Dev database: applied 00164 after a rolled-back dry run, then ran backfill-insights-daily.js for the demo brand (142 rows, none missing the new columns). Without it, the topic page doesn't load on main locally either.

🤖 Generated with Claude Code

The topic page's best and worst prompts read the topic's last 50 runs
through getPromptResults: every column of those rows (answers, citations,
competitors, searches) sent to the browser, and every answer the topic
ever had counted (count: 'exact', under RLS) for a total the page never
showed. Once ansvisor#867 moved the other reads to the rollups (00164), it was
the one raw read over prompt_results left.

getTopicRecentRuns reads the topic's prompts with their text, then the
last 50 runs' prompt and visibility only, with no count: two queries
instead of four, and the card shows the same. On the dev database's demo
topic the response went from 91.6 KB to 13.7 KB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk A follow-up to your #867 for the topic page. Its "Prompt performance" card still read the last 50 runs through getPromptResults, with every column plus a count: 'exact' over all of the topic's answers. It now reads only prompt and visibility for those 50 runs. Web only, no migration.

One thing to check on production: the topic page needs 00164 and backfill-insights-daily.js there. On the dev database 00164 wasn't applied, and the topic page didn't load at all on main ("Could not find the function public.competitor_aggregates_daily(p_brand_id, p_topic_id)"). I've applied it on dev now.

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

Checked on main with the rest of tonight's PRs stacked on top: the tracking tests, typecheck, lint and format pass, and CI is green. Measured on production for the largest topic (91 prompts) as a signed-in member: the latest 50 runs read in 27 ms.

@gkhngyk
gkhngyk merged commit 1450f15 into ansvisor:main Oct 18, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants