Skip to content

feat(mentions): add daily mention rollups and the Mentions overview read - #1115

Merged
gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/mentions-data
Oct 17, 2026
Merged

gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/mentions-data

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Second of six PRs for Mentions & Sentiment: the data layer (MS-03/MS-04). Two daily rollups, their refresh and backfill, and one read that gives the Overview everything it shows.

Stacked on #1114. Its diff includes #1114's commit until that merges. Please merge #1114 first, and ship both with the Overview (PR 3).

No new LLM call. Every figure aggregates what tracking already stores on each answer (prompt_results):

Column What it holds
mention_count how often the brand is named
mention_position where the brand is named among the brands an answer lists (00038)
competitor_mentions each competitor's count
sentiment the answer's tone toward the brand

That answers the spec's open question (§3, §18): sentiment is stored per answer, toward your brand only, and only when the brand is named. Competitors have no tone of their own yet, so V1 shows sentiment for your brand and leaves competitor tone to the V1.5 classifier.

What changes

  • Migration 00157_mentions_daily.sql:
    • mentions_prompt_daily, one row per (UTC day, prompt, platform, region):
      • answers;
      • answers naming the brand: a mention, so an answer naming it twice is still one mention (spec §5);
      • occurrences;
      • positive and negative mentions: neutral is the rest, never dropped;
      • the brand's position sums.
      • The prompt grain lets the page filter by topic, search prompts and count prompts with mentions, all from the rollup. Topic is read from prompts at query time, so a topic move needs no rebuild.
    • mentions_competitor_daily, sparse: per competitor named, the answers naming it, and those naming your brand too ("in the same answer as you").
    • RLS read policies in the same shape as prompt_results. chatgpt-shopping stays out (feat(shopping): org-level "Shopping mode" toggle + ChatGPT Shopping platform + Insights isolation #155).
    • refresh_mentions_daily(brand, day_from, day_to): delete + insert per day range, idempotent, service role only.
    • mentions_overview(brand, from, to, platform, region, topic, search) returns jsonb for the window and the one of the same length before it:
      • current, previous: answers, mentions, occurrences, positive, negative, positions, prompts, prompts with mentions, last answer;
      • platforms;
      • daily;
      • competitors: every configured competitor, with mentions, co-mentions and the previous count.
    • How it reads, as topics_overview_window (00156) does:
      • under two days (24h) it reads the answers directly, since today's run may not be in the rollups yet;
      • longer windows read whole UTC days.
      • It is plpgsql with force_custom_plan and work_mem, and security definer with the organization check made explicit; a caller outside the organization gets null.
  • Server:
    • lib/mentions-rollups.js (refreshMentionsDaily, refreshMentionHistory).
    • It rides on the Insights rollup callers, as the Citations rollups do: a run's days are refreshed when the run stamps, and the nightly sweep re-refreshes the trailing days and counts a failed Mentions refresh.
    • scripts/backfill-mentions-daily.js fills each brand's history, chunked and restartable.
    • resultDayBound is now exported from citations-rollups.js and shared.
  • Web:
    • lib/actions/mentions.ts: getMentionsOverview, one call. It returns null on PGRST202, for a build deployed before the migration.
    • lib/mentions-overview.ts: the parsed shape and the Overview's metrics:
      • mention rate;
      • the sentiment split (neutral kept, shares adding up to 100);
      • net sentiment (%positive − %negative);
      • "Low sample" under 20 mentions;
      • average position.
    • mentionsQuery turns the page's filters into a read.
    • types/supabase.ts gets the function.

The Overview's search matches the prompt's text; searching brands and phrases inside answers comes with the Mentions table (PR 4).

Deploy

  1. Apply 00157.
  2. Run node src/scripts/backfill-mentions-daily.js once (server). Without it, the page opens empty until the sweep catches up, and then only for recent days.

supabase/schema.sql was rebuilt here from main's migrations plus 00157. Open PRs with migrations (#1099: 00153, #1103: 00154/00155, #1106: 00156) conflict in that generated file only. Re-run bash supabase/build-schema.sh after merging.

Related issue

None.

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. Apply 00157 and run the backfill.
  2. As a member of the brand's organization, call select mentions_overview('<brand>', now() - interval '30 days').
    • current matches the answers counted over the same UTC days.
    • Add platform, region, topic or search arguments and the figures narrow.
  3. As someone outside the organization: null.
  4. Complete a tracking run: its days appear in mentions_prompt_daily.

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 (874) and yarn build pass.
    • lib/mentions-overview.test.ts covers the parsing and every metric, including the spec's acceptance fixture (§16: 8 answers, 6 mentions, 75%).
    • lib/actions/mentions.test.ts covers the call, PGRST202, no access and errors.
    • lib/mentions-filters.test.ts covers presets, custom days and an empty custom range.
  • server: npm run lint and npm test (1291) pass.
    • lib/mentions-rollups.test.js covers the refresh, history chunks, an empty brand and a failed chunk.
    • insights-rollups.test.js checks that a completed run and the sweep refresh the Mentions rollups, and that a failed one is counted.
  • bash supabase/build-schema.sh: regenerated schema.sql.
  • Dev database:
    • Parity (rolled-back dry run): over 30 UTC days, 107 answers, 107 mentions, 43 positive, 21 negative and 5 prompts, the same as counting the answers directly.

    • 24h read from the answers.

    • Topic and case-insensitive search filters work.

    • A stranger gets null.

    • Timing: 30,000 synthetic answers over 90 days, as authenticated, rolled back and vacuumed. A hosted dev database, ~70 ms of network included:

      90d 175 ms
      30d 96 ms
      7d 85 ms
      24h 92 ms
      filtered 90 ms
      refresh of 14 days 220 ms
    • Applied 00157 and ran the backfill for the demo brand: 139 day rows, 291 competitor rows.

🤖 Generated with Claude Code

cihan-gyk and others added 2 commits October 17, 2026 09:13
…iment page shell

Mentions & Sentiment (MS-05) gets its sidebar item, "Mentions" with a New
pill and a speech-bubble icon, directly above Citations, its sibling
concept. Once Topics moves into Prompts, it sits between Prompts and
Citations.

/dashboard/mentions has the page's title and description, its V1 tabs
(Overview, Mentions, Perception; Sentiment and Fact-check stay hidden until
built) and the filter bar every tab shares: date range (30 days by default,
as in the mockup), AI platform, country, topic and a search over prompts,
brands and phrases. Tab and filters live in the URL, so a refresh or a
shared link restores the view. The tabs' figures and charts arrive with the
next PRs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The data layer for Mentions & Sentiment (MS-03/04), built from what
tracking already stores on each answer: mention_count, mention_position,
competitor_mentions and the answer's sentiment toward the brand. Opening
the page or changing a filter never calls an LLM.

Migration 00157 adds two daily rollups:
- mentions_prompt_daily (day x prompt x platform x region): answers, the
  answers naming the brand, occurrences, positive and negative mentions,
  and the brand's positions;
- mentions_competitor_daily: answers naming each competitor, and those
  naming the brand too.
refresh_mentions_daily rebuilds them (service role only), and
mentions_overview reads a window and the one before it, filtered by
platform, region, topic and a prompt search: under two days from the
answers, longer windows from whole UTC days. It is security definer with
the organization check made explicit.

The server refreshes the rollups with the Insights and Citations ones,
when a run stamps and in the nightly sweep, and
scripts/backfill-mentions-daily.js fills a brand's history. The web gets
getMentionsOverview, the Overview's metrics (mention rate, the sentiment
split with neutral kept, net sentiment, low sample under 20 mentions,
average position) and the filters-to-query helper.

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk this is ready for review: the Mentions data layer — migration 00157 (two daily rollups, refresh_mentions_daily, mentions_overview), the server refresh with the Insights/Citations rollups, and scripts/backfill-mentions-daily.js. It's stacked on #1114. On deploy: apply 00157, then run the backfill script once. It conflicts with the other migration PRs only in the generated supabase/schema.sql (re-run build-schema.sh after merging).

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

Reviewed. Migration 00157 is applied to production. Measured as an authenticated member of the largest brand, with its rollups filled for 60 days: the overview reads 24h in 0.29 s, 7d in 0.22 s, 30d in 0.86 s and 90d in 0.95 s; a user of another organization gets null. A day's rollup refresh takes about 0.2–2 s there. The rollups fill when the server is deployed and the backfill runs.

@gkhngyk
gkhngyk merged commit f422c04 into ansvisor:main Oct 17, 2026
5 checks passed
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.

2 participants