The Usage page's account cards told a window's whole ("Whole ≈ N tokens
· ≈ $X") only where it could be reckoned honestly, and said nothing
elsewhere, so a reader couldn't tell a window magpie can't reckon from
a fault (#2045 asked for "数据不足" there).
The server now says why on each window of an account's own card that
isn't reckoned (QuotaWindow.HoldsWhy):
- low: under 5% used (HoldsFloor);
- clock: no reset clock, so no known start;
- log: the window began before magpie's request log;
- none: magpie routed nothing the window counts in it;
- credits: a call in it may have been paid from credits or extra usage
(#2010's dry runs).
Windows whose whole is never told (a pool, a family, money aside, an
error card, another magpie's) carry no reason and say nothing, as
before. WithWindowHolds now also fills reasons when no window at all can
be reckoned; the caller's cards are still left unwritten.
The GUI shows a muted "Whole: not enough data" on the reset's line, with
the reason in its tooltip, in all five languages. A reason it doesn't
know says nothing. History cards are unchanged.
Run:
- go test ./internal/usage ./internal/gui: pass. The window-holds tests
check each reason, that reckoned windows carry none, and that the
caller's cards stay unwritten. Not writing HoldsWhy turns 3 tests red.
- quota-holds.test.cjs in Chromium and WebKit, en/zh/zh-TW/ja/de, at
360, 420, 440, 1000 and 1100: the line, its reason, muted, not cut,
inside its card. Removing the GUI branch turns it red. Screenshots at
420 in zh and de looked at.
- i18n-keys, quota-curve, keep-login, quota-name-translated in both
engines: pass.
- gofmt -l clean; go vet darwin, linux (-tags nogui), windows; linux and
windows builds.
Not tried: a real account's card in the running app.
78 lines
2.9 KiB
Go
78 lines
2.9 KiB
Go
package provider
|
|
|
|
// What a whole subscription window holds, as the Usage page's account
|
|
// cards say beside its share used: what magpie routed through the account
|
|
// since the window began, over the share the vendor says is used. Package
|
|
// usage reckons it (WithWindowHolds), from its log; here are the figures and
|
|
// the window's bounds.
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// WindowHolds is a window's whole allowance reckoned from magpie's own
|
|
// calls: Tokens of input and output (cache traffic aside, as the Usage
|
|
// page counts tokens) and, when every call's model has an API list price,
|
|
// Cost at those prices (cache reads and writes at the vendor's own rates).
|
|
// Routed is what magpie routed through the account in the window so far,
|
|
// up to when the share was read, and Used that share. A call made with the
|
|
// account outside magpie is in Used but not in Routed, so the estimate is
|
|
// then lower than the window really holds.
|
|
type WindowHolds struct {
|
|
Tokens int64 `json:"tokens"`
|
|
Cost float64 `json:"cost,omitempty"`
|
|
Priced bool `json:"priced"`
|
|
Used float64 `json:"used"`
|
|
Routed struct {
|
|
Calls int `json:"calls"`
|
|
Tokens int64 `json:"tokens"`
|
|
CacheRead int64 `json:"cacheRead,omitempty"`
|
|
CacheWrite int64 `json:"cacheWrite,omitempty"`
|
|
Cost float64 `json:"cost,omitempty"`
|
|
} `json:"routed"`
|
|
Since time.Time `json:"since"`
|
|
}
|
|
|
|
// HoldsFloor is the least share used a window's whole is reckoned from:
|
|
// below it a percent the vendor rounds is too coarse to scale up.
|
|
const HoldsFloor = 5.0
|
|
|
|
// Why a window's whole isn't told (QuotaWindow.HoldsWhy), for the GUI to
|
|
// say "not enough data" and why rather than leave it out (#2045).
|
|
const (
|
|
HoldsLow = "low" // under HoldsFloor used
|
|
HoldsNoClock = "clock" // no reset clock, so no known start
|
|
HoldsBeforeLog = "log" // began before magpie's log does
|
|
HoldsNoneRouted = "none" // magpie routed nothing it counts in it
|
|
HoldsCredits = "credits" // a call in it may have been paid from credits
|
|
)
|
|
|
|
// Bounds is when w, read at readAt, began and ends: its reset, and that
|
|
// less how long it runs; a month's window began on the same day a month
|
|
// before. ok is false when either isn't known.
|
|
func (w QuotaWindow) Bounds(readAt time.Time) (start, end time.Time, ok bool) {
|
|
switch {
|
|
case w.ResetsAt != nil:
|
|
end = *w.ResetsAt
|
|
case w.ResetSecs > 0:
|
|
end = readAt.Add(time.Duration(w.ResetSecs) * time.Second)
|
|
default:
|
|
return time.Time{}, time.Time{}, false
|
|
}
|
|
span := windowSpan(w)
|
|
if span <= 0 {
|
|
return time.Time{}, time.Time{}, false
|
|
}
|
|
if span == 30*24*time.Hour {
|
|
return end.AddDate(0, -1, 0), end, true
|
|
}
|
|
return end.Add(-span), end, true
|
|
}
|
|
|
|
// Counts is whether a call to model counts against w: every model, or
|
|
// only the ones its scope names ("opus", a plugin's list).
|
|
func (w QuotaWindow) Counts(model string) bool {
|
|
return Limit{Model: w.Model, matches: w.matches}.applies(strings.ToLower(model))
|
|
}
|