|
14 | 14 | use std::collections::HashMap; |
15 | 15 | use std::fs; |
16 | 16 | use std::io::{BufRead, BufReader}; |
17 | | -use std::path::PathBuf; |
| 17 | +use std::path::{Path, PathBuf}; |
18 | 18 |
|
19 | 19 | use async_trait::async_trait; |
20 | 20 | use chrono::{DateTime, Duration, NaiveDate, Utc}; |
@@ -285,13 +285,43 @@ fn read_sessions(dir: &PathBuf, since: &Since) -> Vec<UsageEvent> { |
285 | 285 | let Ok(entries) = fs::read_dir(dir) else { |
286 | 286 | return Vec::new(); |
287 | 287 | }; |
| 288 | + // Claude session files are `<uuid>.jsonl` — no date in the filename. mtime is the only |
| 289 | + // cheap signal we have for "was this session touched in the window?", so stat each file |
| 290 | + // (one syscall, ~10µs) and skip the open+line-parse path when the file is stale. |
| 291 | + let cutoff = mtime_cutoff(since); |
288 | 292 | entries |
289 | 293 | .flatten() |
290 | 294 | .filter(|e| e.path().extension().is_some_and(|x| x == "jsonl")) |
| 295 | + .filter(|e| mtime_within_cutoff(&e.path(), cutoff)) |
291 | 296 | .flat_map(|e| read_session_file(&e.path(), since)) |
292 | 297 | .collect() |
293 | 298 | } |
294 | 299 |
|
| 300 | +fn mtime_cutoff(since: &Since) -> Option<NaiveDate> { |
| 301 | + let today = Utc::now().date_naive(); |
| 302 | + match since { |
| 303 | + Since::All => None, |
| 304 | + // One-day buffer covers sessions Claude touched late yesterday whose mtime races our |
| 305 | + // midnight comparison. |
| 306 | + Since::Today => Some(today - Duration::days(1)), |
| 307 | + Since::Days(n) => Some(today - Duration::days(*n)), |
| 308 | + } |
| 309 | +} |
| 310 | + |
| 311 | +fn mtime_within_cutoff(path: &Path, cutoff: Option<NaiveDate>) -> bool { |
| 312 | + let Some(cutoff) = cutoff else { |
| 313 | + return true; |
| 314 | + }; |
| 315 | + let Ok(meta) = fs::metadata(path) else { |
| 316 | + return true; |
| 317 | + }; |
| 318 | + let Ok(mtime) = meta.modified() else { |
| 319 | + return true; |
| 320 | + }; |
| 321 | + let mtime_date = DateTime::<Utc>::from(mtime).date_naive(); |
| 322 | + mtime_date >= cutoff |
| 323 | +} |
| 324 | + |
295 | 325 | fn read_session_file(path: &PathBuf, since: &Since) -> Vec<UsageEvent> { |
296 | 326 | let Ok(file) = fs::File::open(path) else { |
297 | 327 | return Vec::new(); |
@@ -956,6 +986,48 @@ mod tests { |
956 | 986 | assert!(t.value.contains("3k"), "value was {:?}", t.value); |
957 | 987 | } |
958 | 988 |
|
| 989 | + #[test] |
| 990 | + fn mtime_cutoff_is_none_for_all_and_one_day_buffer_for_today() { |
| 991 | + assert!(mtime_cutoff(&Since::All).is_none()); |
| 992 | + let today = Utc::now().date_naive(); |
| 993 | + assert_eq!(mtime_cutoff(&Since::Today), Some(today - Duration::days(1))); |
| 994 | + assert_eq!( |
| 995 | + mtime_cutoff(&Since::Days(7)), |
| 996 | + Some(today - Duration::days(7)) |
| 997 | + ); |
| 998 | + } |
| 999 | + |
| 1000 | + #[test] |
| 1001 | + fn mtime_within_cutoff_keeps_freshly_written_files() { |
| 1002 | + // A file written now has mtime = now → should always be within any non-empty cutoff. |
| 1003 | + let tmp = tempdir().unwrap(); |
| 1004 | + let fresh = tmp.path().join("recent.jsonl"); |
| 1005 | + fs::write(&fresh, "").unwrap(); |
| 1006 | + let cutoff = mtime_cutoff(&Since::Today); |
| 1007 | + assert!(mtime_within_cutoff(&fresh, cutoff)); |
| 1008 | + } |
| 1009 | + |
| 1010 | + #[test] |
| 1011 | + fn mtime_within_cutoff_keeps_unreadable_files_conservatively() { |
| 1012 | + // If we can't stat the path, the safe thing is to let `read_session_file` try and |
| 1013 | + // fail silently — the per-line `since.includes(ts)` filter is the source of truth. |
| 1014 | + let cutoff = Some(Utc::now().date_naive()); |
| 1015 | + assert!(mtime_within_cutoff( |
| 1016 | + Path::new("/nonexistent/path/does/not/exist.jsonl"), |
| 1017 | + cutoff |
| 1018 | + )); |
| 1019 | + } |
| 1020 | + |
| 1021 | + #[test] |
| 1022 | + fn mtime_within_cutoff_lets_anything_through_when_cutoff_is_none() { |
| 1023 | + // Since::All path: cutoff is None, every file (even nonexistent) passes. |
| 1024 | + let tmp = tempdir().unwrap(); |
| 1025 | + let f = tmp.path().join("any.jsonl"); |
| 1026 | + fs::write(&f, "").unwrap(); |
| 1027 | + assert!(mtime_within_cutoff(&f, None)); |
| 1028 | + assert!(mtime_within_cutoff(Path::new("/nonexistent.jsonl"), None)); |
| 1029 | + } |
| 1030 | + |
959 | 1031 | #[test] |
960 | 1032 | fn day_grouping_buckets_by_calendar_date() { |
961 | 1033 | let day1 = Utc.with_ymd_and_hms(2026, 5, 5, 10, 0, 0).unwrap(); |
|
0 commit comments