|
15 | 15 | use std::collections::HashMap; |
16 | 16 | use std::fs; |
17 | 17 | use std::io::{BufRead, BufReader}; |
18 | | -use std::path::PathBuf; |
| 18 | +use std::path::{Path, PathBuf}; |
19 | 19 |
|
20 | 20 | use async_trait::async_trait; |
21 | 21 | use chrono::{DateTime, Duration, NaiveDate, Utc}; |
@@ -266,12 +266,43 @@ fn build_snapshot(since: &Since, group_by: GroupBy, limit: usize, prices: &Price |
266 | 266 | } |
267 | 267 |
|
268 | 268 | 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); |
269 | 273 | files |
270 | 274 | .iter() |
| 275 | + .filter(|p| filename_within_cutoff(p, cutoff)) |
271 | 276 | .flat_map(|f| read_session_file(f, since)) |
272 | 277 | .collect() |
273 | 278 | } |
274 | 279 |
|
| 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 | + |
275 | 306 | fn read_session_file(path: &PathBuf, since: &Since) -> Vec<UsageEvent> { |
276 | 307 | let Ok(file) = fs::File::open(path) else { |
277 | 308 | return Vec::new(); |
@@ -852,6 +883,60 @@ mod tests { |
852 | 883 | assert!(t.value.contains("3k"), "value was {:?}", t.value); |
853 | 884 | } |
854 | 885 |
|
| 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 | + |
855 | 940 | fn snap_with_rows() -> Snapshot { |
856 | 941 | Snapshot { |
857 | 942 | rows: vec![ |
|
0 commit comments