Skip to content

Commit 5b552a9

Browse files
committed
refactor(codex): pass aggregation inputs as one run struct
Threading the replay plan through the aggregation helpers pushed `aggregate_file` to eight parameters, which fails `clippy::too_many_arguments` and the `-D warnings` clippy check in CI. Group the inputs that are constant for one aggregation run - sessions directory, file list, shared args, report kind, and replay plan - into `CodexAggregateRun`, and pass that instead. The per-file helpers keep only the state that actually varies: the file, the timezone, the dedupe shards, and the accumulator.
1 parent 6278ec3 commit 5b552a9

1 file changed

Lines changed: 57 additions & 93 deletions

File tree

‎rust/crates/ccusage/src/adapter/codex/aggregate.rs‎

Lines changed: 57 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ struct CodexAggregation {
3737
seen: FxHashSet<CodexEventKey>,
3838
}
3939

40+
/// Read-only inputs every file of one aggregation run shares.
41+
struct CodexAggregateRun<'a> {
42+
sessions_dir: &'a Path,
43+
files: &'a [PathBuf],
44+
shared: &'a SharedArgs,
45+
kind: AgentReportKind,
46+
replay_plan: &'a CodexReplayPlan,
47+
}
48+
4049
pub(crate) fn load_groups(
4150
shared: &SharedArgs,
4251
kind: AgentReportKind,
@@ -62,16 +71,18 @@ fn load_groups_from_sources(
6271
);
6372
let mut groups = BTreeMap::new();
6473
let seen = create_dedupe_shards();
65-
for group in file_groups {
74+
for group in &file_groups {
6675
merge_groups(
6776
&mut groups,
6877
aggregate_files_with_dedupe(
69-
&group.dir,
70-
&group.files,
71-
shared,
72-
kind,
78+
&CodexAggregateRun {
79+
sessions_dir: &group.dir,
80+
files: &group.files,
81+
shared,
82+
kind,
83+
replay_plan: &replay_plan,
84+
},
7385
&seen,
74-
&replay_plan,
7586
)?,
7687
);
7788
}
@@ -86,87 +97,65 @@ pub(super) fn load_groups_from_directory(
8697
let files = paths::collect_codex_usage_files(sessions_dir);
8798
let replay_plan =
8899
CodexReplayPlan::new([(sessions_dir, files.as_slice())], shared.single_thread);
100+
let run = CodexAggregateRun {
101+
sessions_dir,
102+
files: &files,
103+
shared,
104+
kind,
105+
replay_plan: &replay_plan,
106+
};
89107
if shared.single_thread {
90-
return aggregate_files_local(sessions_dir, &files, shared, kind, &replay_plan);
108+
return aggregate_files_local(&run);
91109
}
92110
let seen = create_dedupe_shards();
93-
aggregate_files_parallel(sessions_dir, &files, shared, kind, &seen, &replay_plan)
111+
aggregate_files_parallel(&run, &seen)
94112
}
95113

96114
fn aggregate_files_with_dedupe(
97-
sessions_dir: &Path,
98-
files: &[PathBuf],
99-
shared: &SharedArgs,
100-
kind: AgentReportKind,
115+
run: &CodexAggregateRun<'_>,
101116
seen: &CodexDedupeShards,
102-
replay_plan: &CodexReplayPlan,
103117
) -> Result<BTreeMap<String, CodexGroup>> {
104-
if shared.single_thread {
105-
return aggregate_files(sessions_dir, files, shared, kind, seen, replay_plan);
118+
if run.shared.single_thread {
119+
return aggregate_files(run, seen);
106120
}
107-
aggregate_files_parallel(sessions_dir, files, shared, kind, seen, replay_plan)
121+
aggregate_files_parallel(run, seen)
108122
}
109123

110124
fn aggregate_files(
111-
sessions_dir: &Path,
112-
files: &[PathBuf],
113-
shared: &SharedArgs,
114-
kind: AgentReportKind,
125+
run: &CodexAggregateRun<'_>,
115126
seen: &CodexDedupeShards,
116-
replay_plan: &CodexReplayPlan,
117127
) -> Result<BTreeMap<String, CodexGroup>> {
118128
let mut groups = BTreeMap::new();
119-
let timezone = parse_tz(shared.timezone.as_deref()).or_else(|| Some(JiffTimeZone::system()));
120-
for file in files {
121-
aggregate_file(
122-
sessions_dir,
123-
file,
124-
kind,
125-
timezone.as_ref(),
126-
shared,
127-
seen,
128-
replay_plan,
129-
&mut groups,
130-
)?;
129+
let timezone =
130+
parse_tz(run.shared.timezone.as_deref()).or_else(|| Some(JiffTimeZone::system()));
131+
for file in run.files {
132+
aggregate_file(run, file, timezone.as_ref(), seen, &mut groups)?;
131133
}
132134
Ok(groups)
133135
}
134136

135137
fn aggregate_files_parallel(
136-
sessions_dir: &Path,
137-
files: &[PathBuf],
138-
shared: &SharedArgs,
139-
kind: AgentReportKind,
138+
run: &CodexAggregateRun<'_>,
140139
seen: &CodexDedupeShards,
141-
replay_plan: &CodexReplayPlan,
142140
) -> Result<BTreeMap<String, CodexGroup>> {
143141
let worker_count = thread::available_parallelism()
144142
.map(usize::from)
145143
.unwrap_or(1)
146-
.min(files.len());
144+
.min(run.files.len());
147145
if worker_count <= 1 {
148-
return aggregate_files(sessions_dir, files, shared, kind, seen, replay_plan);
146+
return aggregate_files(run, seen);
149147
}
150148

151-
let chunks = crate::chunk_file_indexes_by_size(files, worker_count);
149+
let chunks = crate::chunk_file_indexes_by_size(run.files, worker_count);
152150
thread::scope(|scope| {
153151
let mut handles = Vec::with_capacity(chunks.len());
154152
for chunk in chunks {
155153
handles.push(scope.spawn(move || {
156154
let mut groups = BTreeMap::new();
157-
let timezone =
158-
parse_tz(shared.timezone.as_deref()).or_else(|| Some(JiffTimeZone::system()));
155+
let timezone = parse_tz(run.shared.timezone.as_deref())
156+
.or_else(|| Some(JiffTimeZone::system()));
159157
for index in chunk {
160-
aggregate_file(
161-
sessions_dir,
162-
&files[index],
163-
kind,
164-
timezone.as_ref(),
165-
shared,
166-
seen,
167-
replay_plan,
168-
&mut groups,
169-
)?;
158+
aggregate_file(run, &run.files[index], timezone.as_ref(), seen, &mut groups)?;
170159
}
171160
Result::<BTreeMap<String, CodexGroup>>::Ok(groups)
172161
}));
@@ -186,73 +175,48 @@ fn aggregate_files_parallel(
186175
}
187176

188177
fn aggregate_file(
189-
sessions_dir: &Path,
178+
run: &CodexAggregateRun<'_>,
190179
file: &Path,
191-
kind: AgentReportKind,
192180
timezone: Option<&JiffTimeZone>,
193-
shared: &SharedArgs,
194181
seen: &CodexDedupeShards,
195-
replay_plan: &CodexReplayPlan,
196182
groups: &mut BTreeMap<String, CodexGroup>,
197183
) -> Result<()> {
198184
parser::visit_codex_session_file(
199-
sessions_dir,
185+
run.sessions_dir,
200186
file,
201-
replay_plan.parent_usage(file),
202-
|event| add_event_to_groups(&event, kind, timezone, shared, seen, groups),
187+
run.replay_plan.replay_prefix(file),
188+
|event| add_event_to_groups(&event, run.kind, timezone, run.shared, seen, groups),
203189
)
204190
}
205191

206-
fn aggregate_files_local(
207-
sessions_dir: &Path,
208-
files: &[PathBuf],
209-
shared: &SharedArgs,
210-
kind: AgentReportKind,
211-
replay_plan: &CodexReplayPlan,
212-
) -> Result<BTreeMap<String, CodexGroup>> {
213-
Ok(aggregate_files_local_with_seen(sessions_dir, files, shared, kind, replay_plan)?.groups)
192+
fn aggregate_files_local(run: &CodexAggregateRun<'_>) -> Result<BTreeMap<String, CodexGroup>> {
193+
Ok(aggregate_files_local_with_seen(run)?.groups)
214194
}
215195

216-
fn aggregate_files_local_with_seen(
217-
sessions_dir: &Path,
218-
files: &[PathBuf],
219-
shared: &SharedArgs,
220-
kind: AgentReportKind,
221-
replay_plan: &CodexReplayPlan,
222-
) -> Result<CodexAggregation> {
196+
fn aggregate_files_local_with_seen(run: &CodexAggregateRun<'_>) -> Result<CodexAggregation> {
223197
let mut aggregation = CodexAggregation {
224198
groups: BTreeMap::new(),
225199
seen: FxHashSet::default(),
226200
};
227-
let timezone = parse_tz(shared.timezone.as_deref()).or_else(|| Some(JiffTimeZone::system()));
228-
for file in files {
229-
aggregate_file_local(
230-
sessions_dir,
231-
file,
232-
kind,
233-
timezone.as_ref(),
234-
shared,
235-
replay_plan,
236-
&mut aggregation,
237-
)?;
201+
let timezone =
202+
parse_tz(run.shared.timezone.as_deref()).or_else(|| Some(JiffTimeZone::system()));
203+
for file in run.files {
204+
aggregate_file_local(run, file, timezone.as_ref(), &mut aggregation)?;
238205
}
239206
Ok(aggregation)
240207
}
241208

242209
fn aggregate_file_local(
243-
sessions_dir: &Path,
210+
run: &CodexAggregateRun<'_>,
244211
file: &Path,
245-
kind: AgentReportKind,
246212
timezone: Option<&JiffTimeZone>,
247-
shared: &SharedArgs,
248-
replay_plan: &CodexReplayPlan,
249213
aggregation: &mut CodexAggregation,
250214
) -> Result<()> {
251215
parser::visit_codex_session_file(
252-
sessions_dir,
216+
run.sessions_dir,
253217
file,
254-
replay_plan.parent_usage(file),
255-
|event| add_event_to_groups_local(&event, kind, timezone, shared, aggregation),
218+
run.replay_plan.replay_prefix(file),
219+
|event| add_event_to_groups_local(&event, run.kind, timezone, run.shared, aggregation),
256220
)
257221
}
258222

0 commit comments

Comments
 (0)