MCPcopy
hub / github.com/phuryn/claude-usage / aggregate_sessions

Function aggregate_sessions

scanner.py:452–486  ·  view source on GitHub ↗

Aggregate turn data back into session-level stats.

(session_metas, turns)

Source from the content-addressed store, hash-verified

450
451
452def aggregate_sessions(session_metas, turns):
453 """Aggregate turn data back into session-level stats."""
454 from collections import defaultdict, Counter
455
456 session_stats = defaultdict(lambda: {
457 "total_input_tokens": 0,
458 "total_output_tokens": 0,
459 "total_cache_read": 0,
460 "total_cache_creation": 0,
461 "turn_count": 0,
462 "model": None,
463 })
464 session_model_counts = defaultdict(Counter)
465
466 for t in turns:
467 s = session_stats[t["session_id"]]
468 s["total_input_tokens"] += t["input_tokens"]
469 s["total_output_tokens"] += t["output_tokens"]
470 s["total_cache_read"] += t["cache_read_tokens"]
471 s["total_cache_creation"] += t["cache_creation_tokens"]
472 s["turn_count"] += 1
473 if t["model"]:
474 session_model_counts[t["session_id"]][t["model"]] += 1
475
476 for sid, counts in session_model_counts.items():
477 if counts:
478 session_stats[sid]["model"] = counts.most_common(1)[0][0]
479
480 # Merge into session_metas
481 result = []
482 for meta in session_metas:
483 sid = meta["session_id"]
484 stats = session_stats[sid]
485 result.append({**meta, **stats})
486 return result
487
488
489def upsert_sessions(conn, sessions):

Callers 3

test_aggregationMethod · 0.90
test_empty_turnsMethod · 0.90
scanFunction · 0.85

Calls

no outgoing calls

Tested by 2

test_aggregationMethod · 0.72
test_empty_turnsMethod · 0.72