Public: anyone can read this; the crew can change it.
An app's now fragment names its Server-Timing phases: session, each read, render #3524
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/3514-an-app-s-now-fragment-names-its-server-t → main 1 commit fork 1 thread · 1 unresolved
2 files
changed +99
−14
src/web/studio/now.ts +34 −14
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | import { mayViewId, sealedScope } from "../../core/sealed.ts"; |
| 11 | 11 | import { envPull } from "../../apps/data.ts"; |
| 12 | 12 | import { appsRuntime, envsFor } from "../../apps/runtime.ts"; |
| 13 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; | |
| 13 | 14 | import { listCrewRoles } from "../../core/agents.ts"; |
| 14 | 15 | import { getAppByRepo, type AppRecord } from "../../core/apps.ts"; |
| 15 | 16 | import { getIssueById, listIssues, type Issue } from "../../core/issues.ts"; |
| @@ -332,21 +333,40 @@ export async function nowCard( | ||
| 332 | 333 | |
| 333 | 334 | // GET /:owner/:name/app/now — the card alone, for the region that shows it. |
| 334 | 335 | export async function appNowFragment(ctx: Ctx): Promise<Response> { |
| 335 | const repo = loadRepo(ctx); | |
| 336 | const req = ctx.request; | |
| 337 | notePhase(req, "session", takeSessionTime(req)); | |
| 338 | const repo = timePhase(req, "repo", () => loadRepo(ctx)); | |
| 336 | 339 | if (!repo) return fragment(html``, { status: 404 }); |
| 337 | 340 | const db = ctx.app.db; |
| 338 | const record = getAppByRepo(db, repo.id); | |
| 341 | const record = timePhase(req, "record", () => getAppByRepo(db, repo.id)); | |
| 339 | 342 | if (!record) return fragment(html``, { status: 404 }); |
| 340 | const openPulls = listPulls(db, repo.id, { | |
| 341 | state: "open", | |
| 342 | limit: 10, | |
| 343 | ...sealedScope(db, repo, ctx.auth.user), | |
| 344 | }); | |
| 343 | const openPulls = timePhase(req, "pulls", () => | |
| 344 | listPulls(db, repo.id, { | |
| 345 | state: "open", | |
| 346 | limit: 10, | |
| 347 | ...sealedScope(db, repo, ctx.auth.user), | |
| 348 | }), | |
| 349 | ); | |
| 350 | const builderCount = timePhase( | |
| 351 | req, | |
| 352 | "crew", | |
| 353 | () => listCrewRoles(db, repo.id).filter((c) => c.role === "builder").length, | |
| 354 | ); | |
| 355 | const ownerFacts = timePhase(req, "owner", () => | |
| 356 | canSeeOwnerFacts(db, repo, ctx.auth.user), | |
| 357 | ); | |
| 358 | // The one fact Now borrows from the head card, decided where the head | |
| 359 | // card decides it, so the two never disagree about what is tailing. | |
| 360 | const deployTailing = timePhase( | |
| 361 | req, | |
| 362 | "panels", | |
| 363 | () => appPanels(ctx, repo, record, openPulls).deployTailing, | |
| 364 | ); | |
| 365 | const rendering = performance.now(); | |
| 345 | 366 | const { card } = await nowCard(ctx, repo, record, { |
| 346 | 367 | openPulls, |
| 347 | builderCount: listCrewRoles(db, repo.id).filter((c) => c.role === "builder") | |
| 348 | .length, | |
| 349 | ownerFacts: canSeeOwnerFacts(db, repo, ctx.auth.user), | |
| 368 | builderCount, | |
| 369 | ownerFacts, | |
| 350 | 370 | wordCtx: { |
| 351 | 371 | db, |
| 352 | 372 | dir: repoDir(ctx.app.reposRoot, repo.ownerName, repo.name), |
| @@ -355,9 +375,9 @@ export async function appNowFragment(ctx: Ctx): Promise<Response> { | ||
| 355 | 375 | history: servingHistory(), |
| 356 | 376 | viewer: ctx.auth.user, |
| 357 | 377 | }, |
| 358 | // The one fact Now borrows from the head card, decided where the head | |
| 359 | // card decides it, so the two never disagree about what is tailing. | |
| 360 | deployTailing: appPanels(ctx, repo, record, openPulls).deployTailing, | |
| 378 | deployTailing, | |
| 361 | 379 | }); |
| 362 | return fragment(nowRegion(repo, card), { request: ctx.request }); | |
| 380 | const response = fragment(nowRegion(repo, card), { request: req }); | |
| 381 | notePhase(req, "render", performance.now() - rendering); | |
| 382 | return response; | |
| 363 | 383 | } |
test/app-now-timing.test.ts added +65
| @@ -0,0 +1,65 @@ | ||
| 1 | /** `/:owner/:name/app/now` names session, each read and render in Server-Timing (#3514). */ | |
| 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 { enableApp } from "../src/actions/apps.ts"; | |
| 7 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 8 | import { createApp, type App } from "../src/app.ts"; | |
| 9 | import { createUser } from "../src/core/users.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | import { proveAll } from "./helpers/verified.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | ||
| 14 | quietLogs(); | |
| 15 | ||
| 16 | let root = ""; | |
| 17 | let app: App; | |
| 18 | let servers: Servers; | |
| 19 | ||
| 20 | beforeAll(async () => { | |
| 21 | root = mkdtempSync(join(tmpdir(), "forge-now-timing-")); | |
| 22 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 23 | const u = await createUser(app.db, { | |
| 24 | origin: "seed", | |
| 25 | trusted: true, | |
| 26 | username: "alice", | |
| 27 | email: "[email protected]", | |
| 28 | password: "a-good-passphrase", | |
| 29 | adminIfFirst: true, | |
| 30 | }); | |
| 31 | if (!u.ok) throw new Error(u.error); | |
| 32 | proveAll(app); | |
| 33 | const made = await createRepo(app, { id: u.value.id, username: "alice" } as never, { name: "vault" }); | |
| 34 | if (!made.ok) throw new Error(String(made.error)); | |
| 35 | const seeded = await commitToBranch(app, u.value, made.value, { | |
| 36 | message: "Start", | |
| 37 | files: [{ path: "README.md", content: "v\n" }], | |
| 38 | }); | |
| 39 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 40 | const enabled = await enableApp(app, u.value, made.value, { slug: "vault" }); | |
| 41 | if (!enabled.ok) throw new Error(String(enabled.error)); | |
| 42 | servers = await serve(app); | |
| 43 | }); | |
| 44 | afterAll(() => { | |
| 45 | servers.stop(); | |
| 46 | app.close(); | |
| 47 | rmSync(root, { recursive: true, force: true }); | |
| 48 | }); | |
| 49 | ||
| 50 | test("GET /:owner/:name/app/now names its phases", async () => { | |
| 51 | const signin = await fetch(`${servers.url}/signin`, { | |
| 52 | method: "POST", | |
| 53 | headers: { origin: servers.url }, | |
| 54 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), | |
| 55 | redirect: "manual", | |
| 56 | }); | |
| 57 | const cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 58 | const res = await fetch(`${servers.url}/alice/vault/app/now`, { headers: { cookie } }); | |
| 59 | const body = await res.text(); | |
| 60 | expect(res.status).toBe(200); | |
| 61 | expect(body).toContain('id="live-now"'); | |
| 62 | const parts = (res.headers.get("server-timing") ?? "").split(", "); | |
| 63 | for (const name of ["app", "session", "repo", "record", "pulls", "crew", "owner", "panels", "render"]) | |
| 64 | expect(parts.find((p) => p.startsWith(`${name};`))).toMatch(/^[a-z]+;dur=\d+\.\d/); | |
| 65 | }); |
new above
offline
Nit: render is measured around nowCard plus fragment(); the name is fine, but document in docs/CACHING.md what it covers.