Commit Graph
3 Commits
Author SHA1 Message Date
yetone d7796bf409 usage: a day card's switch builds one page, not one per agent
WaiooT on X: "每日用量卡,切换日期的时候,账单计算很卡". The day-card
page's usage:day read is usageapi.SpanOf. For each date it built the
day's page, then one more page of the same day for every agent (up to
12) only to read that agent's By["modelAt"], and every one of those
builds listed and stat'd every agent session file again
(readRequestState). A switch to an older day misses the request-page
cache (16 pages), so it paid all of them each time.

- RequestPage.AgentModels: each agent's By["modelAt"], counted in the
  same cell pass as By (cellGroup bit of modelAt, keyed by ReportedAs as
  a filter's Agent is), and from the ledger's rows on the LogCalls path.
  SpanOf reads it in place of a page per agent. The calls that named no
  agent are still listed by model only when they are all there are.
- SpanOf holds sessions.OneListing() for its reads, as the sessions
  overview and stats handlers do, so the session folders are listed once
  a read.

Measured on mote (a throwaway test, not committed: 401,502 gateway
records over 365 days in 6 agents x 6 providers, plus 20 Claude Code
session files a day of 60 calls each, half mirrored by gateway records),
origin/main 6ac19f4bc against this:
- switch to a past day: 243-300ms -> 32-48ms
- today again after it: 57-94ms -> 20-28ms
- 7-day span 400ms -> 44ms, 30-day 1.17s -> 91ms, all time 12.3s -> 2.2s
- 30 switches back a day each (180-day ledger): 4.0-4.9s -> 0.87s
The CPU profile before: readRequestState 46%, then the per-agent builds.

TestAgentModelsAreEachAgentsPage holds AgentModels to the page of
each agent, for a whole week and for a day in it, on the cells path and
the ledger path. Broken by hand: counting from the chart's bits (the
whole period) goes red on cells; the whole ledger in place of the
filter's rows goes red on ledger. Keying by the raw agent instead of
ReportedAs stays green: rows carry the reported name already, and only a
renamed client could tell the two apart.
2026-11-19 18:09:56 +08:00
yetone 745d209dcf usage:day takes a from: a week, a month or all time in one read (HeheLi29136651 on X)
HeheLi29136651 asked for the Day card's weekly, monthly and total bill. The
read only answered one day, so the plugin had nothing to draw. usage:day
now takes {from: "YYYY-MM-DD" | "all"} beside date: the days from there to
date, with the same totals, agents, models and days list (usageapi.SpanOf;
DayOf is SpanOf with no from). "all" starts at the first day with calls:
the ledger keeps old calls in wider buckets that start before the first
call, so From is taken from the first day the heatmap lists. A from after
date, or a date after today, is an error. Answers carry `from`, which is how
a plugin tells a magpie that has spans from one that ignores the param.

Run, on mote:
- TestSpanOf (week, month, all, all up to an earlier date, day alone,
  bad inputs) with calls 300 days back, where the bucket starts before the
  first call: red when the From correction is removed (From 2026-01-12 for
  a first call on 01-15), green with it.
- TestPluginPageReadsADay gains from "2000-01-01", from "all" and a from
  after the day.
- go vet (darwin, windows, linux -tags nogui), usageapi and internal/plugin
  tests ok.
- The plugin side (page-day-card 0.3.0) reads it: seen in the sandbox web
  instance in chromium and webkit, empty ledger only.

Not tried: a ledger with real calls end to end in the page (the plugin's
test covers the card from a made-up read), and a magpie from before this
change opening the 0.3.0 page (the code path throws "usage:day from" when
there is no `from`, and says to update).
2026-11-11 17:38:42 +08:00
yetone 623e797fde plugin pages: read a day's usage and share a picture they drew
A plugin's page can now read one day's usage and share an image it
drew. This is for the day card (JungleyYeh on X asked for a "daily AI
statement"); the card itself is an optional community plugin,
@magpie-community/page-day-card.

- A page that asks for the "usage:day" read gets that day as
  magpie.read("usage:day", {date, tz}): totals, each agent and its
  models, the streak and the average active day (usageapi/day.go,
  gui/pageday.go). The Plugins page names the read before it is granted.
- magpie.image.copy(png) and magpie.image.save(png, name) share a PNG
  of at most 16 MB. In the window they go through
  POST /api/plugin-pages/image (pageimage.go): copy puts the picture on
  the clipboard (clipimage.go: osascript PNGf on macOS, PowerShell
  PNG + Bitmap on Windows, wl-copy or xclip on Linux), and save writes
  it to Downloads under a name made safe. In magpie web, the browser
  saves or copies the picture itself.
- The window's side, pageshare.js, is imported the first time a page
  shares a picture. The core grows by its one call only, paid for by
  comments cut, so the budget is not raised.

Tests:
- New: TestDayOf, TestPluginPageReadsADay, TestPluginPageSharesAPicture,
  TestPageImageName and TestClipImageCommand. plugin-page.test.cjs
  covers the bridge, both engines and a refused SVG.

What was run (mote):
- gofmt, plus go vet on darwin, linux and windows: clean.
- Full go test ./... at the pre-rebase base: 369 packages ok, 3 FAILs.
  - TestEveryRouteHasACommandOrIsNamed and TestCoreScriptBudget were
    this change's own reds. The new route now has a parity entry, and
    the share code moved into lazy pageshare.js with no raise.
  - TestGatewayModeStatsNameTheGatewaySessions is #5690, the red in the
    first hour of the day (it ran at 00:32).
- GUI suite (affected and plugin files, both engines): 1160 of 1179
  passed. Every red is filed and none touches this change:
  - i18n-keys "a plugin's words are its own": #5332, #5426, #5482.
  - page-width-steady: #5013.
  - plugin-card-update in WebKit: #3079.
  - plugin-move-unserved "Try again": #4410.
- At this head, after the rebase on bc8b01d60:
  - gofmt clean; go vet ./internal/... on darwin, linux (nogui) and
    windows ok.
  - internal/usageapi, internal/plugin and internal/cli ok.
  - internal/gui: TestCoreScriptBudget, the TestPluginPage* tests and
    the TestPageImage/TestClipImage tests pass.
  - plugin-page and old-webkit (both engines): 12 of 12.

Real use:
- A sandbox HOME with a copy of real usage, through the real GUI
  server, in Chromium and WebKit, light and dark, en/zh/zh-TW/ja/de, at
  420 and 1100 px. The card showed 87.9M tokens, $9.85 and 4076 calls
  with a 22-day streak.
- Picking an earlier day and WebKit's Copy ("Copied") worked, with no
  page errors.
- Not tried at push: Chromium's Copy (its headless clipboard was
  denied) and magpie web's Save as a captured download. A second real
  run that grants both was still queued on mote's suite lock.
- Clipboard: checked live on macOS (osascript) and Windows
  (PowerShell). Not tried live on Linux: WSLg had no display.
2026-11-09 23:19:14 +08:00