diff --git a/internal/usage/ledger.go b/internal/usage/ledger.go index 3266c120a..b1cd8b2cb 100644 --- a/internal/usage/ledger.go +++ b/internal/usage/ledger.go @@ -119,18 +119,18 @@ func SourceOf(r Row) string { // keepsRow is keeps with the one dimension a Record does not carry: which of // the two sources the row is of (Row.Source, not the record's). -func (f Filter) keepsRow(r Row) bool { - if f.Through != "" && SourceOf(r) != f.Through { +func (f Filter) keepsRow(r *Row) bool { + if f.Through != "" && SourceOf(*r) != f.Through { return false } - return f.keepsRecord(r.Record, r.SessionRole != "", r.Subagent, r.ParentAgent) + return f.keepsRecord(&r.Record, r.SessionRole != "", r.Subagent, r.ParentAgent) } // OtherComputers is the Filter.Computer of every other computer's calls. const OtherComputers = "others" // computer is whether r is of the computers Computer picks. -func (f Filter) computer(r Record) bool { +func (f Filter) computer(r *Record) bool { switch f.Computer { case "": return true @@ -143,10 +143,10 @@ func (f Filter) computer(r Record) bool { } func (f Filter) keeps(r Record) bool { - return f.keepsRecord(r, false, "", "") + return f.keepsRecord(&r, false, "", "") } -func (f Filter) keepsRecord(r Record, codexIdentity bool, subagent, parentAgent string) bool { +func (f Filter) keepsRecord(r *Record, codexIdentity bool, subagent, parentAgent string) bool { if f.Day != "" { var day [len(time.DateOnly)]byte if string(r.Time.In(f.loc()).AppendFormat(day[:0], time.DateOnly)) != f.Day { @@ -285,7 +285,7 @@ func (l Ledgered) Filtered(f Filter) Ledgered { } out := Ledgered{Rows: []Row{}, Agents: l.Agents, Providers: l.Providers} for _, r := range l.Rows { - if f.keepsRow(r) { + if f.keepsRow(&r) { out.Rows = append(out.Rows, r) if !r.IsRejected() { out.Sum.addRow(r) @@ -442,7 +442,7 @@ func ledgerMatched(since, until time.Time, f Filter, recs []Record, logs []sessi sent = provider.SentNameIn(wires, r.Provider, r.Model, r.Effort) } row := codexIdentityRow(Row{Record: r, Swapped: r.Served != "" && Swapped(sent, r.Served), Routed: GroupRouted(sent, r.Served), Source: source}, actor, codexIdentities) - if !f.keepsRow(row) { + if !f.keepsRow(&row) { return } if pr != nil && r.Input+r.Output > 0 { @@ -758,6 +758,36 @@ func (t *Totals) addRow(r Row) { } } +// plus adds what another sum counted: a row's own, made once by addRow, +// added to each of the many groups it is in instead of each reading the +// row again (#3493). +func (t *Totals) plus(d *Totals) { + t.Calls += d.Calls + t.Errors += d.Errors + t.Retried += d.Retried + t.Loops += d.Loops + t.Input += d.Input + t.Output += d.Output + t.CacheRead += d.CacheRead + t.CacheWrite += d.CacheWrite + t.CacheWrite1h += d.CacheWrite1h + t.CacheUnknown += d.CacheUnknown + t.CacheUnknownCalls += d.CacheUnknownCalls + t.Reasoning += d.Reasoning + t.Cost += d.Cost + t.Unpriced += d.Unpriced + t.Credits += d.Credits + t.Credited += d.Credited + t.Timed += d.Timed + t.TTFT += d.TTFT + t.DecodeMs += d.DecodeMs + t.DecodeOut += d.DecodeOut + t.DecodeN += d.DecodeN + t.FloorOut += d.FloorOut + t.FloorMs += d.FloorMs + t.FloorN += d.FloorN +} + // FloorAgents are the agents whose session files time a call from what // asked for it to its reply's end, never from inside it: Claude Code's and // Claude Desktop's from the line that asked, written before it was sent; @@ -884,7 +914,7 @@ func (f Filter) Without(d string) Filter { func ModelAtKey(provider, model string) string { return provider + "/" + model } // key is the row's part of a dimension: its provider's id, its agent, its model. -func (r Row) key(by string) string { +func (r *Row) key(by string) string { switch by { case "provider": return r.Provider @@ -911,7 +941,7 @@ func (r Row) key(by string) string { // would otherwise build again for each. type keyer map[[2]string]string -func (m keyer) key(r Row, by string) string { +func (m keyer) key(r *Row, by string) string { if by != "modelAt" { return r.key(by) } @@ -942,7 +972,7 @@ func Breakdown(rows []Row, by string) []Share { if r.IsRejected() { continue } - k := keys.key(r, by) + k := keys.key(&r, by) s := at[k] if s == nil { s = &Share{ID: k} @@ -998,6 +1028,18 @@ func (p *Part) add(r Row) { } } +// plus counts in the part a row whose own sums are d (Totals.addRow): what +// add counts, read from them. +func (p *Part) plus(d *Totals) { + p.Calls += d.Calls + p.Tokens += d.AllTokens() + p.Cost += d.Cost + p.Timed += d.Timed + p.TTFT += d.TTFT + p.DecodeMs += d.DecodeMs + p.DecodeOut += d.DecodeOut +} + // SeriesPoint is a point of the timeline with its calls told apart, by each // dimension, for a chart to stack them: the models of one point, say. type SeriesPoint struct { @@ -1061,7 +1103,7 @@ func ledgerSeriesAt(p Period, rows []Row, now time.Time) (bucket string, pts []S } pts[i].addRow(r) for _, d := range Dimensions { - if k := keys.key(r, d); kept[d][k] { + if k := keys.key(&r, d); kept[d][k] { part := pts[i].By[d][k] part.add(r) pts[i].By[d][k] = part diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index c9aab8647..9309150af 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -1187,7 +1187,7 @@ func buildRequestBlocksOf(p Period, f Filter, offset, limit int, now time.Time, gateway.Chunk.setCodexCompaction(gateway.Index, compactions[local]) } all := append(append(slices.Clone(gateways), chunks...), others...) - visit := func(fn func(timedRef, Row)) { + visit := func(fn func(timedRef, *Row)) { var rows chunkRows for _, c := range all { open := rows.of(c) @@ -1197,7 +1197,8 @@ func buildRequestBlocksOf(p Period, f Filter, offset, limit int, now time.Time, if pr.Time.Before(since) || after(until, pr.Time) || len(skip) > 0 && skip[ref] || len(matched) > 0 && matched[ref] { continue } - fn(timedOf(ref, pr), open.row(i)) + r := open.row(i) + fn(timedOf(ref, pr), &r) } } } @@ -1208,9 +1209,9 @@ func buildRequestBlocksOf(p Period, f Filter, offset, limit int, now time.Time, // year the groups, shares and series below took seconds a call (#3493). if p == heatmapPeriod { out.Heat = heatmapOf(since, now, func(add func(Row)) { - visit(func(_ timedRef, r Row) { + visit(func(_ timedRef, r *Row) { if chartFilter.keepsRow(r) { - add(r) + add(*r) } }) }) @@ -1248,7 +1249,26 @@ func buildRequestBlocksOf(p Period, f Filter, offset, limit int, now time.Time, // the rows the chart counts, kept as the first pass // finds them so that theirs needn't read every row of the period again var charted []rowRef - visit(func(ref timedRef, r Row) { + // Each dimension's groups are counted with its own pick cleared, and + // the computers' with the computer's and day's: made once, and read as + // the row's own keep when clearing changed nothing, which with no pick + // at all is every one of them. A Row is 896 bytes and a page of the + // whole history 400,000 of them, so each row's sums are made once (d) + // and added to the dozen groups it is in, not read again by each + // (#3493). + without, withoutDay := map[string]Filter{}, map[string]Filter{} + unpicked := map[string]bool{} + for _, d := range Dimensions { + without[d] = f.Without(d) + unpicked[d] = without[d] == f + g := without[d] + g.Day = "" + withoutDay[d] = g + } + computerFilter := f + computerFilter.Computer, computerFilter.Day = "", "" + computerUnpicked := computerFilter == f + visit(func(ref timedRef, r *Row) { // looked up before they are set: most rows name what an earlier // one did, and a map's lookup is cheaper than its write if !agents[r.Agent] { @@ -1260,8 +1280,6 @@ func buildRequestBlocksOf(p Period, f Filter, offset, limit int, now time.Time, if r.Provider != "" && !providers[r.Provider] { providers[r.Provider] = true } - addCallerRow(callers, r) - addAccountRow(accounts, r) keep := f.keepsRow(r) if keep { out.Total++ @@ -1271,10 +1289,16 @@ func buildRequestBlocksOf(p Period, f Filter, offset, limit int, now time.Time, selected[0] = ref heap.Fix(&selected, 0) } - if !r.IsRejected() { - out.Sum.addRow(r) + } + var d Totals + if !r.IsRejected() { + d.addRow(*r) + if keep { + out.Sum.plus(&d) } } + addCallerRow(callers, r, &d) + addAccountRow(accounts, r, &d) // The two sources are counted with the Via pick cleared, as the // dimension groups above are: the cells are what the reader // switches between, so each must go on saying its own number @@ -1283,11 +1307,11 @@ func buildRequestBlocksOf(p Period, f Filter, offset, limit int, now time.Time, if !r.IsRejected() && viaFilter.keepsRow(r) { switch r.Source { case "log": - out.Direct.addRow(r) + out.Direct.plus(&d) case SourceReported: - out.Reported.addRow(r) + out.Reported.plus(&d) default: - out.Through.addRow(r) + out.Through.plus(&d) } } if r.IsRejected() { @@ -1295,53 +1319,44 @@ func buildRequestBlocksOf(p Period, f Filter, offset, limit int, now time.Time, } if keep || f.Day != "" && chartFilter.keepsRow(r) { charted = append(charted, ref.rowRef) - for _, d := range Dimensions { - k := keys.key(r, d) - s := seriesGroups[d][k] + for _, dim := range Dimensions { + k := keys.key(r, dim) + s := seriesGroups[dim][k] if s == nil { s = &Share{ID: k} - seriesGroups[d][k] = s + seriesGroups[dim][k] = s } - s.addRow(r) + s.plus(&d) } if first.IsZero() || r.Time.Before(first) { first = r.Time } } - if g := f; names != nil { - g.Computer = "" - g.Day = "" - if g.keepsRow(r) { - k := r.key("computer") - if computers[k] == nil { - computers[k] = &Share{ID: k} - } - computers[k].addRow(r) + if names != nil && (computerUnpicked && keep || !computerUnpicked && computerFilter.keepsRow(r)) { + k := r.key("computer") + if computers[k] == nil { + computers[k] = &Share{ID: k} } + computers[k].plus(&d) } - for _, d := range Dimensions { - g := f.Without(d) - if f.Day != "" { - g.Day = "" - if g.keepsRow(r) { - k := keys.key(r, d) - if chartGroups[d][k] == nil { - chartGroups[d][k] = &Share{ID: k} - } - chartGroups[d][k].addRow(r) + for _, dim := range Dimensions { + if f.Day != "" && withoutDay[dim].keepsRow(r) { + k := keys.key(r, dim) + if chartGroups[dim][k] == nil { + chartGroups[dim][k] = &Share{ID: k} } - g.Day = f.Day + chartGroups[dim][k].plus(&d) } - if !g.keepsRow(r) { + if unpicked[dim] && !keep || !unpicked[dim] && !without[dim].keepsRow(r) { continue } - k := keys.key(r, d) - s := groups[d][k] + k := keys.key(r, dim) + s := groups[dim][k] if s == nil { s = &Share{ID: k} - groups[d][k] = s + groups[dim][k] = s } - s.addRow(r) + s.plus(&d) } }) slices.SortFunc(selected, func(a, b timedRef) int { @@ -1411,12 +1426,14 @@ func buildRequestBlocksOf(p Period, f Filter, offset, limit int, now time.Time, continue } pt := &out.Series[i] - pt.addRow(r) - for _, d := range Dimensions { - k := keys.key(r, d) - part := pt.By[d][k] - part.add(r) - pt.By[d][k] = part + var d Totals + d.addRow(r) + pt.plus(&d) + for _, dim := range Dimensions { + k := keys.key(&r, dim) + part := pt.By[dim][k] + part.plus(&d) + pt.By[dim][k] = part } } // Match LedgerSeries's top-24 selection on the filtered data, not facets. @@ -1454,7 +1471,7 @@ func pageFromLedgerAt(p Period, f Filter, offset, limit int, all Ledgered, now t viaFilter := f viaFilter.Through = "" for _, r := range all.Rows { - if r.IsRejected() || !viaFilter.keepsRow(r) { + if r.IsRejected() || !viaFilter.keepsRow(&r) { continue } switch r.Source { @@ -1470,8 +1487,10 @@ func pageFromLedgerAt(p Period, f Filter, offset, limit int, all Ledgered, now t purposes := map[string]bool{} for i := len(all.Rows) - 1; i >= 0; i-- { purposes[PurposeOf(all.Rows[i].Kind)] = true - addCallerRow(callers, all.Rows[i]) - addAccountRow(accounts, all.Rows[i]) + var d Totals + d.addRow(all.Rows[i]) + addCallerRow(callers, &all.Rows[i], &d) + addAccountRow(accounts, &all.Rows[i], &d) } for purpose := range purposes { out.Purposes = append(out.Purposes, purpose) @@ -1532,7 +1551,7 @@ func computerShares(m map[string]*Share, names map[string]string) ([]Share, map[ } // Caller choices cover the period, not just the current page or selected key. -func addCallerRow(groups map[string]*Group, r Row) { +func addCallerRow(groups map[string]*Group, r *Row, d *Totals) { if r.CallerKeyID == "" || r.IsRejected() { return } @@ -1542,12 +1561,12 @@ func addCallerRow(groups map[string]*Group, r Row) { groups[r.CallerKeyID] = g } g.CallerKeyName = r.CallerKeyName - g.addRow(r) + g.plus(d) } // Account choices cover the period too: each account that answered a call, // by provider, for the Account filter, whatever else is picked. -func addAccountRow(groups map[[2]string]*Group, r Row) { +func addAccountRow(groups map[[2]string]*Group, r *Row, d *Totals) { who := r.Account() if who == "" || r.IsRejected() { return @@ -1558,7 +1577,7 @@ func addAccountRow(groups map[[2]string]*Group, r Row) { g = &Group{ID: r.Provider + "@" + who, Provider: r.Provider, Account: who} groups[k] = g } - g.addRow(r) + g.plus(d) } func callerGroups[K comparable](groups map[K]*Group) []Group { diff --git a/internal/usage/totals_plus_test.go b/internal/usage/totals_plus_test.go new file mode 100644 index 000000000..9441012dc --- /dev/null +++ b/internal/usage/totals_plus_test.go @@ -0,0 +1,64 @@ +package usage + +import ( + "reflect" + "testing" + "time" +) + +// TestTotalsPlusAddsEveryField fails when a field added to Totals is left +// out of plus: the Requests page's groups would read it as 0. +func TestTotalsPlusAddsEveryField(t *testing.T) { + var d Totals + v := reflect.ValueOf(&d).Elem() + for i := 0; i < v.NumField(); i++ { + switch f := v.Field(i); f.Kind() { + case reflect.Int, reflect.Int64: + f.SetInt(int64(i + 1)) + case reflect.Float64: + f.SetFloat(float64(i) + 0.5) + default: + t.Fatalf("Totals.%s is a %s: plus and this test need it", v.Type().Field(i).Name, f.Kind()) + } + } + var got Totals + got.plus(&d) + got.plus(&d) + w := reflect.ValueOf(&got).Elem() + for i := 0; i < v.NumField(); i++ { + name := v.Type().Field(i).Name + switch a, b := w.Field(i), v.Field(i); a.Kind() { + case reflect.Float64: + if a.Float() != 2*b.Float() { + t.Errorf("plus left out %s: %v of %v", name, a.Float(), 2*b.Float()) + } + default: + if a.Int() != 2*b.Int() { + t.Errorf("plus left out %s: %d of %d", name, a.Int(), 2*b.Int()) + } + } + } +} + +// TestPartPlusIsAdd: a part counts a row from its sums as add counts it +// from the row, for each kind of row a chart has. +func TestPartPlusIsAdd(t *testing.T) { + credits := 2.5 + rows := []Row{ + {Record: Record{Time: time.Now(), Agent: "claude", Input: 10, Output: 1200, CacheRead: 3, CacheWrite: 4, Status: 200, TTFT: 50, FirstText: 60, Millis: 3000}, Source: "log", Priced: true, Cost: 0.25}, + {Record: Record{Agent: "codex", Input: 7, Output: 500, Reasoning: 200, Status: 200, TTFT: 900, Millis: 4000}, Source: "log"}, + {Record: Record{Agent: "opencode", Input: 5, Status: 429, Error: "limited", TTFT: 10, Millis: 20}}, + {Record: Record{Agent: "opencode", Input: 5, Output: 9, HandedOn: true, Status: 500, Credits: &credits}}, + {Record: Record{Agent: "claude", Input: 5, Output: 9, Tries: 2, Status: 200, TTFT: 30, FirstText: 31, Millis: 400}, Priced: true, Cost: 1e-7}, + } + for i, r := range rows { + var want, got Part + want.add(r) + var d Totals + d.addRow(r) + got.plus(&d) + if got != want { + t.Errorf("row %d: plus %+v, add %+v", i, got, want) + } + } +}