Skip to content

fix: topic detail trend chart & reports trend now use the AI Visibility Score formula (#685) - #693

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
Maqbool61:fix/685-topic-trend-legacy-score
Aug 10, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
Maqbool61:fix/685-topic-trend-legacy-score

Conversation

@Maqbool61

@Maqbool61 Maqbool61 commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The topic detail page's Visibility Trend chart and the Reports trend section
both called getVisibilityTrend, a legacy function that averages raw
visibility_score over all answers — including 0-scored rows where the
brand was absent. This produced values in the 0.3–1.5 band.

The headline Visibility Score on the same page uses computeAiVisibilityScore
(mention/citation/position blend, 0–100 scale). The two numbers were on
completely different scales, making the chart look broken and systematically
understating topic visibility.

Closes: #685

Fix

tracking.ts — getTopicDetail: call getVisibilityRateTrend instead
of getVisibilityTrend. The visibility_rate_trend RPC (migration 00039)
already accepts p_topic_id and covers brand + competitors on the same
formula as the Insights headline. An in-place adapter maps
VisibilityRateTrendData → VisibilityTrendPoint[] so topic_charts.tsx
needs no changes.

reports.ts — createReport: same swap for the has('trend') branch.
Reports pair the trend with new-formula KPIs, so they carried the same
mismatch.

No SQL migration needed — migration 00039 already exists.

After this PR

getVisibilityTrend has zero callers and can be deleted in a follow-up.

Acceptance criteria

  • Topic detail trend chart's latest-day value sits on the same 0–100
    scale as the headline Visibility Score
  • Competitor series uses the same formula as the brand series
  • Reports trend migrated (or tracked in follow-up)
  • No TypeScript errors, lint clean

#685)

getTopicDetail was calling getVisibilityTrend which averaged raw
visibility_score over ALL answers (including 0-scored absent-brand rows),
producing a 0.3–1.5 band. The headline Visibility Score uses the
computeAiVisibilityScore blend (mention/citation/position, 0–100 scale),
so the chart and the headline were on completely different scales.

Fix: call getVisibilityRateTrend (visibility_rate_trend RPC, migration
00039) in getTopicDetail instead. The RPC already accepts p_topic_id and
covers brand + competitors. Adapt the richer VisibilityRateTrendData return
to VisibilityTrendPoint[] in-place so topic_charts.tsx needs no changes.

Also migrate the reports trend section (reports.ts line 991) from
getVisibilityTrend to getVisibilityRateTrend — reports pair the trend with
new-formula KPIs, so they carried the same mismatch.

getVisibilityTrend now has zero callers and can be removed in a follow-up.

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

The core of this is right, and you traced the problem accurately. Both call sites now go through getVisibilityRateTrend, the adapter reads the correct 'you' key, topicId and the date range are both supported, and the chart component keeps its existing VisibilityTrendPoint shape so nothing downstream had to change. Two things before merge.

1. The diff carries a lot of unrelated reformatting. Of ~126 added lines, roughly 18 are the actual fix; the rest are object literals expanded across ~14 hunks in functions this issue never touches — getPromptResults, getVisibilityRateKpi, getTrackedPromptsKpi, getInsightsFilterOptions, getTrackingWindow, foldAiVisibility, getCompetitorComparison, getShareOfVoiceData, and several spots in reports.ts.

This isn't your mistake exactly — Prettier 3.5+ defaults to objectWrap: "preserve", so both the one-line and the expanded form pass format:check and CI can't flag it. It's likely your editor expanding them on save. But it makes the real change hard to review, muddies git blame, and tracking.ts is a file other in-flight work touches (#686 among them), so the extra hunks invite conflicts that buy nothing. Please revert everything except the two call-site changes and their adapters.

2. getVisibilityTrend is now dead code. After this PR nothing in web/src calls it — I checked, the remaining hits are getVisibilityTrendFor in the MCP layer, which is a different function and deliberately out of scope. Since the issue's plan was to migrate both callers and drop the legacy function, please delete it here rather than leave an unused legacy path that looks callable.

One thing worth stating in the PR description, since a reviewer will wonder: report snapshots are immutable, so existing reports keep rendering the old-scale values they stored and only new reports use the new formula. That matches how the visibility-score migration handled older snapshots — just make it explicit.

Happy to approve once the diff is down to the real change.

Rebase on reviewer feedback (gkhngyk):
- Reset both files to main and re-applied only the two call-site changes
  and adapters; removes ~108 lines of Prettier-expanded object literals
  that had nothing to do with the fix
- Delete getVisibilityTrend entirely — zero callers remain after the
  two call sites were migrated to getVisibilityRateTrend
@Maqbool61
Maqbool61 requested a review from gkhngyk August 10, 2026 20:21

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

Both points addressed cleanly — the diff went from 124/19 to 47/103 and now reads as exactly what it does: two call sites moved to getVisibilityRateTrend, two adapters, and the legacy function gone. Thanks for the second pass.

Verified on the branch: VisibilityTrendPoint is still exported and used (5 references), and nothing the deleted function depended on became orphaned — applyModelFilter, expandDateToEndOfDay, CompetitorMention and roundTo1 all still have live callers. Good call adding the snapshot-immutability note in reports.ts too.

One leftover, not worth blocking on: the deleted function's JSDoc stayed behind at tracking.ts:2084-2087 —

/**
 * Fetch daily visibility score trend for a brand (and avg competitor score).
 * Queries ALL prompt_results (not deduplicated) to build a time-series.
 */

// ─── Visibility Rate trend (brand + each competitor) ─────────

It now reads as documentation for the section below it, which describes the opposite behaviour. If you push once more, drop those four lines; otherwise we'll sweep it and merge as is.

@gkhngyk
gkhngyk merged commit 1a87806 into ansvisor:main Aug 10, 2026
3 checks passed
@Maqbool61
Maqbool61 deleted the fix/685-topic-trend-legacy-score branch August 10, 2026 21:15
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.

Topic detail: visibility trend chart still uses the legacy raw-average score, contradicting the headline score

2 participants