Outvi V reads a remote magpie through `magpie web`. The Usage page
bucketed calls by the server's days and hours, so a call at 9:00 in
UTC+8 showed under 1:00.
What changed:
- The Usage page (plugins/usage/page.js) names its browser's IANA zone
in ?tz= on /api/usage, /api/usage/requests, /api/usage/heatmap, the
CSV link and the export.
- usage.Zone turns the name into one cached *time.Location per name, so
a Filter holding it stays a comparable cache key. An empty or unknown
name, or a zone that keeps this computer's clock, is time.Local, so
the desktop page, the tray and the TUI see no change and share caches.
- Filter.Zone carries the zone. Today/7d/30d, the chart's hour and day
buckets and labels, the Day filter and the heatmap's days are all
read in it (Clock().In(zone)). The summary cache is keyed by period
and zone, and the request-page cache by the reader's date.
- Shared days from other computers are read with a 2-day margin, not
1, since two zones can be 26h apart.
- Limit and quota resets are unchanged. The gateway's
/v1/magpie/usage stays on the computer's zone.
Run at e435b8867 + this change:
- gofmt -l and go vet on internal/usage, internal/usageapi and
internal/gui: darwin, GOOS=linux -tags nogui, and GOOS=windows. The
vet ran before the new _test files were added.
- go test ./internal/usage ./internal/usageapi and
-tags nogui ./internal/gui: ok with TZ=UTC and with
TZ=Asia/Shanghai.
- New tests:
- TestUsageReadsTheViewersZone and TestZoneNames (internal/usage).
- TestUsageRoutesReadTheViewersZone (internal/gui) asks the routes
with ?tz= naming a zone other than the server's.
- Each holds usage.Clock and checks the hour label, today's
membership, the Day filter, the week chart's day label and the
heatmap's days. With no ?tz=, the server's days are kept.
Broken by hand:
- Clock().In(zone) / Clock().In(f.loc()) back to Clock(): red in both
zones (Summary, Requests, ledger, heatmap, /api/usage,
/api/usage/requests, /api/usage/heatmap).
- The Day filter read in time.Local: red under TZ=UTC.
- The summary's and chart's bucketing in time.Local: the week-day label
check goes red under TZ=UTC. Under TZ=Asia/Shanghai the chosen
moment falls on one date in both zones, so it can't tell there.
Each was restored after.
Not tried:
- A Playwright GUI test with timezoneId in Chromium and WebKit was
not written. The GUI suite and usage-stale-answer.test.cjs (which got
a usageZoneQ stub) were not run: the disk was at ~350 MB free.
- TestCoreScriptBudget was not run again after the change. app.js is
untouched, and page.js is a plugin page outside the core budget.
- A real remote `magpie web` from another zone, and the Windows box.
- Siblings left alone:
- The Sessions tab buckets by the server's days and hours in its
cached rollups, and its "By hour" card labels them with the
browser's zone name.
- The Routing history's day files are the server's days.
These need issues of their own.
111 lines
3.3 KiB
Go
111 lines
3.3 KiB
Go
package usage
|
|
|
|
// The heatmap (#1369): the last 53 weeks a day each, as a calendar of
|
|
// contributions lays them out, Monday on top. It is the request page's own
|
|
// index read over that span — QueryPage's cached chunks, priced once — and
|
|
// not another pass over the logs, so a page asking for it again reads only
|
|
// what changed. It counts what the Requests tab's chart counts: the rows its
|
|
// filters keep, but for the day picked, and not the calls the gateway turned
|
|
// away itself.
|
|
|
|
import (
|
|
"slices"
|
|
"time"
|
|
)
|
|
|
|
// HeatmapWeeks is how many weeks the heatmap shows, this one the last.
|
|
const HeatmapWeeks = 53
|
|
|
|
// heatmapPeriod is the span the heatmap is read over: QueryPage's period for
|
|
// it, from the Monday HeatmapWeeks-1 weeks before this week's. Not one the
|
|
// page's period switch offers.
|
|
const heatmapPeriod Period = "heatmap"
|
|
|
|
// heatmapSince is the Monday the heatmap starts on, as of now.
|
|
func heatmapSince(now time.Time) time.Time {
|
|
day := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
|
return day.AddDate(0, 0, -(int(day.Weekday())+6)%7-7*(HeatmapWeeks-1))
|
|
}
|
|
|
|
// Day is one day's calls, by the local calendar day they fell on.
|
|
type Day struct {
|
|
Date string `json:"date"` // YYYY-MM-DD
|
|
Calls int `json:"calls"`
|
|
Tokens int `json:"tokens"` // in, out and through the cache, as the chart counts them
|
|
Cost float64 `json:"cost"` // of the priced calls, USD
|
|
// Unpriced: calls with tokens but no known price, not in Cost
|
|
Unpriced int `json:"unpriced,omitempty"`
|
|
}
|
|
|
|
// Heatmap is the days from From to To (today), both YYYY-MM-DD in the
|
|
// reader's time zone (Filter.Zone): only those with calls are listed, oldest first.
|
|
type Heatmap struct {
|
|
From string `json:"from"`
|
|
To string `json:"to"`
|
|
Days []Day `json:"days"`
|
|
}
|
|
|
|
// HeatmapOf is the last HeatmapWeeks weeks of the calls the filter keeps, a
|
|
// day each. The filter's Day is not one of its dimensions here: the heatmap
|
|
// is every day's.
|
|
func HeatmapOf(f Filter) Heatmap {
|
|
f.Day = ""
|
|
if h := QueryPage(heatmapPeriod, f, 0, 1).Heat; h != nil {
|
|
return *h
|
|
}
|
|
return Heatmap{Days: []Day{}}
|
|
}
|
|
|
|
// heatmapOf sums the rows visit gives it by their day in now's zone (the
|
|
// reader's), from since to now's day; a row outside those days is left out.
|
|
func heatmapOf(since, now time.Time, visit func(add func(Row))) *Heatmap {
|
|
h := &Heatmap{From: since.Format(time.DateOnly), To: now.Format(time.DateOnly), Days: []Day{}}
|
|
at := map[string]*Day{}
|
|
// a day's name is made once for each day, not for each of its calls
|
|
type ymd struct{ y, yd int }
|
|
names := map[ymd]string{}
|
|
visit(func(r Row) {
|
|
if r.IsRejected() || r.Time.Before(since) {
|
|
return
|
|
}
|
|
t := r.Time.In(now.Location())
|
|
k := ymd{t.Year(), t.YearDay()}
|
|
d, ok := names[k]
|
|
if !ok {
|
|
d = t.Format(time.DateOnly)
|
|
names[k] = d
|
|
}
|
|
if d > h.To {
|
|
return
|
|
}
|
|
x := at[d]
|
|
if x == nil {
|
|
x = &Day{Date: d}
|
|
at[d] = x
|
|
}
|
|
if !r.HandedOn {
|
|
x.Calls++ // a request, not each try at it
|
|
}
|
|
x.Tokens += r.Input + r.Output + r.CacheRead + r.CacheWrite
|
|
switch {
|
|
case r.Priced:
|
|
x.Cost += r.Cost
|
|
case r.Input+r.Output > 0:
|
|
x.Unpriced++
|
|
}
|
|
})
|
|
for _, x := range at {
|
|
h.Days = append(h.Days, *x)
|
|
}
|
|
slices.SortFunc(h.Days, func(a, b Day) int {
|
|
if a.Date < b.Date {
|
|
return -1
|
|
}
|
|
if a.Date > b.Date {
|
|
return 1
|
|
}
|
|
return 0
|
|
})
|
|
return h
|
|
}
|