Skip to content

fix(home): sync home overview window with usage records - #754

Merged
lzt404 merged 2 commits into
router-for-me:devfrom
wenbaolu6666-design:WenPaul/sync-home-overview-usage-range
Dec 4, 2026
Merged

lzt404 merged 2 commits into
router-for-me:devfrom
wenbaolu6666-design:WenPaul/sync-home-overview-usage-range

Conversation

@wenbaolu6666-design

@wenbaolu6666-design wenbaolu6666-design commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Syncs the Home Overview usage window with the time range selected in Usage Records (one-way follow: users continue changing the time range in Usage Records, and Home automatically follows it instead of being locked to the last 24 hours).

Root Cause

  • loadHomeOverview in src/services/homeOverview.ts hardcoded a rolling 24-hour window (nowMs - 24h to nowMs) instead of reading the persisted Usage Records range preference (cpa-gui.usage-records-range.v1).
  • HomeOverviewCards in src/pages/HomeOverviewCards.tsx hardcoded the static home.overview.period label ("Last 24 hours").
  • UsageRecordsPage persisted range = 'custom' without persisting customStart / customEnd, causing custom bounds to be lost after leaving the page or reopening the app (falling back to an unbounded all-time query).
  • useHomeOverview did not subscribe to onUsageRecordsUpdated events and only refreshed every 60 seconds, causing noticeable lag relative to Usage Records.

Changes

  • Shared usage range service (src/services/usageRange.ts, src/services/usagePreferences.ts, src/pages/UsageRecordsPage.tsx):
    • Extracted shared UsageRange / UsageRangeSelection types, loadUsageRangeSelection(), buildUsageRangeQuery(), and formatUsageRangePeriod().
    • Persisted custom bounds under cpa-gui.usage-records-custom-start.v1 and cpa-gui.usage-records-custom-end.v1 (both match the existing cpa-gui.usage- preference allowlist in Rust; no backend changes required).
    • Added lightweight subscribe() and syncFromStorage() support to UsagePreferences for in-app and cross-tab updates.
    • Updated UsageRecordsPage to persist range and custom timestamp changes immediately and reset pagination when custom bounds change.
  • Home overview synchronization & live refresh (src/services/homeOverview.ts, src/pages/useHomeOverview.ts, src/pages/HomeOverviewCards.tsx, src/pages/Kernel.tsx):
    • Added loadHomeUsage() and updated loadHomeOverview() to query get_usage_overview with the shared range selection (defaulting to rolling 24 hours when unset for backward compatibility).
    • Updated useHomeOverview to track usage range preference changes and listen to onUsageRecordsUpdated via a throttled createRefreshScheduler(1_000) that refreshes only usage metrics (avoiding extra credential/model reloads or disturbing model health checks), with monotonic request ID guards against out-of-order responses.
    • Updated HomeOverviewCards to render the active usage.range.* label dynamically (and formatted custom date-time bounds with a full-year title tooltip when custom bounds are set).
  • Tests (tests/usageRange.test.ts, tests/homeOverview.test.ts, tests/homeOverviewCards.test.tsx):
    • Added coverage for all 7 ranges (4h, 24h, today, 7d, 30d, all, custom), custom bound persistence, invalid range fallback, cross-tab storage synchronization, and Home overview heading period rendering.

Verification

  • bun run check (tsc --noEmit): passed with 0 errors.
  • bun run build (tsc && vite build): passed.
  • bun test across 8 related test suites (usageRange, homeOverview, homeOverviewCards, usagePreferences, usageBackgroundRefresh, usageViewScope, refreshScheduler, activePolling): 41 passed, 0 failed (1,213 assertions).
  • Built Windows amd64 test package (v0.3.130-test.home-usage-range.1) via GitHub Actions and verified live on Windows: switching between preset ranges (4h, 24h, today, 7d, all) and custom bounds updates the Home overview period label, request count, and success rate to match Usage Records, preserves custom bounds across restarts, and refreshes promptly on new requests.

@wenbaolu6666-design
wenbaolu6666-design force-pushed the WenPaul/sync-home-overview-usage-range branch from 0dbd161 to f8a8abf Compare December 4, 2026 14:43
@wenbaolu6666-design wenbaolu6666-design changed the title fix(home): 首页运行概览同步跟随使用记录时间范围与实时刷新 fix(home): sync home overview window with usage records Dec 4, 2026
@lzt404
lzt404 merged commit 04d15a5 into router-for-me:dev Dec 4, 2026
5 checks passed
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.

2 participants