feat(bar): live Codex usage sync with force refresh and spend-chart periods

Codex quota in the menu bar came only from frozen local session logs, so it
showed stale data ("older session") that no refresh could update. Fetch it live
from the same source the dashboard uses, under the existing Claude-style safety
controls (10-min TTL, in-flight coalescing, 429 backoff, circuit breaker,
serve-stale), falling back to local logs when offline or rate-limited. A success
with no usable 5h/weekly window keeps the local reading rather than caching a
contentless row. The footer refresh and a new inline button on the Codex card
force a live pull past the open debounce, and a forced /summary re-pulls native
rows while serving the last-known cached rows if the live pull overruns its
budget, so the Claude/Codex cards never blank mid-refresh.

The spend strip gains a Today / 7d / 30d selector (default 7d), a taller chart,
and bottom time-axis labels (local hours for today, weekday for 7d, dates for
30d). The analytics endpoint now exposes a 24-bucket hourly series for today,
converted from the pipeline's UTC hour keys to the user's local clock so the
intra-day chart matches the dashboard.
This commit is contained in:
Tam Nhu Tran committed 2026-06-20 18:37:18 -04:00
1 parent 9977c49fab
commit 6ed5b2d62b
13 files changed
+1263 -53

No files matched your search

@@ -57,6 +57,20 @@ public struct BarAnalytics: Codable, Sendable, Equatable {
}
}
/// Spend and request count for one hour of today. 24 entries, zero-filled,
/// oldest (00:00) to newest (23:00). The `hour` key is "YYYY-MM-DD HH:00".
public struct Hour: Codable, Sendable, Equatable, Identifiable {
public let hour: String
public let cost: Double
public let requests: Int
public var id: String { hour }
public init(hour: String, cost: Double, requests: Int) {
self.hour = hour
self.cost = cost
self.requests = requests
}
}
public let today: Window
public let last7d: Window
public let last30d: Window
@@ -67,6 +81,9 @@ public struct BarAnalytics: Codable, Sendable, Equatable {
public let allTime: Window
/// Oldest → newest, exactly 30 zero-filled entries, for the sparkline.
public let byDay: [Day]
/// Today's hourly breakdown: 24 entries, oldest (00:00) → newest (23:00),
/// zero-filled. Absent from older payloads; defaults to [].
public let byHour: [Hour]
public let topModels: [Model]
/// "30d" when recent data exists, else "all".
public let topModelsWindow: String
@@ -90,6 +107,7 @@ public struct BarAnalytics: Codable, Sendable, Equatable {
monthToDate: Window = Window(cost: 0, requests: 0),
allTime: Window,
byDay: [Day],
byHour: [Hour] = [],
topModels: [Model],
topModelsWindow: String,
lastActivityAt: String? = nil,
@@ -104,6 +122,7 @@ public struct BarAnalytics: Codable, Sendable, Equatable {
self.monthToDate = monthToDate
self.allTime = allTime
self.byDay = byDay
self.byHour = byHour
self.topModels = topModels
self.topModelsWindow = topModelsWindow
self.lastActivityAt = lastActivityAt
@@ -127,6 +146,9 @@ public struct BarAnalytics: Codable, Sendable, Equatable {
(try c.decodeIfPresent(Window.self, forKey: .monthToDate)) ?? Window(cost: 0, requests: 0)
allTime = try c.decode(Window.self, forKey: .allTime)
byDay = try c.decode([Day].self, forKey: .byDay)
// `byHour` is a new field; older payloads omit it. Default to [] so cached
// or older-backend payloads decode cleanly without throwing.
byHour = (try c.decodeIfPresent([Hour].self, forKey: .byHour)) ?? []
topModels = try c.decode([Model].self, forKey: .topModels)
topModelsWindow = try c.decode(String.self, forKey: .topModelsWindow)
lastActivityAt = try c.decodeIfPresent(String.self, forKey: .lastActivityAt)
@@ -33,3 +33,32 @@ public enum SpendChartStyleStore {
UserDefaults.standard.set(style.rawValue, forKey: defaultsKey)
}
}
// MARK: - SpendPeriod
/// Time window for the spend sparkline selector: today (hourly), last 7 days,
/// or last 30 days. Mirrors the SpendChartStyle pattern: CaseIterable + Sendable.
public enum SpendPeriod: String, CaseIterable, Sendable {
case today
case last7d
case last30d
}
// MARK: - SpendPeriodStore
/// Persists the chosen spend period. Mirrors SpendChartStyleStore exactly:
/// a UserDefaults key, a static load, and a static save. The `?? .last7d`
/// fallback is the sole source of the default.
public enum SpendPeriodStore {
public static let defaultsKey = "ccsbar.spendPeriod"
public static func load() -> SpendPeriod {
let raw = UserDefaults.standard.string(forKey: defaultsKey)
?? SpendPeriod.last7d.rawValue
return SpendPeriod(rawValue: raw) ?? .last7d
}
public static func save(_ period: SpendPeriod) {
UserDefaults.standard.set(period.rawValue, forKey: defaultsKey)
}
}