Skip to content

fix(citations): page past the silent 1000-row cap in overview and gaps scans - #430

Merged
gkhngyk merged 1 commit into
mainfrom
fix/citations-pagination
Jul 15, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/citations-pagination

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Summary

getCitationsOverview and getCitationGaps fetched the filtered prompt_results window in a single un-paginated select, which PostgREST silently caps at 1000 rows. On brands with more than 1000 results in the selected window, every citations number — the dashboard Citations page, the citation-gaps view, and the reports citations section — was computed from a truncated sample. A generated report made it visible: its citations totals reported exactly results: 1000 for a window that actually contained more.

Both scans now share a paginated scanner (scanFilteredResults) that:

  • rebuilds the filtered query per page and walks it with .range() in 1000-row batches under a deterministic created_at, id order,
  • feeds each batch straight into the existing in-memory aggregation, so full citation payloads never accumulate (all-preset scans stay memory-safe),
  • resolves topic → prompt ids once, not per page,
  • caps the walk at 50k rows to bound pathological brands.

Aggregation logic is unchanged — the loop bodies moved verbatim into per-batch callbacks. Same fix pattern as the fan-out scans in #428.

Related Issue

Surfaced while investigating report feedback (evidence follow-up tracked in #429).

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 🧹 Chore / refactor
  • 📝 Documentation
  • 🚨 Breaking change

How to Test

  1. On a brand with >1000 prompt_results in the window, open Citations → totals show the real result count (not exactly 1000) and domain/URL counts grow accordingly.
  2. Generate a report including the citations section for the same window → payload.citations.totals.results exceeds 1000.
  3. Citation gaps view still renders; totalAnswers reflects the full window.
  4. Filters (platform family, region, topic, prompt) still narrow all three surfaces identically.
  5. all preset on a large brand completes without memory issues (batches are discarded after aggregation).

Checklist

  • My code follows the project's style guidelines (Prettier clean)
  • I have performed a self-review of my code
  • npx tsc --noEmit passes
  • npm test passes (45/45)
  • Verified against a production-size brand

…s scans

getCitationsOverview and getCitationGaps fetched the filtered
prompt_results window in one un-paginated select, which PostgREST
silently caps at 1000 rows — on busy brands every citations number
(dashboard Citations page, citation gaps, and the reports citations
section, which showed exactly results: 1000) was computed from a
truncated sample. Both scans now share a paginated scanner that feeds
1000-row batches straight into the existing in-memory aggregation, so
citation payloads never pile up, with a 50k-row ceiling bounding the
'all' preset.
@gkhngyk
gkhngyk merged commit da3c679 into main Jul 15, 2026
4 checks passed
gkhngyk added a commit that referenced this pull request Jul 18, 2026
The Prompts page's cold load was slow because Next.js queues client-fired
server actions sequentially — each is its own POST — and the page fired
three (volumes + prompt sets + visibility summaries) with the suggestions
card's fourth queued behind them: total wait was the SUM of all of them
(the #313 lesson from Insights, never applied here).

- New getPromptsPageData action runs the three reads in a real
  server-side Promise.all — one round trip. A volumes failure/timeout
  degrades to an empty list with a warning toast instead of blanking the
  prompt table: the page's core content is never hostage to the volumes
  upstream.
- Visibility summaries move to a prompt_visibility_summaries RPC
  (migration 00025, SECURITY INVOKER): one GROUP BY per brand instead of
  pulling the raw 30-day result window into JS — which was also silently
  capped at 1000 rows by PostgREST, so busy brands showed wrong health
  columns (the #430/#464 defect family). Verified on production data:
  104 prompt rows, max 210 runs per prompt — depth the 1000-row sample
  could never see.
- The aeo-server fetches for volumes and the suggestions-card read get
  AbortSignal.timeout(15s) (#427 lesson) so a hung upstream can't pin
  the queue.

Fixes the reported slow/never-ending first load of /dashboard/prompts.
gkhngyk added a commit that referenced this pull request Jul 18, 2026
#473)

The Prompts page's cold load was slow because Next.js queues client-fired
server actions sequentially — each is its own POST — and the page fired
three (volumes + prompt sets + visibility summaries) with the suggestions
card's fourth queued behind them: total wait was the SUM of all of them
(the #313 lesson from Insights, never applied here).

- New getPromptsPageData action runs the three reads in a real
  server-side Promise.all — one round trip. A volumes failure/timeout
  degrades to an empty list with a warning toast instead of blanking the
  prompt table: the page's core content is never hostage to the volumes
  upstream.
- Visibility summaries move to a prompt_visibility_summaries RPC
  (migration 00025, SECURITY INVOKER): one GROUP BY per brand instead of
  pulling the raw 30-day result window into JS — which was also silently
  capped at 1000 rows by PostgREST, so busy brands showed wrong health
  columns (the #430/#464 defect family). Verified on production data:
  104 prompt rows, max 210 runs per prompt — depth the 1000-row sample
  could never see.
- The aeo-server fetches for volumes and the suggestions-card read get
  AbortSignal.timeout(15s) (#427 lesson) so a hung upstream can't pin
  the queue.

Fixes the reported slow/never-ending first load of /dashboard/prompts.
gkhngyk pushed a commit that referenced this pull request Jul 31, 2026
…iluted average (#568)

getPromptPerformance and getTopicPerformance averaged visibility_score over
all runs, letting zero-visibility runs dilute heavily-mentioned prompts/topics
into misleading single digits — the same defect #562 already fixed for the
All Prompts tab. Both now rank/report by visibility rate (share of runs with
a mention or citation), matching getPromptVisibilitySummaries' rounding.

Both scans were also un-paginated, so PostgREST silently capped them at 1000
rows on busy brands (the #430/#464 defect family); they now page in 1000-row
batches like the existing citation-evidence scan.

Rendering (detail page + PDF) shows the rate with a % sign, falling back to
the legacy avgVisibility/100 cell for reports generated before this shipped —
mirroring the competitor table's existing visibilityRate fallback. The AI
executive-summary prompt is updated with the same distinction so generated
prose doesn't misquote avgVisibility as a percentage.

Closes #564

Co-authored-by: Claude Sonnet 5 <[email protected]>
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.

1 participant