Skip to content

fix(usage): update dashboard stats in real time - #3388

Open
ndhao164 wants to merge 1 commit into
decolua:masterfrom
ndhao164:fix/usage-realtime-stats
Open

fix(usage): update dashboard stats in real time#3388
ndhao164 wants to merge 1 commit into
decolua:masterfrom
ndhao164:fix/usage-realtime-stats

Conversation

@ndhao164

Copy link
Copy Markdown

Problem

The Usage dashboard does not update aggregate metrics after a request completes. Total requests, input tokens, output tokens, cached tokens, estimated cost, breakdown tables, and the chart remain stale until the page is reloaded or the selected period changes.

For providers that report Anthropic-shaped usage (input_tokens and output_tokens), Today and 24h totals can also remain at zero even though the request was persisted.

Root cause

The SSE endpoint recalculates usage after persisted requests, but the client discards the aggregate fields and only merges active requests, recent requests, pending state, and provider errors.

The stream also calls getUsageStats() without the selected period, producing an all-time snapshot that cannot safely replace the period-filtered REST response. In addition, the Today/24h aggregation path reads only prompt_tokens and completion_tokens from the raw token payload instead of using the normalized SQLite columns.

Fix

  • pass the selected period to the SSE endpoint and recalculate the matching snapshot
  • replace client usage state with the complete period-filtered SSE snapshot
  • abort stale REST requests when newer SSE data arrives
  • reconnect the stream when the selected period changes
  • refresh the usage chart when the request total changes
  • aggregate Today/24h tokens from normalized persisted columns, with raw-field fallbacks
  • send SSE headers that prevent intermediary response transformation and buffering
  • add regression coverage for input_tokens and output_tokens

Verification

  • git diff --check
  • JavaScript syntax checks passed for the API route, usage repository, and regression test
  • Node SQLite integration smoke test passed: the Today SSE snapshot excluded a ten-day-old request and reported the new request as 1 request, 77 input tokens, and 23 output tokens

The full Vitest and ESLint suites were not run because dependencies are not installed in this workspace.

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