Skip to content

Commit 9592f4f

Browse files
committed
perf(statusline): bound the active block load
The active block query loaded the whole Claude history on every cold render. It now sets --since to one day behind the session window so the Claude adapter only reads recently written files. Refs #1790
1 parent 6e0f359 commit 9592f4f

1 file changed

Lines changed: 49 additions & 3 deletions

File tree

  • rust/crates/ccusage/src/commands

‎rust/crates/ccusage/src/commands/mod.rs‎

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use serde_json::json;
1313
use crate::pricing::PricingMap;
1414
use crate::{
1515
BucketKind, Color, Context, DEFAULT_RECENT_DAYS, DEFAULT_SESSION_DURATION_HOURS,
16-
MILLIS_PER_DAY, MILLIS_PER_MINUTE, Result, SessionAccumulator, TimestampMs, UsageSummary,
17-
block_json, calculate_burn_rate,
16+
MILLIS_PER_DAY, MILLIS_PER_HOUR, MILLIS_PER_MINUTE, Result, SessionAccumulator, TimestampMs,
17+
UsageSummary, block_json, calculate_burn_rate,
1818
cli::{
1919
BlocksArgs, CostSource, DailyArgs, SessionArgs, SharedArgs, SortOrder, StatuslineArgs,
2020
VisualBurnRate, WeekDay, WeeklyArgs,
@@ -451,7 +451,7 @@ fn render_statusline(
451451
})
452452
.unwrap_or(0.0);
453453

454-
let blocks = load_entries(shared, None)
454+
let blocks = load_entries(&statusline_block_shared(args, shared, utc_now()), None)
455455
.map(|entries| identify_session_blocks(entries, DEFAULT_SESSION_DURATION_HOURS))
456456
.unwrap_or_default();
457457
let active_block = blocks.iter().find(|block| block.is_active && !block.is_gap);
@@ -556,6 +556,31 @@ fn statusline_today_shared(
556556
}
557557
}
558558

559+
/// Bounds the active-block load to recently written usage files.
560+
///
561+
/// The active block only needs entries from the last session window. The
562+
/// lower bound reaches one extra day behind it so block boundaries, which chain
563+
/// from earlier entries, still line up with an unbounded load. `until` stays
564+
/// open because an active block ends in the future.
565+
fn statusline_block_shared(
566+
args: &StatuslineArgs,
567+
shared: &SharedArgs,
568+
now: TimestampMs,
569+
) -> SharedArgs {
570+
let lookback =
571+
(DEFAULT_SESSION_DURATION_HOURS * MILLIS_PER_HOUR as f64) as i64 + MILLIS_PER_DAY;
572+
let since = format_date(
573+
TimestampMs::from_millis(now.as_millis() - lookback),
574+
args.timezone.as_deref(),
575+
)
576+
.replace('-', "");
577+
SharedArgs {
578+
since: Some(since),
579+
timezone: args.timezone.clone(),
580+
..shared.clone()
581+
}
582+
}
583+
559584
fn calculate_session_cost(session_id: &str, shared: &SharedArgs) -> Result<f64> {
560585
Ok(load_entries(shared, None)?
561586
.into_iter()
@@ -1103,6 +1128,27 @@ mod tests {
11031128
);
11041129
}
11051130

1131+
#[test]
1132+
fn bounds_statusline_block_load_one_day_behind_the_session_window() {
1133+
let args = StatuslineArgs {
1134+
timezone: Some("Asia/Tokyo".to_string()),
1135+
..StatuslineArgs::default()
1136+
};
1137+
let shared = SharedArgs {
1138+
offline: true,
1139+
..SharedArgs::default()
1140+
};
1141+
// 2026-05-22T01:27:00+09:00
1142+
let now = TimestampMs::from_millis(1_779_380_820_000);
1143+
1144+
let block_shared = statusline_block_shared(&args, &shared, now);
1145+
1146+
assert_eq!(block_shared.since.as_deref(), Some("20260520"));
1147+
assert_eq!(block_shared.until, None);
1148+
assert_eq!(block_shared.timezone.as_deref(), Some("Asia/Tokyo"));
1149+
assert!(block_shared.offline);
1150+
}
1151+
11061152
#[test]
11071153
fn reuses_statusline_cache_while_fresh_and_transcript_unchanged() {
11081154
let cache = StatuslineCache {

0 commit comments

Comments
 (0)