Public: anyone can read this; the crew can change it.
The subscriptions page's Server-Timing names its phases: session, each read, render #3466
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/3427-the-subscriptions-page-s-server-timing-n → main 1 commit fork 1 thread · 1 unresolved
2 files
changed +67
−9
src/web/pages/subscriptions.ts +22 −9
| @@ -34,6 +34,7 @@ import { | ||
| 34 | 34 | } from "../../core/standing.ts"; |
| 35 | 35 | import { getUserById } from "../../core/users.ts"; |
| 36 | 36 | import type { Ctx } from "../../http/context.ts"; |
| 37 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; | |
| 37 | 38 | import { notFoundPage, settingsLayout, signInFirst } from "../chrome.ts"; |
| 38 | 39 | import { field, readForm, type Form } from "../form.ts"; |
| 39 | 40 | import { html, when, type Html } from "../html.ts"; |
| @@ -127,24 +128,34 @@ function shell(ctx: Ctx, body: Html): Response { | ||
| 127 | 128 | |
| 128 | 129 | // GET /settings/subscriptions |
| 129 | 130 | export function subscriptionsPage(ctx: Ctx): Response { |
| 131 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 130 | 132 | const denied = requireAdmin(ctx); |
| 131 | 133 | if (denied) return denied; |
| 132 | 134 | const now = new Date(); |
| 133 | const pool = listCredentials(ctx.app.db); | |
| 134 | const places = listGrantPlaces(ctx.app.db); | |
| 135 | const scopes = scopesOf(ctx.app.db); | |
| 135 | const pool = timePhase(ctx.request, "credentials", () => | |
| 136 | listCredentials(ctx.app.db), | |
| 137 | ); | |
| 138 | const places = timePhase(ctx.request, "places", () => | |
| 139 | listGrantPlaces(ctx.app.db), | |
| 140 | ); | |
| 141 | const scopes = timePhase(ctx.request, "scopes", () => scopesOf(ctx.app.db)); | |
| 136 | 142 | const scopeText = (s: Subscription): string => |
| 137 | 143 | s.id === PLATFORM |
| 138 | 144 | ? "the crew's: every app no subscription is granted to" |
| 139 | 145 | : scopes.has(s.id) |
| 140 | 146 | ? `for ${scopes.get(s.id)!.join(", ")}` |
| 141 | 147 | : "not offered to the crew — granted nowhere"; |
| 142 | const budgets = budgetsOf(ctx.app.db); | |
| 143 | const standingOf = standings( | |
| 144 | ctx.app.db, | |
| 145 | pool.map((s) => s.id), | |
| 146 | now, | |
| 148 | const budgets = timePhase(ctx.request, "budgets", () => | |
| 149 | budgetsOf(ctx.app.db), | |
| 147 | 150 | ); |
| 151 | const standingOf = timePhase(ctx.request, "standings", () => | |
| 152 | standings( | |
| 153 | ctx.app.db, | |
| 154 | pool.map((s) => s.id), | |
| 155 | now, | |
| 156 | ), | |
| 157 | ); | |
| 158 | const rendering = performance.now(); | |
| 148 | 159 | |
| 149 | 160 | const sealed = pool.filter((s) => s.id !== PLATFORM); |
| 150 | 161 | const none = |
| @@ -368,7 +379,7 @@ export function subscriptionsPage(ctx: Ctx): Response { | ||
| 368 | 379 | }, |
| 369 | 380 | ); |
| 370 | 381 | |
| 371 | return shell( | |
| 382 | const response = shell( | |
| 372 | 383 | ctx, |
| 373 | 384 | html`${list} ${none === null ? null : html`<div class="mt">${none}</div>`} |
| 374 | 385 | ${when( |
| @@ -387,6 +398,8 @@ export function subscriptionsPage(ctx: Ctx): Response { | ||
| 387 | 398 | <div class="mt">${add}</div> |
| 388 | 399 | ${used === null ? null : html`<div class="mt">${used}</div>`}`, |
| 389 | 400 | ); |
| 401 | notePhase(ctx.request, "render", performance.now() - rendering); | |
| 402 | return response; | |
| 390 | 403 | } |
| 391 | 404 | |
| 392 | 405 | // POST /settings/subscriptions |
test/subscriptions-timing.test.ts added +45
| @@ -0,0 +1,45 @@ | ||
| 1 | /** The subscriptions page names its phases in Server-Timing (#3427). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 8 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 9 | ||
| 10 | let dataDir = ""; | |
| 11 | let app: App, servers: Servers, bearer = ""; | |
| 12 | ||
| 13 | beforeAll(async () => { | |
| 14 | dataDir = await mkdtemp(join(tmpdir(), "forge-subtime-")); | |
| 15 | app = createApp({ dataDir, port: 0 }); | |
| 16 | servers = await serve(app); | |
| 17 | const admin = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "correct-horse-battery" }); | |
| 18 | if (!admin.ok) throw new Error(admin.error); | |
| 19 | const minted = await fetch(`${servers.url}/api/v1/tokens`, { | |
| 20 | method: "POST", | |
| 21 | headers: { "content-type": "application/json" }, | |
| 22 | body: JSON.stringify({ adminConfirm: true, username: "alice", password: "correct-horse-battery", name: "cli", scope: "admin" }), | |
| 23 | }); | |
| 24 | bearer = ((await minted.json()) as { token: string }).token; | |
| 25 | }); | |
| 26 | ||
| 27 | afterAll(async () => { | |
| 28 | servers.stop(); | |
| 29 | app.close(); | |
| 30 | await rm(dataDir, { recursive: true, force: true }); | |
| 31 | }); | |
| 32 | ||
| 33 | test("the subscriptions page names session, each read and render once each", async () => { | |
| 34 | const res = await fetch(`${servers.url}/settings/subscriptions`, { headers: { authorization: `Bearer ${bearer}` } }); | |
| 35 | const page = await res.text(); | |
| 36 | expect(res.status).toBe(200); | |
| 37 | expect(page).toContain("Subscriptions"); | |
| 38 | const parts = (res.headers.get("server-timing") ?? "").split(", "); | |
| 39 | const names = parts.map((p) => p.split(";")[0]!); | |
| 40 | for (const n of ["session", "credentials", "places", "scopes", "budgets", "standings", "render"]) { | |
| 41 | expect(names.filter((x) => x === n).length).toBe(1); | |
| 42 | const part = parts.find((p) => p.startsWith(`${n};`))!; | |
| 43 | expect(Number(/dur=([\d.]+)/.exec(part)?.[1])).not.toBeNaN(); | |
| 44 | } | |
| 45 | }); |
new above
offline
renderstarts here and so also covers the per-row user lookups, which are reads. The scout said so in the pull. If you want those named too, wrap them in ausersphase later.