Public: anyone can read this; the crew can change it.
Design: merge usage, spend, capacity and telemetry into one Usage page #2757
scout/forge:scout/2692-design-merge-usage-spend-capacity-and-te → main 2 commits fork
10 files
changed +104
−52
docs/design/audit.md +2 −1
| @@ -33,7 +33,7 @@ their own vocabulary instead of composing the named one. | ||
| 33 | 33 | |
| 34 | 34 | ## Finding 2 — the pages, and what each is for |
| 35 | 35 | |
| 36 | There are 59 top-level pages, one entry each: every static `GET` route in `src/http/server.ts` | |
| 36 | There are 60 top-level pages, one entry each: every static `GET` route in `src/http/server.ts` | |
| 37 | 37 | that returns a page (`test/design-audit.test.ts` holds this list to the router, |
| 38 | 38 | both ways). Each entry reads `route` — module · what it is for · what |
| 39 | 39 | duplicates it · what breaks `docs/DESIGN.md`. "Breaks" counts colour literals |
| @@ -91,6 +91,7 @@ Machine routes are not pages and have no entry: `/audit.csv`, | ||
| 91 | 91 | - `/settings/capacity` — `pages/capacity.ts` · how many runs and checks go at once · `/admin/limits`, `/settings/usage` · none seen. |
| 92 | 92 | - `/settings/usage` — `pages/usage.ts` · route patterns by use, unused first · `/settings/telemetry`, `/board/returns` · none seen. Not about cost, despite the name. |
| 93 | 93 | - `/settings/telemetry` — `pages/telemetry.ts` · which agents may read telemetry · `/settings/crew` (agent grants) · none seen. |
| 94 | - `/settings/spend` — `pages/spend-tab.ts` · what the crew's runs spent, by credential, app, agent, issue and run kind · `/settings/subscriptions`, `/board/returns` · none seen. First tab of Usage (#2692). | |
| 94 | 95 | - `/settings/resilience` — `pages/domains.ts` · failure domains and their last drill · `/resilience` · none seen. |
| 95 | 96 | - `/settings/backups` — `pages/backups.ts` (621 lines) · backups and restore · `/settings/resilience`, `/releases` · none seen. |
| 96 | 97 | - `/settings/flags` — `pages/flags.ts` · the forge's switches · none · none seen. |
src/http/server.ts +2
| @@ -20,6 +20,7 @@ import { telemetryPage, telemetrySet } from "../web/pages/telemetry.ts"; | ||
| 20 | 20 | import { contributePage } from "../web/pages/contribute.ts"; |
| 21 | 21 | import { flagsPage, flagsSet } from "../web/pages/flags.ts"; |
| 22 | 22 | import { usagePage } from "../web/pages/usage.ts"; |
| 23 | import { spendPage } from "../web/pages/spend-tab.ts"; | |
| 23 | 24 | import { domainsPage } from "../web/pages/domains.ts"; |
| 24 | 25 | import { resiliencePage } from "../web/pages/resilience.ts"; |
| 25 | 26 | import { limitsPage, limitsSet, quotasSet, signupSet, inviteMint, inviteRevoke } from "../web/pages/admin-limits.ts"; |
| @@ -817,6 +818,7 @@ export function buildRouter(): Router<Handler> { | ||
| 817 | 818 | router.add("GET", "/settings/flags", flagsPage); |
| 818 | 819 | router.add("POST", "/settings/flags", flagsSet); |
| 819 | 820 | router.add("GET", "/settings/usage", (ctx) => usagePage(ctx, knownPatterns)); |
| 821 | router.add("GET", "/settings/spend", spendPage); | |
| 820 | 822 | router.add("GET", "/admin/limits", limitsPage); |
| 821 | 823 | router.add("POST", "/admin/limits", limitsSet); |
| 822 | 824 | router.add("POST", "/admin/quotas", quotasSet); |
src/web/pages/capacity.ts +2 −11
| @@ -14,7 +14,7 @@ import { field, readForm } from "../form.ts"; | ||
| 14 | 14 | import { html, when } from "../html.ts"; |
| 15 | 15 | import { flashTo, page } from "../layout.ts"; |
| 16 | 16 | import { btn, card, icon, row, time } from "../ui.ts"; |
| 17 | import { accountSections } from "./origins.ts"; | |
| 17 | import { usageShell } from "./usage-tabs.ts"; | |
| 18 | 18 | |
| 19 | 19 | function requireAdmin(ctx: Ctx): Response | undefined { |
| 20 | 20 | if (!ctx.auth.user) return signInFirst(ctx); |
| @@ -169,16 +169,7 @@ export function capacityPage(ctx: Ctx): Response { | ||
| 169 | 169 | pad: false, |
| 170 | 170 | }, |
| 171 | 171 | )}`; |
| 172 | return page({ | |
| 173 | ctx, | |
| 174 | title: "Capacity", | |
| 175 | body: html`<div class="page-head"><h1>Capacity</h1></div> | |
| 176 | ${settingsLayout( | |
| 177 | accountSections("capacity", { admin: true }), | |
| 178 | body, | |
| 179 | "Account settings", | |
| 180 | )}`, | |
| 181 | }); | |
| 172 | return usageShell(ctx, "capacity", body); | |
| 182 | 173 | } |
| 183 | 174 | |
| 184 | 175 | const ERRORS: Record<string, string> = { |
src/web/pages/origins.ts +5 −14
| @@ -77,6 +77,7 @@ export type AccountSection = | ||
| 77 | 77 | | "passkeys" |
| 78 | 78 | | "origins" |
| 79 | 79 | | "usage" |
| 80 | | "spend" | |
| 80 | 81 | | "crew" |
| 81 | 82 | | "accounts" |
| 82 | 83 | | "people" |
| @@ -97,6 +98,8 @@ export type AccountSection = | ||
| 97 | 98 | * admin's section — an agent is an account that acts — so it is on the nav |
| 98 | 99 | * only for one, the way the repo header hides Settings from a non-admin. |
| 99 | 100 | */ |
| 101 | const USAGE: ReadonlySet<AccountSection> = new Set(["usage", "spend", "capacity", "telemetry"]); | |
| 102 | ||
| 100 | 103 | export function accountSections( |
| 101 | 104 | current: AccountSection, |
| 102 | 105 | o: { admin?: boolean } = {}, |
| @@ -140,18 +143,6 @@ export function accountSections( | ||
| 140 | 143 | icon: "key" as const, |
| 141 | 144 | current: current === "cloud", |
| 142 | 145 | }, |
| 143 | { | |
| 144 | label: "Capacity", | |
| 145 | href: "/settings/capacity", | |
| 146 | icon: "activity" as const, | |
| 147 | current: current === "capacity", | |
| 148 | }, | |
| 149 | { | |
| 150 | label: "Telemetry", | |
| 151 | href: "/settings/telemetry", | |
| 152 | icon: "activity" as const, | |
| 153 | current: current === "telemetry", | |
| 154 | }, | |
| 155 | 146 | { |
| 156 | 147 | label: "Access", |
| 157 | 148 | href: "/settings/access", |
| @@ -178,9 +169,9 @@ export function accountSections( | ||
| 178 | 169 | }, |
| 179 | 170 | { |
| 180 | 171 | label: "Usage", |
| 181 | href: "/settings/usage", | |
| 172 | href: "/settings/spend", | |
| 182 | 173 | icon: "activity" as const, |
| 183 | current: current === "usage", | |
| 174 | current: USAGE.has(current), | |
| 184 | 175 | }, |
| 185 | 176 | { |
| 186 | 177 | label: "Backups", |
src/web/pages/spend-tab.ts added +24
| @@ -0,0 +1,24 @@ | ||
| 1 | /** | |
| 2 | * Usage → Spend, for a forge admin: what the crew's runs spent, by | |
| 3 | * credential, app, agent, issue and run kind (`pages/spend.ts`). It kept live | |
| 4 | * on the Subscriptions page before; the numbers are the same. | |
| 5 | */ | |
| 6 | ||
| 7 | import { readSpend } from "../../actions/spend.ts"; | |
| 8 | import type { Ctx } from "../../http/context.ts"; | |
| 9 | import { notFoundPage, signInFirst } from "../chrome.ts"; | |
| 10 | import { html } from "../html.ts"; | |
| 11 | import { card, icon } from "../ui.ts"; | |
| 12 | import { spendView } from "./spend.ts"; | |
| 13 | import { usageShell } from "./usage-tabs.ts"; | |
| 14 | ||
| 15 | // GET /settings/spend | |
| 16 | export function spendPage(ctx: Ctx): Response { | |
| 17 | if (!ctx.auth.user) return signInFirst(ctx); | |
| 18 | if (!ctx.auth.user.isAdmin) return notFoundPage(ctx); | |
| 19 | const body = html`<div id="spend" data-live="run.*,usage.*">${card( | |
| 20 | spendView(readSpend(ctx.app, new Date())), | |
| 21 | { head: html`${icon("clock")}<h2>Spend</h2>` }, | |
| 22 | )}</div>`; | |
| 23 | return usageShell(ctx, "spend", body); | |
| 24 | } |
src/web/pages/subscriptions.ts +1 −12
| @@ -385,18 +385,7 @@ export function subscriptionsPage(ctx: Ctx): Response { | ||
| 385 | 385 | Claude's tokenizer. |
| 386 | 386 | </p>`, |
| 387 | 387 | )} |
| 388 | <div class="mt" id="spend" data-live="run.*,usage.*"> | |
| 389 | ${card( | |
| 390 | html`<details id="spend-detail" open> | |
| 391 | <summary>By credential, app, agent, issue and run kind</summary> | |
| 392 | ${spendView(readSpend(ctx.app, now))} | |
| 393 | </details>`, | |
| 394 | { | |
| 395 | head: html`${icon("clock")} | |
| 396 | <h2>Spend</h2>`, | |
| 397 | }, | |
| 398 | )} | |
| 399 | </div> | |
| 388 | <p class="mt muted small">What these accounts spent, by credential, app, agent, issue and run kind: <a href="/settings/spend">Usage → Spend</a>.</p> | |
| 400 | 389 | <div class="mt">${add}</div> |
| 401 | 390 | ${used === null ? null : html`<div class="mt">${used}</div>`}`, |
| 402 | 391 | ); |
src/web/pages/telemetry.ts +2 −7
| @@ -10,7 +10,7 @@ import { field, readForm } from "../form.ts"; | ||
| 10 | 10 | import { html } from "../html.ts"; |
| 11 | 11 | import { flashTo, page } from "../layout.ts"; |
| 12 | 12 | import { btn, card, icon, row, time } from "../ui.ts"; |
| 13 | import { accountSections } from "./origins.ts"; | |
| 13 | import { usageShell } from "./usage-tabs.ts"; | |
| 14 | 14 | |
| 15 | 15 | function owner(ctx: Ctx): Response | undefined { |
| 16 | 16 | if (!ctx.auth.user) return signInFirst(ctx); |
| @@ -60,12 +60,7 @@ export function telemetryPage(ctx: Ctx): Response { | ||
| 60 | 60 | )}</ul>`, |
| 61 | 61 | { head: html`${icon("history")}<h2>Reads</h2>`, pad: false }, |
| 62 | 62 | )}`; |
| 63 | return page({ | |
| 64 | ctx, | |
| 65 | title: "Telemetry", | |
| 66 | body: html`<div class="page-head"><h1>Telemetry</h1></div> | |
| 67 | ${settingsLayout(accountSections("telemetry", { admin: true }), body, "Account settings")}`, | |
| 68 | }); | |
| 63 | return usageShell(ctx, "telemetry", body); | |
| 69 | 64 | } |
| 70 | 65 | |
| 71 | 66 | // POST /settings/telemetry |
src/web/pages/usage-tabs.ts added +45
| @@ -0,0 +1,45 @@ | ||
| 1 | /** | |
| 2 | * The one Usage page of Settings: what is used (spend), what is asked | |
| 3 | * (routes), what is full (capacity) and who may read the telemetry. Each tab | |
| 4 | * is its own URL, so a link and a back button keep working; this renders the | |
| 5 | * frame they share. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { Html } from "../html.ts"; | |
| 9 | import { settingsLayout } from "../chrome.ts"; | |
| 10 | import { html } from "../html.ts"; | |
| 11 | import { liveRegionsJs } from "../client.ts"; | |
| 12 | import { page } from "../layout.ts"; | |
| 13 | import { tabs } from "../ui.ts"; | |
| 14 | import type { Ctx } from "../../http/context.ts"; | |
| 15 | import { accountSections } from "./origins.ts"; | |
| 16 | ||
| 17 | export type UsageTab = "spend" | "routes" | "capacity" | "telemetry"; | |
| 18 | ||
| 19 | const TABS: readonly { id: UsageTab; label: string; href: string }[] = [ | |
| 20 | { id: "spend", label: "Spend", href: "/settings/spend" }, | |
| 21 | { id: "routes", label: "Routes", href: "/settings/usage" }, | |
| 22 | { id: "capacity", label: "Capacity", href: "/settings/capacity" }, | |
| 23 | { id: "telemetry", label: "Telemetry", href: "/settings/telemetry" }, | |
| 24 | ]; | |
| 25 | ||
| 26 | /** The Usage page around `body`, with `current` marked in the tabs. */ | |
| 27 | export function usageShell(ctx: Ctx, current: UsageTab, body: Html, status?: number): Response { | |
| 28 | return page( | |
| 29 | { | |
| 30 | ctx, | |
| 31 | title: "Usage", | |
| 32 | scriptJs: liveRegionsJs, | |
| 33 | body: html`<div class="page-head"><h1>Usage</h1></div> | |
| 34 | ${settingsLayout( | |
| 35 | accountSections("usage", { admin: true }), | |
| 36 | html`${tabs( | |
| 37 | TABS.map((t) => ({ label: t.label, href: t.href, current: t.id === current })), | |
| 38 | "Usage", | |
| 39 | )}${body}`, | |
| 40 | "Account settings", | |
| 41 | )}`, | |
| 42 | }, | |
| 43 | status, | |
| 44 | ); | |
| 45 | } |
src/web/pages/usage.ts +2 −7
| @@ -13,7 +13,7 @@ import { settingsLayout, signInFirst } from "../chrome.ts"; | ||
| 13 | 13 | import { html } from "../html.ts"; |
| 14 | 14 | import { page } from "../layout.ts"; |
| 15 | 15 | import { btn, card, empty, icon } from "../ui.ts"; |
| 16 | import { accountSections } from "./origins.ts"; | |
| 16 | import { usageShell } from "./usage-tabs.ts"; | |
| 17 | 17 | |
| 18 | 18 | const part = (counts: Readonly<Record<string, number>>, keys: readonly string[]) => |
| 19 | 19 | keys.map((k) => `${k} ${counts[k] ?? 0}`).join(" · "); |
| @@ -55,10 +55,5 @@ export function usagePage(ctx: Ctx, patterns: () => readonly string[]): Response | ||
| 55 | 55 | </table>`, |
| 56 | 56 | { head: html`${icon("activity")}<h2>Usage</h2>`, pad: false }, |
| 57 | 57 | ); |
| 58 | return page({ | |
| 59 | ctx, | |
| 60 | title: "Usage", | |
| 61 | body: html`<div class="page-head"><h1>Usage</h1></div> | |
| 62 | ${settingsLayout(accountSections("usage", { admin: true }), body, "Account settings")}`, | |
| 63 | }); | |
| 58 | return usageShell(ctx, "routes", body); | |
| 64 | 59 | } |
test/usage-report.test.ts +19
| @@ -94,3 +94,22 @@ test("usage is a boards source for an admin and for nobody else", async () => { | ||
| 94 | 94 | expect((await q("bob")).status).toBe(403); |
| 95 | 95 | expect((await q("carol")).status).toBe(403); // an admin agent is refused too |
| 96 | 96 | }); |
| 97 | ||
| 98 | test("Usage is one page: the four tabs on every tab, Spend admin-only, one sidebar entry", async () => { | |
| 99 | const get = (path: string, who: string) => | |
| 100 | fetch(`${servers.url}${path}`, { headers: { authorization: `Bearer ${tok[who]}` } }); | |
| 101 | for (const [path, current] of [ | |
| 102 | ["/settings/spend", "Spend"], | |
| 103 | ["/settings/usage", "Routes"], | |
| 104 | ["/settings/capacity", "Capacity"], | |
| 105 | ["/settings/telemetry", "Telemetry"], | |
| 106 | ] as const) { | |
| 107 | const text = await (await get(path, "admin")).text(); | |
| 108 | for (const label of ["Spend", "Routes", "Capacity", "Telemetry"]) | |
| 109 | expect(text).toMatch(new RegExp(`href="/settings/[a-z]+"[^>]*>\\s*${label}\\s*</a\\s*>`)); | |
| 110 | expect(text).toMatch(new RegExp(`href="${path}"\\s+aria-current="page"[^>]*>\\s*${current}\\s*</a\\s*>`)); | |
| 111 | expect(text.match(/data-nav/g) ?? []).not.toHaveLength(0); | |
| 112 | expect(text).not.toMatch(/data-nav[^>]*>[^<]*(<svg[\s\S]*?<\/svg>)?\s*(Capacity|Telemetry)\s*</); | |
| 113 | } | |
| 114 | expect((await get("/settings/spend", "bob")).status).toBe(404); | |
| 115 | }); |
new above
offline