Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

usage: the heatmap works out only its days, kept 30s while only session files change (#3493)

yetonecf8b1e933888
diff --git a/internal/usage/heatmap_test.go b/internal/usage/heatmap_test.go
index da91a5182..966d226c9 100644
--- a/internal/usage/heatmap_test.go
+++ b/internal/usage/heatmap_test.go
@@ -2,9 +2,12 @@ package usage
import (
"math"
+ "path/filepath"
"reflect"
"testing"
"time"
+
+ "github.com/yetone/magpie/internal/sessions"
)
// The heatmap (#1369) is the last 53 weeks from a Monday, a day each by the
@@ -93,3 +96,48 @@ func TestHeatmapOf(t *testing.T) {
t.Fatalf("heatmap: %+v", h)
}
}
+
+// A live agent writes its session file all the time. The heatmap is kept
+// while only those files change, for heatFresh, so the Requests tab's
+// refresh doesn't work out a year of calls again each time (#3493); a call
+// through magpie is still counted at once, and a session's call once
+// heatFresh is up.
+func TestHeatmapKeptWhileOnlySessionsChange(t *testing.T) {
+ pageHome(t)
+ now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
+ builds := 0
+ building = func() { builds++ }
+ t.Cleanup(func() { building = nil })
+ calls := func() int {
+ n := 0
+ for _, d := range HeatmapOf(Filter{}).Days {
+ n += d.Calls
+ }
+ return n
+ }
+ file := func(id string) string {
+ return filepath.Join(sessions.CodexDir(), "sessions", "2026", "09", "30", "rollout-2026-09-30T10-00-00-"+id+".jsonl")
+ }
+ writeCodexCall(t, file("a"), "a", "sa", `"vscode"`, "resp-a", "m", "low", now.Add(-time.Hour), 100, 10)
+ if n := calls(); n != 1 || builds != 1 {
+ t.Fatalf("%d calls, %d builds", n, builds)
+ }
+ writeCodexCall(t, file("b"), "b", "sb", `"vscode"`, "resp-b", "m", "low", now.Add(-time.Hour), 100, 10)
+ if n := calls(); n != 1 || builds != 1 {
+ t.Fatalf("a session's new call worked the heatmap out again: %d calls, %d builds", n, builds)
+ }
+ if p := QueryPage(Today, Filter{}, 0, 100); p.Total != 2 {
+ t.Fatalf("the Requests page kept too: %d rows", p.Total)
+ }
+ Append(Record{Time: now.Add(-time.Minute), Agent: "claude-code", Provider: "anthropic", Model: "x", Input: 5, Status: 200})
+ if n := calls(); n != 3 {
+ t.Fatalf("a call through magpie isn't counted at once: %d calls", n)
+ }
+ writeCodexCall(t, file("c"), "c", "sc", `"vscode"`, "resp-c", "m", "low", now.Add(-time.Hour), 100, 10)
+ old := heatFresh
+ heatFresh = 0
+ t.Cleanup(func() { heatFresh = old })
+ if n := calls(); n != 4 {
+ t.Fatalf("heatFresh up, %d calls", n)
+ }
+}
diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go
index a14bc2703..ecc335db9 100644
--- a/internal/usage/request_page.go
+++ b/internal/usage/request_page.go
@@ -377,8 +377,28 @@ type requestIndex struct {
// for it rather than reading the whole history beside it (a refresh
// pressed while a 750k-call All was still being read, Vantiboo1ean)
flights map[flightKey]*pageFlight
+ // heats are the heatmap's last pages, kept while the agents' session
+ // files are all that changed (heatFresh)
+ heats map[pageKey]heatPage
}
+// heatPage is a heatmap's page, of the gateway log's snapshot version,
+// worked out at.
+type heatPage struct {
+ page RequestPage
+ version uint64
+ at time.Time
+}
+
+// heatFresh is how long a heatmap is kept while only the agents' session
+// files change. A live agent writes its session file all the time, so the
+// page's key changed for every ask, and the Requests tab's refresh worked
+// out a year of calls again each time: seconds of CPU over a long history
+// (#3493). A year's calendar is the same a few seconds on; a change to
+// what prices or names a call, or a call through magpie, is still shown at
+// once.
+var heatFresh = 30 * time.Second
+
// building, in tests, is told each time a page is worked out.
var building func()
@@ -505,6 +525,7 @@ func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.C
idx.root, idx.meta = root, meta
idx.chunks = map[string]*rowChunk{}
idx.pages = nil
+ idx.heats = nil
idx.key = ""
}
if idx.key != key {
@@ -516,6 +537,11 @@ func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.C
idx.Unlock()
return page
}
+ if h, ok := idx.heats[q]; ok && h.version == version && time.Since(h.at) < heatFresh {
+ idx.Unlock()
+ return h.page
+ }
+ began := time.Now()
fk := flightKey{key, q}
if fl := idx.flights[fk]; fl != nil {
idx.Unlock()
@@ -704,6 +730,12 @@ func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.C
shared.pages = map[pageKey]RequestPage{}
}
shared.pages[q] = page
+ if p == heatmapPeriod {
+ if shared.heats == nil || len(shared.heats) >= 16 {
+ shared.heats = map[pageKey]heatPage{}
+ }
+ shared.heats[q] = heatPage{page: page, version: version, at: began}
+ }
// The session files' chunks are bounded; the gateway's index is not
// counted here, as it is kept whatever its size (log_index.go).
var bytes int64
@@ -1021,6 +1053,20 @@ func buildRequestBlocksWithCodex(p Period, f Filter, offset, limit int, now time
}
}
out := RequestPage{Rows: []Row{}, Agents: []string{}, Providers: []string{}, Purposes: []string{}, By: map[string][]Share{}}
+ chartFilter := f
+ chartFilter.Day = ""
+ // The heatmap's page is only its days, all HeatmapOf reads: over its
+ // 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(_ rowRef, r Row) {
+ if chartFilter.keepsRow(r) {
+ add(r)
+ }
+ })
+ })
+ return out
+ }
agents, providers := map[string]bool{}, map[string]bool{}
kinds := map[string]bool{}
callers := map[string]*Group{}
@@ -1046,13 +1092,11 @@ func buildRequestBlocksWithCodex(p Period, f Filter, offset, limit int, now time
take = offset + min(limit, maxRows-offset)
}
selected := newestHeap{}
- chartFilter := f
- chartFilter.Day = ""
// the two source cells' own numbers, counted without the source pick
viaFilter := f
viaFilter.Through = ""
var first time.Time
- // the rows the chart and the heatmap count, kept as the first pass
+ // 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 rowRef, r Row) {
@@ -1220,13 +1264,6 @@ func buildRequestBlocksWithCodex(p Period, f Filter, offset, limit int, now time
pt.By[d][k] = part
}
}
- if p == heatmapPeriod {
- out.Heat = heatmapOf(since, now, func(add func(Row)) {
- for _, ref := range charted {
- add(ref.row())
- }
- })
- }
// Match LedgerSeries's top-24 selection on the filtered data, not facets.
for _, d := range Dimensions {
kept := map[string]bool{}