today, 7d and 30d begin at a local midnight, so a caller wanting "the
last few hours" just after midnight had to take 7d's CSV, seven days of
rows for one. `magpie usage [--csv [--account <name>]] --last 24h` (any
Go duration, 90m) or `--days 1.5` now reads a window that began that
long ago; the presets are unchanged.
From fcy222fcy's branch (usage-rolling-window, 60472c661): a rolling
window is a period "last:<dur>" (usage.LastOf / Period.Last) that Since,
Known, shown and the chart's timeline answer for, so the ledger, Direct
and the summary read the same window.
Review fixes on top:
- a summary of a rolling window is worked out each time, like a picked
range: kept, its start didn't move with the clock while the log
stayed the same (reachable through the web API's ?period=last:24h);
- the CSV export's file name has no colon from "last:24h0m0s", which
Windows refuses;
- `--last 24h 7d` is refused rather than silently taking --last;
- --days refuses NaN, Inf and over a century;
- the help line and docs/reference.md name the flags.
Run: new TestRollingWindowIsNotKeptStale fails with the cache fix taken
out ("an hour later: 1 calls, want 0"); TestCSVStamp fails without the
colon fix ("magpie-requests-last:24h0m0s-2026-09-30");
TestUsageRollingWindowRejectsJunk fails without the preset/--days
checks; TestUsageRollingWindow and the new ...WithAccount fail (3 rows,
want 2) with Since moved back. go test -tags nogui ./internal/usage/...
./internal/cli/... ./internal/usageapi/... ./internal/gui/... in a
sandbox HOME: ok. gofmt -l clean; go vet darwin, GOOS=linux -tags nogui,
GOOS=windows; go build darwin, linux nogui, windows.
Real use: the built binary on a sandbox HOME with a generated 50,000-call
7-day usage.jsonl: --csv 7d 47,233 rows / 6.97 MB; today 4,376; --last
24h 7,140 / 1.05 MB; --last 90m 444; --days 1.5 10,712; --account me@x
--last 24h 3,570 (that account's half); oldest row of each at the
window's start.
Not tried: the full go test ./... suite; the process still parses the
whole usage.jsonl into its in-memory index on each run (as every period
does), so the cut is in rows decoded and written, not bytes read.
Co-authored-by: fchengyan <3181088318@qq.com>
108 lines
3.1 KiB
Go
108 lines
3.1 KiB
Go
package usage
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// A period can also be days the reader picked (#1492): from one date to
|
|
// another, both whole days and both included, in the local time zone,
|
|
// written "2026-10-01..2026-10-07". It begins at the first day's midnight and
|
|
// ends at the midnight after the last, where the presets end at now.
|
|
const rangeSep = ".."
|
|
|
|
// RangeOf is the period of the days from one date (YYYY-MM-DD) to another,
|
|
// in either order; ok is false when either is not a date.
|
|
func RangeOf(from, to string) (Period, bool) {
|
|
a, err1 := time.Parse(time.DateOnly, from)
|
|
b, err2 := time.Parse(time.DateOnly, to)
|
|
if err1 != nil || err2 != nil {
|
|
return "", false
|
|
}
|
|
if b.Before(a) {
|
|
a, b = b, a
|
|
}
|
|
return Period(a.Format(time.DateOnly) + rangeSep + b.Format(time.DateOnly)), true
|
|
}
|
|
|
|
// A period can also be hours or days long, ending now (#2075): a rolling
|
|
// window, written "last:24h" or "last:90m", read from LastOf. Unlike the
|
|
// presets, which begin at a local midnight, it begins that long ago —
|
|
// what `magpie usage --last 24h` and `--days 1` ask for, where a preset
|
|
// would give a whole calendar day or week instead.
|
|
const lastSep = "last:"
|
|
|
|
// LastOf is the rolling window of d ending now; ok is false when d is
|
|
// not positive.
|
|
func LastOf(d time.Duration) (Period, bool) {
|
|
if d <= 0 {
|
|
return "", false
|
|
}
|
|
return Period(lastSep + d.String()), true
|
|
}
|
|
|
|
// Last is the length of a rolling window ("last:24h"), 0 when p is not
|
|
// one.
|
|
func (p Period) Last() time.Duration {
|
|
s, ok := strings.CutPrefix(string(p), lastSep)
|
|
if !ok {
|
|
return 0
|
|
}
|
|
d, err := time.ParseDuration(s)
|
|
if err != nil || d <= 0 {
|
|
return 0
|
|
}
|
|
return d
|
|
}
|
|
|
|
// days is the first and last day of a picked range, at midnight in loc.
|
|
func (p Period) days(loc *time.Location) (first, last time.Time, ok bool) {
|
|
from, to, found := strings.Cut(string(p), rangeSep)
|
|
if !found {
|
|
return
|
|
}
|
|
a, err1 := time.ParseInLocation(time.DateOnly, from, loc)
|
|
b, err2 := time.ParseInLocation(time.DateOnly, to, loc)
|
|
if err1 != nil || err2 != nil || b.Before(a) {
|
|
return
|
|
}
|
|
return a, b, true
|
|
}
|
|
|
|
// IsRange is whether p is days the reader picked rather than a preset.
|
|
func (p Period) IsRange() bool {
|
|
_, _, ok := p.days(time.UTC)
|
|
return ok
|
|
}
|
|
|
|
// Known is whether p is a period the usage pages answer for: a preset, a
|
|
// picked range or a rolling window.
|
|
func (p Period) Known() bool {
|
|
switch p {
|
|
case Today, Week, Month, All:
|
|
return true
|
|
}
|
|
return p.IsRange() || p.Last() > 0
|
|
}
|
|
|
|
// Until is when the period ends, as of now: the midnight after a picked
|
|
// range's last day, zero for a preset, which runs up to now.
|
|
func (p Period) Until(now time.Time) time.Time {
|
|
if _, last, ok := p.days(now.Location()); ok {
|
|
return last.AddDate(0, 0, 1)
|
|
}
|
|
return time.Time{}
|
|
}
|
|
|
|
// after is whether t falls past the period's end.
|
|
func after(until, t time.Time) bool { return !until.IsZero() && !t.Before(until) }
|
|
|
|
// shown is the period a summary says it is of: the preset or range asked
|
|
// for, and All for anything else.
|
|
func (p Period) shown() Period {
|
|
if p == Today || p == Week || p == Month || p.IsRange() || p.Last() > 0 {
|
|
return p
|
|
}
|
|
return All
|
|
}
|