Repository navigation
fix: topic detail trend chart & reports trend now use the AI Visibility Score formula (#685) - #693
Conversation
#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
left a comment
There was a problem hiding this comment.
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
gkhngyk
left a comment
There was a problem hiding this comment.
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.
Problem
The topic detail page's Visibility Trend chart and the Reports trend section
both called
getVisibilityTrend, a legacy function that averages rawvisibility_scoreover all answers — including 0-scored rows where thebrand 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: callgetVisibilityRateTrendinsteadof
getVisibilityTrend. Thevisibility_rate_trendRPC (migration 00039)already accepts
p_topic_idand covers brand + competitors on the sameformula as the Insights headline. An in-place adapter maps
VisibilityRateTrendData → VisibilityTrendPoint[]sotopic_charts.tsxneeds no changes.
reports.ts—createReport: same swap for thehas('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
getVisibilityTrendhas zero callers and can be deleted in a follow-up.Acceptance criteria
scale as the headline Visibility Score