Skip to content

fix(insights): trend chart plots the rolling selected window so it always matches the headline - #576

Merged
gkhngyk merged 2 commits into
mainfrom
fix/trend-chart-rolling-window
Aug 1, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
fix/trend-chart-rolling-window

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Summary

On longer date presets the trend chart's line endpoints disagreed with the headline card and the leaderboard — e.g. a brand showing 16.6 in the cards (the whole 90-day window's score) while its line ended at 18.6 (just the last day's score). Both numbers were correct, but they answered different questions on the same card.

The chart now plots a rolling window: each day's point is the AI Visibility Score over the trailing window of the selected length ending that day. Consequences:

  • The line's last point equals the headline card exactly, by construction — the chart's inside and outside can never disagree again, and the leaderboard matches the line endpoints.
  • Every earlier point answers "what would the headline have shown that day", which also smooths daily noise on long ranges.
  • On the 24h preset the behavior is effectively unchanged (trailing 1-day window ≈ the old daily buckets).

Mechanics:

  • Migration 00042 (already applied to the hosted database): visibility_rate_trend day buckets gain position_n (answers carrying a mention position) for the brand and each competitor — needed to weight rolling position factors correctly (Σ pf×n / Σ n). Purely additive keys; the MCP trend endpoint and existing consumers are unaffected.
  • getVisibilityRateTrend fetches one extra window of trailing days (reusing the already-computed prevFrom) to warm up the earliest points, then computes rolling sums with a two-pointer sweep — no extra RPC round trips.

Related issue

—

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 any preset (7d/30d/90d) — the own-brand line's last point equals the number in the card header and the leaderboard entry.
  2. Hover earlier points — values change smoothly (rolling window), not with daily spikes.
  3. Switch presets — the equality holds for every window length.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

@gkhngyk
gkhngyk merged commit 56d638d into main Aug 1, 2026
4 checks passed
@gkhngyk
gkhngyk deleted the fix/trend-chart-rolling-window branch August 1, 2026 12:24
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.

1 participant