MCPcopy Create free account
hub / github.com/ccusage/ccusage / run_session

Function run_session

rust/crates/ccusage/src/commands/mod.rs:144–215  ·  view source on GitHub ↗
(args: SessionArgs)

Source from the content-addressed store, hash-verified

142}
143
144pub(crate) fn run_session(args: SessionArgs) -> Result<()> {
145 let shared = args.shared.clone();
146 if let Some(id) = args.id {
147 return run_session_id(&id, &shared);
148 }
149
150 let mut session_shared = shared.clone();
151 session_shared.order = SortOrder::Desc;
152 let entries = load_entries(&session_shared, None)?;
153 let mut grouped = Vec::<SessionAccumulator>::new();
154 let mut group_indexes = FxHashMap::<(Arc<str>, Arc<str>), usize>::default();
155 for entry in &entries {
156 let key = (
157 Arc::clone(&entry.project_path),
158 Arc::clone(&entry.session_id),
159 );
160 let index = *group_indexes.entry(key).or_insert_with(|| {
161 let index = grouped.len();
162 grouped.push(SessionAccumulator::default());
163 index
164 });
165 grouped[index].add_entry(entry);
166 }
167
168 let mut rows = Vec::with_capacity(grouped.len());
169 for group in grouped {
170 rows.push(group.into_summary()?);
171 }
172 if session_shared.since.is_some() || session_shared.until.is_some() {
173 rows.retain(|row| {
174 let date = row
175 .last_activity
176 .as_deref()
177 .unwrap_or_default()
178 .replace('-', "");
179 session_shared
180 .since
181 .as_ref()
182 .is_none_or(|since| &date >= since)
183 && session_shared
184 .until
185 .as_ref()
186 .is_none_or(|until| &date <= until)
187 });
188 }
189 rows.retain(|row| {
190 row.input_tokens + row.output_tokens + row.cache_creation_tokens + row.cache_read_tokens > 0
191 });
192 rows.sort_by(|a, b| match session_shared.order {
193 SortOrder::Asc => a.total_cost.total_cmp(&b.total_cost),
194 SortOrder::Desc => b.total_cost.total_cmp(&a.total_cost),
195 });
196
197 if wants_json(&session_shared) {
198 let output = json!({
199 "sessions": rows.iter().map(session_summary_json).collect::<Vec<_>>(),
200 "totals": totals_json(&rows),
201 });

Callers 1

mainFunction · 0.85

Calls 9

run_session_idFunction · 0.85
wants_jsonFunction · 0.85
print_json_or_jqFunction · 0.85
print_usage_tableFunction · 0.85
lenMethod · 0.80
pushMethod · 0.80
load_entriesFunction · 0.50
add_entryMethod · 0.45
into_summaryMethod · 0.45

Tested by

no test coverage detected