Skip to content

fix: keep chart in sync with toolbar on background stat refreshes#830

Merged
rajbos merged 1 commit into
mainfrom
rajbos/fix-chart-today-data-mismatch
May 9, 2026
Merged

fix: keep chart in sync with toolbar on background stat refreshes#830
rajbos merged 1 commit into
mainfrom
rajbos/fix-chart-today-data-mismatch

Conversation

@rajbos
Copy link
Copy Markdown
Owner

@rajbos rajbos commented May 9, 2026

Problem

The chart and toolbar were showing different "today" token counts. For example, the toolbar would show 17M tokens used today while the chart only showed ~4.3M for the same day. The details screen was also counting more sessions (20) than the chart (3).

Root cause

_runUpdateTokenStats() runs every 5 minutes and correctly refreshes lastDailyStats (fresh 30-day window) to update the toolbar and details panel. When it also pushes an update to the open chart panel, it prefers lastFullDailyStats (the 365-day dataset computed once when the chart was first opened) -- but that full-year dataset is never refreshed by the background timer. So the chart always displayed the stale snapshot from when the chart was opened, while the toolbar reflected current data.

Fix

After each calculateDetailedStats() run, merge the fresh 30-day dailyStats entries into lastFullDailyStats by upsert on the date key. Sessions older than 30 days are unaffected (their historical dates are stable), while the recent window stays accurate on every background refresh.

if (this.lastFullDailyStats) {
    const fullMap = new Map(this.lastFullDailyStats.map(d => [d.date, d]));
    for (const day of dailyStats) {
        fullMap.set(day.date, day);
    }
    this.lastFullDailyStats = Array.from(fullMap.values()).sort(...);
}

This is a surgical 9-line addition in extension.ts with no API or schema changes.

When updateTokenStats() runs every 5 minutes, it refreshes lastDailyStats
(fresh 30-day data) and correctly updates the toolbar. However, when
pushing data to the chart it prefers lastFullDailyStats (the 365-day
dataset computed once when the chart was first opened) -- which is never
refreshed by the background timer.

This caused the chart to show stale session/token counts for today while
the toolbar showed the correct (much higher) numbers.

Fix: after each calculateDetailedStats() call, merge the fresh 30-day
dailyStats entries into lastFullDailyStats. Sessions older than 30 days
are unaffected (their historical dates don't change), while the recent
30-day window now stays accurate on every background refresh.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rajbos rajbos merged commit 631d1bd into main May 9, 2026
17 checks passed
@rajbos rajbos deleted the rajbos/fix-chart-today-data-mismatch branch May 9, 2026 14:41
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