Skip to content

bug(competitors): head-to-head is computed from at most the newest 1,000 results #923

Closed

Description

@gkhngyk

Description

getHeadToHeadComparison in web/src/lib/actions/tracking.ts (line 2677) loads the brand's results with:

supabase.from('prompt_results').select('*').eq('brand_id', brandId)
  .neq('platform', 'chatgpt-shopping').order('created_at', { ascending: false });

and then computes every head-to-head number from those rows in JavaScript. PostgREST returns at most 1,000 rows per request (max_rows = 1000, supabase/config.toml line 18), and a query without .range() silently gets the first 1,000.

For a brand with more than 1,000 results, the head-to-head on the Competitors page is computed only from the newest 1,000 — the win/loss counts, per-prompt rows and percentages are all silently partial. It also selects * (including full answer text) when only a few columns are used.

Expected behavior

Head-to-head reflects every result in the window it claims to cover.

Scale

Paging alone is not enough (tried in #935). Some brands have well over 100,000 results: the largest has ~200k, ~114k of them in the last 30 days, and the 90th percentile is ~16k. Today the function returns one promptRow per result, each with its full answer text and citations. Fetching all of them would mean ~200 sequential requests and a response far too large to send to the browser.

The Competitors page only needs aggregates. groupH2HByPrompt and groupH2HByModel in competitors/page.tsx reduce the rows to:

  • per prompt: the averages
  • per (platform, model, region): the averages and the latest result

Suggested fix

  • Totals in SQL. Compute the brand and competitor averages and the per-platform breakdown in an RPC over prompt_results. The competitor's score comes from competitor_mentions. Exclude chatgpt-shopping as today.
  • Per-group summaries. Return one row per (prompt, platform, model, region): the brand and competitor averages, the result count, and the id, sentiment and created_at of the latest result.
  • Load the answer on demand. Load the latest answer's text and citations by result id when the user expands a group, instead of shipping every response up front.
  • Stable ordering. Any remaining paging orders by created_at and id. About 50 results a month share a timestamp within a brand, and an order on created_at alone can skip or duplicate them at page boundaries.
  • Window. Keep the window aligned with feat(competitors): add a date range filter to the Competitors page #892's date range once that lands.

Needs a migration (new RPC).

Activity

  1. self-assigned this
    on Oct 4, 2026
  2. added
    bug
    on Oct 4, 2026
  3. knockknock10 commented on Oct 5, 2026

    @knockknock10
    Contributor

    Hey @gkhngyk as am working and roaming around codebase of Ansvisor
    its actually pretty interesting to me and I would like to keep on contributing to Ansvisor
    can u please assign it to me and if possible few other issues too if u could assign it me !

    Thank you !!

  4. added
    needs-migration
    on Oct 5, 2026
  5. gkhngyk commented on Oct 7, 2026

    @gkhngyk
    ContributorAuthor

    Thanks for the interest, @knockknock10. This one stays with us: it builds on the rollup tables from #867, which are still in review. Two good first issues that are open and independent: #909 (Team section copy into messages/en.json) and #913 (start new AI Agent chats on the active brand). Comment on the one you'd like and we'll assign it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugneeds-migration

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions