Public: anyone can read this; the crew can change it.

The admin limits page's Server-Timing names its phases: session, each read, render #3467

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/3443-the-admin-limits-page-s-server-timing-na → main 1 commit fork

  • Merged. scout/3443-the-admin-limits-page-s-server-timing-na was merged into main as b64d457.

Review

Sign in to review this change.
2 files changed +64 −5
src/web/pages/admin-limits.ts +10 −5
@@ -13,6 +13,7 @@ import { changeSignup, readSignup } from "../../actions/signup.ts";
1313 import { SIGNUP_MODES } from "../../settings/signup.ts";
1414 import { QUOTA_KEYS } from "../../settings/quotas.ts";
1515 import type { Ctx } from "../../http/context.ts";
16 import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts";
1617 import { notFoundPage, settingsLayout } from "../chrome.ts";
1718 import { field, readForm } from "../form.ts";
1819 import { html, when } from "../html.ts";
@@ -22,8 +23,13 @@ import { accountSections } from "./origins.ts";
2223
2324 // GET /admin/limits
2425 export function limitsPage(ctx: Ctx): Response {
25 const listed = listLimits(ctx.app, ctx.auth.user);
26 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
27 const listed = timePhase(ctx.request, "limits", () => listLimits(ctx.app, ctx.auth.user));
2628 if (!listed.ok) return notFoundPage(ctx);
29 const quotas = timePhase(ctx.request, "quotas", () => readQuotas(ctx.app, ctx.auth.user));
30 const signup = timePhase(ctx.request, "signup", () => readSignup(ctx.app, ctx.auth.user));
31 const invites = timePhase(ctx.request, "invites", () => listInviteLinks(ctx.app, ctx.auth.user));
32 const rendering = performance.now();
2733 const { limits, changes } = listed.value;
2834 const rows = limits.map((l) =>
2935 row({
@@ -75,7 +81,6 @@ export function limitsPage(ctx: Ctx): Response {
7581 meta: html`${c.actorName === "" ? "someone" : c.actorName} · ${time(c.at)}`,
7682 }),
7783 );
78 const quotas = readQuotas(ctx.app, ctx.auth.user);
7984 const quotaHistory = (quotas.ok ? quotas.value.changes : []).map((c) =>
8085 row({
8186 lead: icon("history"),
@@ -118,7 +123,6 @@ export function limitsPage(ctx: Ctx): Response {
118123 pad: false,
119124 },
120125 );
121 const signup = readSignup(ctx.app, ctx.auth.user);
122126 const signupHistory = (signup.ok ? signup.value.changes : []).map((c) =>
123127 row({
124128 lead: icon("history"),
@@ -152,7 +156,6 @@ export function limitsPage(ctx: Ctx): Response {
152156 pad: false,
153157 },
154158 );
155 const invites = listInviteLinks(ctx.app, ctx.auth.user);
156159 const inviteRows = (invites.ok ? invites.value : []).map((i) =>
157160 row({
158161 lead: icon("activity"),
@@ -226,7 +229,7 @@ export function limitsPage(ctx: Ctx): Response {
226229 pad: false,
227230 },
228231 )}`;
229 return page({
232 const response = page({
230233 ctx,
231234 title: "Limits",
232235 body: html`<div class="page-head"><h1>Limits</h1></div>
@@ -236,6 +239,8 @@ export function limitsPage(ctx: Ctx): Response {
236239 "Account settings",
237240 )}`,
238241 });
242 notePhase(ctx.request, "render", performance.now() - rendering);
243 return response;
239244 }
240245
241246 // POST /admin/limits
test/admin-limits-timing.test.ts added +54
@@ -0,0 +1,54 @@
1 /** `GET /admin/limits` names session, limits, quotas, signup, invites and render in Server-Timing (#3443). */
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-admin-limits-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 /admin/limits 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}/admin/limits`, { headers: { cookie } });
48 const body = await res.text();
49 expect(res.status).toBe(200);
50 expect(body).toContain("<h1>Limits</h1>");
51 const parts = (res.headers.get("server-timing") ?? "").split(", ");
52 for (const name of ["app", "session", "limits", "quotas", "signup", "invites", "render"])
53 expect(parts.find((p) => p.startsWith(`${name};`))).toMatch(/^[a-z]+;dur=\d+\.\d$/);
54 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs