Public: anyone can read this; the crew can change it.
`This week` is counted, not sampled: the app page's numbers come from queries #101
torque/forge:torque/91-this-week-is-counted-not-sampled-the-app → main 1 commit fork
6 files
changed +404
−20
src/core/events.ts +45
| @@ -76,6 +76,51 @@ export class EventBus { | ||
| 76 | 76 | } |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | /** Which event to look for: a repository's newest of one kind about one | |
| 80 | * issue or pull number, with any payload fields that must match. */ | |
| 81 | export interface EventAbout { | |
| 82 | repoId: string; | |
| 83 | kind: string; | |
| 84 | /** The issue or pull number the event's payload names (`payload.number`). */ | |
| 85 | number: number; | |
| 86 | /** Extra payload fields that must match, compared as text. */ | |
| 87 | payload?: Readonly<Record<string, string>>; | |
| 88 | } | |
| 89 | ||
| 90 | /** A payload field a query may name. Anything else would be a JSON path | |
| 91 | * built from a caller's string, so it is refused rather than escaped. */ | |
| 92 | const FIELD = /^[a-z][a-z0-9_]*$/i; | |
| 93 | ||
| 94 | /** | |
| 95 | * The newest event of one kind on a repository that names this number — | |
| 96 | * "which merge closed #12?", asked as a query. | |
| 97 | * | |
| 98 | * A page that wants one old event used to read the newest two hundred and | |
| 99 | * look through them, which is the right answer only while the forge is | |
| 100 | * quiet: on a busy repository the event it wants has already scrolled off | |
| 101 | * the page it read, and the fact simply disappears from the page with | |
| 102 | * nothing saying so. The number lives in the payload, so the filter is a | |
| 103 | * `json_extract`, and migration 25 indexes exactly that expression. | |
| 104 | */ | |
| 105 | export function lastEventAbout( | |
| 106 | db: Database, | |
| 107 | about: EventAbout, | |
| 108 | ): ForgeEvent | undefined { | |
| 109 | const params: (string | number)[] = [about.repoId, about.kind, about.number]; | |
| 110 | let where = `repo_id = ?1 AND kind = ?2 AND json_extract(payload, '$.number') = ?3`; | |
| 111 | for (const [field, value] of Object.entries(about.payload ?? {})) { | |
| 112 | if (!FIELD.test(field)) throw new TypeError(`bad payload field: ${field}`); | |
| 113 | params.push(value); | |
| 114 | where += ` AND json_extract(payload, '$.${field}') = ?${params.length}`; | |
| 115 | } | |
| 116 | const row = db | |
| 117 | .query<EventRow, (string | number)[]>( | |
| 118 | `SELECT * FROM event WHERE ${where} ORDER BY id DESC LIMIT 1`, | |
| 119 | ) | |
| 120 | .get(...params); | |
| 121 | return row === null ? undefined : toEvent(row); | |
| 122 | } | |
| 123 | ||
| 79 | 124 | export interface ListEventsOptions { |
| 80 | 125 | repoId?: string; |
| 81 | 126 | /** Return events with id greater than this (exclusive). */ |
src/core/pulls.ts +22
| @@ -288,6 +288,28 @@ export function mergedPullsForRepos( | ||
| 288 | 288 | ); |
| 289 | 289 | } |
| 290 | 290 | |
| 291 | /** | |
| 292 | * How many of this repository's pulls merged since a moment — one query, | |
| 293 | * the *Shipped* count on the studio's *This week* card. | |
| 294 | * | |
| 295 | * Counting a page of closed pulls in JavaScript gave the same answer only | |
| 296 | * while the page outran the week: past a hundred closures the count fell | |
| 297 | * behind, and the cost-per-shipped-change it divides fell the other way. | |
| 298 | */ | |
| 299 | export function mergedCountSince( | |
| 300 | db: Database, | |
| 301 | repoId: string, | |
| 302 | since: string, | |
| 303 | ): number { | |
| 304 | const row = db | |
| 305 | .query<{ n: number }, [string, string]>( | |
| 306 | `SELECT COUNT(*) AS n FROM issue JOIN pull ON pull.issue_id = issue.id | |
| 307 | WHERE issue.repo_id = ?1 AND issue.kind = 'pull' AND pull.merged_at >= ?2`, | |
| 308 | ) | |
| 309 | .get(repoId, since); | |
| 310 | return row?.n ?? 0; | |
| 311 | } | |
| 312 | ||
| 291 | 313 | /** The pull rows for a page of issues, keyed by issue id. */ |
| 292 | 314 | export function pullsForIssues( |
| 293 | 315 | db: Database, |
src/core/runs.ts +36
| @@ -499,6 +499,42 @@ export function automaticRunsSince( | ||
| 499 | 499 | return row?.n ?? 0; |
| 500 | 500 | } |
| 501 | 501 | |
| 502 | /** Runs of one kind in a window, counted and costed. */ | |
| 503 | export interface RunTotals { | |
| 504 | readonly runs: number; | |
| 505 | /** Their cost, summed in the database and rounded only where it is shown. */ | |
| 506 | readonly spentUsd: number; | |
| 507 | } | |
| 508 | ||
| 509 | /** | |
| 510 | * How many runs of one kind this repository has had since a moment, and | |
| 511 | * what they cost — one query, correct past any number of rows. | |
| 512 | * | |
| 513 | * The studio's *This week* card used to read a page of recent runs and add | |
| 514 | * up the ones inside the window, which is the same answer only while the | |
| 515 | * page is bigger than the week. Past it the card quietly under-reports the | |
| 516 | * money, which is the one number nobody should have to check. Summed the | |
| 517 | * way the ceiling sums it (`spentToday`, crew/autonomy.ts), so the two | |
| 518 | * never disagree. | |
| 519 | */ | |
| 520 | export function runTotalsSince( | |
| 521 | db: Database, | |
| 522 | repoId: string, | |
| 523 | kind: RunKind, | |
| 524 | since: string, | |
| 525 | ): RunTotals { | |
| 526 | const row = db | |
| 527 | .query< | |
| 528 | { runs: number; spent: number | null }, | |
| 529 | [string, RunKind, string] | |
| 530 | >( | |
| 531 | `SELECT COUNT(*) AS runs, SUM(cost_usd) AS spent FROM run | |
| 532 | WHERE repo_id = ?1 AND kind = ?2 AND created_at >= ?3`, | |
| 533 | ) | |
| 534 | .get(repoId, kind, since); | |
| 535 | return { runs: row?.runs ?? 0, spentUsd: row?.spent ?? 0 }; | |
| 536 | } | |
| 537 | ||
| 502 | 538 | /** What an agent has done, in totals: the number a person weighs a crew by. */ |
| 503 | 539 | export interface AgentTotals { |
| 504 | 540 | readonly runs: number; |
src/core/schema.ts +21
| @@ -752,4 +752,25 @@ export const migrations: readonly string[] = [ | ||
| 752 | 752 | UNIQUE (app_id, env, commit_oid, kind) |
| 753 | 753 | ); |
| 754 | 754 | `, |
| 755 | ||
| 756 | // 25 — the event that names an issue, found by name rather than by | |
| 757 | // reading the recent page and hoping. `lastEventAbout` (events.ts) asks | |
| 758 | // "the newest issue.closed on this repo naming #12", and without an index | |
| 759 | // over the payload's number that is a walk back through every event the | |
| 760 | // repository has appended since. Expression index because the number | |
| 761 | // lives in the JSON, not in a column: json_extract is deterministic, so | |
| 762 | // SQLite may index it. | |
| 763 | // | |
| 764 | // And the window a repository's spend is asked over. `runTotalsSince` | |
| 765 | // (runs.ts) and `spentToday` (crew/autonomy.ts) both ask "this repo, this | |
| 766 | // kind, since this moment"; `run_repo` answers only the first third of | |
| 767 | // that, so both walked every run the repository ever had. With the cost | |
| 768 | // in the index the week's money is a range scan that never visits a row. | |
| 769 | ` | |
| 770 | CREATE INDEX event_repo_kind_number | |
| 771 | ON event(repo_id, kind, json_extract(payload, '$.number'), id DESC); | |
| 772 | ||
| 773 | CREATE INDEX run_repo_kind_created | |
| 774 | ON run(repo_id, kind, created_at, cost_usd); | |
| 775 | `, | |
| 755 | 776 | ]; |
src/web/pages/studio.ts +45 −19
| @@ -35,8 +35,9 @@ import { | ||
| 35 | 35 | import { listCrewRoles, type Agent } from "../../core/agents.ts"; |
| 36 | 36 | import { appsForRepos, getAppByRepo, type AppRecord } from "../../core/apps.ts"; |
| 37 | 37 | import { dependenciesOf } from "../../core/dependencies.ts"; |
| 38 | import { listEvents } from "../../core/events.ts"; | |
| 38 | import { lastEventAbout } from "../../core/events.ts"; | |
| 39 | 39 | import { |
| 40 | countIssues, | |
| 40 | 41 | getIssueById, |
| 41 | 42 | listComments, |
| 42 | 43 | listIssues, |
| @@ -47,6 +48,7 @@ import { | ||
| 47 | 48 | getPull, |
| 48 | 49 | joinMany, |
| 49 | 50 | listPulls, |
| 51 | mergedCountSince, | |
| 50 | 52 | openPullsClosing, |
| 51 | 53 | pullForBranch, |
| 52 | 54 | type Pull, |
| @@ -62,6 +64,7 @@ import { | ||
| 62 | 64 | latestRunsForIssues, |
| 63 | 65 | listActiveRuns, |
| 64 | 66 | listRuns, |
| 67 | runTotalsSince, | |
| 65 | 68 | type CheckSummary, |
| 66 | 69 | type Run, |
| 67 | 70 | } from "../../core/runs.ts"; |
| @@ -818,6 +821,10 @@ function previewRow( | ||
| 818 | 821 | * — no per-pull query, so the list costs nothing extra as pulls pile up. A |
| 819 | 822 | * pull with no environment yet still gets a row, so the list matches the |
| 820 | 823 | * open pulls exactly. |
| 824 | * | |
| 825 | * The list is one page long; `openCount` is how many open changes there | |
| 826 | * actually are, and when it is more the card says so rather than letting | |
| 827 | * the rest disappear. A list that silently ends is a list that lies. | |
| 821 | 828 | */ |
| 822 | 829 | function previewsPanel( |
| 823 | 830 | base: string, |
| @@ -825,6 +832,7 @@ function previewsPanel( | ||
| 825 | 832 | record: AppRecord, |
| 826 | 833 | openPulls: readonly Pull[], |
| 827 | 834 | envs: readonly EnvStatus[], |
| 835 | openCount: number, | |
| 828 | 836 | ): Html | null { |
| 829 | 837 | if (openPulls.length === 0) return null; |
| 830 | 838 | const byPull = new Map( |
| @@ -832,6 +840,7 @@ function previewsPanel( | ||
| 832 | 840 | .filter((e) => e.env !== "production") |
| 833 | 841 | .map((e) => [envPull(e.env), e] as const), |
| 834 | 842 | ); |
| 843 | const more = openCount - openPulls.length; | |
| 835 | 844 | return listCard( |
| 836 | 845 | openPulls.map((p) => |
| 837 | 846 | previewRow(base, runtime, record, p, byPull.get(p.number)), |
| @@ -839,6 +848,14 @@ function previewsPanel( | ||
| 839 | 848 | { |
| 840 | 849 | head: html`${icon("rocket")} |
| 841 | 850 | <h2>Previews</h2>`, |
| 851 | ...(more > 0 | |
| 852 | ? { | |
| 853 | foot: html`<span class="muted small" | |
| 854 | >Showing ${openPulls.length} of ${openCount} open changes. | |
| 855 | <a href="${base}/pulls">See them all.</a></span | |
| 856 | >`, | |
| 857 | } | |
| 858 | : {}), | |
| 842 | 859 | }, |
| 843 | 860 | ); |
| 844 | 861 | } |
| @@ -859,7 +876,14 @@ function appPanels( | ||
| 859 | 876 | const base = repoBase(repo); |
| 860 | 877 | const runtime = appsRuntime(ctx.app); |
| 861 | 878 | const envs: EnvStatus[] = runtime === undefined ? [] : runtime.status(record); |
| 862 | const previews = previewsPanel(base, runtime, record, openPulls, envs); | |
| 879 | const previews = previewsPanel( | |
| 880 | base, | |
| 881 | runtime, | |
| 882 | record, | |
| 883 | openPulls, | |
| 884 | envs, | |
| 885 | countIssues(db, repo.id, "pull", "open"), | |
| 886 | ); | |
| 863 | 887 | const production = envs.find((e) => e.env === "production"); |
| 864 | 888 | const url = runtime?.urlFor(record, "production"); |
| 865 | 889 | const writer = canSeeOwnerFacts(db, repo, ctx.auth.user); |
| @@ -962,17 +986,15 @@ export async function studioApp( | ||
| 962 | 986 | const decisions = withLikely(ctx, decisionsFor(ctx, [repo])); |
| 963 | 987 | const openPulls = listPulls(db, repo.id, { state: "open", limit: 10 }); |
| 964 | 988 | const week = new Date(Date.now() - 7 * 24 * 60 * 60_000).toISOString(); |
| 965 | const shipped = listPulls(db, repo.id, { | |
| 966 | state: "closed", | |
| 967 | limit: 100, | |
| 968 | }).filter((p) => p.mergedAt !== null && p.mergedAt >= week).length; | |
| 989 | // *This week* is counted, not sampled: every number below is one aggregate | |
| 990 | // bounded by the window, so a busy week reads right instead of stopping at | |
| 991 | // whatever page a list happened to return. | |
| 992 | const shipped = mergedCountSince(db, repo.id, week); | |
| 969 | 993 | // The rest is spend, worked out only for a viewer who is going to see it. |
| 970 | 994 | const weekRuns = ownerFacts |
| 971 | ? listRuns(db, repo.id, { kind: "agent", limit: 200 }).filter( | |
| 972 | (r) => r.createdAt >= week, | |
| 973 | ) | |
| 974 | : []; | |
| 975 | const spent = weekRuns.reduce((s, r) => s + r.costUsd, 0); | |
| 995 | ? runTotalsSince(db, repo.id, "agent", week) | |
| 996 | : { runs: 0, spentUsd: 0 }; | |
| 997 | const spent = weekRuns.spentUsd; | |
| 976 | 998 | const ledgerWeek = ownerFacts ? decisionStats(db, repo.id, week) : []; |
| 977 | 999 | const autonomy = |
| 978 | 1000 | !ownerFacts || repo.autonomy === "off" |
| @@ -1168,7 +1190,7 @@ export async function studioApp( | ||
| 1168 | 1190 | <dt>Shipped</dt> |
| 1169 | 1191 | <dd>${shipped} change${shipped === 1 ? "" : "s"}</dd> |
| 1170 | 1192 | <dt>Runs</dt> |
| 1171 | <dd>${weekRuns.length}</dd> | |
| 1193 | <dd>${weekRuns.runs}</dd> | |
| 1172 | 1194 | <dt>Spent</dt> |
| 1173 | 1195 | <dd> |
| 1174 | 1196 | $${spent.toFixed(2)}${shipped > 0 |
| @@ -1659,15 +1681,19 @@ export async function studioPull( | ||
| 1659 | 1681 | /** The pull whose merge closed this issue, from the event it left behind — |
| 1660 | 1682 | * the same record `stateEvents` (thread.ts) reads for the timeline's own |
| 1661 | 1683 | * "closed this" line, so this never disagrees with what the page already |
| 1662 | * shows. Undefined for an issue closed by hand, or still open. */ | |
| 1684 | * shows. Undefined for an issue closed by hand, or still open. | |
| 1685 | * | |
| 1686 | * Asked for by name (`lastEventAbout`): reading the newest two hundred | |
| 1687 | * events and looking for this one lost the link as soon as the repository | |
| 1688 | * had two hundred newer events, which on a working app is a day. */ | |
| 1663 | 1689 | function shippedBy(db: Database, repo: Repo, issue: Issue): Pull | undefined { |
| 1664 | 1690 | if (issue.state !== "closed") return undefined; |
| 1665 | const closed = listEvents(db, { repoId: repo.id, limit: 200 }).find( | |
| 1666 | (e) => | |
| 1667 | e.kind === "issue.closed" && | |
| 1668 | Number(e.payload.number) === issue.number && | |
| 1669 | e.payload.by === "merge", | |
| 1670 | ); | |
| 1691 | const closed = lastEventAbout(db, { | |
| 1692 | repoId: repo.id, | |
| 1693 | kind: "issue.closed", | |
| 1694 | number: issue.number, | |
| 1695 | payload: { by: "merge" }, | |
| 1696 | }); | |
| 1671 | 1697 | if (closed === undefined) return undefined; |
| 1672 | 1698 | const number = Number(closed.payload.pull); |
| 1673 | 1699 | return Number.isInteger(number) ? getPull(db, repo.id, number) : undefined; |
test/studio.test.ts +235 −1
| @@ -27,7 +27,12 @@ import { | ||
| 27 | 27 | } from "../src/actions/repos.ts"; |
| 28 | 28 | import { setCrewRole } from "../src/core/agents.ts"; |
| 29 | 29 | import { recordDecision } from "../src/core/decisions.ts"; |
| 30 | import { getIssue, listIssues, type Issue } from "../src/core/issues.ts"; | |
| 30 | import { | |
| 31 | createIssue, | |
| 32 | getIssue, | |
| 33 | listIssues, | |
| 34 | type Issue, | |
| 35 | } from "../src/core/issues.ts"; | |
| 31 | 36 | import { recordLook } from "../src/core/looks.ts"; |
| 32 | 37 | import { getPull, type Pull } from "../src/core/pulls.ts"; |
| 33 | 38 | import { setDirectionIssue, type Repo } from "../src/core/repos.ts"; |
| @@ -2670,3 +2675,232 @@ describe("servedPhrase", () => { | ||
| 2670 | 2675 | ).toBeUndefined(); |
| 2671 | 2676 | }); |
| 2672 | 2677 | }); |
| 2678 | ||
| 2679 | /** | |
| 2680 | * *This week* is counted, not sampled. Every number on the card is an | |
| 2681 | * aggregate the database computes over the window, so the seeds here are | |
| 2682 | * deliberately past the page sizes the card used to read: 240 agent runs | |
| 2683 | * where it read 200, and 120 merges where it read 100. On today's code | |
| 2684 | * these numbers come back short, and the money with them. | |
| 2685 | */ | |
| 2686 | describe("this week is counted, not sampled", () => { | |
| 2687 | let dataDir: string; | |
| 2688 | let app: App; | |
| 2689 | let servers: Servers; | |
| 2690 | let base: string; | |
| 2691 | let alice: User; | |
| 2692 | let repo: Repo; | |
| 2693 | let cookie: string; | |
| 2694 | let tell: Issue; | |
| 2695 | ||
| 2696 | /** Past the 200-row page of runs the card used to filter. */ | |
| 2697 | const WEEK_RUNS = 240; | |
| 2698 | /** Past the 100-row page of closed pulls it used to filter. */ | |
| 2699 | const WEEK_MERGES = 120; | |
| 2700 | /** A quarter is exact in binary, so 240 of them is exactly $60.00. */ | |
| 2701 | const RUN_COST = 0.25; | |
| 2702 | /** One page of previews, plus one that used to vanish. */ | |
| 2703 | const OPEN_PULLS = 11; | |
| 2704 | ||
| 2705 | const get = (path: string) => | |
| 2706 | fetch(`${base}${path}`, { | |
| 2707 | headers: { cookie: `${cookie}; forge_mode=studio` }, | |
| 2708 | redirect: "manual", | |
| 2709 | }); | |
| 2710 | ||
| 2711 | /** A merged pull as a row, the way the bench seeds them: the count under | |
| 2712 | * test reads these rows, not the git history behind them. */ | |
| 2713 | const seedMergedPulls = (n: number, mergedAt: string) => { | |
| 2714 | for (let i = 0; i < n; i++) { | |
| 2715 | const issue = createIssue(app.db, { | |
| 2716 | repoId: repo.id, | |
| 2717 | authorId: alice.id, | |
| 2718 | title: `shipped ${mergedAt} ${i}`, | |
| 2719 | kind: "pull", | |
| 2720 | }); | |
| 2721 | if (!issue.ok) throw new Error(String(issue.error)); | |
| 2722 | app.db | |
| 2723 | .query( | |
| 2724 | `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, | |
| 2725 | head_oid, head_repo_id, merged_at, merged_oid) | |
| 2726 | VALUES (?1, 'main', ?2, 'base', 'head', ?3, ?4, 'merge')`, | |
| 2727 | ) | |
| 2728 | .run(issue.value.id, `seed/${issue.value.number}`, repo.id, mergedAt); | |
| 2729 | app.db | |
| 2730 | .query("UPDATE issue SET state = 'closed', closed_at = ?1 WHERE id = ?2") | |
| 2731 | .run(mergedAt, issue.value.id); | |
| 2732 | } | |
| 2733 | }; | |
| 2734 | ||
| 2735 | beforeAll(async () => { | |
| 2736 | dataDir = await mkdtemp(join(tmpdir(), "forge-studio-busy-")); | |
| 2737 | app = createApp({ dataDir, port: 0 }); | |
| 2738 | servers = await serve(app); | |
| 2739 | base = servers.url; | |
| 2740 | const a = await createUser(app.db, { | |
| 2741 | username: "alice", | |
| 2742 | email: "[email protected]", | |
| 2743 | password: "a-good-passphrase", | |
| 2744 | adminIfFirst: true, | |
| 2745 | }); | |
| 2746 | if (!a.ok) throw new Error(a.error); | |
| 2747 | alice = a.value; | |
| 2748 | cookie = ( | |
| 2749 | ( | |
| 2750 | await fetch(`${base}/signin`, { | |
| 2751 | method: "POST", | |
| 2752 | body: new URLSearchParams({ | |
| 2753 | username: "alice", | |
| 2754 | password: "a-good-passphrase", | |
| 2755 | }), | |
| 2756 | redirect: "manual", | |
| 2757 | }) | |
| 2758 | ).headers.get("set-cookie") ?? "" | |
| 2759 | ).split(";")[0]!; | |
| 2760 | const made = await createRepo(app, alice, { | |
| 2761 | name: "busy", | |
| 2762 | visibility: "private", | |
| 2763 | description: "an app with a week behind it", | |
| 2764 | }); | |
| 2765 | if (!made.ok) throw new Error(String(made.error)); | |
| 2766 | repo = made.value; | |
| 2767 | const seeded = await commitToBranch(app, alice, repo, { | |
| 2768 | message: "Start", | |
| 2769 | files: [{ path: "README.md", content: "busy\n" }], | |
| 2770 | }); | |
| 2771 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 2772 | const enabled = await enableApp(app, alice, repo, { slug: "busy" }); | |
| 2773 | if (!enabled.ok) throw new Error(String(enabled.error)); | |
| 2774 | const scout = await createAgent(app, alice, { username: "scout" }); | |
| 2775 | if (!scout.ok) throw new Error(String(scout.error)); | |
| 2776 | expect(addToCrew(app, alice, repo, "scout").ok).toBe(true); | |
| 2777 | ||
| 2778 | // One real tell, shipped by a real merge: the link this page must not | |
| 2779 | // lose. It is also the hundred-and-twentieth merge of the week. | |
| 2780 | const told = openIssue(app, alice, repo, { title: "Light the lamp" }); | |
| 2781 | if (!told.ok) throw new Error(String(told.error)); | |
| 2782 | tell = told.value; | |
| 2783 | const branch = await commitToBranch(app, alice, repo, { | |
| 2784 | branch: "scout/light-the-lamp", | |
| 2785 | base: "main", | |
| 2786 | message: "Light the lamp", | |
| 2787 | files: [{ path: "LAMP.md", content: "lit\n" }], | |
| 2788 | }); | |
| 2789 | if (!branch.ok) throw new Error(JSON.stringify(branch.error)); | |
| 2790 | const opened = await openPull(app, alice, repo, { | |
| 2791 | title: "Light the lamp", | |
| 2792 | body: `Closes #${tell.number}`, | |
| 2793 | baseBranch: "main", | |
| 2794 | headBranch: "scout/light-the-lamp", | |
| 2795 | }); | |
| 2796 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 2797 | const merged = await mergePull(app, alice, repo, opened.value, "squash"); | |
| 2798 | if (!merged.ok) throw new Error(String(merged.error)); | |
| 2799 | ||
| 2800 | // Bury that merge under a page and a half of newer events. | |
| 2801 | for (let i = 0; i < 250; i++) | |
| 2802 | app.bus.append({ | |
| 2803 | kind: "repo.push", | |
| 2804 | repoId: repo.id, | |
| 2805 | payload: { ref: "refs/heads/main", n: i }, | |
| 2806 | }); | |
| 2807 | ||
| 2808 | // The week's runs, and one from before it that must not be counted. | |
| 2809 | const scoutId = getUserByName(app.db, "scout")!.id; | |
| 2810 | for (let i = 0; i < WEEK_RUNS; i++) { | |
| 2811 | const run = createRun(app.db, { | |
| 2812 | repoId: repo.id, | |
| 2813 | kind: "agent", | |
| 2814 | actorId: scoutId, | |
| 2815 | reason: "assigned", | |
| 2816 | }); | |
| 2817 | claimRun(app.db, run.id); | |
| 2818 | finishRun(app.db, run.id, { | |
| 2819 | state: "ok", | |
| 2820 | summary: "done", | |
| 2821 | costUsd: RUN_COST, | |
| 2822 | turns: 1, | |
| 2823 | }); | |
| 2824 | } | |
| 2825 | const before = new Date(Date.now() - 9 * 24 * 60 * 60_000).toISOString(); | |
| 2826 | const old = createRun(app.db, { | |
| 2827 | repoId: repo.id, | |
| 2828 | kind: "agent", | |
| 2829 | actorId: scoutId, | |
| 2830 | reason: "assigned", | |
| 2831 | }); | |
| 2832 | claimRun(app.db, old.id); | |
| 2833 | finishRun(app.db, old.id, { | |
| 2834 | state: "ok", | |
| 2835 | summary: "last month's money", | |
| 2836 | costUsd: 100, | |
| 2837 | turns: 1, | |
| 2838 | }); | |
| 2839 | app.db | |
| 2840 | .query("UPDATE run SET created_at = ?1 WHERE id = ?2") | |
| 2841 | .run(before, old.id); | |
| 2842 | ||
| 2843 | seedMergedPulls(WEEK_MERGES - 1, new Date().toISOString()); | |
| 2844 | seedMergedPulls(5, before); | |
| 2845 | ||
| 2846 | // Open changes: one more than the previews list shows. | |
| 2847 | for (let i = 0; i < OPEN_PULLS; i++) { | |
| 2848 | const issue = createIssue(app.db, { | |
| 2849 | repoId: repo.id, | |
| 2850 | authorId: alice.id, | |
| 2851 | title: `open ${i}`, | |
| 2852 | kind: "pull", | |
| 2853 | }); | |
| 2854 | if (!issue.ok) throw new Error(String(issue.error)); | |
| 2855 | app.db | |
| 2856 | .query( | |
| 2857 | `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, | |
| 2858 | head_oid, head_repo_id) | |
| 2859 | VALUES (?1, 'main', ?2, 'base', 'head', ?3)`, | |
| 2860 | ) | |
| 2861 | .run(issue.value.id, `open/${issue.value.number}`, repo.id); | |
| 2862 | } | |
| 2863 | }); | |
| 2864 | ||
| 2865 | afterAll(async () => { | |
| 2866 | servers.stop(); | |
| 2867 | app.close(); | |
| 2868 | await rm(dataDir, { recursive: true, force: true }); | |
| 2869 | }); | |
| 2870 | ||
| 2871 | test("runs, spend, shipped and the cost per change are counted over the window, not sampled from a page", async () => { | |
| 2872 | const page = await (await get("/alice/busy")).text(); | |
| 2873 | expect(page).toMatch(/Runs<\/dt>\s*<dd>240<\/dd>/); | |
| 2874 | expect(page).toContain(`${WEEK_MERGES} changes`); | |
| 2875 | // 240 × $0.25 = $60.00, and $60.00 over 120 changes is $0.50 each. | |
| 2876 | expect(page).toContain("$60.00"); | |
| 2877 | expect(page).toContain("$0.50 per shipped change"); | |
| 2878 | // The run and the merges from before the window stay outside it. | |
| 2879 | expect(page).not.toContain("$160.00"); | |
| 2880 | expect(page).not.toContain("125 changes"); | |
| 2881 | }); | |
| 2882 | ||
| 2883 | test("the previews list says it is showing only the first ten of more", async () => { | |
| 2884 | const page = await (await get("/alice/busy")).text(); | |
| 2885 | expect(page).toContain(`Showing 10 of ${OPEN_PULLS} open changes.`); | |
| 2886 | const fragment = await (await get("/alice/busy/app/state")).text(); | |
| 2887 | expect(fragment).toContain(`Showing 10 of ${OPEN_PULLS} open changes.`); | |
| 2888 | }); | |
| 2889 | ||
| 2890 | test("a tell shipped by a merge keeps its change, buried under 250 newer events", async () => { | |
| 2891 | const page = await (await get(`/alice/busy/issues/${tell.number}`)).text(); | |
| 2892 | // The word comes from the pull the merge closed it with; without the | |
| 2893 | // event the page falls back to the issue's plain "closed" badge. | |
| 2894 | expect(page).toMatch( | |
| 2895 | />shipped<\/span\s*>\s*<span class="muted small">merged<\/span>/, | |
| 2896 | ); | |
| 2897 | }); | |
| 2898 | ||
| 2899 | test("the app page still answers in milliseconds with a week of rows behind it", async () => { | |
| 2900 | await get("/alice/busy"); | |
| 2901 | const started = performance.now(); | |
| 2902 | for (let i = 0; i < 5; i++) expect((await get("/alice/busy")).status).toBe(200); | |
| 2903 | const each = (performance.now() - started) / 5; | |
| 2904 | expect(each).toBeLessThan(250); | |
| 2905 | }); | |
| 2906 | }); |
new above
offline