Skip to content

Commit 27c45ac

Browse files
qianhao02claude
andcommitted
refactor(ccusage): improve all command code quality
- Compute codexTotals from unsorted codexRowsRaw so sort order never affects aggregation (ccusage#1) - Add comments documenting the since/until normalization asymmetry between Claude and Codex sides (ccusage#2) - Remove codexDisplayTotals accumulator; derive table totals row from codexTotals via splitUsageTokens (single source of truth) (ccusage#3) - Remove redundant `?? undefined` in locale cast (ccusage#7) Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 300f413 commit 27c45ac

1 file changed

Lines changed: 19 additions & 28 deletions

File tree

  • apps/ccusage/src/commands

‎apps/ccusage/src/commands/all.ts‎

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,15 @@ export const allCommand = define({
4444
}
4545

4646
// ── Claude Code ──────────────────────────────────────────────────────
47+
// Note: loadDailyUsageData accepts since/until as raw user input (YYYYMMDD or YYYY-MM-DD)
48+
// and handles normalization internally via string comparison.
4749
const claudeData = await loadDailyUsageData({ ...mergedOptions, groupByProject: false });
4850
const claudeTotals = claudeData.length > 0 ? calculateTotals(claudeData) : null;
4951

5052
// ── Codex ────────────────────────────────────────────────────────────
53+
// Note: Codex normalizeFilterDate explicitly converts YYYYMMDD → YYYY-MM-DD before passing
54+
// to buildDailyReport, whereas Claude side handles both formats internally. The behavior is
55+
// equivalent for valid inputs.
5156
const normalizeDate = Result.try({
5257
try: (date: string | undefined) => normalizeFilterDate(date),
5358
catch: (error) => (error instanceof Error ? error : new Error(String(error))),
@@ -79,12 +84,10 @@ export const allCommand = define({
7984
since: codexSince,
8085
until: codexUntil,
8186
});
82-
// Apply same sort order as Claude section
83-
const codexRows = mergedOptions.order === 'desc' ? [...codexRowsRaw].reverse() : codexRowsRaw;
84-
87+
// Compute totals from the unsorted raw rows so that sort order never affects aggregation.
8588
const codexTotals =
86-
codexRows.length > 0
87-
? codexRows.reduce(
89+
codexRowsRaw.length > 0
90+
? codexRowsRaw.reduce(
8891
(acc, row) => {
8992
acc.inputTokens += row.inputTokens;
9093
acc.cachedInputTokens += row.cachedInputTokens;
@@ -104,6 +107,8 @@ export const allCommand = define({
104107
},
105108
)
106109
: null;
110+
// Apply sort order after totals are computed.
111+
const codexRows = mergedOptions.order === 'desc' ? [...codexRowsRaw].reverse() : codexRowsRaw;
107112

108113
if (useJson) {
109114
const output = {
@@ -161,7 +166,7 @@ export const allCommand = define({
161166
formatDateCompact(
162167
dateStr,
163168
mergedOptions.timezone,
164-
(mergedOptions.locale as string | undefined) ?? undefined,
169+
mergedOptions.locale as string | undefined,
165170
),
166171
forceCompact: Boolean(mergedOptions.compact),
167172
};
@@ -225,24 +230,8 @@ export const allCommand = define({
225230
dateFormatter: (dateStr: string) => formatDateCompactCodex(dateStr),
226231
});
227232

228-
const codexDisplayTotals = {
229-
inputTokens: 0,
230-
outputTokens: 0,
231-
reasoningTokens: 0,
232-
cacheReadTokens: 0,
233-
totalTokens: 0,
234-
costUSD: 0,
235-
};
236-
237233
for (const row of codexRows) {
238234
const split = splitUsageTokens(row);
239-
codexDisplayTotals.inputTokens += split.inputTokens;
240-
codexDisplayTotals.outputTokens += split.outputTokens;
241-
codexDisplayTotals.reasoningTokens += split.reasoningTokens;
242-
codexDisplayTotals.cacheReadTokens += split.cacheReadTokens;
243-
codexDisplayTotals.totalTokens += row.totalTokens;
244-
codexDisplayTotals.costUSD += row.costUSD;
245-
246235
codexTable.push([
247236
row.date,
248237
formatModelsDisplayMultiline(formatModelsList(row.models)),
@@ -255,16 +244,18 @@ export const allCommand = define({
255244
]);
256245
}
257246

247+
// Derive display totals from the already-computed codexTotals (same source of truth).
248+
const totalSplit = codexTotals != null ? splitUsageTokens(codexTotals) : null;
258249
addEmptySeparatorRow(codexTable, CODEX_TABLE_COLUMN_COUNT);
259250
codexTable.push([
260251
pc.yellow('Total'),
261252
'',
262-
pc.yellow(formatNumber(codexDisplayTotals.inputTokens)),
263-
pc.yellow(formatNumber(codexDisplayTotals.outputTokens)),
264-
pc.yellow(formatNumber(codexDisplayTotals.reasoningTokens)),
265-
pc.yellow(formatNumber(codexDisplayTotals.cacheReadTokens)),
266-
pc.yellow(formatNumber(codexDisplayTotals.totalTokens)),
267-
pc.yellow(formatCurrency(codexDisplayTotals.costUSD)),
253+
pc.yellow(formatNumber(totalSplit?.inputTokens ?? 0)),
254+
pc.yellow(formatNumber(totalSplit?.outputTokens ?? 0)),
255+
pc.yellow(formatNumber(totalSplit?.reasoningTokens ?? 0)),
256+
pc.yellow(formatNumber(totalSplit?.cacheReadTokens ?? 0)),
257+
pc.yellow(formatNumber(codexTotals?.totalTokens ?? 0)),
258+
pc.yellow(formatCurrency(codexTotals?.costUSD ?? 0)),
268259
]);
269260

270261
log(codexTable.toString());

0 commit comments

Comments
 (0)