Skip to content

Commit 6d53c57

Browse files
ryoppippijusti
andauthored
fix(opencode): honor --since/--until in the SQLite loader and JSON scan (#1492)
The OpenCode loader read every SQLite row and re-parsed every legacy JSON message on each invocation, applying --since/--until only after everything had been loaded. Narrow the work in the loader instead: - Resolve the bounds to half-open millisecond instants in the reporting timezone, so the window means the same instants the report means and there is no slack to justify. - Push the bounds into SQL through a subquery selecting `id` alone, which the (session_id, time_created, id) index answers as a covering scan, so out-of-range rows never have their `data` blob read. - Skip the full parse for rows and files outside the window using a bounded scan of the payload's own `time.created`. - Collapse the three copies of the window predicate into one `date_within_range`. Every pre-filter fails open: partial or non-ASCII bounds, a legacy schema with no `time_created`, a column that is not millisecond scale, a missing payload timestamp, and column/payload drift up to a day all defer to the authoritative per-entry check. Source detection asks the filesystem, so the aggregate report still lists OpenCode when the window excludes everything. A 7-day window against a synthetic 200k-row / 0.83 GB database goes from 1.133 s to 34.0 ms. Output is byte-identical to main across 67 real-data comparisons spanning five timezones and twelve window shapes. Supersedes #1188. Reported in #801 and requested in #867. Co-authored-by: Justyna Wojtczak <[email protected]>
1 parent d7d2dd7 commit 6d53c57

6 files changed

Lines changed: 981 additions & 17 deletions

File tree

‎rust/adapters/common/src/lib.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
thread,
55
};
66

7-
use ccusage_core::{LoadedEntry, cli::SharedArgs};
7+
use ccusage_core::{LoadedEntry, cli::SharedArgs, date_within_range};
88

99
pub mod jsonl;
1010
pub mod report;
@@ -38,9 +38,11 @@ pub fn filter_loaded_entries_by_date(entries: &mut Vec<LoadedEntry>, shared: &Sh
3838
return;
3939
}
4040
entries.retain(|entry| {
41-
let date = entry.date.replace('-', "");
42-
shared.since.as_ref().is_none_or(|since| &date >= since)
43-
&& shared.until.as_ref().is_none_or(|until| &date <= until)
41+
date_within_range(
42+
&entry.date,
43+
shared.since.as_deref(),
44+
shared.until.as_deref(),
45+
)
4446
});
4547
}
4648

‎rust/adapters/opencode/src/lib.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ use crate::{
1515
Result, cli::AgentCommandArgs, print_json_or_jq, print_usage_table, sort_summaries, wants_json,
1616
};
1717

18+
/// Reports whether an OpenCode install has usage data, independent of the
19+
/// requested date window.
20+
///
21+
/// The aggregate report lists which agents it found, and the loader narrows to
22+
/// `--since`/`--until` as it reads, so an out-of-range query cannot answer that
23+
/// question from the loaded entries alone.
24+
pub fn has_data() -> bool {
25+
paths::paths().is_ok_and(|paths| paths.iter().any(|path| loader::has_source(path)))
26+
}
27+
1828
pub fn run(args: AgentCommandArgs) -> Result<()> {
1929
let shared = args.shared;
2030
let mut entries = loader::load_entries(&shared)?;

0 commit comments

Comments
 (0)