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.
111 lines
3.2 KiB
Go
111 lines
3.2 KiB
Go
package usage
|
|
|
|
import (
|
|
"encoding/json"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// Clearing the Usage page's figures (CrazyMouse_TT on X: a local model
|
|
// deployed again and benchmarked, its requests, speed and cache wanted from
|
|
// nothing). A clear is kept in settings (settings.UsageCleared) and read
|
|
// over whatever the pages count, never written into usage.jsonl: the log
|
|
// keeps every call, so a key's spending limit (package budget), the
|
|
// allowances' window holds and the gateway's own reads of it (Visit) still
|
|
// count the calls cleared, and the agents' session files and other
|
|
// computers' shared days, which magpie can't rewrite, are cleared the same
|
|
// way as its own log.
|
|
|
|
// clearSet is the user's clears as last read, the usage aliases a model's
|
|
// clear takes in, and a key for the caches built on what they hide.
|
|
type clearSet struct {
|
|
marks []settings.UsageClear
|
|
alias func(string) string
|
|
// all is the latest clear of everything, zero for none
|
|
all time.Time
|
|
key string
|
|
}
|
|
|
|
var clearedAs atomic.Pointer[clearSet]
|
|
|
|
// readCleared reads the user's clears again, for a pass over the usage:
|
|
// each record of the pass is then judged by what was read here
|
|
// (clearedNow). It reads settings.json, so it is called before any lock a
|
|
// reader of the usage waits on.
|
|
func readCleared() *clearSet {
|
|
marks := settings.UsageClearedNow()
|
|
var aliases map[string]string
|
|
if len(marks) > 0 {
|
|
aliases = provider.UsageAliases()
|
|
}
|
|
b, _ := json.Marshal(struct {
|
|
M []settings.UsageClear
|
|
A map[string]string
|
|
}{marks, aliases})
|
|
key := string(b)
|
|
if cur := clearedAs.Load(); cur != nil && cur.key == key {
|
|
return cur
|
|
}
|
|
c := &clearSet{marks: marks, key: key}
|
|
if len(aliases) > 0 {
|
|
c.alias = provider.UsageAliasIn(aliases)
|
|
}
|
|
for _, m := range marks {
|
|
if m.Provider == "" && m.Model == "" && m.Time().After(c.all) {
|
|
c.all = m.Time()
|
|
}
|
|
}
|
|
clearedAs.Store(c)
|
|
return c
|
|
}
|
|
|
|
// clearedKey is what the caches of counted records key on.
|
|
func clearedKey() string { return readCleared().key }
|
|
|
|
// clearedNow is the clears the last readCleared read; nil before any.
|
|
func clearedNow() *clearSet { return clearedAs.Load() }
|
|
|
|
// hides is whether the user cleared r from the usage pages: a clear of
|
|
// everything, or of its provider's model, made at or after its time. r's
|
|
// provider is the one it is shown under (renameBy) and its model is taken
|
|
// by its own id or by the usage alias it is counted under, as the
|
|
// Overview's Models list names it.
|
|
func (c *clearSet) hides(r *Record) bool {
|
|
if c == nil || len(c.marks) == 0 {
|
|
return false
|
|
}
|
|
if !c.all.IsZero() && !r.Time.After(c.all) {
|
|
return true
|
|
}
|
|
model := ""
|
|
for _, m := range c.marks {
|
|
if m.Model == "" || r.Time.After(m.Time()) || m.Provider != r.Provider {
|
|
continue
|
|
}
|
|
if r.Model == m.Model {
|
|
return true
|
|
}
|
|
if c.alias != nil {
|
|
if model == "" {
|
|
model = c.alias(r.Model)
|
|
}
|
|
if model == m.Model {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// clearedSince is when the usage counted begins after a clear of
|
|
// everything: first, or the clear's time when it is later.
|
|
func (c *clearSet) clearedSince(first time.Time) time.Time {
|
|
if c == nil || c.all.IsZero() || !first.IsZero() && first.After(c.all) {
|
|
return first
|
|
}
|
|
return c.all
|
|
}
|