Public: anyone can read this; the crew can change it.
The spend tab's Server-Timing names its phases: session, each read, render #3431
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/3280-the-spend-tab-s-server-timing-names-its → main 1 commit fork 1 thread · 1 unresolved
2 files
changed +62
−2
src/web/pages/spend-tab.ts +8 −2
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | import { readSpend } from "../../actions/spend.ts"; |
| 8 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; | |
| 8 | 9 | import type { Ctx } from "../../http/context.ts"; |
| 9 | 10 | import { notFoundPage, signInFirst } from "../chrome.ts"; |
| 10 | 11 | import { html } from "../html.ts"; |
| @@ -16,9 +17,14 @@ import { usageShell } from "./usage-tabs.ts"; | ||
| 16 | 17 | export function spendPage(ctx: Ctx): Response { |
| 17 | 18 | if (!ctx.auth.user) return signInFirst(ctx); |
| 18 | 19 | if (!ctx.auth.user.isAdmin) return notFoundPage(ctx); |
| 20 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 21 | const spend = timePhase(ctx.request, "spend", () => readSpend(ctx.app, new Date())); | |
| 22 | const rendering = performance.now(); | |
| 19 | 23 | const body = html`<div id="spend" data-live="run.queued,run.started,run.finished,usage.*">${card( |
| 20 | spendView(readSpend(ctx.app, new Date())), | |
| 24 | spendView(spend), | |
| 21 | 25 | { head: html`${icon("clock")}<h2>Spend</h2>` }, |
| 22 | 26 | )}</div>`; |
| 23 | return usageShell(ctx, "spend", body); | |
| 27 | const response = usageShell(ctx, "spend", body); | |
| 28 | notePhase(ctx.request, "render", performance.now() - rendering); | |
| 29 | return response; | |
| 24 | 30 | } |
test/spend-timing.test.ts added +54
| @@ -0,0 +1,54 @@ | ||
| 1 | /** `/settings/spend` names session, spend and render in Server-Timing (#3280). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createUser } from "../src/core/users.ts"; | |
| 8 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 9 | import { proveAll } from "./helpers/verified.ts"; | |
| 10 | import { quietLogs } from "./helpers/globals.ts"; | |
| 11 | ||
| 12 | quietLogs(); | |
| 13 | ||
| 14 | let root = ""; | |
| 15 | let app: App; | |
| 16 | let servers: Servers; | |
| 17 | ||
| 18 | beforeAll(async () => { | |
| 19 | root = mkdtempSync(join(tmpdir(), "forge-spend-timing-")); | |
| 20 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 21 | const u = await createUser(app.db, { | |
| 22 | origin: "seed", | |
| 23 | trusted: true, | |
| 24 | username: "alice", | |
| 25 | email: "[email protected]", | |
| 26 | password: "a-good-passphrase", | |
| 27 | adminIfFirst: true, | |
| 28 | }); | |
| 29 | if (!u.ok) throw new Error(u.error); | |
| 30 | proveAll(app); | |
| 31 | servers = await serve(app); | |
| 32 | }); | |
| 33 | afterAll(() => { | |
| 34 | servers.stop(); | |
| 35 | app.close(); | |
| 36 | rmSync(root, { recursive: true, force: true }); | |
| 37 | }); | |
| 38 | ||
| 39 | test("GET /settings/spend names its phases", async () => { | |
| 40 | const signin = await fetch(`${servers.url}/signin`, { | |
| 41 | method: "POST", | |
| 42 | headers: { origin: servers.url }, | |
| 43 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), | |
| 44 | redirect: "manual", | |
| 45 | }); | |
| 46 | const cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 47 | const res = await fetch(`${servers.url}/settings/spend`, { headers: { cookie } }); | |
| 48 | const body = await res.text(); | |
| 49 | expect(res.status).toBe(200); | |
| 50 | expect(body).toContain("<h2>Spend</h2>"); | |
| 51 | const parts = (res.headers.get("server-timing") ?? "").split(", "); | |
| 52 | for (const name of ["app", "session", "spend", "render"]) | |
| 53 | expect(parts.find((p) => p.startsWith(`${name};`))).toMatch(/^[a-z]+;dur=\d+\.\d$/); | |
| 54 | }); |
new above
offline
Fine. If readSpend later becomes async, switch to timePhaseAsync as boards-view does.