Skip to content

Commit 64a67f7

Browse files
committed
fix(ccusage-opencode): Add support for --since and --until flags
Currently, the `--since` and `--until` flags are ignored in `ccusage-opencode`. When running commands like `ccusage-opencode daily --since 20260101 --json`, the output includes all data regardless of the date filter specified by `--since`. This commit fixes the issue. We also create `date-utils.ts` and `_shared-args.ts` to extract repeated code from ccusage-opencode commands. Fixes: ccusage#801
1 parent 41a699c commit 64a67f7

6 files changed

Lines changed: 268 additions & 44 deletions

File tree

‎apps/opencode/src/_shared-args.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import type { Args } from 'gunshi';
2+
import * as v from 'valibot';
3+
4+
/**
5+
* Filter date schema for YYYYMMDD format (e.g., 20250125)
6+
*/
7+
const filterDateRegex = /^\d{8}$/;
8+
export const filterDateSchema = v.pipe(
9+
v.string(),
10+
v.regex(filterDateRegex, 'Date must be in YYYYMMDD format (e.g., 20250125)'),
11+
v.brand('FilterDate'),
12+
);
13+
14+
/**
15+
* Parses and validates a date argument in YYYYMMDD format
16+
* @param value - Date string to parse
17+
* @returns Validated date string
18+
*/
19+
function parseDateArg(value: string): string {
20+
return v.parse(filterDateSchema, value);
21+
}
22+
23+
/**
24+
* Shared command line arguments used across multiple opencode CLI commands
25+
*/
26+
export const sharedArgs = {
27+
since: {
28+
type: 'custom',
29+
short: 's',
30+
description: 'Filter from date (YYYYMMDD format, e.g., 20250125)',
31+
parse: parseDateArg,
32+
},
33+
until: {
34+
type: 'custom',
35+
short: 'u',
36+
description: 'Filter until date (YYYYMMDD format, e.g., 20250130)',
37+
parse: parseDateArg,
38+
},
39+
json: {
40+
type: 'boolean',
41+
short: 'j',
42+
description: 'Output in JSON format',
43+
},
44+
compact: {
45+
type: 'boolean',
46+
description: 'Force compact table mode',
47+
},
48+
} as const satisfies Args;

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,18 @@ import {
1010
import { groupBy } from 'es-toolkit';
1111
import { define } from 'gunshi';
1212
import pc from 'picocolors';
13+
import { sharedArgs } from '../_shared-args.ts';
1314
import { calculateCostForEntry } from '../cost-utils.ts';
1415
import { loadOpenCodeMessages } from '../data-loader.ts';
16+
import { isDateInRange } from '../date-utils.ts';
1517
import { logger } from '../logger.ts';
1618

1719
const TABLE_COLUMN_COUNT = 8;
1820

1921
export const dailyCommand = define({
2022
name: 'daily',
2123
description: 'Show OpenCode token usage grouped by day',
22-
args: {
23-
json: {
24-
type: 'boolean',
25-
short: 'j',
26-
description: 'Output in JSON format',
27-
},
28-
compact: {
29-
type: 'boolean',
30-
description: 'Force compact table mode',
31-
},
32-
},
24+
args: sharedArgs,
3325
async run(ctx) {
3426
const jsonOutput = Boolean(ctx.values.json);
3527

@@ -39,6 +31,13 @@ export const dailyCommand = define({
3931

4032
let entries = await loadOpenCodeMessages();
4133

34+
const since = ctx.values.since ?? null;
35+
const until = ctx.values.until ?? null;
36+
37+
if (since != null || until != null) {
38+
entries = entries.filter((entry) => isDateInRange(entry.timestamp, since, until));
39+
}
40+
4241
if (entries.length === 0) {
4342
const output = jsonOutput
4443
? JSON.stringify({ daily: [], totals: null })

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,18 @@ import {
1010
import { groupBy } from 'es-toolkit';
1111
import { define } from 'gunshi';
1212
import pc from 'picocolors';
13+
import { sharedArgs } from '../_shared-args.ts';
1314
import { calculateCostForEntry } from '../cost-utils.ts';
1415
import { loadOpenCodeMessages } from '../data-loader.ts';
16+
import { isDateInRange } from '../date-utils.ts';
1517
import { logger } from '../logger.ts';
1618

1719
const TABLE_COLUMN_COUNT = 8;
1820

1921
export const monthlyCommand = define({
2022
name: 'monthly',
2123
description: 'Show OpenCode token usage grouped by month',
22-
args: {
23-
json: {
24-
type: 'boolean',
25-
short: 'j',
26-
description: 'Output in JSON format',
27-
},
28-
compact: {
29-
type: 'boolean',
30-
description: 'Force compact table mode',
31-
},
32-
},
24+
args: sharedArgs,
3325
async run(ctx) {
3426
const jsonOutput = Boolean(ctx.values.json);
3527

@@ -39,6 +31,13 @@ export const monthlyCommand = define({
3931

4032
let entries = await loadOpenCodeMessages();
4133

34+
const since = ctx.values.since ?? null;
35+
const until = ctx.values.until ?? null;
36+
37+
if (since != null || until != null) {
38+
entries = entries.filter((entry) => isDateInRange(entry.timestamp, since, until));
39+
}
40+
4241
if (entries.length === 0) {
4342
const output = jsonOutput
4443
? JSON.stringify({ monthly: [], totals: null })

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,18 @@ import {
1010
import { groupBy } from 'es-toolkit';
1111
import { define } from 'gunshi';
1212
import pc from 'picocolors';
13+
import { sharedArgs } from '../_shared-args.ts';
1314
import { calculateCostForEntry } from '../cost-utils.ts';
1415
import { loadOpenCodeMessages, loadOpenCodeSessions } from '../data-loader.ts';
16+
import { isDateInRange } from '../date-utils.ts';
1517
import { logger } from '../logger.ts';
1618

1719
const TABLE_COLUMN_COUNT = 8;
1820

1921
export const sessionCommand = define({
2022
name: 'session',
2123
description: 'Show OpenCode token usage grouped by session',
22-
args: {
23-
json: {
24-
type: 'boolean',
25-
short: 'j',
26-
description: 'Output in JSON format',
27-
},
28-
compact: {
29-
type: 'boolean',
30-
description: 'Force compact table mode',
31-
},
32-
},
24+
args: sharedArgs,
3325
async run(ctx) {
3426
const jsonOutput = Boolean(ctx.values.json);
3527

@@ -39,6 +31,13 @@ export const sessionCommand = define({
3931

4032
let entries = await loadOpenCodeMessages();
4133

34+
const since = ctx.values.since ?? null;
35+
const until = ctx.values.until ?? null;
36+
37+
if (since != null || until != null) {
38+
entries = entries.filter((entry) => isDateInRange(entry.timestamp, since, until));
39+
}
40+
4241
const sessionMetadataMap = await loadOpenCodeSessions();
4342

4443
if (entries.length === 0) {

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import {
1010
import { groupBy } from 'es-toolkit';
1111
import { define } from 'gunshi';
1212
import pc from 'picocolors';
13+
import { sharedArgs } from '../_shared-args.ts';
1314
import { calculateCostForEntry } from '../cost-utils.ts';
1415
import { loadOpenCodeMessages } from '../data-loader.ts';
16+
import { isDateInRange } from '../date-utils.ts';
1517
import { logger } from '../logger.ts';
1618

1719
const TABLE_COLUMN_COUNT = 8;
@@ -44,17 +46,7 @@ function getISOWeek(date: Date): string {
4446
export const weeklyCommand = define({
4547
name: 'weekly',
4648
description: 'Show OpenCode token usage grouped by week (ISO week format)',
47-
args: {
48-
json: {
49-
type: 'boolean',
50-
short: 'j',
51-
description: 'Output in JSON format',
52-
},
53-
compact: {
54-
type: 'boolean',
55-
description: 'Force compact table mode',
56-
},
57-
},
49+
args: sharedArgs,
5850
async run(ctx) {
5951
const jsonOutput = Boolean(ctx.values.json);
6052

@@ -64,6 +56,13 @@ export const weeklyCommand = define({
6456

6557
let entries = await loadOpenCodeMessages();
6658

59+
const since = ctx.values.since ?? null;
60+
const until = ctx.values.until ?? null;
61+
62+
if (since != null || until != null) {
63+
entries = entries.filter((entry) => isDateInRange(entry.timestamp, since, until));
64+
}
65+
6766
if (entries.length === 0) {
6867
const output = jsonOutput
6968
? JSON.stringify({ weekly: [], totals: null })

0 commit comments

Comments
 (0)