Skip to content

Commit bfc728f

Browse files
unhappychoiceclaude
andcommitted
perf(fetcher/codex_usage): skip JSONL files outside the since window by filename
Codex names every session file `rollout-YYYY-MM-DDTHH-MM-SS-uuid.jsonl`, so we can read the date right off the filename without opening it. Filter the list ahead of the line-by-line walk and skip everything older than the `since` cutoff (with a one-day buffer for sessions that started before midnight and spilled past it). Concrete numbers from one developer's `~/.codex/sessions/`: 1,453 JSONL files, ~527 MB total. With `since = "today"` the filter drops it to a handful of files, taking first-paint from seconds to milliseconds. For `since = "all"` no files are skipped — same behaviour as before. The per-line `since.includes(ts)` filter is still the source of truth for event-level inclusion; this commit only prunes the file *list* up front. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent a01d64f commit bfc728f

1 file changed

Lines changed: 86 additions & 1 deletion

File tree

‎src/fetcher/codex/usage.rs‎

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use std::collections::HashMap;
1616
use std::fs;
1717
use std::io::{BufRead, BufReader};
18-
use std::path::PathBuf;
18+
use std::path::{Path, PathBuf};
1919

2020
use async_trait::async_trait;
2121
use chrono::{DateTime, Duration, NaiveDate, Utc};
@@ -266,12 +266,43 @@ fn build_snapshot(since: &Since, group_by: GroupBy, limit: usize, prices: &Price
266266
}
267267

268268
fn collect_events(files: &[PathBuf], since: &Since) -> Vec<UsageEvent> {
269+
// Codex session filenames are `rollout-YYYY-MM-DDTHH-MM-SS-uuid.jsonl`; skip the whole
270+
// file when its date prefix is older than the `since` cutoff. Saves opening hundreds of
271+
// MB of JSONL on cold cache when the user just wants Today / 7d.
272+
let cutoff = cutoff_date(since);
269273
files
270274
.iter()
275+
.filter(|p| filename_within_cutoff(p, cutoff))
271276
.flat_map(|f| read_session_file(f, since))
272277
.collect()
273278
}
274279

280+
fn cutoff_date(since: &Since) -> Option<NaiveDate> {
281+
let today = Utc::now().date_naive();
282+
match since {
283+
Since::All => None,
284+
// One-day buffer covers sessions that started before midnight and spilled past it.
285+
Since::Today => Some(today - Duration::days(1)),
286+
Since::Days(n) => Some(today - Duration::days(*n)),
287+
}
288+
}
289+
290+
fn filename_within_cutoff(path: &Path, cutoff: Option<NaiveDate>) -> bool {
291+
let Some(cutoff) = cutoff else {
292+
return true;
293+
};
294+
parse_filename_date(path).is_none_or(|date| date >= cutoff)
295+
}
296+
297+
fn parse_filename_date(path: &Path) -> Option<NaiveDate> {
298+
let name = path.file_name()?.to_str()?;
299+
let rest = name.strip_prefix("rollout-")?;
300+
if rest.len() < 10 {
301+
return None;
302+
}
303+
NaiveDate::parse_from_str(&rest[..10], "%Y-%m-%d").ok()
304+
}
305+
275306
fn read_session_file(path: &PathBuf, since: &Since) -> Vec<UsageEvent> {
276307
let Ok(file) = fs::File::open(path) else {
277308
return Vec::new();
@@ -852,6 +883,60 @@ mod tests {
852883
assert!(t.value.contains("3k"), "value was {:?}", t.value);
853884
}
854885

886+
#[test]
887+
fn parse_filename_date_extracts_iso_prefix_from_rollout_name() {
888+
let p = PathBuf::from("rollout-2026-05-23T03-37-58-uuid.jsonl");
889+
let date = parse_filename_date(&p).expect("must parse");
890+
assert_eq!(date.to_string(), "2026-05-23");
891+
}
892+
893+
#[test]
894+
fn parse_filename_date_returns_none_for_unexpected_names() {
895+
assert!(parse_filename_date(&PathBuf::from("session.jsonl")).is_none());
896+
assert!(parse_filename_date(&PathBuf::from("rollout-notadate.jsonl")).is_none());
897+
}
898+
899+
#[test]
900+
fn filename_within_cutoff_keeps_files_inside_window_and_drops_older() {
901+
let today = Utc::now().date_naive();
902+
let cutoff = Some(today - Duration::days(6));
903+
let recent = PathBuf::from(format!("rollout-{today}T00-00-00-x.jsonl"));
904+
let edge = PathBuf::from(format!(
905+
"rollout-{}T23-59-59-x.jsonl",
906+
today - Duration::days(6)
907+
));
908+
let ancient = PathBuf::from(format!(
909+
"rollout-{}T12-00-00-x.jsonl",
910+
today - Duration::days(60)
911+
));
912+
assert!(filename_within_cutoff(&recent, cutoff));
913+
assert!(filename_within_cutoff(&edge, cutoff));
914+
assert!(!filename_within_cutoff(&ancient, cutoff));
915+
}
916+
917+
#[test]
918+
fn filename_within_cutoff_keeps_unparseable_names_conservatively() {
919+
// If we can't parse the date, we must not skip the file — falling through to the
920+
// per-line `since.includes(ts)` filter is the safe path.
921+
let cutoff = Some(Utc::now().date_naive());
922+
assert!(filename_within_cutoff(
923+
&PathBuf::from("session.jsonl"),
924+
cutoff
925+
));
926+
}
927+
928+
#[test]
929+
fn cutoff_date_is_none_for_all_and_one_day_buffer_for_today() {
930+
assert!(cutoff_date(&Since::All).is_none());
931+
let today = Utc::now().date_naive();
932+
// Today's cutoff buffers one day so sessions that started before midnight are kept.
933+
assert_eq!(cutoff_date(&Since::Today), Some(today - Duration::days(1)));
934+
assert_eq!(
935+
cutoff_date(&Since::Days(7)),
936+
Some(today - Duration::days(7))
937+
);
938+
}
939+
855940
fn snap_with_rows() -> Snapshot {
856941
Snapshot {
857942
rows: vec![

0 commit comments

Comments
 (0)