Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion Sources/PingClaude/UsageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import AppKit
struct UsageWindow: Codable {
let utilization: Double
let resets_at: String?

private enum CodingKeys: String, CodingKey {
case utilization
case resets_at
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.utilization = try container.decodeIfPresent(Double.self, forKey: .utilization) ?? 0.0
self.resets_at = try container.decodeIfPresent(String.self, forKey: .resets_at)
}
}

struct UsageResponse: Codable {
Expand Down Expand Up @@ -274,6 +285,10 @@ class UsageService: ObservableObject {
self.isUpdatingSessionKey = false
}

// Log raw response for schema debugging (truncated to 500 chars)
let rawPreview = String(data: data, encoding: .utf8).map { String($0.prefix(500)) } ?? "<non-UTF8>"
NSLog("PingClaude: Usage API raw (first 500): %@", rawPreview)

do {
let decoded = try JSONDecoder().decode(UsageResponse.self, from: data)

Expand Down Expand Up @@ -307,14 +322,29 @@ class UsageService: ObservableObject {
self.latestUsage = usage
self.consecutiveErrors = 0

// Log key usage values
// Log key usage values with breakdown details
var logParts = ["Usage API OK: session=\(Int(usage.sessionUtilization))%"]
if let weekly = usage.weeklyUtilization { logParts.append("weekly=\(Int(weekly))%") }
if let resetStr = usage.sessionResetsInString { logParts.append("resets=\(resetStr)") }
for bd in breakdowns {
var bdStr = "\(bd.label)=\(Int(bd.utilization))%"
if let r = bd.resetsAt {
let rem = r.timeIntervalSinceNow
if rem > 0 {
let h = Int(rem) / 3600; let m = (Int(rem) % 3600) / 60
bdStr += h > 0 ? " resets=\(h)h\(m)m" : " resets=\(m)m"
}
}
logParts.append(bdStr)
}
self.logStore?.log(logParts.joined(separator: ", "))
} catch {
self.consecutiveErrors += 1
let rawJSON = String(data: data, encoding: .utf8) ?? "<non-UTF8 data>"
NSLog("PingClaude: Usage API parse error: %@", error.localizedDescription)
NSLog("PingClaude: Usage API raw response: %@", rawJSON)
self.logStore?.log("Usage API parse error: \(error.localizedDescription)")
self.logStore?.log("Usage API raw response: \(rawJSON)")
}
}
}.resume()
Expand Down
Loading