codeburn/mac/Sources/CodeBurnMenubar/Views/ModelsSection.swift
Resham Joshi 0f55a446da
Fix per-provider data loss, history regression, and decode fragility (#362)
* Fix per-provider data loss, division-by-zero, and decode fragility

- Per-provider multi-day queries only merged cost/calls from cache,
  dropping categories/models/sessions/tokens. Remove broken cache
  shortcut and always do full parse for per-provider periods.
- Remove per-provider daily history double-counting from overlapping
  cache + live data.
- Guard maxCost against zero in ActivitySection and ModelsSection to
  prevent NaN in bar width calculations.
- Use offset-based ForEach ID in BarTooltipCard to avoid duplicate
  model name collisions.
- Make cacheHitPercent, topActivities, topModels, providers use
  decodeIfPresent for backward compat with older CLI versions.
- Skip currency switch when FX rate fetch fails with no cache,
  preventing rate/symbol desync.
- Use readSessionFile in Gemini parser for 128MB size cap.
- Truncate Codex userMessage to 500 chars like other providers.

* Restore cache-backed trend history for provider-filtered views

The previous commit removed the broken per-provider cache shortcut but
also dropped cache-backed daily history, causing provider-filtered views
to lose trend data outside the selected period range.

Use allCacheDays for historical days (cost/calls per provider is accurate
in cache) and today's entry from the full parse. No overlap since cache
ends at yesterday.
2026-05-20 04:16:48 -07:00

97 lines
3 KiB
Swift

import SwiftUI
struct ModelsSection: View {
@Environment(AppStore.self) private var store
@State private var isExpanded: Bool = true
var body: some View {
CollapsibleSection(
caption: "Models",
isExpanded: $isExpanded,
trailing: {
HStack(spacing: 8) {
Text("Cost").frame(minWidth: 54, alignment: .trailing)
Text("Calls").frame(minWidth: 52, alignment: .trailing)
}
.font(.system(size: 10, weight: .medium))
.foregroundStyle(.tertiary)
.tracking(-0.05)
}
) {
VStack(alignment: .leading, spacing: 7) {
let maxCost = max(store.payload.current.topModels.map(\.cost).max() ?? 1, 0.01)
ForEach(store.payload.current.topModels, id: \.name) { model in
ModelRow(model: model, maxCost: maxCost)
}
TokensLine()
.padding(.top, 5)
}
}
}
}
private struct ModelRow: View {
let model: ModelEntry
let maxCost: Double
var body: some View {
HStack(spacing: 8) {
FixedBar(fraction: model.cost / maxCost)
.frame(width: 56, height: 6)
Text(model.name)
.font(.system(size: 12.5, weight: .medium))
.frame(maxWidth: .infinity, alignment: .leading)
Text(model.cost.asCompactCurrency())
.font(.codeMono(size: 12, weight: .medium))
.tracking(-0.2)
.frame(minWidth: 54, alignment: .trailing)
Text("\(model.calls)")
.font(.system(size: 11))
.monospacedDigit()
.foregroundStyle(.secondary)
.frame(minWidth: 52, alignment: .trailing)
}
.padding(.horizontal, 2)
.padding(.vertical, 1)
}
}
private struct TokensLine: View {
@Environment(AppStore.self) private var store
var body: some View {
let t = store.payload.current
let cacheHit = String(format: "%.0f", t.cacheHitPercent)
HStack(spacing: 4) {
Text("Tokens")
.foregroundStyle(.tertiary)
Text(formatTokens(t.inputTokens) + " in")
.foregroundStyle(.secondary)
Text("·")
.foregroundStyle(.tertiary)
Text(formatTokens(t.outputTokens) + " out")
.foregroundStyle(.secondary)
Text("·")
.foregroundStyle(.tertiary)
Text(cacheHit + "% cache hit")
.foregroundStyle(.secondary)
Spacer()
}
.font(.system(size: 10.5))
.monospacedDigit()
}
private func formatTokens(_ n: Int) -> String {
if n >= 1_000_000 {
return String(format: "%.1fM", Double(n) / 1_000_000)
} else if n >= 1_000 {
return String(format: "%.1fK", Double(n) / 1_000)
}
return "\(n)"
}
}