|
| 1 | +import process from "node:process"; |
| 2 | +import Table from "cli-table3"; |
| 3 | +import { define } from "gunshi"; |
| 4 | +import pc from "picocolors"; |
| 5 | +import { |
| 6 | + calculateTotals, |
| 7 | + createTotalsObject, |
| 8 | + getTotalTokens, |
| 9 | +} from "../calculate-cost.ts"; |
| 10 | +import { type LoadOptions, loadUsageData } from "../data-loader.ts"; |
| 11 | +import type { DailyUsage } from "../data-loader.ts"; |
| 12 | +import { detectMismatches, printMismatchReport } from "../debug.ts"; |
| 13 | +import { log, logger } from "../logger.ts"; |
| 14 | +import { sharedCommandConfig } from "../shared-args.ts"; |
| 15 | +import { formatCurrency, formatNumber } from "../utils.ts"; |
| 16 | + |
| 17 | +interface MonthlyUsage { |
| 18 | + month: string; // YYYY-MM format |
| 19 | + inputTokens: number; |
| 20 | + outputTokens: number; |
| 21 | + cacheCreationTokens: number; |
| 22 | + cacheReadTokens: number; |
| 23 | + totalCost: number; |
| 24 | +} |
| 25 | + |
| 26 | +const aggregateByMonth = (dailyData: DailyUsage[]): MonthlyUsage[] => { |
| 27 | + const monthlyMap = new Map<string, MonthlyUsage>(); |
| 28 | + |
| 29 | + for (const data of dailyData) { |
| 30 | + // Extract YYYY-MM from YYYY-MM-DD |
| 31 | + const month = data.date.substring(0, 7); |
| 32 | + |
| 33 | + const existing = monthlyMap.get(month) || { |
| 34 | + month, |
| 35 | + inputTokens: 0, |
| 36 | + outputTokens: 0, |
| 37 | + cacheCreationTokens: 0, |
| 38 | + cacheReadTokens: 0, |
| 39 | + totalCost: 0, |
| 40 | + }; |
| 41 | + |
| 42 | + existing.inputTokens += data.inputTokens; |
| 43 | + existing.outputTokens += data.outputTokens; |
| 44 | + existing.cacheCreationTokens += data.cacheCreationTokens; |
| 45 | + existing.cacheReadTokens += data.cacheReadTokens; |
| 46 | + existing.totalCost += data.totalCost; |
| 47 | + |
| 48 | + monthlyMap.set(month, existing); |
| 49 | + } |
| 50 | + |
| 51 | + // Convert to array and sort by month descending |
| 52 | + return Array.from(monthlyMap.values()).sort((a, b) => |
| 53 | + b.month.localeCompare(a.month), |
| 54 | + ); |
| 55 | +}; |
| 56 | + |
| 57 | +export const monthlyCommand = define({ |
| 58 | + name: "monthly", |
| 59 | + description: "Show usage report grouped by month", |
| 60 | + ...sharedCommandConfig, |
| 61 | + async run(ctx) { |
| 62 | + if (ctx.values.json) { |
| 63 | + logger.level = 0; |
| 64 | + } |
| 65 | + |
| 66 | + const options: LoadOptions = { |
| 67 | + since: ctx.values.since, |
| 68 | + until: ctx.values.until, |
| 69 | + claudePath: ctx.values.path, |
| 70 | + mode: ctx.values.mode, |
| 71 | + }; |
| 72 | + const dailyData = await loadUsageData(options); |
| 73 | + |
| 74 | + if (dailyData.length === 0) { |
| 75 | + if (ctx.values.json) { |
| 76 | + log(JSON.stringify([])); |
| 77 | + } else { |
| 78 | + logger.warn("No Claude usage data found."); |
| 79 | + } |
| 80 | + process.exit(0); |
| 81 | + } |
| 82 | + |
| 83 | + // Aggregate daily data by month |
| 84 | + const monthlyData = aggregateByMonth(dailyData); |
| 85 | + |
| 86 | + // Calculate totals |
| 87 | + const totals = monthlyData.reduce( |
| 88 | + (acc, item) => ({ |
| 89 | + inputTokens: acc.inputTokens + item.inputTokens, |
| 90 | + outputTokens: acc.outputTokens + item.outputTokens, |
| 91 | + cacheCreationTokens: acc.cacheCreationTokens + item.cacheCreationTokens, |
| 92 | + cacheReadTokens: acc.cacheReadTokens + item.cacheReadTokens, |
| 93 | + totalCost: acc.totalCost + item.totalCost, |
| 94 | + }), |
| 95 | + { |
| 96 | + inputTokens: 0, |
| 97 | + outputTokens: 0, |
| 98 | + cacheCreationTokens: 0, |
| 99 | + cacheReadTokens: 0, |
| 100 | + totalCost: 0, |
| 101 | + }, |
| 102 | + ); |
| 103 | + |
| 104 | + // Show debug information if requested |
| 105 | + if (ctx.values.debug && !ctx.values.json) { |
| 106 | + const mismatchStats = await detectMismatches(ctx.values.path); |
| 107 | + printMismatchReport(mismatchStats, ctx.values.debugSamples); |
| 108 | + } |
| 109 | + |
| 110 | + if (ctx.values.json) { |
| 111 | + // Output JSON format |
| 112 | + const jsonOutput = { |
| 113 | + monthly: monthlyData.map((data) => ({ |
| 114 | + month: data.month, |
| 115 | + inputTokens: data.inputTokens, |
| 116 | + outputTokens: data.outputTokens, |
| 117 | + cacheCreationTokens: data.cacheCreationTokens, |
| 118 | + cacheReadTokens: data.cacheReadTokens, |
| 119 | + totalTokens: getTotalTokens(data), |
| 120 | + totalCost: data.totalCost, |
| 121 | + })), |
| 122 | + totals: createTotalsObject(totals), |
| 123 | + }; |
| 124 | + log(JSON.stringify(jsonOutput, null, 2)); |
| 125 | + } else { |
| 126 | + // Print header |
| 127 | + logger.box("Claude Code Token Usage Report - Monthly"); |
| 128 | + |
| 129 | + // Create table |
| 130 | + const table = new Table({ |
| 131 | + head: [ |
| 132 | + "Month", |
| 133 | + "Input", |
| 134 | + "Output", |
| 135 | + "Cache Create", |
| 136 | + "Cache Read", |
| 137 | + "Total Tokens", |
| 138 | + "Cost (USD)", |
| 139 | + ], |
| 140 | + style: { |
| 141 | + head: ["cyan"], |
| 142 | + }, |
| 143 | + colAligns: [ |
| 144 | + "left", |
| 145 | + "right", |
| 146 | + "right", |
| 147 | + "right", |
| 148 | + "right", |
| 149 | + "right", |
| 150 | + "right", |
| 151 | + ], |
| 152 | + }); |
| 153 | + |
| 154 | + // Add monthly data |
| 155 | + for (const data of monthlyData) { |
| 156 | + table.push([ |
| 157 | + data.month, |
| 158 | + formatNumber(data.inputTokens), |
| 159 | + formatNumber(data.outputTokens), |
| 160 | + formatNumber(data.cacheCreationTokens), |
| 161 | + formatNumber(data.cacheReadTokens), |
| 162 | + formatNumber(getTotalTokens(data)), |
| 163 | + formatCurrency(data.totalCost), |
| 164 | + ]); |
| 165 | + } |
| 166 | + |
| 167 | + // Add separator |
| 168 | + table.push([ |
| 169 | + "─".repeat(12), |
| 170 | + "─".repeat(12), |
| 171 | + "─".repeat(12), |
| 172 | + "─".repeat(12), |
| 173 | + "─".repeat(12), |
| 174 | + "─".repeat(12), |
| 175 | + "─".repeat(10), |
| 176 | + ]); |
| 177 | + |
| 178 | + // Add totals |
| 179 | + table.push([ |
| 180 | + pc.yellow("Total"), |
| 181 | + pc.yellow(formatNumber(totals.inputTokens)), |
| 182 | + pc.yellow(formatNumber(totals.outputTokens)), |
| 183 | + pc.yellow(formatNumber(totals.cacheCreationTokens)), |
| 184 | + pc.yellow(formatNumber(totals.cacheReadTokens)), |
| 185 | + pc.yellow(formatNumber(getTotalTokens(totals))), |
| 186 | + pc.yellow(formatCurrency(totals.totalCost)), |
| 187 | + ]); |
| 188 | + |
| 189 | + log(table.toString()); |
| 190 | + } |
| 191 | + }, |
| 192 | +}); |
0 commit comments