* Parse a JSONL log file into usage entries. * Handles both old format (without tier/baselineCost) and new format.
(filePath: string)
| 45 | * Handles both old format (without tier/baselineCost) and new format. |
| 46 | */ |
| 47 | async function parseLogFile(filePath: string): Promise<UsageEntry[]> { |
| 48 | try { |
| 49 | const content = await readTextFile(filePath); |
| 50 | const lines = content.trim().split("\n").filter(Boolean); |
| 51 | const entries: UsageEntry[] = []; |
| 52 | for (const line of lines) { |
| 53 | try { |
| 54 | const entry = JSON.parse(line) as Partial<UsageEntry>; |
| 55 | entries.push({ |
| 56 | timestamp: entry.timestamp || new Date().toISOString(), |
| 57 | model: entry.model || "unknown", |
| 58 | tier: entry.tier || "UNKNOWN", |
| 59 | cost: entry.cost || 0, |
| 60 | baselineCost: entry.baselineCost || entry.cost || 0, |
| 61 | savings: entry.savings || 0, |
| 62 | latencyMs: entry.latencyMs || 0, |
| 63 | }); |
| 64 | } catch { |
| 65 | // Skip malformed lines, keep valid ones |
| 66 | } |
| 67 | } |
| 68 | return entries; |
| 69 | } catch { |
| 70 | return []; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get list of available log files sorted by date (newest first). |
no test coverage detected