Skip to content

Commit 9edca55

Browse files
committed
fix: normalize totals outputs across ccusage/codex/opencode/pi/amp
The ccusage apps have **inconsistent behavior** when no usage data exists. The reference app (`ccusage`) is itself inconsistent, and the other apps don't follow a consistent pattern. This makes it difficult to write scripts that work across all apps. This commit makes the behaviour consistent across all apps, by using the same structure for the totals dict across all of them. Fixes: ccusage#832
1 parent 3ab2803 commit 9edca55

15 files changed

Lines changed: 243 additions & 58 deletions

File tree

‎apps/amp/src/commands/daily.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,22 @@ export const dailyCommand = define({
4848
const { events } = await loadAmpUsageEvents();
4949

5050
if (events.length === 0) {
51-
const output = jsonOutput
52-
? JSON.stringify({ daily: [], totals: null })
53-
: 'No Amp usage data found.';
54-
// eslint-disable-next-line no-console
55-
console.log(output);
51+
if (jsonOutput) {
52+
const emptyTotals = {
53+
inputTokens: 0,
54+
outputTokens: 0,
55+
cacheCreationTokens: 0,
56+
cacheReadTokens: 0,
57+
totalTokens: 0,
58+
credits: 0,
59+
totalCost: 0,
60+
};
61+
// eslint-disable-next-line no-console
62+
console.log(JSON.stringify({ daily: [], totals: emptyTotals }, null, 2));
63+
} else {
64+
// eslint-disable-next-line no-console
65+
console.log('No Amp usage data found.');
66+
}
5667
return;
5768
}
5869

‎apps/amp/src/commands/monthly.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,25 @@ export const monthlyCommand = define({
4848
const { events } = await loadAmpUsageEvents();
4949

5050
if (events.length === 0) {
51-
const output = jsonOutput
52-
? JSON.stringify({ monthly: [], totals: null })
53-
: 'No Amp usage data found.';
54-
// eslint-disable-next-line no-console
55-
console.log(output);
51+
if (jsonOutput) {
52+
const emptyOutput = {
53+
monthly: [],
54+
totals: {
55+
inputTokens: 0,
56+
outputTokens: 0,
57+
cacheCreationTokens: 0,
58+
cacheReadTokens: 0,
59+
totalTokens: 0,
60+
credits: 0,
61+
totalCost: 0,
62+
},
63+
};
64+
// eslint-disable-next-line no-console
65+
console.log(JSON.stringify(emptyOutput, null, 2));
66+
} else {
67+
// eslint-disable-next-line no-console
68+
console.log('No Amp usage data found.');
69+
}
5670
return;
5771
}
5872

‎apps/amp/src/commands/session.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,22 @@ export const sessionCommand = define({
4747
const { events, threads } = await loadAmpUsageEvents();
4848

4949
if (events.length === 0) {
50-
const output = jsonOutput
51-
? JSON.stringify({ sessions: [], totals: null })
52-
: 'No Amp usage data found.';
53-
// eslint-disable-next-line no-console
54-
console.log(output);
50+
if (jsonOutput) {
51+
const emptyTotals = {
52+
inputTokens: 0,
53+
outputTokens: 0,
54+
cacheCreationTokens: 0,
55+
cacheReadTokens: 0,
56+
totalTokens: 0,
57+
credits: 0,
58+
totalCost: 0,
59+
};
60+
// eslint-disable-next-line no-console
61+
console.log(JSON.stringify({ sessions: [], totals: emptyTotals }, null, 2));
62+
} else {
63+
// eslint-disable-next-line no-console
64+
console.log('No Amp usage data found.');
65+
}
5566
return;
5667
}
5768

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,17 @@ export const dailyCommand = define({
8282

8383
if (dailyData.length === 0) {
8484
if (useJson) {
85-
log(JSON.stringify([]));
85+
const totals = createTotalsObject({
86+
inputTokens: 0,
87+
outputTokens: 0,
88+
cacheCreationTokens: 0,
89+
cacheReadTokens: 0,
90+
totalCost: 0,
91+
});
92+
const jsonOutput = mergedOptions.instances
93+
? { projects: {}, totals }
94+
: { daily: [], totals };
95+
log(JSON.stringify(jsonOutput, null, 2));
8696
} else {
8797
logger.warn('No Claude usage data found.');
8898
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,14 @@ export const sessionCommand = define({
7676

7777
if (sessionData.length === 0) {
7878
if (useJson) {
79-
log(JSON.stringify([]));
79+
const totals = createTotalsObject({
80+
inputTokens: 0,
81+
outputTokens: 0,
82+
cacheCreationTokens: 0,
83+
cacheReadTokens: 0,
84+
totalCost: 0,
85+
});
86+
log(JSON.stringify({ sessions: [], totals }, null, 2));
8087
} else {
8188
logger.warn('No Claude usage data found.');
8289
}

‎apps/codex/src/commands/daily.ts‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,20 @@ export const dailyCommand = define({
4848
}
4949

5050
if (events.length === 0) {
51-
log(jsonOutput ? JSON.stringify({ daily: [], totals: null }) : 'No Codex usage data found.');
51+
if (jsonOutput) {
52+
const emptyTotals = {
53+
inputTokens: 0,
54+
cacheCreationTokens: 0,
55+
cacheReadTokens: 0,
56+
outputTokens: 0,
57+
reasoningOutputTokens: 0,
58+
totalTokens: 0,
59+
totalCost: 0,
60+
};
61+
log(JSON.stringify({ daily: [], totals: emptyTotals }, null, 2));
62+
} else {
63+
log('No Codex usage data found.');
64+
}
5265
return;
5366
}
5467

@@ -65,11 +78,20 @@ export const dailyCommand = define({
6578
});
6679

6780
if (rows.length === 0) {
68-
log(
69-
jsonOutput
70-
? JSON.stringify({ daily: [], totals: null })
71-
: 'No Codex usage data found for provided filters.',
72-
);
81+
if (jsonOutput) {
82+
const emptyTotals = {
83+
inputTokens: 0,
84+
cacheCreationTokens: 0,
85+
cacheReadTokens: 0,
86+
outputTokens: 0,
87+
reasoningOutputTokens: 0,
88+
totalTokens: 0,
89+
totalCost: 0,
90+
};
91+
log(JSON.stringify({ daily: [], totals: emptyTotals }, null, 2));
92+
} else {
93+
log('No Codex usage data found for provided filters.');
94+
}
7395
return;
7496
}
7597

‎apps/codex/src/commands/monthly.ts‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,20 @@ export const monthlyCommand = define({
4848
}
4949

5050
if (events.length === 0) {
51-
log(
52-
jsonOutput ? JSON.stringify({ monthly: [], totals: null }) : 'No Codex usage data found.',
53-
);
51+
if (jsonOutput) {
52+
const emptyTotals = {
53+
inputTokens: 0,
54+
cacheCreationTokens: 0,
55+
cacheReadTokens: 0,
56+
outputTokens: 0,
57+
reasoningOutputTokens: 0,
58+
totalTokens: 0,
59+
totalCost: 0,
60+
};
61+
log(JSON.stringify({ monthly: [], totals: emptyTotals }, null, 2));
62+
} else {
63+
log('No Codex usage data found.');
64+
}
5465
return;
5566
}
5667

@@ -69,7 +80,22 @@ export const monthlyCommand = define({
6980
if (rows.length === 0) {
7081
log(
7182
jsonOutput
72-
? JSON.stringify({ monthly: [], totals: null })
83+
? JSON.stringify(
84+
{
85+
monthly: [],
86+
totals: {
87+
inputTokens: 0,
88+
cacheCreationTokens: 0,
89+
cacheReadTokens: 0,
90+
outputTokens: 0,
91+
reasoningOutputTokens: 0,
92+
totalTokens: 0,
93+
totalCost: 0,
94+
},
95+
},
96+
null,
97+
2,
98+
)
7399
: 'No Codex usage data found for provided filters.',
74100
);
75101
return;

‎apps/codex/src/commands/session.ts‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,20 @@ export const sessionCommand = define({
5353
}
5454

5555
if (events.length === 0) {
56-
log(
57-
jsonOutput ? JSON.stringify({ sessions: [], totals: null }) : 'No Codex usage data found.',
58-
);
56+
if (jsonOutput) {
57+
const emptyTotals = {
58+
inputTokens: 0,
59+
cacheCreationTokens: 0,
60+
cacheReadTokens: 0,
61+
outputTokens: 0,
62+
reasoningOutputTokens: 0,
63+
totalTokens: 0,
64+
totalCost: 0,
65+
};
66+
log(JSON.stringify({ sessions: [], totals: emptyTotals }, null, 2));
67+
} else {
68+
log('No Codex usage data found.');
69+
}
5970
return;
6071
}
6172

@@ -72,11 +83,20 @@ export const sessionCommand = define({
7283
});
7384

7485
if (rows.length === 0) {
75-
log(
76-
jsonOutput
77-
? JSON.stringify({ sessions: [], totals: null })
78-
: 'No Codex usage data found for provided filters.',
79-
);
86+
if (jsonOutput) {
87+
const emptyTotals = {
88+
inputTokens: 0,
89+
cacheCreationTokens: 0,
90+
cacheReadTokens: 0,
91+
outputTokens: 0,
92+
reasoningOutputTokens: 0,
93+
totalTokens: 0,
94+
totalCost: 0,
95+
};
96+
log(JSON.stringify({ sessions: [], totals: emptyTotals }, null, 2));
97+
} else {
98+
log('No Codex usage data found for provided filters.');
99+
}
80100
return;
81101
}
82102

‎apps/opencode/src/commands/daily.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,21 @@ export const dailyCommand = define({
3939
}
4040

4141
if (entries.length === 0) {
42-
const output = jsonOutput
43-
? JSON.stringify({ daily: [], totals: null })
44-
: 'No OpenCode usage data found.';
45-
// eslint-disable-next-line no-console
46-
console.log(output);
42+
if (jsonOutput) {
43+
const emptyTotals = {
44+
inputTokens: 0,
45+
outputTokens: 0,
46+
cacheCreationTokens: 0,
47+
cacheReadTokens: 0,
48+
totalTokens: 0,
49+
totalCost: 0,
50+
};
51+
// eslint-disable-next-line no-console
52+
console.log(JSON.stringify({ daily: [], totals: emptyTotals }, null, 2));
53+
} else {
54+
// eslint-disable-next-line no-console
55+
console.log('No OpenCode usage data found.');
56+
}
4757
return;
4858
}
4959

‎apps/opencode/src/commands/monthly.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,21 @@ export const monthlyCommand = define({
3939
}
4040

4141
if (entries.length === 0) {
42-
const output = jsonOutput
43-
? JSON.stringify({ monthly: [], totals: null })
44-
: 'No OpenCode usage data found.';
45-
// eslint-disable-next-line no-console
46-
console.log(output);
42+
if (jsonOutput) {
43+
const emptyTotals = {
44+
inputTokens: 0,
45+
outputTokens: 0,
46+
cacheCreationTokens: 0,
47+
cacheReadTokens: 0,
48+
totalTokens: 0,
49+
totalCost: 0,
50+
};
51+
// eslint-disable-next-line no-console
52+
console.log(JSON.stringify({ monthly: [], totals: emptyTotals }, null, 2));
53+
} else {
54+
// eslint-disable-next-line no-console
55+
console.log('No OpenCode usage data found.');
56+
}
4757
return;
4858
}
4959

0 commit comments

Comments
 (0)