Files
ming_jiang 4b0aef5327 amp-usage: Amp usage page with background collection; day-card 0.3.1 (#1000)
Adds packages/amp-usage (@magpie-community/page-amp-usage 0.1.0). It is a magpie page that reads Amp's local thread files and the gateway's usage.jsonl and shows per-day usage. The service listens on loopback only and requires a per-start service token on every API call. The page that carries the token is served only to a navigation (Sec-Fetch-Mode). The gateway log is read incrementally from a saved offset, whole lines only, and is re-indexed on rotation. The UI words are in ui/words.js (en, zh).

Also bumps packages/day-card to 0.3.1.

Round 3 (13c1f33) adds three things:
- a test that bytes before the gateway-log offset are never re-read;
- ui.test.mjs, which checks that every error code the server can return has words in each language, and that an unknown code falls back;
- words.js, moved out of app.js, served at /words.js and included in the pack.

What ran, at 13c1f33 merged with main 14187408, on mica:
- amp-usage `node --test`: 47/47.
- `bun test packages/amp-usage packages/day-card`: 65 pass, 0 fail.
- `bun scripts/check.mjs`, all packages: ok.
- `npm pack --dry-run`: amp-usage 0.1.0 has 19 files including ui/words.js; day-card 0.3.1 has 17 files.
- Root `bun test` with a temp HOME: 8362 pass, 0 fail (8382 tests, 590 files).
- Render smoke in Chromium and WebKit at 1200 and 420px, en and zh: words.js loads, both languages show their own words, no page errors, no horizontal overflow.

Broken by hand, each run with node and bun:
- collector.mjs:102 made to re-read the whole file every scan: the new offset test goes red.
- `const cut = buf.length`, so a partial line is taken: red.
- Raw error codes shown instead of their words (`w.err[code] || code`): red.
- A zh string deleted: red.

Not tried:
- A real Amp account or the real `amp` CLI.
- Changing the auto-refresh default from 300 to 60 fails no test. The author left it untested and round 2 called it optional.

Rounds 1 and 2 also ran the 1M-line log probe (rss steady at about 363MB, scans of 70 to 114ms after the first), the bad-record and paging probes, and the full render matrix including dark mode.
2026-11-11 21:43:59 +08:00

110 lines
3.6 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="service-token" content="__SERVICE_TOKEN__" />
<title>Amp usage</title>
<link rel="stylesheet" href="style.css" />
<script src="words.js" defer></script>
<script src="app.js" defer></script>
</head>
<body>
<main>
<header>
<div id="periods" class="segmented" role="group">
<button type="button" data-period="today" data-t="today"></button>
<button type="button" data-period="24h" data-t="h24"></button>
<button type="button" data-period="7d" data-t="d7"></button>
<button type="button" data-period="30d" data-t="d30"></button>
<button type="button" data-period="all" data-t="all"></button>
<button type="button" data-period="custom">
<svg viewBox="0 0 16 16" aria-hidden="true">
<path
d="M3 4.5h10v8.5H3zM3 7h10M5.5 2.5v3M10.5 2.5v3"
fill="none"
stroke="currentColor"
stroke-width="1.3"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
<span data-t="custom"></span>
</button>
</div>
<div class="tools">
<button id="refresh" class="icon" type="button" disabled>
<svg viewBox="0 0 16 16" aria-hidden="true">
<path
d="M13.5 8a5.5 5.5 0 1 1-1.6-3.9M13.5 2v3.4h-3.4"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
</button>
<select id="auto"></select>
</div>
</header>
<div id="range" class="range" hidden>
<input id="from" type="date" />
<span class="muted">–</span>
<input id="to" type="date" />
</div>
<div id="errors" class="card errors" role="alert" hidden></div>
<p id="empty" class="muted" role="status" hidden></p>
<div id="board" hidden>
<dl id="tiles" class="tiles"></dl>
<section id="chart" class="card">
<div class="chart-head">
<h2 id="chart-title"></h2>
<span id="readout" class="muted"></span>
</div>
<div id="bars" class="bars"></div>
<div class="axis muted">
<span id="first-tick"></span><span id="last-tick"></span>
</div>
</section>
<section class="card">
<h2 data-t="byModel"></h2>
<div class="scroll">
<table>
<thead>
<tr>
<th data-t="model"></th>
<th data-t="calls"></th>
<th data-t="input"></th>
<th data-t="output"></th>
<th data-t="cacheRead"></th>
<th data-t="cacheWrite"></th>
<th data-t="share"></th>
</tr>
</thead>
<tbody id="models"></tbody>
</table>
</div>
</section>
</div>
<details id="issues-box" hidden>
<summary id="issues-summary"></summary>
<ul id="issues"></ul>
</details>
<details>
<summary data-t="howTitle"></summary>
<p data-t="how1"></p>
<p data-t="how2"></p>
</details>
<footer class="muted">
<span id="progress" role="status" aria-live="polite"></span>
<span data-t="credit"></span>
</footer>
</main>
</body>
</html>