Skip to main content

Module aggregate

Module aggregate 

Source
Expand description

Deterministic transforms — slice 2 of folding usage_signal.py into Rust (ROADMAP E1). Ported 1:1 from empty_metrics/_add_metrics, split_logical_sessions, _empty_bucket/_accumulate, bucket_aggregates, and project_name_from_cwd. Pure given (events, NOW, UTC offset) — pinned by a golden fixture generated from the Python (tests/aggregate_golden.rs).

Day/week/month bucketing uses a fixed UTC offset (seconds east of UTC). The Python uses the system local tz via astimezone(); for fixed-offset zones (e.g. Türkiye, permanent UTC+3) that is identical, and the golden pins it with TZ=UTC / offset 0. DST-aware per-timestamp offsets are a later refinement (would need a tz database); documented in COST_MODEL/ROADMAP.

Structs§

Buckets
Result of bucket_aggregates — mirrors the snapshot’s aggregate fields.
FileEvents
One transcript file’s per-turn events plus its resolved model/cwd.
Session
A logical session chunk that feeds bucket_aggregates.
TurnMetrics
Per-turn token buckets + estimated cost. total is the stats token total (fresh_in + output) the parser computes; the four buckets feed the cost view.

Constants§

SESSION_IDLE_GAP
Idle gap (seconds) that splits one transcript file into logical sessions — matches Claude’s 5h window, which resets 5h after the first turn.
WIN_30D
30-day rolling window in seconds.

Functions§

bucket_aggregates
Roll sessions into day/week/month/model/project buckets + the day×project cross-tab + 30d totals. now is epoch seconds; offset is the fixed local UTC offset. Mirrors bucket_aggregates (days=365, weeks=52, months=24, instance_days=30, instance_rows=200).
iso_utc
Format an epoch-seconds value as a UTC ISO8601 string ending in Z. Subsecond is emitted as 6 digits only when nonzero (mirrors Python’s datetime.fromtimestamp(ts, tz=utc).isoformat().replace("+00:00","Z")).
parse_iso
Parse an ISO-8601 / RFC-3339 timestamp to epoch seconds (mirrors Python datetime.fromisoformat(...).timestamp() for the Z/offset forms used in transcripts). Returns None on empty/unparseable input.
project_name_from_cwd
project_name_from_cwd: basename of the cwd, or when absent.
split_logical_sessions
Split each file’s events into logical sessions on the 5h idle gap, returning (sessions, recent) — sessions feed bucket_aggregates, recent feeds recent_sessions. files is iterated in its existing order (use a BTreeMap for determinism). Mirrors split_logical_sessions.