Skip to content

fix(insights): show the same visibility on the card, breakdown and leaderboard - #1035

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/insights-unified-visibility
Oct 15, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/insights-unified-visibility

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

For the same filters, the Visibility card, the "View breakdown" panel and the leaderboard's own row could show three different numbers. On the live page, the card showed 17.4 (+1.5) for 30 days while the breakdown showed 24.1 (+5). This is VIS-001 in the Visibility spec v2.

Why they disagreed:

Surface Read Previous window
Card daily rollups (ai_visibility_aggregates_daily) whole UTC days before the window
Breakdown panel raw prompt_results rows, capped at 1,000 per window, unordered a timestamp span that overlaps the boundary
Leaderboard (All time) rollups the trailing 7 days, while the card showed no change

The panel also recomputed now on every render, and on All time it quietly showed the last 7 days.

What changes:

  • One set of windows. web/src/lib/insights-windows.ts holds the day-window helpers moved out of tracking.ts, plus comparisonWindows(). The card (getVisibilityRateTrend), the leaderboard (getCompetitorComparison) and the panel (getInsightsBreakdown) all take their current and previous windows from it, and read Visibility through one helper (aiVisibilityWindow).
  • No row cap in the panel. New RPC insights_breakdown_groups (migration 00129) returns the score components and mention totals per (prompt, platform), already grouped, inside one jsonb value. It also returns the prompts' text and topic. Whole-day windows use the rollups' own UTC day boundaries. The rows are built in web/src/lib/insights-breakdown.ts.
  • Panel totals come from the cards' own aggregates, so the panel's headline is the card's number for Visibility and for Mentions. This includes the Mentions card's "no change without answers in both windows" rule.
  • All time has no previous window anywhere.
    • The card already showed no change.
    • The leaderboard no longer compares with the last 7 days.
    • The panel says there is no earlier period and shows only current values.
  • The panel explains the loaded numbers. The page passes the panel the window the cards were loaded with, including the 24h window anchored to the last tracking run. It no longer passes a new Date() recomputed on every render.
  • Small fixes on the same calculation:
    • The "vs " label names the days actually compared. It showed Aug 16 – Sep 15 for a window that ended Sep 14, in local time.
    • Whole-day presets count their days. Before, the 30d chart's newest point covered 29 days before 12:00 UTC, also with a topic selected.

Out of scope, per spec v2:

  • New prompts still show as "0 → x" gains in the panel. That is VIS-002 ("Same prompts").
  • The panel's number format ("78,8") vs the card's ("78.8") is VIS-005.
  • The Mentions card still shows a last-7-days change on All time. That rule lives in getInsightsSummary, which the topic detail page also uses, so it is left for VIS-003.

Side effect worth knowing: the Action Center AI Visibility KPI calls getVisibilityRateTrend with only days. Its sparkline used to be a cumulative window starting at the warm-up day, so its last point didn't equal the KPI value. It is now the same rolling window as the Insights chart, and its last point equals the value.

Review

I ran a code review before opening this. It found 10 things and 6 are fixed:

  • the root-cause line said "all time" for an empty previous window;
  • topic-filtered presets still rounded to 29 days;
  • the Mentions panel showed a change where the card shows none;
  • duplicated window-bound code;
  • a duplicated filters type;
  • missing tests for those paths.

Left as notes:

  • the Action Center sparkline change above;
  • the Mentions card on All time;
  • scan cost on All time. The panel now groups every answer in the window instead of reading 1,000 rows. 00098 measured about 0.3 s per 100k rows, and the panel loads only when opened.
  • an answer stamped exactly at a raw window boundary counts in both windows, the same as the card's existing RPC, so all surfaces still agree.

Related issue

None. Product spec: Visibility spec v2, task VIS-001.

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 supabase/migrations/00129_insights_breakdown_groups.sql.
  2. Open Insights with 30d and note the Visibility card's value and change, the trend's "vs …" dates, and your row in the Leaderboard.
  3. Click the Visibility card. The panel's "Current 30d" value and change match the card. Repeat with Mentions.
  4. Repeat with 7d, then with a topic selected.
  5. Select All. The card and your leaderboard row show no change. The panel says "All time. There is no earlier period of the same length to compare." and shows only a Cur column.
  6. On a brand with more than 1,000 answers in 30 days, compare the panel with the card. Before this PR they differ; after it they match.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/): 0 errors; the 7 warnings are in code this PR doesn't touch
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run:

  • yarn test: 539/539. New tests are insights-windows.test.ts, insights-breakdown.test.ts and tracking-visibility-parity.test.ts. The parity test feeds the card, panel and leaderboard the same aggregates and checks they agree. On the old code it fails on All time.
  • yarn build: passed.
  • bash supabase/build-schema.sh: schema.sql is up to date.
  • The migration is applied to a dev database. I checked in the browser at 30d, 7d (Visibility and Mentions) and All, in light and dark mode, and at 1024px.

🤖 Generated with Claude Code

…aderboard

The Visibility card, the "View breakdown" panel and the leaderboard's own
row could show different numbers for the same filters (VIS-001):

- the panel fetched raw answers, capped at 1,000 per window in no fixed
  order, so it scored an arbitrary sample (24.1 in the panel against 17.4
  on the card for the same 30 days);
- the panel built its previous window from timestamps, the card from
  whole UTC days;
- on All time the leaderboard compared all time with the last 7 days, and
  the panel quietly showed the last 7 days.

All three now take their windows from one place (insights-windows.ts),
and the totals come from the card's own aggregates. The panel's prompt,
platform and topic slices are grouped in Postgres by a new RPC,
insights_breakdown_groups (00129), so no row cap applies. All time shows
no change anywhere. The "vs" label names the days actually compared, and
whole-day presets count their days instead of rounding elapsed time.

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk this is ready for review. It's VIS-001 from the Visibility spec v2.

The Visibility card, the "View breakdown" panel and the leaderboard now show the same number and change for the same filters. The panel used to score a 1,000-row raw sample over its own windows (17.4 on the card vs 24.1 in the panel for the same 30 days).

Things to look at:

  • Migration 00129 adds the insights_breakdown_groups RPC (security invoker, same filters as ai_visibility_aggregates). It needs to run on deploy; schema.sql is regenerated.
  • Behavior on All time: no change is shown anywhere. Before, the leaderboard compared all time with the last 7 days.
  • Side effect: the Action Center AI Visibility KPI sparkline is now the same rolling window as the Insights chart. Its last point now equals the KPI value.
  • Scan cost: the panel now groups every answer in the window instead of reading 1,000 rows. It's on demand and should be well under the timeout, but worth a look on the largest brand.

Code review, yarn test (539/539), lint, typecheck and build pass. I checked it in the browser at 30d, 7d and All, in light and dark mode.

…d-visibility

# Conflicts:
#	supabase/schema.sql
@gkhngyk

gkhngyk commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Minor, non-blocking.

  • The topic detail page calls getCompetitorComparison(brandId, { topicId }) with no date, so comparisonWindows returns prev: null and scoreChange is always null.
  • As a result, the Visibility KPI change and the competitor table's change column on topics/[id] disappear. Mentions, Citations and Sentiment on the same strip still show 7-day changes.
  • Dropping the old comparison (all time vs days 8–14) is defensible, but it isn't listed among the side effects. Either pass a window there or note the change.

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

Approved. 00129 is applied to the hosted database. The breakdown RPC runs in 1.8s on the largest brand as an authenticated member.

@gkhngyk
gkhngyk merged commit f868b09 into ansvisor:main Oct 15, 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