Skip to content

fix(topics): paginate the overview scan and exclude chatgpt-shopping - #465

Merged
gkhngyk merged 1 commit into
mainfrom
fix/topics-overview-accuracy
Jul 17, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/topics-overview-accuracy

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Summary

The Topics leaderboard disagreed with the Insights KPIs for the same brand. Three stacked causes in getTopicsOverview (verified on a production brand):

This PR:

  • Pages the results scan with deterministic (created_at, id) ordering and a 50k hard ceiling, aggregating per batch (rows are never all held in memory) — the citations-scan pattern.
  • Adds .neq('platform', 'chatgpt-shopping') so Topics totals match the Insights aggregates.
  • Scopes the prompts query to the brand's prompt sets at the DB level, with the same paging guard.

Note: the issue also asked for a window label in the UI — the page header already says "(last 30 days)", so no UI change was needed. The remaining legitimate difference vs Insights is the date preset: numbers match when Insights is on 30d.

Related issue

Closes #464

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. Pick a brand with >1000 prompt_results rows in the last 30 days.
  2. Open Topics: leaderboard mentions/citations totals rise vs before (full window counted, shopping excluded).
  3. Set Insights to the 30d preset: total mentions/citations across topics (plus unassigned prompts' share) agree with the Insights KPIs.
  4. A brand with shopping enabled no longer shows inflated topic totals vs Insights.

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

getTopicsOverview fetched the 30-day prompt_results window in one
un-paginated select: PostgREST silently caps that at 1000 rows, and with
no ORDER BY the leaderboard aggregated an arbitrary sample — on a
production brand with 13k rows in the window, ~7.5% of the data. It also
included chatgpt-shopping rows, which every other analytical surface
excludes (#155), and fetched every org prompt un-paginated, filtering to
the brand client-side.

Page the results scan with deterministic (created_at, id) ordering and a
50k ceiling, aggregating per batch; exclude chatgpt-shopping to match
the Insights KPIs; scope the prompts query to the brand's prompt sets at
the DB level with the same paging guard.

Closes #464
@gkhngyk
gkhngyk merged commit 6126c76 into main Jul 17, 2026
5 checks passed
@gkhngyk
gkhngyk deleted the fix/topics-overview-accuracy branch July 17, 2026 12:22
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.

Topics: leaderboard numbers disagree with Insights — unpaginated scan, no shopping exclusion, fixed window

1 participant