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.
63 lines
1.9 KiB
Go
63 lines
1.9 KiB
Go
package usage
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// The Usage page buckets its calls by the reader's days and hours, not the
|
|
// computer's: `magpie web` read from across an ocean showed a call at 9:00
|
|
// in UTC+8 under 1:00 (Outvi V on Discord). The page names its browser's
|
|
// zone, and Zone is that zone here.
|
|
|
|
var zones sync.Map // name → *time.Location, one for each name
|
|
|
|
// Zone is the time zone a browser names (an IANA name such as
|
|
// "Asia/Shanghai"), the same *time.Location for every ask of one name, so
|
|
// that a Filter holding it is the same cache key each time. A zone that
|
|
// keeps this computer's clock, the page on this computer, is time.Local,
|
|
// whose answers the tray and the TUI share. An empty name or one this
|
|
// computer doesn't know is time.Local: the computer's days, as before.
|
|
func Zone(name string) *time.Location {
|
|
if name == "" || name == "Local" {
|
|
return time.Local
|
|
}
|
|
if l, ok := zones.Load(name); ok {
|
|
return l.(*time.Location)
|
|
}
|
|
loc, err := time.LoadLocation(name)
|
|
if err != nil {
|
|
return time.Local // not kept: any text can be asked for
|
|
}
|
|
if sameClock(loc, time.Local) {
|
|
loc = time.Local
|
|
}
|
|
l, _ := zones.LoadOrStore(name, loc)
|
|
return l.(*time.Location)
|
|
}
|
|
|
|
// sameClock is whether a and b tell the same offset every twelve hours
|
|
// from three years back to a year on: the span the Usage page's days reach
|
|
// (its heatmap is a year), and either side of every daylight-saving change
|
|
// in it. Worked out once for each name.
|
|
func sameClock(a, b *time.Location) bool {
|
|
y := time.Now().Year()
|
|
end := time.Date(y+2, time.January, 1, 0, 0, 0, 0, time.UTC)
|
|
for t := time.Date(y-3, time.January, 1, 0, 0, 0, 0, time.UTC); t.Before(end); t = t.Add(12 * time.Hour) {
|
|
_, oa := t.In(a).Zone()
|
|
_, ob := t.In(b).Zone()
|
|
if oa != ob {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// loc is the zone f reads its days in: the reader's, else this computer's.
|
|
func (f Filter) loc() *time.Location {
|
|
if f.Zone != nil {
|
|
return f.Zone
|
|
}
|
|
return time.Local
|
|
}
|