provider: quota history draws no line for a plugin's aside window that says no share (#4825)
ggbdpq's patch (gua2100 on Discord), reviewed and landed with an
end-to-end test and a doc line.
What changed
- quotaOfPlugin marks an aside window that leaves `used` out, with no
count to reckon one from, as noShare. pointOf refuses such a window,
so the history no longer draws a flat 100%-left line for it. The card
still shows the row as before.
- noShare is unexported and so lost on JSON. Cards restored from the
cache have AsOf set, and noteQuotaHistory skips them, so this never
matters.
- Review changes: TestAsideWindowWithoutUsedDrawsNoLine now also runs
the windows through noteQuotaHistory and QuotaHistories in a sandbox
HOME, and wants lines [Bonus Weekly] only.
docs/subsystems/providers-accounts.md says such a window isn't kept.
Run at the pushed head (on mote, base 4d55a0a02)
- go test -race ./internal/provider passes for the touched tests, and
the provider package passes.
- The full suite, gofmt and the cross-OS vet are as recorded in the
#4824 netproxy commit. The only reds are #4826 and #4807, neither in
provider.
Broken by hand: with noShare removed from pointOf, the test is red:
"On-demand/Points: history point true, want false" and "history lines
[Bonus On-demand Points Weekly], want [Bonus Weekly]".
Not tried: a real plugin reporting such a row, or the chart in a
browser. A wallet Balance window draws the same flat line; that is
#4831, not this commit.
Co-authored-by: yetone <yetoneful@gmail.com>
This commit is contained in:
committed by
yetone
co-authored by
yetone
parent
33f21bdb1f
commit
ca1fc43fc9
@@ -253,7 +253,10 @@ Allowance readings are kept by provider, lowercased account and window in
|
||||
for up to 45 days and 6000 points per window. Failed or stale readings add
|
||||
nothing. A window set aside for routing is kept when it is measured (a
|
||||
pool's window, or the Cursor plugin's Weekly and Grok Bot, #4612); a
|
||||
text-only one tells no share and is not. `newCycle` preserves a boundary
|
||||
text-only one tells no share and is not, and neither is a plugin's aside
|
||||
window that leaves `used` out with no count to reckon one from: its `Used`
|
||||
is 0 for want of a share, not a reading, and drew a flat 100%-left line
|
||||
(`noShare`, #4825). `newCycle` preserves a boundary
|
||||
when the previous deadline passes,
|
||||
appears/disappears, moves backward by more than five minutes or forward by
|
||||
more than the elapsed sampling time plus five minutes. A refill of more
|
||||
|
||||
@@ -175,7 +175,12 @@ func quotaOfPlugin(q SubscriptionQuota, u plugin.Usage) SubscriptionQuota {
|
||||
}
|
||||
if used != nil {
|
||||
w.Used = *used
|
||||
} else if !x.Aside && x.Balance == nil {
|
||||
} else if x.Aside {
|
||||
// a window set aside that says no share: its Used is 0 for want
|
||||
// of one said, not a reading at 0, which quota history drew as
|
||||
// a flat 100% left line (#4825)
|
||||
w.noShare = true
|
||||
} else if x.Balance == nil {
|
||||
// the plugin didn't say how much of it is used: its text is all
|
||||
// there is, and routing has no share to count (#3650)
|
||||
w.Text, w.Aside = true, true
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -163,3 +164,39 @@ func TestAWindowsCountIsItsShare(t *testing.T) {
|
||||
t.Errorf("routing: known %v, used %v; want the count's 37", ok, used)
|
||||
}
|
||||
}
|
||||
|
||||
// A window set aside whose used was left out says no share: quota history
|
||||
// took its Used, left at 0, for a reading and drew a flat 100% left line
|
||||
// (#4825). One that said used, or whose amount and limit reckon to a
|
||||
// share, is a reading and keeps its line (chowyuan1314, #4612).
|
||||
func TestAsideWindowWithoutUsedDrawsNoLine(t *testing.T) {
|
||||
var u plugin.Usage
|
||||
if err := json.Unmarshal([]byte(`{"windows":[
|
||||
{"name":"On-demand","display":"$12.34","amount":12.34,"aside":true},
|
||||
{"name":"Points","amount":500,"aside":true},
|
||||
{"name":"Weekly","used":56.1,"aside":true},
|
||||
{"name":"Bonus","amount":5,"limit":20,"aside":true}]}`), &u); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
q := quotaOfPlugin(SubscriptionQuota{Provider: "acme-plugin", User: "u"}, u)
|
||||
now := time.Now()
|
||||
for i, w := range q.Windows {
|
||||
if _, ok := pointOf(w, now, now); ok != (i >= 2) {
|
||||
t.Errorf("%s: history point %v, want %v", w.Name, ok, i >= 2)
|
||||
}
|
||||
}
|
||||
// the whole way to what the chart reads: the reading noted, the
|
||||
// history read back has the two measured lines alone
|
||||
testenv.SetHome(t, t.TempDir())
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
noteQuotaHistory([]SubscriptionQuota{q}, now)
|
||||
var lines []string
|
||||
for _, h := range QuotaHistories(time.Time{}, "acme-plugin", "u") {
|
||||
for _, l := range h.Lines {
|
||||
lines = append(lines, l.Name)
|
||||
}
|
||||
}
|
||||
if want := []string{"Bonus", "Weekly"}; !slices.Equal(lines, want) {
|
||||
t.Errorf("history lines %v, want %v", lines, want)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,9 +226,10 @@ func newCycle(a, b QuotaPoint) bool {
|
||||
// (max2020 on Discord: an Antigravity card had no curve and no "runs
|
||||
// out" line; chowyuan1314, #4612: the Cursor plugin's Weekly and Grok
|
||||
// Bot are aside and had no curve). A window told as its text alone has
|
||||
// no share to draw.
|
||||
// no share to draw, and neither has one set aside whose Used is 0 for
|
||||
// want of a share said (noShare, #4825).
|
||||
func pointOf(w QuotaWindow, at, now time.Time) (QuotaPoint, bool) {
|
||||
if w.Unlimited || w.Text || w.Family != "" || w.Name == "" || math.IsNaN(w.Used) {
|
||||
if w.Unlimited || w.Text || w.noShare || w.Family != "" || w.Name == "" || math.IsNaN(w.Used) {
|
||||
return QuotaPoint{}, false
|
||||
}
|
||||
p := QuotaPoint{At: at.UTC().Truncate(time.Second), Left: math.Round(max(0, min(100, 100-w.Used))*100) / 100}
|
||||
|
||||
@@ -134,6 +134,11 @@ type QuotaWindow struct {
|
||||
// rate_limit_event names one window at a time), never read whole by
|
||||
// /usage — five hours alone there needn't mean no week.
|
||||
partial bool
|
||||
// noShare is set on a window set aside whose used the plugin left
|
||||
// out: its Used is 0 for want of a share said, not a reading at 0, so
|
||||
// quota history keeps no line for it (#4825). The card still tells
|
||||
// the row's text and routing still sets it aside.
|
||||
noShare bool
|
||||
}
|
||||
|
||||
// SubscriptionQuota is provider-reported allowance usage. This is separate
|
||||
|
||||
Reference in New Issue
Block a user