Documentation
¶
Overview ¶
Package snapshot persists each machine's resolved usage history to a per-machine file and merges every machine's snapshot present in a shared target repo into aggregate totals. Git (outside this package's concern, see U6) is the transport that carries these files between machines; this package only handles the local read/write/merge logic.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Write ¶
Write persists rows as machineID's current resolve, merged into whatever snapshot already exists for this machine (see mergeRowsByKey): fresh rows override any existing row sharing a (date, agent, model) key — so re-running on the same day naturally overwrites rather than double-counts — while rows for days the current resolve no longer covers are preserved, so this machine's history accumulates across runs instead of rolling off with the trailing window. History is sharded into one file per calendar month (see chunkKeyFor) so an actively-used machine's snapshot doesn't grow into a single ever-larger file.
Types ¶
type MergedDataset ¶
type MergedDataset struct {
Rows []Row
}
MergedDataset is the union of every machine's snapshot under a target repo, pre-summed by (date, agent, model) across machine boundaries: each Row already reflects every machine's combined contribution to that bucket, ready for direct consumption (e.g. streak/summary computation) without the caller needing to re-derive per-machine totals.
func FilterSince ¶ added in v0.0.2
func FilterSince(ds MergedDataset, since time.Time) MergedDataset
FilterSince returns the subset of ds.Rows dated strictly after since's own calendar day, preserving order — the window-scoping step cli/run.go applies to an accumulated (potentially multi-window) MergedDataset before handing it to render.Render, so trend/breakdown reflect only the current window rather than a machine's full accumulated history. since's calendar day itself is excluded (not just days strictly before it): a window of N is computed upstream as asOf.Add(-window), landing exactly N calendar days before "now" — keeping that boundary day too would span N+1 days, not N (the off-by-one render.titleLine's "last N days" label previously surfaced whenever a caller passed the exact window boundary as since).
func Merge ¶
func Merge(targetRepo string) (MergedDataset, error)
Merge reads every machine's snapshot chunk files under targetRepo's snapshots directory and unions their rows, summing different machines' contributions to the same (date, agent, model) bucket. Each machine's own chunks already hold that machine's complete accumulated history, deduplicated by key (Write merges rather than replaces — see mergeRowsByKey), so a machine re-running never inflates its own totals here — only distinct machines' rows are additive.
A chunk file that fails to parse (corrupted or a partial write) is skipped with a logged warning rather than aborting the whole merge, so one machine's bad file can't take down every other machine's data.
A missing snapshots directory (no machine has ever run against this target repo) is not an error: it yields an empty MergedDataset.
type Row ¶
type Row struct {
Date string `json:"date"`
Agent string `json:"agent"`
Model string `json:"model"`
Tokens int64 `json:"tokens"`
Cost float64 `json:"cost"`
}
Row is one (date, agent, model) usage observation. It intentionally mirrors agentsview.Row's shape rather than importing that package, so snapshot stays a generic persistence/merge concern decoupled from agentsview's specific types; callers convert at the call site.
func Read ¶
Read decodes machineID's full snapshot history under targetRepo, concatenating every calendar-month chunk file (see chunkKeyFor). Chunk file names sort chronologically, and each chunk's own rows are already sorted by mergeRowsByKey, so the result comes back in overall date order with no extra sort needed.