Skip to content

Commit 39fefa2

Browse files
ryoppippijgorres
andauthored
fix(cli): accept --start-of-week on top-level weekly (ccusage#1945)
Since 20.x, `ccusage weekly` is the unified all-agent report and was parsed by the generic all-agent path, which only accepted shared flags. Only `ccusage claude weekly` still handled `-w/--start-of-week`, so the documented `ccusage weekly -w monday` failed with "Unknown option '-w'" and the flag was missing from `ccusage weekly --help`. Parse `-w/--start-of-week` (and `commands.weekly.startOfWeek` config) for the top-level weekly report, carry it on AgentCommandArgs, and use it for both the unified week bucketing and the `--last` window. The default stays Monday, so existing output is unchanged. Docs and loader tests cover the new behaviour. Co-authored-by: jgorres <[email protected]>
1 parent aa7c675 commit 39fefa2

12 files changed

Lines changed: 259 additions & 46 deletions

File tree

‎docs/guide/cli-options.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ ccusage daily --last 7
4242
ccusage monthly --last 3
4343
```
4444

45-
The count is inclusive of the current period, so `--last 2` on a daily report covers yesterday and today. Weeks start on the same day the report buckets by, which is Monday everywhere except `ccusage claude weekly`, where `--start-of-week` decides.
45+
The count is inclusive of the current period, so `--last 2` on a daily report covers yesterday and today. Weeks start on the same day the report buckets by: Monday by default and Sunday for `ccusage claude weekly`, unless `commands.weekly.startOfWeek` in a [configuration file](/guide/config-files) or `--start-of-week` on `ccusage weekly` or `ccusage claude weekly` sets another day, with the flag taking precedence.
4646

4747
`--last` works on every daily, weekly, and monthly report, including the per-agent ones such as `ccusage codex daily --last 1`. It is not available on `session`, `blocks`, or `statusline`, which have no calendar period, and it cannot be combined with `--since`, `--until`, or `--sections`.
4848

‎docs/guide/weekly-reports.md‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ The columns are identical to daily reports but aggregated by week:
4747
Configure which day starts the week:
4848

4949
```bash
50-
# Start week on Sunday (default)
51-
ccusage weekly --start-of-week sunday
52-
53-
# Start week on Monday
50+
# Start week on Monday (default for ccusage weekly)
5451
ccusage weekly --start-of-week monday
5552
ccusage weekly -w monday
5653

54+
# Start week on Sunday
55+
ccusage weekly --start-of-week sunday
56+
57+
# ccusage claude weekly starts on Sunday by default
58+
ccusage claude weekly
59+
5760
# Other options: tuesday, wednesday, thursday, friday, saturday
5861
```
5962

‎rust/crates/ccusage-adapter-all/src/lib.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ pub fn run(args: AgentCommandArgs) -> Result<()> {
3939
let kind = args.kind;
4040
let shared = args.shared;
4141
let include_agents = args.by_agent;
42+
let start_of_week = args.start_of_week;
4243
if let Some(sections) = args.sections {
4344
let sections = requested_sections(kind, sections);
44-
let result = loader::load_sections(&sections, &shared)?;
45+
let result = loader::load_sections(&sections, &shared, start_of_week)?;
4546
if wants_json(&shared) {
4647
return report::print_sections_report_json(
4748
&result.sections,
@@ -61,7 +62,7 @@ pub fn run(args: AgentCommandArgs) -> Result<()> {
6162
}
6263
return Ok(());
6364
}
64-
let result = loader::load_rows(kind, &shared)?;
65+
let result = loader::load_rows(kind, &shared, start_of_week)?;
6566
if wants_json(&shared) {
6667
let output = report::report_json_with_agents(&result.rows, kind, include_agents);
6768
return print_json_or_jq(output, shared.jq.as_deref(), shared.no_cost);

‎rust/crates/ccusage-adapter-all/src/loader.rs‎

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,24 @@ use super::{
2626
},
2727
};
2828

29-
pub(super) fn load_rows(kind: AgentReportKind, shared: &SharedArgs) -> Result<AllLoadResult> {
29+
pub(super) fn load_rows(
30+
kind: AgentReportKind,
31+
shared: &SharedArgs,
32+
start_of_week: WeekDay,
33+
) -> Result<AllLoadResult> {
3034
let pricing = load_pricing(shared);
3135
let load_kind = load_kind_for_report(kind);
3236
let loaded = load_base_rows(load_kind, shared, &pricing)?;
3337
Ok(AllLoadResult {
34-
rows: finish_rows(kind, loaded.rows, shared),
38+
rows: finish_rows(kind, loaded.rows, shared, start_of_week),
3539
detected_agents: loaded.detected_agents,
3640
})
3741
}
3842

3943
pub(super) fn load_sections(
4044
kinds: &[AgentReportKind],
4145
shared: &SharedArgs,
46+
start_of_week: WeekDay,
4247
) -> Result<AllSectionsLoadResult> {
4348
let pricing = load_pricing(shared);
4449
let daily_base = needs_daily_family(kinds)
@@ -62,12 +67,12 @@ pub(super) fn load_sections(
6267
let rows = if *kind == AgentReportKind::Session {
6368
session_base
6469
.as_ref()
65-
.map(|base| finish_rows(*kind, base.rows.clone(), shared))
70+
.map(|base| finish_rows(*kind, base.rows.clone(), shared, start_of_week))
6671
.unwrap_or_default()
6772
} else {
6873
daily_base
6974
.as_ref()
70-
.map(|base| finish_rows(*kind, base.rows.clone(), shared))
75+
.map(|base| finish_rows(*kind, base.rows.clone(), shared, start_of_week))
7176
.unwrap_or_default()
7277
};
7378
sections.push((*kind, rows));
@@ -395,7 +400,12 @@ fn load_base_rows(
395400
})
396401
}
397402

398-
fn finish_rows(kind: AgentReportKind, mut rows: Vec<AllRow>, shared: &SharedArgs) -> Vec<AllRow> {
403+
fn finish_rows(
404+
kind: AgentReportKind,
405+
mut rows: Vec<AllRow>,
406+
shared: &SharedArgs,
407+
start_of_week: WeekDay,
408+
) -> Vec<AllRow> {
399409
if kind == AgentReportKind::Session {
400410
for row in &mut rows {
401411
row.metadata_agents = None;
@@ -414,7 +424,7 @@ fn finish_rows(kind: AgentReportKind, mut rows: Vec<AllRow>, shared: &SharedArgs
414424
return rows;
415425
}
416426

417-
let mut aggregated = aggregate_rows(rows, kind);
427+
let mut aggregated = aggregate_rows(rows, kind, start_of_week);
418428
sort_rows(&mut aggregated, &shared.order);
419429
aggregated
420430
}
@@ -886,7 +896,11 @@ where
886896
}
887897
}
888898

889-
pub(super) fn aggregate_rows(rows: Vec<AllRow>, kind: AgentReportKind) -> Vec<AllRow> {
899+
pub(super) fn aggregate_rows(
900+
rows: Vec<AllRow>,
901+
kind: AgentReportKind,
902+
start_of_week: WeekDay,
903+
) -> Vec<AllRow> {
890904
let mut groups = BTreeMap::<String, AllAccumulator>::new();
891905
for mut row in rows {
892906
let period = match kind {
@@ -895,8 +909,9 @@ pub(super) fn aggregate_rows(rows: Vec<AllRow>, kind: AgentReportKind) -> Vec<Al
895909
.period
896910
.get(..7)
897911
.map_or_else(|| row.period.clone(), str::to_string),
898-
AgentReportKind::Weekly => crate::week_start(&row.period, WeekDay::Monday)
899-
.unwrap_or_else(|| row.period.clone()),
912+
AgentReportKind::Weekly => {
913+
crate::week_start(&row.period, start_of_week).unwrap_or_else(|| row.period.clone())
914+
}
900915
AgentReportKind::Session => row.period.clone(),
901916
};
902917
row.period = period.clone();
@@ -957,7 +972,7 @@ mod tests {
957972
sorting_row("z-cheap", "claude", 1.0),
958973
sorting_row("b-zero", "claude", 0.0),
959974
];
960-
let sorted = finish_rows(AgentReportKind::Session, rows, &shared);
975+
let sorted = finish_rows(AgentReportKind::Session, rows, &shared, WeekDay::Monday);
961976
let keys: Vec<_> = sorted
962977
.iter()
963978
.map(|row| (row.period.as_str(), row.agent))
@@ -991,7 +1006,7 @@ mod tests {
9911006
order_explicit,
9921007
..SharedArgs::default()
9931008
};
994-
let sorted = finish_rows(AgentReportKind::Session, rows, &shared);
1009+
let sorted = finish_rows(AgentReportKind::Session, rows, &shared, WeekDay::Monday);
9951010
let keys: Vec<_> = sorted
9961011
.iter()
9971012
.map(|row| (row.period.as_str(), row.agent))
@@ -1012,11 +1027,20 @@ mod tests {
10121027
#[test]
10131028
fn session_cost_order_handles_empty_and_single_row_reports() {
10141029
let shared = SharedArgs::default();
1015-
assert!(finish_rows(AgentReportKind::Session, Vec::new(), &shared).is_empty());
1030+
assert!(
1031+
finish_rows(
1032+
AgentReportKind::Session,
1033+
Vec::new(),
1034+
&shared,
1035+
WeekDay::Monday
1036+
)
1037+
.is_empty()
1038+
);
10161039
let rows = finish_rows(
10171040
AgentReportKind::Session,
10181041
vec![sorting_row("only-session", "claude", 0.0)],
10191042
&shared,
1043+
WeekDay::Monday,
10201044
);
10211045
assert_eq!(rows.len(), 1);
10221046
assert_eq!(rows[0].period, "only-session");
@@ -1042,7 +1066,7 @@ mod tests {
10421066
order,
10431067
..SharedArgs::default()
10441068
};
1045-
let sorted = finish_rows(kind, rows, &shared);
1069+
let sorted = finish_rows(kind, rows, &shared, WeekDay::Monday);
10461070
let costs: Vec<_> = sorted.iter().map(|row| row.total_cost).collect();
10471071
assert_eq!(costs, expected_costs, "{kind:?} {order:?}");
10481072
}
@@ -1204,7 +1228,7 @@ mod tests {
12041228
..SharedArgs::default()
12051229
};
12061230

1207-
let result = load_rows(AgentReportKind::Session, &shared).unwrap();
1231+
let result = load_rows(AgentReportKind::Session, &shared, WeekDay::Monday).unwrap();
12081232
let omp_rows = result
12091233
.rows
12101234
.iter()
@@ -1277,7 +1301,7 @@ mod tests {
12771301
..SharedArgs::default()
12781302
};
12791303

1280-
let result = load_rows(AgentReportKind::Session, &shared).unwrap();
1304+
let result = load_rows(AgentReportKind::Session, &shared, WeekDay::Monday).unwrap();
12811305
let pi_rows = result
12821306
.rows
12831307
.iter()
@@ -1440,7 +1464,7 @@ mod tests {
14401464
..SharedArgs::default()
14411465
};
14421466

1443-
let Err(error) = load_rows(AgentReportKind::Daily, &shared) else {
1467+
let Err(error) = load_rows(AgentReportKind::Daily, &shared, WeekDay::Monday) else {
14441468
panic!("expected default pi path collision");
14451469
};
14461470

@@ -1478,7 +1502,7 @@ mod tests {
14781502
..SharedArgs::default()
14791503
};
14801504

1481-
let Err(error) = load_rows(AgentReportKind::Daily, &shared) else {
1505+
let Err(error) = load_rows(AgentReportKind::Daily, &shared, WeekDay::Monday) else {
14821506
panic!("expected named pi store path collision");
14831507
};
14841508

@@ -1510,7 +1534,7 @@ mod tests {
15101534
..SharedArgs::default()
15111535
};
15121536

1513-
let Err(error) = load_rows(AgentReportKind::Daily, &shared) else {
1537+
let Err(error) = load_rows(AgentReportKind::Daily, &shared, WeekDay::Monday) else {
15141538
panic!("expected nested default pi path collision");
15151539
};
15161540

@@ -1549,7 +1573,7 @@ mod tests {
15491573
..SharedArgs::default()
15501574
};
15511575

1552-
let Err(error) = load_rows(AgentReportKind::Daily, &shared) else {
1576+
let Err(error) = load_rows(AgentReportKind::Daily, &shared, WeekDay::Monday) else {
15531577
panic!("expected partial named pi store path collision");
15541578
};
15551579

@@ -1578,7 +1602,7 @@ mod tests {
15781602
..SharedArgs::default()
15791603
};
15801604

1581-
let result = load_rows(AgentReportKind::Daily, &shared).unwrap();
1605+
let result = load_rows(AgentReportKind::Daily, &shared, WeekDay::Monday).unwrap();
15821606

15831607
assert!(result.detected_agents.contains(&"omp"));
15841608
assert_eq!(result.rows.len(), 1);
@@ -1619,7 +1643,7 @@ mod tests {
16191643
.rows,
16201644
);
16211645

1622-
let aggregated = aggregate_rows(rows, AgentReportKind::Daily);
1646+
let aggregated = aggregate_rows(rows, AgentReportKind::Daily, WeekDay::Monday);
16231647

16241648
assert_eq!(aggregated.len(), 1);
16251649
assert_eq!(aggregated[0].metadata_agents, Some(vec!["omp", "pi"]));

0 commit comments

Comments
 (0)