CrazyMouse_TT on X asked for a way to clear usage, all of it or one model's, to measure a local model fresh after deploying it. The Usage page's Models list has a "…" on each row (and a right-click menu) with "Clear usage…", and "Clear all usage…" sits under the lists below the "Counted from…" note. Each asks first, in a confirmation that says what is deleted, that it can't be undone, and that key spending limits and allowances still count the calls. A clear is a mark in settings (usageCleared: provider, model, unix ms), read at read time; usage.jsonl is never rewritten. The Overview hides calls up to the mark in summarizeFrom; the Requests page, heatmap and ledger/CSV hide them in Filter.keepsRow, their one choke point, and the request index's meta carries the marks so its cache is dropped. Key budgets, window holds and Codex reads go through usage.Visit and the raw log, so a clear never resets a key's spending limit or a quota. A later clear of the same model or of everything takes the place of an earlier one. The marks ride the settings part of a sync (sync-backup.md) and a save of the Settings page keeps them (api.go's keep list; the guard test TestSettingsSaveKeepsWhatItDoesNotSend caught it missing). gateway-sessions.md and sync-backup.md say so. Run on mote (mote-run) at this tree, rebased on origin/mainc26b2992e(the Go packages and usage-clear.test.cjs; vet and the 68 GUI files ran at the earlier basef835bd713, four commits behind): - go test -tags nogui -timeout 45m ./internal/usage ./internal/settings ./internal/usageapi ./internal/gui: all ok after the keep-list fix (before it, TestSettingsSaveKeepsWhatItDoesNotSend red). - gofmt -l: clean. GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./...: exit 0. (Linux vet without nogui fails in wails' cgo, as on main.) - usage-clear.test.cjs: 32/32 in Chromium and WebKit, en/zh/zh-TW/ja/de, at 1200, 560 and 390 (web). With the old page.js/client.js/i18n.js it fails (TimeoutError). - 68 GUI files the change can reach (every usage-*, gui-de/ja/zh-tw, i18n-keys, click-scroll, idle-ticks, mobile-web, panel-usage, sessions-purge): 941/980. The reds are filed and red on main (usage-ledger run alone at origin/main84a6a2c42too): i18n-keys "masking…" (#4053), mobile-web (#3952), usage-agents-hide (#2276, #3414), usage-ledger "newest live route needs no return-to-live button" (#3104, run alone at main too). i18n-keys first also named "Clear usage…" in Go comments of this change; reworded. Broken by hand, each red: hides() always false (usage and gui tests), keepsRow's clear check removed ("Requests after the clear: 5 rows"), clearedKey out of the request meta ("after clearing everything: 3"), the api.go keep line removed (UsageCleared [] after a save). Not tried: the full go ./... suite and the whole GUI suite (mote was busy with other sessions' full suites), the real app, a sync between two computers. usage.jsonl doesn't shrink, and the Sessions tab's figures are not cleared.
102 lines
3.9 KiB
Go
102 lines
3.9 KiB
Go
package usage
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// CrazyMouse_TT on X: a local model deployed again is benchmarked from
|
|
// nothing. A clear of one model hides its calls up to the clear from the
|
|
// Overview, the Requests page, its heatmap and the ledger (the CSV), its
|
|
// alias's ids with it, and leaves other models and later calls counted; a
|
|
// clear of everything hides every call up to it. The log keeps them, so
|
|
// Visit (key spending limits, window holds) still counts them all.
|
|
func TestClearedUsageLeavesThePages(t *testing.T) {
|
|
resetUsageTest(t)
|
|
now := Clock()
|
|
at := now.Add(-2 * time.Hour)
|
|
Append(Record{Time: at, Agent: "codex", Provider: "ollama", Model: "qwen3:32b", Input: 100, Output: 10, Status: 200})
|
|
Append(Record{Time: at.Add(time.Minute), Agent: "codex", Provider: "ollama", Model: "qwen3-32b-q4", Input: 50, Output: 5, Status: 200})
|
|
Append(Record{Time: at.Add(2 * time.Minute), Agent: "codex", Provider: "ollama", Model: "llama4", Input: 7, Output: 3, Status: 200})
|
|
Append(Record{Time: at.Add(3 * time.Minute), Agent: "codex", Provider: "openai", Model: "qwen3:32b", Input: 9, Output: 1, Status: 200})
|
|
if _, err := provider.SetUsageAlias("qwen3-32b-q4", "qwen3:32b"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
models := func() map[string]int {
|
|
out := map[string]int{}
|
|
for _, g := range Summarize(All).Models {
|
|
out[g.Provider+"/"+g.Model] += g.Calls
|
|
}
|
|
return out
|
|
}
|
|
if got := models(); got["ollama/qwen3:32b"] != 2 || got["ollama/llama4"] != 1 || got["openai/qwen3:32b"] != 1 {
|
|
t.Fatalf("before the clear: %v", got)
|
|
}
|
|
// warm every cache the pages read, so the clear has to reach past them
|
|
_ = QueryPage(All, Filter{}, 0, 50)
|
|
_ = HeatmapOf(Filter{})
|
|
|
|
if err := settings.ClearUsage("ollama", "qwen3:32b", Clock().Add(-time.Hour)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// a call after the clear is counted
|
|
Append(Record{Time: Clock(), Agent: "codex", Provider: "ollama", Model: "qwen3:32b", Input: 1, Output: 1, Status: 200})
|
|
|
|
if got := models(); got["ollama/qwen3:32b"] != 1 || got["ollama/llama4"] != 1 || got["openai/qwen3:32b"] != 1 {
|
|
t.Fatalf("Overview after clearing ollama's qwen3:32b: %v", got)
|
|
}
|
|
if s := Summarize(All); s.Calls != 3 || s.Input != 17 {
|
|
t.Fatalf("Overview totals: %d calls, %d in", s.Calls, s.Input)
|
|
}
|
|
page := QueryPage(All, Filter{}, 0, 50)
|
|
if page.Total != 3 {
|
|
t.Fatalf("Requests after the clear: %d rows", page.Total)
|
|
}
|
|
for _, r := range page.Rows {
|
|
if r.Provider == "ollama" && r.Time.Before(Clock().Add(-time.Hour)) && r.Model != "llama4" {
|
|
t.Fatalf("a cleared row is listed: %+v", r.Record)
|
|
}
|
|
}
|
|
heat := 0
|
|
for _, d := range HeatmapOf(Filter{}).Days {
|
|
heat += d.Calls
|
|
}
|
|
if heat != 3 {
|
|
t.Fatalf("heatmap after the clear: %d calls", heat)
|
|
}
|
|
if rows, _, _ := Ledger(All, Filter{}); len(rows) != 3 {
|
|
t.Fatalf("ledger (CSV) after the clear: %d rows", len(rows))
|
|
}
|
|
// the log is whole: what key limits and window holds read
|
|
seen := 0
|
|
Visit(time.Time{}, func(Record) { seen++ })
|
|
if seen != 5 {
|
|
t.Fatalf("Visit after the clear: %d records, want all 5", seen)
|
|
}
|
|
|
|
// everything, now: nothing up to it is counted
|
|
if err := settings.ClearUsage("", "", Clock()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s := Summarize(All); s.Calls != 0 || len(s.Models) != 0 {
|
|
t.Fatalf("Overview after clearing everything: %d calls, %d models", s.Calls, len(s.Models))
|
|
}
|
|
if page := QueryPage(All, Filter{}, 0, 50); page.Total != 0 {
|
|
t.Fatalf("Requests after clearing everything: %d", page.Total)
|
|
}
|
|
Append(Record{Time: Clock().Add(time.Second), Agent: "pi", Provider: "ollama", Model: "llama4", Input: 2, Output: 2, Status: 200})
|
|
if s := Summarize(All); s.Calls != 1 || !slices.ContainsFunc(s.Agents, func(g Group) bool { return g.ID == "pi" }) {
|
|
t.Fatalf("a call after clearing everything: %+v", s.Agents)
|
|
}
|
|
seen = 0
|
|
Visit(time.Time{}, func(Record) { seen++ })
|
|
if seen != 6 {
|
|
t.Fatalf("Visit after clearing everything: %d", seen)
|
|
}
|
|
}
|