Skip to content

feat(insights): add the one-line summary above the KPI cards - #1038

Merged
gkhngyk merged 14 commits into
ansvisor:mainfrom
cihan-gyk:feature/insights-summary-line
Oct 16, 2026
Merged

gkhngyk merged 14 commits into
ansvisor:mainfrom
cihan-gyk:feature/insights-summary-line

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Stacked on #1037 (VIS-003) → #1036 (VIS-002) → #1035 (VIS-001). Until those merge, this PR also shows their commits. The VIS-006 change is the last commit, e337076.

Summary

The v2 mockup opens Insights with one line of facts above the KPI cards. This is VIS-006 in the Visibility spec v2:

✦ 17.4 visibility +1.5 • #6 of 20 · 0.9 pts behind Peec AI • Up most: Google AI Mode +16.1 • Down most: ChatGPT −3.2 ……… vs Aug 14 – Sep 12 · 509 new prompts excluded [copy]

What each fact is:

  • Score and change: the Visibility card's own numbers.
  • Rank and gap: from the leaderboard.
    • Behind someone: "0.9 pts behind Peec AI", the gap to the brand directly ahead.
    • First: "… pts ahead of" the runner-up.
    • Level: "tied with …".
  • Up most / Down most: the platforms whose Visibility rose and fell most. Platforms are named as in "Share of Voice by Platform", and the changes follow Compare: Same prompts (feat(insights): compare changes on the same prompts by default #1036).
  • Context: the days compared and the new prompts left out (or included, with the switch off). This moves here from the trend card, as in the mockup.
  • Copy: puts the same line on the clipboard as text.

It's a fixed template over numbers the page already shows, with no AI call. A fact without its data is left out: no score if the trend read failed (rather than "0"), no rank alone on the leaderboard, and no movers for All time or before the backfill.

Code:

  • insights/summary-format.ts builds the line once for both the screen and Copy, so they can't drift.
  • lib/insights-summary.ts folds platforms, picks the movers and works out the rank.
  • formatWindowBound and the tone colours moved into kpi-format.ts, shared with the KPI cards.

Data

Migration 00132 adds insights_platform_compare_daily (prompt-grain rollups) and insights_platform_compare (raw rows, for the 24h and topic views). They return the Visibility components per (model, platform, window), over the prompts answered in both windows when p_same_prompts is set. Both are security invoker and use the same windows as insights_same_prompt_compare[_daily].

No new backfill: they read the columns 00130 added, so the backfill run for #1036/#1037 covers this too.

Review

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

  • Copy threw without the Clipboard API, e.g. a self-hosted install over plain http. It now shows an error toast.
  • A failed trend read showed "0 visibility".
  • A tie read "0 pts ahead of".
  • The line was written twice, once for the screen and once for Copy, with no tests.

Left as notes:

  • A low-sample platform can be named a mover. Spec v2 adds the n < 20 rule in VIS-005.
  • The shared-prompt set is recomputed by each comparison RPC.
  • An unmapped platform would show as "Unknown", as it already does in Share of Voice by Platform.

Related issue

None. Product spec: Visibility spec v2, task VIS-006. Depends on #1037, #1036 and #1035.

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 00129 to 00132, then refill the rollups (backfill script, or refresh_insights_daily per brand).
  2. Open Insights with 30d. The summary line sits between the filters and the KPI cards. Check it against the page:
    • the score and change match the Visibility card;
    • the rank and gap match the Leaderboard.
  3. Turn Same prompts off and on. The change and the movers follow it, and the context switches between "excluded" and "includes".
  4. Click the copy button and paste: you get the same line as text.
  5. Select All. The line keeps the score and rank, with no change, movers or "vs".
  6. Check 1024px (the line wraps onto two rows) and dark mode.

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: 581/581. New tests are insights-summary.test.ts (platform fold, movers, rank, tie) and summary-format.test.ts, which checks the full mockup line, the omissions and the switch-off wording.
  • yarn build: passed.
  • bash supabase/build-schema.sh: schema.sql is up to date.
  • The migration is applied on a dev database, and the rollup and raw functions return the same totals. I checked in the browser at 1440px and 1024px, in light and dark mode. I checked Copy with the clipboard stubbed, since the embedded browser blocks it; that also confirmed the failure toast.

🤖 Generated with Claude Code

cihan-gyk and others added 3 commits October 14, 2026 17:51
Adding prompts inflated every change on the Insights page: 509 new prompts
read as +203% mentions, and the breakdown counted each new prompt as a
gain from 0 (VIS-002).

A "Compare: Same prompts" switch in the filter bar, on by default and kept
in the URL (?compare=all when off), measures every change only on the
prompts answered in both periods: the Visibility change, every leaderboard
row, Mentions, Citations, Sentiment and the breakdown panel. The numbers
themselves still cover every prompt. The trend card says how many new
prompts were left out (or included, with the switch off).

The per-prompt components come from the daily rollups: migration 00130
adds them to insights_prompt_daily and insights_competitor_prompt_daily,
writes them in refresh_insights_daily, and adds
insights_same_prompt_compare[_daily]. The new columns read NULL until a
day is refreshed; such a comparison is reported incomplete and the page
compares all prompts, so the history needs one backfill run after deploy
(server/src/scripts/backfill-insights-daily.js).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The KPI row becomes the mockup's two rows (VIS-003): today's cards on the
first (Visibility with the brand's rank, Tracked prompts with the plan's
slots left, Mentions, Citations, and Net sentiment in place of Positive
sentiment), and the new rates on the second (Coverage, Share of voice,
Avg. position, Citation rate). Each card has its value, a change coloured
by whether it's good news, a line of context and a daily sparkline. Every
change follows "Compare: Same prompts", and All time shows none. The
Formulas dialog defines each metric and gains the Visibility confidence
factor.

Migration 00131 adds the negative count and the position sum to the brand
and prompt rollups, returns the new components from insights_aggregates
and the same-prompt functions, and adds insights_kpi_series for the
sparklines. Until the history is backfilled
(server/src/scripts/backfill-insights-daily.js), Net sentiment and
Avg. position show no value rather than a partial one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The v2 mockup opens Insights with one line of facts (VIS-006): the
Visibility score and change, the brand's rank and the gap to its
neighbour (or the lead over the runner-up, or a tie), the platforms whose
Visibility rose and fell most, and what the change compares ("vs <days> ·
N new prompts excluded", moved here from the trend card). A copy button
puts the same line on the clipboard as text. It's a fixed template over
numbers the page already shows; no AI call.

Platform movers follow "Compare: Same prompts". Migration 00132 adds
insights_platform_compare[_daily]: the Visibility components per (model,
platform) in both windows, over the shared prompts when asked, from the
prompt-grain rollups (raw rows for the 24h and topic views).

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-006 from the Visibility spec v2, stacked on #1037 → #1036 → #1035. Until those merge, the VIS-006 change is the last commit.

It adds the mockup's one-line summary above the KPI cards: score and change, rank and the gap to the neighbouring brand, the platforms that rose and fell most (following "Compare: Same prompts"), what the change compares, and a copy button. It's a fixed template over the page's own numbers, with no AI call.

Things to look at:

Code review, yarn test (581/581), lint, typecheck and build pass. I checked it in the browser at 1440px and 1024px, in light and dark mode.

cihan-gyk and others added 6 commits October 15, 2026 19:53
…i-cards

# Conflicts:
#	web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx
…bers

insights_same_prompt_compare_daily timed out on the largest brand: 4.5 s
as the owner, over 30 s for an org member against the 8 s timeout. A SQL
function is planned without its argument values on PostgreSQL 17, and RLS
ran inside the scan.

00147 makes both same-prompt RPCs plpgsql with force_custom_plan and
security definer with an explicit org-membership check (as 00119). The
service role still reads, for the cached 24h view. Rollup rows are folded
per prompt and window first. Output is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
getSamePromptComparison returned null on an RPC error, so strict mode
cached the all-prompt changes until the next tracking run. It now throws:
loadWindowedInsights wraps it in lenient(), and the breakdown panel
catches it and compares all prompts, as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
00147 (ansvisor#1036) makes insights_same_prompt_compare[_daily] fast but carries
00130's output. Migrations run in number order, so it would replace
00131's version here and drop the fields the KPI cards read (negative
count, position sum and count, visible prompts, competitor mentions).

00149 is 00131's output in 00147's shape: plpgsql with force_custom_plan,
security definer with the org-membership check, rows folded per prompt
and window first. Output is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nto feature/insights-kpi-cards

# Conflicts:
#	supabase/schema.sql
#	web/src/lib/actions/tracking.ts
…i-cards

# Conflicts:
#	supabase/schema.sql
#	web/messages/en.json
#	web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx
#	web/src/lib/actions/tracking-visibility-parity.test.ts
#	web/src/lib/actions/tracking.ts
#	web/src/lib/insights-same-prompts.test.ts
#	web/src/lib/insights-same-prompts.ts
#	web/src/types/supabase.ts
# Conflicts:
#	supabase/schema.sql
#	web/src/lib/actions/tracking.ts
gkhngyk
gkhngyk previously approved these changes Oct 16, 2026

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

Looks good, thanks. Checked:

  • It sits on the latest #1037 and merges cleanly with main. On the merged tree, schema.sql matches build-schema.sh and web tests pass (684). CI is green.
  • 00132 hasn't changed since it was applied to production. Production runs 00133 (#1039), which keeps 00132's fields and argument order, so this reads it unchanged.
  • Timed on production as a signed-in member of the largest brand:
    • insights_platform_compare_daily: 1.4 s for 30 days, 0.7 s for 90 days.
    • insights_platform_compare: 0.24 s for 24h (7.0 s on a cold first read), 0.37 s for the largest topic over 30 days.

…mmary-line

# Conflicts:
#	supabase/schema.sql
#	web/messages/en.json
#	web/src/app/[locale]/(dashboard)/dashboard/insights/_kpi-cards.tsx
#	web/src/app/[locale]/(dashboard)/dashboard/insights/kpi-format.test.ts
#	web/src/app/[locale]/(dashboard)/dashboard/insights/kpi-format.ts
#	web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx
#	web/src/lib/actions/tracking.ts
#	web/src/types/supabase.ts
@cihan-gyk
cihan-gyk dismissed gkhngyk’s stale review October 16, 2026 07:15

The merge-base changed after approval.

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

Re-approving after the main merge: the tree is identical to the one I reviewed and tested on top of main, and CI is green.

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