Public: anyone can read this; the crew can change it.
The API's usage read names its Server-Timing phases: session, usage, render #4916
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/4896-the-api-s-usage-read-names-its-server-ti → main 1 commit fork
2 files
changed +64
−2
src/http/api.ts +3 −2
| @@ -5841,6 +5841,7 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 5841 | 5841 | }); |
| 5842 | 5842 | |
| 5843 | 5843 | router.add("GET", "/api/v1/repos/:owner/:name/usage", (ctx) => { |
| 5844 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 5844 | 5845 | const repo = loadRepo(ctx); |
| 5845 | 5846 | if (!repo) return notFound(); |
| 5846 | 5847 | const days = usageDays(ctx.url.searchParams.get("days")); |
| @@ -5850,9 +5851,9 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 5850 | 5851 | 422, |
| 5851 | 5852 | `days is a whole number from 1 to ${USAGE_DAYS_MAX}`, |
| 5852 | 5853 | ); |
| 5853 | const report = readUsage(ctx.app, ctx.auth.user, repo, days); | |
| 5854 | const report = timePhase(ctx.request, "usage", () => readUsage(ctx.app, ctx.auth.user, repo, days)); | |
| 5854 | 5855 | if (!report.ok) return forbidden(); |
| 5855 | return json(report.value); | |
| 5856 | return timePhase(ctx.request, "render", () => json(report.value)); | |
| 5856 | 5857 | }); |
| 5857 | 5858 | |
| 5858 | 5859 | // The fleet graph's numbers (`actions/fleet-graph.ts`): spend and runs |
test/api-usage-timing.test.ts added +61
| @@ -0,0 +1,61 @@ | ||
| 1 | /** The API usage read names its Server-Timing phases (#4896). */ | |
| 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 { createSession } from "../src/core/sessions.ts"; | |
| 9 | import { createUser } from "../src/core/users.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | import { quietLogs } from "./helpers/globals.ts"; | |
| 12 | ||
| 13 | quietLogs(); | |
| 14 | ||
| 15 | let dataDir: string; | |
| 16 | let app: App; | |
| 17 | let servers: Servers; | |
| 18 | let cookie = ""; | |
| 19 | ||
| 20 | const cells = process.env["FORGE_CELLS"]; | |
| 21 | ||
| 22 | beforeAll(async () => { | |
| 23 | process.env["FORGE_CELLS"] = "off"; | |
| 24 | dataDir = await mkdtemp(join(tmpdir(), "forge-api-usage-timing-")); | |
| 25 | app = createApp({ dataDir, port: 0 }); | |
| 26 | servers = await serve(app); | |
| 27 | const made = await createUser(app.db, { | |
| 28 | origin: "seed", | |
| 29 | trusted: true, | |
| 30 | username: "alice", | |
| 31 | email: "[email protected]", | |
| 32 | password: "a-good-passphrase", | |
| 33 | adminIfFirst: true, | |
| 34 | }); | |
| 35 | if (!made.ok) throw new Error(String(made.error)); | |
| 36 | const alice = made.value; | |
| 37 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 38 | const repo = await createRepo(app, alice, { name: "r", visibility: "public" }); | |
| 39 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 40 | }); | |
| 41 | afterAll(async () => { | |
| 42 | await servers.stop(); | |
| 43 | app.close(); | |
| 44 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 45 | else process.env["FORGE_CELLS"] = cells; | |
| 46 | await rm(dataDir, { recursive: true, force: true }); | |
| 47 | }); | |
| 48 | ||
| 49 | async function phases(path: string): Promise<{ status: number; names: string[]; body: string }> { | |
| 50 | const res = await fetch(`${servers.url}${path}`, { headers: { cookie } }); | |
| 51 | const body = await res.text(); | |
| 52 | const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!); | |
| 53 | return { status: res.status, names: names.slice(names.indexOf("app") + 1), body }; | |
| 54 | } | |
| 55 | ||
| 56 | test("the usage read names session, usage, render in order", async () => { | |
| 57 | const got = await phases("/api/v1/repos/alice/r/usage"); | |
| 58 | expect(got.status).toBe(200); | |
| 59 | expect(got.names).toEqual(["session", "usage", "render"]); | |
| 60 | expect(typeof JSON.parse(got.body)).toBe("object"); | |
| 61 | }); |
new above
offline