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.
45 lines
1.5 KiB
Go
45 lines
1.5 KiB
Go
package gui
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// The Usage page's clear on a model's row, and the one for all usage
|
|
// under the lists (CrazyMouse_TT on X), POST /api/usage/clear: the
|
|
// model's calls, or every call, leave the page's figures.
|
|
func TestUsageClearRoute(t *testing.T) {
|
|
sandboxHome(t)
|
|
at := time.Now().Add(-time.Hour)
|
|
usage.Append(usage.Record{Time: at, Agent: "codex", Provider: "ollama", Model: "qwen3", Input: 10, Status: 200})
|
|
usage.Append(usage.Record{Time: at, Agent: "codex", Provider: "ollama", Model: "llama4", Input: 5, Status: 200})
|
|
mux := http.NewServeMux()
|
|
usageRoutes(mux, folderOnly{})
|
|
post := func(body string, code int) {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/usage/clear", strings.NewReader(body)))
|
|
if w.Code != code {
|
|
t.Fatalf("POST %s: %d %s", body, w.Code, w.Body)
|
|
}
|
|
}
|
|
post(`{"provider":"ollama","model":"qwen3"}`, 200)
|
|
if s := usage.Summarize(usage.All); s.Calls != 1 || len(s.Models) != 1 || s.Models[0].Model != "llama4" {
|
|
t.Fatalf("after clearing qwen3: %+v", s.Models)
|
|
}
|
|
post(`{"provider":"ollama"}`, http.StatusBadRequest)
|
|
post(`{}`, 200)
|
|
if s := usage.Summarize(usage.All); s.Calls != 0 {
|
|
t.Fatalf("after clearing everything: %d calls", s.Calls)
|
|
}
|
|
if got := settings.Load().UsageCleared; len(got) != 1 {
|
|
t.Fatalf("settings: %+v", got)
|
|
}
|
|
}
|