Skip to content

fix(insights): show No data, mark low samples and use the UI's locale - #1049

Merged
gkhngyk merged 109 commits into
ansvisor:mainfrom
cihan-gyk:fix/insights-number-rules
Oct 16, 2026
Merged

gkhngyk merged 109 commits into
ansvisor:mainfrom
cihan-gyk:fix/insights-number-rules

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Stacked on #1048 (VIS-012) → #1047 → #1046 → #1045 → #1044 → #1043 → #1042 → #1041 → #1039 → #1038 → #1037 → #1036 → #1035. Until those merge, this PR also shows their commits. This part is the last commit, 668cae4.

Summary

VIS-005 in the Visibility spec v2 applies its global number rules across the Insights page:

  • A slice with no runs returns "No data", never 0.
  • Every rate comes with its sample size; under 20 is "Low sample".
  • Every number uses the UI's locale format.

The By AI platform table (#1039) and the heat map (#1044) already followed the first two rules. This PR covers the rest of the page.

"No data", never 0

  • Visibility card: it showed "0" whenever the trend read failed or came back empty; now it shows "No data".
  • Breakdown panel: a row without answers in a window shows "No data" in that cell, and no change. A prompt added this period used to read "0 → 80, +80 pts".
    • The "Biggest gain/drop" sentence (pickDriver, used by both the panel and the server sentence) now only credits rows answered in both windows, so a new prompt no longer "rose 80 pts".
  • By AI platform: share of voice is no longer "0%" on a platform where no brand was named at all (0 of 0).
  • Opportunities: a suggestion without a volume estimate no longer reads "~0/mo".

Low sample

Every rate now names its sample in its tooltip, e.g. "Based on 12 answers in this period". Each rate uses its own denominator:

  • Net sentiment: the answers that name you.
  • Avg. position: the answers that place you.
  • Visibility, Share of voice, Citation rate: all answers.

The summary gains mentioningResults and positionedResults for this (rateSamples in kpi-format.ts). Under 20 answers:

  • KPI rate cards: the context line reads "Low sample · 12 answers" and no change is shown. Visibility keeps its rank: "fix(tracking): keep job active until webhook results drain #1 of 4 brands · Low sample". Coverage counts prompts and already shows them, so it's unchanged.
  • Summary line: "80.8 visibility (low sample · 12 answers)", without a change.
  • Leaderboard: "Low sample · 12 answers" under the title, and no score changes.
  • Trend card: "Visibility · last 30 days · low sample · 12 answers".
  • Why it changed: a row whose slice has under 20 answers in either window reads "Low sample" in place of "prev → cur". Its effect on the overall score stays, since that is weighted by volume.
  • Breakdown panel: a Visibility row with under 20 answers reads "Low sample" in place of its change.

With 20 or more answers, nothing changes from the mockup.

The UI's locale

  • Shared formatter: lib/number-format.ts (number, percent, compact, signed) wraps Intl.NumberFormat with the active next-intl locale, and keeps the true minus (−) the cards use.
    • Why: a plain {n} argument in a next-intl message is inserted as String(n), so numbers are formatted before they go in.
  • Replaced throughout the page's cards, the breakdown panel and its server-built root-cause sentence:
    • toFixed;
    • toLocaleString('en-US'), or with no locale (the browser's, not the UI's);
    • hand-built ${n}%;
    • English-only formatCompactNumber.
  • Dates: the trend's axis, tooltip and markers, the "vs" window, and Why it changed's "added on" use the locale too.
    • The chart keeps the server's date label only as the key markers are placed by.
  • Only en today: the app ships English alone, so the page reads the same apart from one visible change. Compact counts are Intl's ("26.1K", "~39.9K/mo"), where the old helper and the mockup write a lower-case k. In Turkish the page would read "1.234,5" and "%64,3".

Review

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

  • Net sentiment and Avg. position were judged on all answers, not their own;
  • the Visibility card's rank disappeared under the low-sample mark;
  • the breakdown's server sentence still wrote en-US numbers;
  • the trend headline had no mark;
  • the card rules had no test (now rateSamples with tests).

Left as is: compact counts in Intl's "K". It's the locale's format, which is what the rule asks for.

Not in this PR:

  • The breakdown panel's copy is still hard-coded English; only its numbers changed.
  • The Formulas dialog's fixed weights are static text.
  • The Topics page's share-of-voice chart in _charts.tsx isn't on the Insights page.

Related issue

None. Product spec: Visibility spec v2, task VIS-005. Depends on #1048 and the PRs under it.

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 Insights on 30d for a brand with plenty of answers. The page looks as before; hover a rate card and its tooltip ends "Based on N answers…".
  2. Pick a narrow AI Model (one with under 20 answers in the period):
    • the summary line, the Visibility, Net sentiment, Share of voice, Avg. position and Citation rate cards, the leaderboard and the trend card read "Low sample";
    • their changes are hidden.
  3. Turn Same prompts off and open the Visibility breakdown:
    • prompts added this period show "No data" under Prev and "—" under Change;
    • rows with under 20 answers show "Low sample";
    • "Biggest gain" names a prompt answered in both periods.
  4. Check light and dark mode, at 1440px and 1024px.

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: 671/671. New and updated tests:
    • number-format.test.ts: English and Turkish numbers, percents, compact counts, signs;
    • kpi-format.test.ts: the writers in Turkish, rateSamples;
    • summary-format.test.ts: the low-sample mark and the locale;
    • insights-breakdown.test.ts: no driver credited from one window;
    • insights-drivers.test.ts: each driver's answers;
    • insights-opportunities.test.ts: no volume rather than 0.
  • yarn build: passed.
  • npm run version:check: passed.
  • In the browser, on the demo brand:
    • 30d, unchanged from the mockup;
    • Perplexity only (12 answers): every rate marked, changes hidden;
    • the Visibility breakdown with Same prompts off ("No data", "Low sample", the biggest gain);
    • light and dark mode, at 1440px and 1024px.
  • No server change, no migration.

🤖 Generated with Claude Code

cihan-gyk and others added 14 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]>
…form table

The v2 mockup shows one "By AI platform" table where the Share of Voice
donut and trend were (VIS-008): per platform, Visibility, its change,
Coverage, Share of voice and Avg. position. A platform with fewer than 20
answers shows "Low sample · N runs" and one answered only last period
shows "No runs this period", never 0%. The biggest drop is tinted.

Changes follow "Compare: Same prompts", and a change over fewer than 20
answers in either window is left out, so the summary line's movers and
the table always agree. All time shows the table without changes.

Migration 00133 extends insights_platform_compare[_daily] with the
position sum and, for the current window, the prompt ids answered and
visible per engine, so Coverage counts a prompt once per platform even
when several models answer it. Window bounds now default to open, and no
previous window reads the current one alone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… the gap to the next

The v2 mockup's leaderboard (VIS-004): ranks 1 and 2, "n more", the brand
directly ahead, yours and the one directly behind, then "Show all N";
boards of five or fewer stay whole. Each row shows the score, its change
("↑ 4.1", following Same prompts) and "Coverage · SoV" under the name.

Under your row, a callout gives the gap to the brand ahead (the summary
line's number) and the lever: the platform where that brand appears in
the largest share of prompts more than yours, among platforms with enough
answers for the By AI platform table. The header link reads
"Head-to-Head", and the board's text moves into en.json.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The v2 mockup's "Why it changed" box (VIS-009): the platforms, topics and
prompts that moved Visibility most, as Gains and Drops, each with its own
score before → after and its effect on the overall score, and a line for
the prompts new this period ("6 new prompts · Oct 12 · own baseline, not
counted"). A row opens the breakdown panel on its tab; "Full breakdown"
opens the panel. It sits beside By AI platform and wraps below it on
narrow screens; All time, with nothing to compare, hides it.

A slice's effect is how much higher the overall score is than it would be
had the slice kept last period's rates at this period's volume, so large
slices weigh more and a handful of answers can't top the list. It follows
"Compare: Same prompts"; with fewer than 20 compared answers in a window
the box says so instead of naming drivers.

Migration 00135 adds insights_change_drivers[_daily]: the Visibility
components per prompt and per (model, platform) in both windows, with the
prompts' text and topic and the new prompts' count and added dates, from
the prompt-grain rollups (raw rows for the 24h and topic views).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e trend

The v2 mockup's visibility trend (VIS-007):
- Insights opens on 30 days; 24h stays one click away.
- "Pin" keeps up to five competitors on the chart whatever the range, saved
  on the brand so the team sees the same lines (admins and managers change
  them, like other brand settings). Without pins the strongest four show,
  as before. Chips above the chart name the lines and highlight them.
- Markers: the days the most prompts were added (+) and the latest days
  people sent Action Center actions (A), three of each, listed under the
  chart; days after the last point are listed but not drawn. With a topic
  selected, only that topic's prompts count.
- The active AI visibility KPI's target shows as a dashed goal line, and
  the axis grows to keep it in view.

Migration 00136 adds brands.insights_pinned_competitors (uuid[], at most
five) and insights_trend_markers: prompts created and actions sent per UTC
day, security invoker.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The v2 mockup's heat map (VIS-010): a row per active topic, a column for
you and each competitor on the trend (pinned, or the strongest four), each
cell the Visibility score in that topic shaded light to dark, and the gap
to the topic's leader ("Lead", or e.g. "−26.5"). Scores use the card's
formula with your answers in the topic as the denominator, as the
leaderboard does. Topics you lead come first, then the biggest gaps, then
topics with fewer than 20 answers (faded, no gap). The topic name opens the
topic; six rows show until "Show all". Hidden on the 24h view and with a
topic selected.

Migration 00137 adds insights_topic_competitors_daily: per active topic,
the brand's and each live competitor's Visibility components from the
prompt-grain rollups, security invoker.

"Send biggest gap to Actions" follows in its own PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The heat map shaded cells at fixed steps (15/25/35/45), so a brand whose
scores are all above 45 saw every cell in the same dark grey. Tones now
spread between the lowest and highest non-zero score in the table, so the
cells read in steps whatever the level, and a 0 stays the lightest. The
shades are softer too, with the text color kept on every one, so a full
table doesn't turn into dark blocks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The v2 mockup's "Send biggest gap to Actions" (VIS-010, part 2): the heat
map's button opens a Compete action for the topic you trail by the most,
against the competitor leading it. The server measures the gap again from
the rollups (00137) rather than trusting the page, refuses a topic it
doesn't see a gap in or with fewer than 20 answers, and keeps one open gap
action per topic. The action carries C04's (close_topic_coverage_gap) task
plan and a person's "created" event, and is measured on the topic's
prompts once completed, like an opportunity sent from Content.

- server: POST /api/topics/:id/gap-action and from-topic-gap.js, with
  route and action tests; validate.js measures the new kind on its basket.
- Action Center: the person-sent kind topic_competitor_gap, titled "Close
  the 26.5-pt gap with Peec AI in AI Rank Tracker", without a "0 signals"
  chip, and a drawer section with the topic and the scores when sent.
- Insights: the button, disabled with a reason when no competitor leads,
  a toast with the AC number and a link, and a plain message when the gap
  has closed since the page loaded.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Insights' Recommendations row becomes the v2 mockup's Opportunities card,
under the topic heat map: stored prompt suggestions grouped by topic,
ranked by relevance x demand x gap, those under 0.6 relevance hidden, with
Track (one prompt) and Track N (a topic's prompts) within the plan's prompt
locations.

The suggestion refresh rates each suggestion's relevance in the same model
call. What that adds is capped at 0.1% of the plan price a month, reserved
atomically per organization before a refresh rates (migration 00138).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ontent

The Opportunities card's second buttons, as in the v2 mockup. A topic
item's Send action opens a Growth action for its suggested prompts and
their demand, on the library's G16 plan when none of the brand's prompts
track the topic and G05 when some do; one open action per topic, measured
on the topic's prompts. A prompt item's Suggest content runs "+ New" for
its topic, steered by the prompt, under the Content quota; it's disabled
for an untracked topic or a spent quota.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The v2 mockup's "Acme Coffee · Last run 11h ago · next run in 13h". The
last run is the brand's latest completed full run from the tracking-run
ledger, no longer the newest answer in the filtered range. The next run is
the last scheduled run plus the schedule's period (a day, or a shorter gap
on a self-hosted install), and is left out once it's due.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Visibility spec v2's global rules (VIS-005) across the Insights page:
a slice with no answers reads "No data", never 0; a rate on fewer than 20
answers is marked "Low sample" and no change is read into it, each rate
over its own answers; and every number is written in the UI's locale
through one formatter (next-intl leaves a plain {n} argument unformatted).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 15, 2026
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk this is ready for review. It's VIS-005 from the Visibility spec v2, stacked on #1048 and the PRs under it. Until those merge, this part is the last commit.

It applies the spec's global number rules across the Insights page: "No data" instead of 0, "Low sample" under 20 answers, and every number in the UI's locale.

Things to look at:

  • One locale-aware formatter. lib/number-format.ts wraps Intl.NumberFormat with the next-intl locale. It replaces toFixed, toLocaleString('en-US'), hand-built % and formatCompactNumber on the page, the breakdown panel and its server sentence.
    • Only en ships today, so the page reads the same. The one visible change is compact counts: Intl writes "26.1K" where the mockup has "26.1k".
  • Each rate is judged on its own sample. Net sentiment uses the answers that name you and Avg. position the ones that place you, via new mentioningResults / positionedResults on the summary.
    • Under 20 answers, the cards, summary line, leaderboard and trend card say "Low sample" and hide the change.
    • At 20 or more, nothing changes from the mockup.
  • No more 0s for missing data.
    • The Visibility card without a trend read now shows "No data".
    • Breakdown rows answered in one window only show "No data", and pickDriver no longer credits a new prompt with "rising 80 pts".
    • A platform's 0-of-0 share of voice is no longer shown as 0%.

Code review: 5 of 6 findings fixed; the one left (Intl's "K") is explained in the description. Web tests (671/671), lint, typecheck and build pass. No server change, no migration.

cihan-gyk and others added 25 commits October 16, 2026 12:30
# Conflicts:
#	web/messages/en.json
#	web/src/app/[locale]/(dashboard)/dashboard/insights/_charts.tsx
#	web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx
…ture/insights-topic-heatmap

Brings in main with ansvisor#1042, squash-merged after this branch took its earlier
head. Resolved: en.json, insights/page.tsx, tracking.ts and the generated
supabase types keep both ansvisor#1042's Why-it-changed and this branch's Topics vs
competitors; schema.sql rebuilt.
As reviewed: insights_topic_competitors_daily took 13.5 s for a signed-in
member of the largest brand over 30 days (8 s limit), 1.3 s planned with
its real arguments. 00152 redefines it as 00147/00148 do: plpgsql with
force_custom_plan and work_mem, security definer with the organization
check made explicit (null for a non-member, which hides the map). Each
topic's competitors are folded once by topic and competitor instead of
rescanned per topic. The output is unchanged. A new migration rather than an
edit of 00137, per the repo's migration rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-rules

# Conflicts:
#	supabase/schema.sql
#	web/messages/en.json
#	web/src/app/[locale]/(dashboard)/dashboard/insights/_topic-heatmap.tsx
#	web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx
#	web/src/lib/actions/tracking.ts
#	web/src/lib/insights-topics.test.ts
#	web/src/lib/insights-topics.ts
…-rules

# Conflicts:
#	server/src/lib/action-center/validate.js
#	server/src/routes/topics.js
#	web/messages/en.json
#	web/src/app/[locale]/(dashboard)/dashboard/insights/_topic-heatmap.tsx
#	web/src/components/action-center/action-drawer.tsx
#	web/src/lib/action-center/display.test.ts
#	web/src/lib/action-center/display.ts
#	web/src/lib/action-center/registry.ts
#	web/src/lib/actions/topic.ts
…-rules

# Conflicts:
#	server/src/lib/suggestion-relevance.js
#	web/messages/en.json
#	web/src/app/[locale]/(dashboard)/dashboard/insights/_opportunities.tsx
#	web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx
#	web/src/lib/actions/tracking.ts
#	web/src/lib/insights-opportunities.test.ts
#	web/src/lib/insights-opportunities.ts
@github-actions github-actions Bot removed the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 16, 2026
…-rules

# Conflicts:
#	web/src/app/[locale]/(dashboard)/dashboard/insights/_opportunities.tsx
#	web/src/lib/actions/tracking.ts
…-rules

# Conflicts:
#	web/messages/en.json
#	web/src/app/[locale]/(dashboard)/dashboard/insights/_run-line.tsx
#	web/src/lib/actions/tracking.ts
@gkhngyk
gkhngyk merged commit f597123 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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants