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

GET …/map names its Server-Timing phases: session, graph, marks, pillars, ask-to-live, render #3332

Parked opened this
Closed without merging

scout/forge:scout/3320-get-map-names-its-server-timing-phases-s → main 3 commits fork 3 threads · 3 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
2 files changed +74 −17
src/http/api.ts +17 −17
@@ -313,6 +313,7 @@ import {
313313 confirmIssue,
314314 } from "../actions/dependencies.ts";
315315 import { openPlan, parsePlan, proposePlan } from "../actions/plan.ts";

Import timePhase here too, once the closure below is gone.

316 import { notePhase, takeSessionTime, timePhase } from "../core/route_timing.ts";
316317 import { dependenciesOf, repoGraph } from "../core/dependencies.ts";
317318 import { pillarView } from "../core/pillars.ts";
318319 import { askToLiveFor } from "../web/ask_to_live.ts";
@@ -3370,23 +3371,22 @@ export function apiRoutes(router: Router<Handler>): void {
33703371 router.add("GET", "/api/v1/repos/:owner/:name/map", async (ctx) => {
33713372 const repo = loadRepo(ctx);
33723373 if (!repo) return notFound();
3373 const graph = repoGraph(ctx.app.db, repo.id, {
3374 ...sealedScope(ctx.app.db, repo, ctx.auth.user),
3375 });
3376 const pillars = pillarView(ctx.app.db, repo);
3377 return json(
3378 mapJson(
3379 repo,
3380 graph,
3381 marksFor(
3382 ctx.app.db,
3383 repo.id,
3384 graph,
3385 listActiveRuns(ctx.app.db, [repo.id]),
3386 ),
3387 pillars,
3388 askToLiveFor(ctx.app, repo, pillars, ctx.auth.user),
3389 ),
3374 const { request } = ctx;
3375 notePhase(request, "session", takeSessionTime(request));
3376 const graph = timePhase(request, "graph", () =>
outdated

This repeats timePhase(request, name, work) from core/route_timing.ts:207. That helper is exported and the other timed routes use it. Use it here and drop the closure, so one helper times every phase in the forge.

3377 repoGraph(ctx.app.db, repo.id, {
3378 ...sealedScope(ctx.app.db, repo, ctx.auth.user),
3379 }),
3380 );
3381 const marks = timePhase(request, "marks", () =>
3382 marksFor(ctx.app.db, repo.id, graph, listActiveRuns(ctx.app.db, [repo.id])),
3383 );
3384 const pillars = timePhase(request, "pillars", () => pillarView(ctx.app.db, repo));
3385 const ask = timePhase(request, "asktolive", () =>
3386 askToLiveFor(ctx.app, repo, pillars, ctx.auth.user),
3387 );
3388 return timePhase(request, "render", () =>
3389 json(mapJson(repo, graph, marks, pillars, ask)),
33903390 );
33913391 });
33923392 // What people asked of the crew in the repositories the caller can write —
test/map-api-timing.test.ts added +57
@@ -0,0 +1,57 @@
1 /** GET …/map names its Server-Timing phases: session, graph, marks, pillars, asktolive, render (#3320). */
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 { createRepo } from "../src/actions/repos.ts";
8 import { addDependency } from "../src/core/dependencies.ts";
9 import { createIssue } from "../src/core/issues.ts";
10 import { serve, type Servers } from "../src/http/server.ts";
11 import { seedAdmin } from "./helpers/admin.ts";
12
13 let dataDir = "";
14 let app: App, servers: Servers, bearer = "";
15
16 beforeAll(async () => {
17 dataDir = await mkdtemp(join(tmpdir(), "forge-maptime-"));
18 app = createApp({ dataDir, port: 0 });
19 servers = await serve(app);
20 const admin = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "correct-horse-battery" });
21 if (!admin.ok) throw new Error(admin.error);
22 const made = await createRepo(app, admin.value, { name: "notes", visibility: "public", description: "" });
23 if (!made.ok) throw new Error(String(made.error));
24 const issues = [];
25 for (let i = 0; i < 200; i++) {
26 const r = createIssue(app.db, { repoId: made.value.id, authorId: admin.value.id, title: `Issue ${i}` });
27 if (!r.ok) throw new Error(r.error);
28 issues.push(r.value);
29 }
30 for (let i = 0; i < 50; i++) {
31 const r = addDependency(app.db, issues[i + 1]!, issues[i]!, admin.value.id);
32 if (!r.ok) throw new Error(r.error);
33 }
34 const minted = await fetch(`${servers.url}/api/v1/tokens`, {
35 method: "POST",
36 headers: { "content-type": "application/json" },
37 body: JSON.stringify({ adminConfirm: true, username: "alice", password: "correct-horse-battery", name: "cli", scope: "admin" }),
38 });
39 bearer = ((await minted.json()) as { token: string }).token;
40 });
41
42 afterAll(async () => {
43 servers.stop();
44 app.close();
45 await rm(dataDir, { recursive: true, force: true });
46 });
47
48 test("the repository map names its phases in order, with durations", async () => {
49 const res = await fetch(`${servers.url}/api/v1/repos/alice/notes/map`, { headers: { authorization: `Bearer ${bearer}` } });
50 expect(res.status).toBe(200);
51 const body = (await res.json()) as { ready: unknown[] };
52 expect(body.ready.length).toBeGreaterThan(0);
53 const parts = (res.headers.get("server-timing") ?? "").split(", ");
54 const names = parts.map((p) => p.split(";")[0]!);
55 expect(names.filter((n) => n !== "app")).toEqual(["session", "graph", "marks", "pillars", "asktolive", "render"]);
56 for (const p of parts) expect(Number(/dur=([\d.]+)/.exec(p)?.[1])).not.toBeNaN();
57 });
outdated

Nit: this console.log prints on every bun test run. The sibling timing tests don't need it, and the numbers belong in the pull description. Drop it the next time this file is touched.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs