Public: anyone can read this; the crew can change it.
The board's returns page's Server-Timing names its phases: session, each read, render #3503
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/3462-the-board-s-returns-page-s-server-timing → main 1 commit fork
2 files
changed +77
−11
src/web/pages/returns.ts +20 −11
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | |
| 9 | 9 | import { readReturns, returnsRepos } from "../../actions/returns.ts"; |
| 10 | 10 | import type { Money } from "../../core/returns.ts"; |
| 11 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; | |
| 11 | 12 | import { getReposByIds } from "../../core/repos.ts"; |
| 12 | 13 | import type { Ctx } from "../../http/context.ts"; |
| 13 | 14 | import { notFoundPage, signInFirst } from "../chrome.ts"; |
| @@ -27,6 +28,7 @@ const line = (title: string, m: Money, note?: string): Html => | ||
| 27 | 28 | |
| 28 | 29 | /** GET /board/returns */ |
| 29 | 30 | export function returnsPage(ctx: Ctx): Response { |
| 31 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 30 | 32 | const user = ctx.auth.user; |
| 31 | 33 | if (!user) return signInFirst(ctx); |
| 32 | 34 | const q = ctx.url.searchParams; |
| @@ -36,7 +38,7 @@ export function returnsPage(ctx: Ctx): Response { | ||
| 36 | 38 | .split(",") |
| 37 | 39 | .map((s) => s.trim()) |
| 38 | 40 | .filter(Boolean); |
| 39 | const mine = returnsRepos(ctx.app, user); | |
| 41 | const mine = timePhase(ctx.request, "repos", () => returnsRepos(ctx.app, user)); | |
| 40 | 42 | const named = |
| 41 | 43 | apps.length === 1 |
| 42 | 44 | ? (mine.find( |
| @@ -47,19 +49,24 @@ export function returnsPage(ctx: Ctx): Response { | ||
| 47 | 49 | ) ?? null) |
| 48 | 50 | : undefined; |
| 49 | 51 | if (named === null) return notFoundPage(ctx); |
| 50 | const read = readReturns(ctx.app, user, { | |
| 51 | ...(days === undefined ? {} : { days }), | |
| 52 | ...(named === undefined | |
| 53 | ? {} | |
| 54 | : { repo: `${named.ownerName}/${named.name}` }), | |
| 55 | }); | |
| 52 | const read = timePhase(ctx.request, "returns", () => | |
| 53 | readReturns(ctx.app, user, { | |
| 54 | ...(days === undefined ? {} : { days }), | |
| 55 | ...(named === undefined | |
| 56 | ? {} | |
| 57 | : { repo: `${named.ownerName}/${named.name}` }), | |
| 58 | }), | |
| 59 | ); | |
| 56 | 60 | if (!read.ok) return notFoundPage(ctx); |
| 57 | 61 | const r = read.value; |
| 58 | 62 | const span = days ?? 7; |
| 59 | const repos = getReposByIds( | |
| 60 | ctx.app.db, | |
| 61 | r.costliest.map((c) => c.repoId), | |
| 63 | const repos = timePhase(ctx.request, "names", () => | |
| 64 | getReposByIds( | |
| 65 | ctx.app.db, | |
| 66 | r.costliest.map((c) => c.repoId), | |
| 67 | ), | |
| 62 | 68 | ); |
| 69 | const rendering = performance.now(); | |
| 63 | 70 | const nothing = r.landedNothing; |
| 64 | 71 | const body = html`<h1 id="returns-title"> |
| 65 | 72 | What the tokens bought, ${span} days |
| @@ -153,5 +160,7 @@ export function returnsPage(ctx: Ctx): Response { | ||
| 153 | 160 | { head: html`<h2>A run</h2>` }, |
| 154 | 161 | )} |
| 155 | 162 | </div>`; |
| 156 | return page({ ctx, title: "What the tokens bought", body }); | |
| 163 | const response = page({ ctx, title: "What the tokens bought", body }); | |
| 164 | notePhase(ctx.request, "render", performance.now() - rendering); | |
| 165 | return response; | |
| 157 | 166 | } |
test/returns-timing.test.ts added +57
| @@ -0,0 +1,57 @@ | ||
| 1 | /** `/board/returns` names session, repos, returns, names and render in Server-Timing (#3281). */ | |
| 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 { createRepo } from "../src/actions/repos.ts"; | |
| 7 | import { createApp, type App } from "../src/app.ts"; | |
| 8 | import { createUser } from "../src/core/users.ts"; | |
| 9 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 10 | import { proveAll } from "./helpers/verified.ts"; | |
| 11 | import { quietLogs } from "./helpers/globals.ts"; | |
| 12 | ||
| 13 | quietLogs(); | |
| 14 | ||
| 15 | let root = ""; | |
| 16 | let app: App; | |
| 17 | let servers: Servers; | |
| 18 | ||
| 19 | beforeAll(async () => { | |
| 20 | root = mkdtempSync(join(tmpdir(), "forge-returns-timing-")); | |
| 21 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 22 | const u = await createUser(app.db, { | |
| 23 | origin: "seed", | |
| 24 | trusted: true, | |
| 25 | username: "alice", | |
| 26 | email: "[email protected]", | |
| 27 | password: "a-good-passphrase", | |
| 28 | adminIfFirst: true, | |
| 29 | }); | |
| 30 | if (!u.ok) throw new Error(u.error); | |
| 31 | proveAll(app); | |
| 32 | const made = await createRepo(app, { id: u.value.id, username: "alice" } as never, { name: "forge" }); | |
| 33 | if (!made.ok) throw new Error(made.error); | |
| 34 | servers = await serve(app); | |
| 35 | }); | |
| 36 | afterAll(() => { | |
| 37 | servers.stop(); | |
| 38 | app.close(); | |
| 39 | rmSync(root, { recursive: true, force: true }); | |
| 40 | }); | |
| 41 | ||
| 42 | test("GET /board/returns names its phases", async () => { | |
| 43 | const signin = await fetch(`${servers.url}/signin`, { | |
| 44 | method: "POST", | |
| 45 | headers: { origin: servers.url }, | |
| 46 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), | |
| 47 | redirect: "manual", | |
| 48 | }); | |
| 49 | const cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 50 | const res = await fetch(`${servers.url}/board/returns`, { headers: { cookie } }); | |
| 51 | const body = await res.text(); | |
| 52 | expect(res.status).toBe(200); | |
| 53 | expect(body).toContain("What the tokens bought"); | |
| 54 | const parts = (res.headers.get("server-timing") ?? "").split(", "); | |
| 55 | for (const name of ["app", "session", "repos", "returns", "names", "render"]) | |
| 56 | expect(parts.find((p) => p.startsWith(`${name};`))).toMatch(/^[a-z]+;dur=\d+\.\d$/); | |
| 57 | }); |
new above
offline