Skip to content

fix(competitors): compute head-to-head from every answer through the daily rollups - #1152

Merged
gkhngyk merged 1 commit into
mainfrom
fix/head-to-head-rollups
Oct 18, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/head-to-head-rollups

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Summary

The Competitors page's head-to-head was computed from at most the newest 1,000 answers: it read raw prompt_results in one request, which PostgREST caps at 1,000 rows. For a brand past that (the largest has ~220k answers), the win/loss numbers, per-prompt rows and averages were silently partial.

Since 00164, the daily rollups keep every sum this needs, so:

  • Migration 00169 adds head_to_head_daily(p_brand_id, p_competitor_id). It returns:

    • The groups per (prompt, platform, model, region), from insights_prompt_daily and the competitor's sightings in insights_competitor_prompt_daily.
    • The averages per engine, from insights_brand_daily and insights_competitor_daily.
    • Each prompt's text once, so the app no longer sends hundreds of prompt ids back in a query string.

    It is plpgsql with force_custom_plan and security definer, with the organization check made explicit, as the other Insights reads.

  • headToHeadFromRollups replaces aggregateHeadToHead and keeps the same rules:

    • A group's missing competitor counts as 0.
    • The competitor's averages are over the answers that list it.
    • The brand's averages are over every answer, those without a prompt included.
    • Groups stay most recently run first.

Checked on production (00169 is applied):

  • Same numbers as the raw answers: on a brand with ~40k answers, the function's output matches the same sums computed over the raw answers exactly (918 groups and 9 engines, except both ways empty).
  • Speed: the largest brand's 5,724 groups over 720 prompts read in 0.7 s as a member (1.4 MB).
  • Access: a user from another organization gets null, and anon can't execute it.

Related issue

Closes #923

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. Open Competitors on a brand with more than 1,000 tracked answers and pick a competitor.
  2. The head-to-head lists every tracked prompt, each with its full run count. Before this change, prompts beyond the newest 1,000 answers were missing.
  3. Open a group: its runs still load on demand.

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

🤖 Generated with Claude Code

https://claude.ai/code/session_0121amRjX7UFrgmGELAtkc7A

…daily rollups

The Competitors page's head-to-head read the brand's raw prompt_results in
one request, which PostgREST caps at 1,000 rows: a brand past 1,000
answers was compared on its newest 1,000 only, with no sign of it. The
largest brand has ~220k.

Every number on it is a sum the rollups keep since 00164. Migration 00169
adds head_to_head_daily: the groups per (prompt, platform, model, region)
from insights_prompt_daily and the competitor's sightings, the averages
per engine from insights_brand_daily and insights_competitor_daily, and
each prompt's text once. On production it matches the same sums over the
raw answers exactly (918 groups, 9 engines on a 40k-answer brand) and
reads the largest brand's 5,724 groups in 0.7 s as a member; a caller
outside the brand's organization gets null.

headToHeadFromRollups replaces aggregateHeadToHead with the same rules:
a group's missing competitor counts 0, the competitor's averages are over
the answers that list it, the brand's over every answer.

Claude-Session: https://claude.ai/code/session_0121amRjX7UFrgmGELAtkc7A
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 18, 2026
@gkhngyk
gkhngyk merged commit f3a50f2 into main Oct 18, 2026
6 checks passed
gkhngyk added a commit that referenced this pull request Oct 18, 2026
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.

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

1 participant