The period bar had only Today, 7 days, 30 days and All. A Custom opt now
follows them. It opens the app's own menu (no native select): Yesterday,
This month, Last month and 90 days a click away, and a month of days to
pick the first day on, then the last. Days after today can't be picked;
arrows walk the days and Escape closes. Once picked, the opt names the
days ("Oct 1 – Oct 7"), and narrow windows show only its icon until then.
The backend takes the days as period "2026-10-01..2026-10-07", both
included, in the local time zone (usage.RangeOf, Period.Known/Until).
The summary, the Requests page (index and ledger fallback), Direct and the
CSV name count only calls from the first day's midnight up to the
midnight after the last. The chart is an hour each for one day, a day each
up to 60 days, and a week each from a Monday past that. A picked range is
summarised afresh each time, so ranges never pile up in the summary cache.
The credits-per-day card follows the days picked. New strings in zh,
zh-TW, ja and de.
Run:
- go test -tags nogui ./internal/usage ./internal/gui (sandbox HOME): ok.
- TestPickedDaysArePeriod with Until returning zero (no range end):
FAIL, Summarize 5 calls / QueryPage 5 rows / LedgerOf 5 rows, want 3.
- usage-range.test.cjs, Chromium and WebKit, en and zh, at 390px: 4/4.
- Every GUI test touching period or the Usage view (87 files): Chromium
388/388; WebKit 386/388, the two purpose-filter's Routing focus check
(scrollTop off by 1px after a wheel), which then passed 52 of 56 lone
runs here and 25 of 25 on origin/main; nothing in this change runs on
the Routing page. gui-de, gui-ja, gui-zh-tw, i18n-keys: pass.
- gofmt -l; GOOS=linux and GOOS=windows go vet -tags nogui on both pkgs.
100 lines
3.2 KiB
Go
100 lines
3.2 KiB
Go
package usage
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
const summaryCacheSessions = 1024
|
|
|
|
type summaryEntry struct {
|
|
version uint64
|
|
meta string
|
|
value Summary
|
|
}
|
|
|
|
var summaries struct {
|
|
sync.Mutex
|
|
entries map[Period]summaryEntry
|
|
}
|
|
|
|
func indexedSummary(p Period, now time.Time) Summary {
|
|
p = p.shown()
|
|
snapshot := logSnapshotFor(true)
|
|
if p.IsRange() {
|
|
// a picked range is read afresh each time, so ranges never pile up
|
|
// in the cache (#1492)
|
|
if snapshot.uncached && len(snapshot.blocks) == 0 {
|
|
snapshot = readLogSnapshot()
|
|
}
|
|
return summarizeFrom(p, now, snapshot.first, snapshot.keyProviders, func(fn func(Record)) { snapshot.visit(p.Since(now), fn) })
|
|
}
|
|
meta := usagePriceCacheKey(now)
|
|
summaries.Lock()
|
|
old, ok := summaries.entries[p]
|
|
summaries.Unlock()
|
|
if ok && old.version == snapshot.version && old.meta == meta {
|
|
return cloneSummary(old.value)
|
|
}
|
|
if snapshot.uncached && len(snapshot.blocks) == 0 {
|
|
snapshot = readLogSnapshot()
|
|
}
|
|
value := summarizeFrom(p, now, snapshot.first, snapshot.keyProviders, func(fn func(Record)) { snapshot.visit(p.Since(now), fn) })
|
|
// Keep every session in the returned result; limit retained cache data only.
|
|
if len(value.Sessions) > summaryCacheSessions {
|
|
summaries.Lock()
|
|
if previous := summaries.entries[p]; previous.version <= snapshot.version {
|
|
delete(summaries.entries, p)
|
|
}
|
|
summaries.Unlock()
|
|
return value
|
|
}
|
|
summaries.Lock()
|
|
if summaries.entries == nil {
|
|
summaries.entries = map[Period]summaryEntry{}
|
|
}
|
|
if previous := summaries.entries[p]; previous.version <= snapshot.version {
|
|
summaries.entries[p] = summaryEntry{snapshot.version, meta, value}
|
|
}
|
|
summaries.Unlock()
|
|
return cloneSummary(value)
|
|
}
|
|
|
|
// The exported result remains owned by its caller.
|
|
func cloneSummary(s Summary) Summary {
|
|
s.Series = slices.Clone(s.Series)
|
|
s.Agents = slices.Clone(s.Agents)
|
|
s.Models = slices.Clone(s.Models)
|
|
s.ProviderKeys = slices.Clone(s.ProviderKeys)
|
|
s.Accounts = slices.Clone(s.Accounts)
|
|
s.CallerKeys = slices.Clone(s.CallerKeys)
|
|
s.Sessions = slices.Clone(s.Sessions)
|
|
return s
|
|
}
|
|
|
|
// usagePriceCacheKey invalidates priced views when local prices or provider feeds change.
|
|
func usagePriceCacheKey(now time.Time) string {
|
|
_, offset := now.Zone()
|
|
meta := fmt.Sprintf("%s|%s|%s|%s|%s|%s|%d", statKey(settings.Path()), statKey(provider.Path()), statKey(catalog.CachePath()), statKey(catalog.LivePath("antigravity")), now.Format("2006-01-02"), now.Location(), offset)
|
|
// Sign-ins and plugin lists can change independently of providers.json.
|
|
// Their identity and price feeds affect both grouping and historical costs.
|
|
h := sha256.New()
|
|
prices, _ := json.Marshal(settings.Load().ModelPrices)
|
|
aliases, _ := json.Marshal(provider.Renamed())
|
|
fmt.Fprint(h, string(prices), string(aliases))
|
|
for _, current := range provider.All() {
|
|
fmt.Fprintf(h, "%q:%q:%v:%t:%t:%t;", current.ID, current.Where(), current.Catalogs(), current.IsPlugin(), current.Account == nil, current.Key != "")
|
|
fmt.Fprint(h, statKey(catalog.LivePath(current.ID)))
|
|
}
|
|
meta += fmt.Sprintf("|%x", h.Sum(nil))
|
|
return meta
|
|
}
|