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

294 lines
4.9 KiB
CSS

/* Magpie's own look. The launcher passes the window's real colours as
--magpie-* variables; the values below are only fallbacks. */
:root {
--magpie-bg: #f5f6f8;
--magpie-card: #ffffff;
--magpie-fg: #23262e;
--magpie-muted: #6b7280;
--magpie-line: #e2e4e9;
--magpie-accent: #5b5bd6;
--magpie-green: #1f7a4d;
--magpie-red: #b3352f;
color-scheme: light;
}
:root[data-theme="dark"] {
--magpie-bg: #15171c;
--magpie-card: #1c1f26;
--magpie-fg: #e8e9ee;
--magpie-muted: #9aa0aa;
--magpie-line: #2c3038;
--magpie-accent: #8f8ff0;
--magpie-green: #5ac08a;
--magpie-red: #e0716a;
color-scheme: dark;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
background: var(--magpie-bg);
color: var(--magpie-fg);
font: 13px/1.5
var(
--magpie-font,
-apple-system,
BlinkMacSystemFont,
"Segoe UI",
system-ui,
sans-serif
);
-webkit-font-smoothing: antialiased;
}
main {
display: flex;
flex-direction: column;
gap: 12px;
max-width: 960px;
margin: 0 auto;
padding: 20px 24px;
}
h1,
h2,
p {
margin: 0;
}
h1 {
font-size: 20px;
font-weight: 600;
letter-spacing: -0.01em;
}
h2 {
font-size: 13px;
font-weight: 600;
}
.muted {
color: var(--magpie-muted);
font-size: 12px;
}
header,
.tools,
.range,
.chart-head,
.axis,
footer {
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
gap: 8px 12px;
}
.range {
justify-content: flex-start;
}
.card {
padding: 14px 16px;
border: 1px solid var(--magpie-line);
border-radius: 10px;
background: var(--magpie-card);
}
button,
select,
input {
min-height: 30px;
padding: 3px 10px;
border: 1px solid var(--magpie-line);
border-radius: 8px;
background: var(--magpie-card);
color: inherit;
font: inherit;
}
button,
select {
cursor: pointer;
}
button:hover:not(:disabled) {
border-color: var(--magpie-muted);
}
button:disabled {
cursor: default;
}
:focus-visible {
outline: 2px solid var(--magpie-accent);
outline-offset: 1px;
}
svg {
width: 16px;
height: 16px;
vertical-align: -3px;
}
/* Magpie's own period control: one pill, the chosen period raised in it. */
.segmented {
display: flex;
flex-wrap: wrap;
gap: 2px;
padding: 3px;
border-radius: 10px;
background: var(--magpie-line);
}
.segmented button {
min-height: 26px;
padding: 2px 12px;
border: 0;
border-radius: 7px;
background: none;
color: var(--magpie-muted);
}
.segmented button:hover {
color: var(--magpie-fg);
}
.segmented button[aria-pressed="true"] {
background: var(--magpie-card);
color: var(--magpie-fg);
font-weight: 600;
}
button.icon {
padding: 3px 6px;
border-color: transparent;
background: none;
color: var(--magpie-muted);
}
button.icon.spin svg {
animation: spin 1s linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: reduce) {
button.icon.spin svg {
animation: none;
}
}
.errors {
border-color: var(--magpie-red);
color: var(--magpie-red);
overflow-wrap: anywhere;
}
#board {
display: flex;
flex-direction: column;
gap: 12px;
}
.tiles {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
gap: 12px;
margin: 0;
}
.tiles div {
padding: 12px 16px;
border: 1px solid var(--magpie-line);
border-radius: 10px;
background: var(--magpie-card);
}
dt {
color: var(--magpie-muted);
font-size: 12px;
}
dd {
margin: 2px 0 0;
font-size: 20px;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
.chart-head {
margin-bottom: 12px;
}
#readout {
font-variant-numeric: tabular-nums;
}
.bars {
display: flex;
align-items: stretch;
gap: 2px;
height: 120px;
border-bottom: 1px solid var(--magpie-line);
}
/* The whole column is the hover target, not only the painted part. */
.bar {
flex: 1 1 0;
display: flex;
align-items: flex-end;
min-width: 0;
}
.bar i {
width: 100%;
min-height: 0;
border-radius: 3px 3px 0 0;
background: var(--magpie-accent);
}
.bar:hover i,
.bar:focus-visible i {
filter: brightness(1.25);
}
.bar:focus-visible {
outline-offset: -1px;
}
.bars.dense {
gap: 0;
}
.bars.dense .bar i {
border-radius: 0;
}
.axis {
margin-top: 6px;
}
.scroll {
overflow-x: auto;
}
table {
width: 100%;
margin-top: 8px;
border-collapse: collapse;
font-variant-numeric: tabular-nums;
}
th,
td {
padding: 7px 0 7px 16px;
text-align: right;
white-space: nowrap;
font-weight: 400;
}
th:first-child {
padding-left: 0;
text-align: left;
}
thead th {
color: var(--magpie-muted);
font-size: 12px;
}
tbody tr {
border-top: 1px solid var(--magpie-line);
}
details {
color: var(--magpie-muted);
}
summary {
cursor: pointer;
}
details p,
details ul {
max-width: 72ch;
margin: 8px 0 0;
}
ul {
padding-left: 18px;
}
footer {
overflow-wrap: anywhere;
}
[hidden] {
display: none !important;
}
@media (max-width: 520px) {
main {
padding: 16px;
}
}