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.
74 lines
2.6 KiB
Go
74 lines
2.6 KiB
Go
package usage
|
|
|
|
import (
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
// Direct is a period's calls the agents made on their own, which the gateway
|
|
// never saw: those their session files record (Codex, Claude Code, Claude
|
|
// Desktop…), summed up as Summarize sums the gateway's, by agent, model and
|
|
// account. The Usage page's Requests tab and the tray panel count them
|
|
// beside the gateway's; magpie usage and the TUI show them in a part of
|
|
// their own (Kumo31 on Discord: Codex used outside magpie was on the
|
|
// window's Usage tab but not in magpie usage).
|
|
func Direct(p Period) Summary {
|
|
now := Clock()
|
|
return direct(p, now, LedgerOfAt(p, Filter{}, now).Rows)
|
|
}
|
|
|
|
// Summaries is Summarize(p) and Direct(p) asked at one moment, for a page
|
|
// that shows the two together: asked one after the other, midnight could
|
|
// fall between them and set one day's calls through magpie beside the next
|
|
// day's calls not through it.
|
|
func Summaries(p Period) (Summary, Summary) {
|
|
now := Clock()
|
|
return indexedSummary(p, now), direct(p, now, LedgerOfAt(p, Filter{}, now).Rows)
|
|
}
|
|
|
|
func direct(p Period, now time.Time, rows []Row) Summary {
|
|
s := Summary{Period: p, Since: p.Since(now), Bucket: "day", Agents: []Group{}, Models: []Group{}, ProviderKeys: []Group{}, Accounts: []Group{}, CallerKeys: []Group{}, Sessions: []Group{}, Series: []Point{}}
|
|
s.Period = p.shown()
|
|
until := p.Until(now)
|
|
agents, models, accounts, sessions := map[string]*Group{}, map[string]*Group{}, map[string]*Group{}, map[string]*Group{}
|
|
put := func(m map[string]*Group, k string, g Group, r Row) {
|
|
if m[k] == nil {
|
|
m[k] = &g
|
|
}
|
|
m[k].addRow(r)
|
|
}
|
|
for _, r := range rows {
|
|
if r.Source != "log" || r.IsRejected() || r.Time.Before(s.Since) || after(until, r.Time) {
|
|
continue
|
|
}
|
|
s.Totals.addRow(r)
|
|
id := AgentOf(r.Agent)
|
|
put(agents, id, Group{ID: id}, r)
|
|
put(models, r.Provider+"/"+r.Model, Group{ID: r.Provider + "/" + r.Model, Provider: r.Provider, Model: r.Model}, r)
|
|
if who := r.Account(); who != "" {
|
|
put(accounts, r.Provider+"@"+who, Group{ID: r.Provider + "@" + who, Provider: r.Provider, Account: who}, r)
|
|
}
|
|
if r.Session != "" {
|
|
put(sessions, id+"|"+r.Session, Group{ID: r.Session, Agent: id}, r)
|
|
}
|
|
}
|
|
list := func(m map[string]*Group) []Group {
|
|
gs := []Group{}
|
|
for _, g := range m {
|
|
gs = append(gs, *g)
|
|
}
|
|
sort.SliceStable(gs, func(i, j int) bool {
|
|
if a, b := gs[i].Tokens(), gs[j].Tokens(); a != b {
|
|
return a > b
|
|
}
|
|
if gs[i].Calls != gs[j].Calls {
|
|
return gs[i].Calls > gs[j].Calls
|
|
}
|
|
return gs[i].ID < gs[j].ID
|
|
})
|
|
return gs
|
|
}
|
|
s.Agents, s.Models, s.Accounts, s.Sessions = list(agents), list(models), list(accounts), list(sessions)
|
|
return s
|
|
}
|