v1.19: fix empty usage-history chart (always fetch utilization)
Build APK / build (push) Successful in 1m52s

recordHistory() only ever stored fiveHourUtilization/weeklyUtilization, but
fetchUsage() returned early with message-count data — before calling the
/usage utilization endpoint — whenever the org JSON carried a message limit.
So utilization was never populated and the history chart stayed stuck on
'Collecting history…'. The prior chart fix only corrected the throttle.

- UsageRepository: always attempt /usage (preferred signal that drives the
  weekly bar + history); fall back to org message-count data only when
  utilization is unavailable.
- UsageData.sessionReadingPct: utilization preferred, message-count % fallback,
  -1f when no reading — so message-only accounts also build history.
- PreferencesManager.recordHistory: record the session line from
  sessionReadingPct instead of utilization-only.
- UsageDataTest: cover sessionReadingPct.
This commit is contained in:
2026-06-12 01:48:04 +00:00
parent 31e18ed5e9
commit 58e3a0fcd7
5 changed files with 67 additions and 15 deletions
@@ -94,13 +94,16 @@ class PreferencesManager(context: Context) {
// ── Usage history (for the in-app chart) ─────────────────────────────────
/**
* Append a history point if [data] carries a real utilization reading.
* Append a history point if [data] carries a real reading.
* The session line uses [UsageData.sessionReadingPct] (utilization preferred, message-count
* progress as fallback) so accounts that only expose message counts still build history.
* De-duplicates rapid double-fires (manual refresh + background worker landing
* together) by skipping points within [MIN_HISTORY_GAP_MS] of the last one, and
* prunes anything older than [HISTORY_RETENTION_MS] / beyond [MAX_HISTORY_POINTS].
*/
fun recordHistory(data: UsageData) {
if (data.fiveHourUtilization < 0f && data.weeklyUtilization < 0f) return
val sessionPct = data.sessionReadingPct
if (sessionPct < 0f && data.weeklyUtilization < 0f) return
val now = System.currentTimeMillis()
val history = getHistory().toMutableList()
// Throttle to at most one point per MIN_HISTORY_GAP_MS by SKIPPING a too-soon reading.
@@ -111,7 +114,7 @@ class PreferencesManager(context: Context) {
history.add(
UsageSnapshot(
epochMs = now,
sessionPct = data.fiveHourUtilization,
sessionPct = sessionPct,
weeklyPct = data.weeklyUtilization
)
)