Public: anyone can read this; the crew can change it.
A milestone's page shows what its limb cost and landed, with its costliest issues #1958
scout/forge:scout/1596-a-milestone-s-page-shows-what-its-limb-c → main 1 commit fork
3 files
changed +132
−0
src/core/milestones.ts +52
| @@ -40,6 +40,9 @@ import { getUsersByIds } from "./users.ts"; | ||
| 40 | 40 | /** A limb with this many open issues beneath it and nobody building is one to look at. */ |
| 41 | 41 | export const TRIM_OPEN = 8; |
| 42 | 42 | |
| 43 | /** How many issues a milestone's spend names. */ | |
| 44 | export const COSTLIEST = 3; | |
| 45 | ||
| 43 | 46 | /** How heavy a limb is: what is beneath it, and whether it moves. */ |
| 44 | 47 | export interface Weight { |
| 45 | 48 | /** The work beneath it: what is open, and what closed as done. */ |
| @@ -100,6 +103,17 @@ export interface Milestone extends Limb { | ||
| 100 | 103 | readonly title: string; |
| 101 | 104 | readonly at: string; |
| 102 | 105 | }[]; |
| 106 | /** | |
| 107 | * What the limb cost and landed, ever: owner facts only, so nothing for a | |
| 108 | * viewer who may not see them. `costliest` is at most `COSTLIEST` issues | |
| 109 | * beneath it, dearest first; an issue that cost nothing is not listed. | |
| 110 | */ | |
| 111 | readonly spend?: { | |
| 112 | readonly usd: number; | |
| 113 | /** Pulls merged, ever, for the issues beneath it. */ | |
| 114 | readonly landed: number; | |
| 115 | readonly costliest: readonly { readonly issue: Issue; readonly usd: number }[]; | |
| 116 | }; | |
| 103 | 117 | /** A plain sentence: when it lands at this week's pace, or why nobody can say. */ |
| 104 | 118 | readonly estimate: string; |
| 105 | 119 | /** What it waits for directly that has a tree of its own. */ |
| @@ -270,6 +284,17 @@ export function readPlan( | ||
| 270 | 284 | } |
| 271 | 285 | } |
| 272 | 286 | |
| 287 | // Every pull ever merged, by the issues it closes: what a limb landed. | |
| 288 | const everMerged: (readonly number[])[] = options.spend | |
| 289 | ? db | |
| 290 | .query<{ body: string }, [string]>( | |
| 291 | `SELECT i.body FROM pull p JOIN issue i ON i.id = p.issue_id | |
| 292 | WHERE i.repo_id = ?1 AND p.merged_at IS NOT NULL`, | |
| 293 | ) | |
| 294 | .all(repo.id) | |
| 295 | .map((row) => closesFromBody(row.body)) | |
| 296 | : []; | |
| 297 | ||
| 273 | 298 | const stateOf = (issue: Issue): LeafState => { |
| 274 | 299 | if (issue.state === "closed") return counted(issue) ? "done" : "gone"; |
| 275 | 300 | if (building.has(issue.id)) return "building"; |
| @@ -504,6 +529,21 @@ export function readPlan( | ||
| 504 | 529 | merged: merged |
| 505 | 530 | .filter((pull) => pull.closes.some((n) => numbers.has(n))) |
| 506 | 531 | .map(({ number: n, title, at }) => ({ number: n, title, at })), |
| 532 | ...(options.spend | |
| 533 | ? { | |
| 534 | spend: { | |
| 535 | usd: limb.weight.spentUsd ?? 0, | |
| 536 | landed: everMerged.filter((closes) => | |
| 537 | closes.some((n) => numbers.has(n)), | |
| 538 | ).length, | |
| 539 | costliest: under | |
| 540 | .filter((i) => (cost.get(i.id) ?? 0) > 0) | |
| 541 | .map((i) => ({ issue: i, usd: cost.get(i.id)! })) | |
| 542 | .sort((a, b) => b.usd - a.usd || a.issue.number - b.issue.number) | |
| 543 | .slice(0, COSTLIEST), | |
| 544 | }, | |
| 545 | } | |
| 546 | : {}), | |
| 507 | 547 | estimate, |
| 508 | 548 | limbs: arranged(issue.id, [...limbIds]).map(limbOf), |
| 509 | 549 | leaves: children |
| @@ -576,6 +616,18 @@ export function milestoneJson( | ||
| 576 | 616 | on: b.on.map(brief), |
| 577 | 617 | })), |
| 578 | 618 | merged: milestone.merged, |
| 619 | ...(milestone.spend === undefined | |
| 620 | ? {} | |
| 621 | : { | |
| 622 | spend: { | |
| 623 | usd: milestone.spend.usd, | |
| 624 | landed: milestone.spend.landed, | |
| 625 | costliest: milestone.spend.costliest.map((c) => ({ | |
| 626 | ...brief(c.issue), | |
| 627 | usd: c.usd, | |
| 628 | })), | |
| 629 | }, | |
| 630 | }), | |
| 579 | 631 | limbs: milestone.limbs.map(limbJson), |
| 580 | 632 | leaves: milestone.leaves.map((leaf) => ({ |
| 581 | 633 | ...brief(leaf.issue), |
src/web/pages/milestones.ts +26
| @@ -376,6 +376,31 @@ function milestoneBody( | ||
| 376 | 376 | )} |
| 377 | 377 | </section>`, |
| 378 | 378 | )} |
| 379 | ${when( | |
| 380 | m.spend, | |
| 381 | (spend) => | |
| 382 | html`<section class="ms-section" id="ms-spend"> | |
| 383 | <h2>Cost and landed</h2> | |
| 384 | <p class="ms-figures"> | |
| 385 | <span><b>${dollars(spend.usd)}</b> spent</span> | |
| 386 | <span | |
| 387 | ><b>${spend.landed}</b> change${spend.landed === 1 ? "" : "s"} | |
| 388 | landed</span | |
| 389 | > | |
| 390 | </p> | |
| 391 | ${spend.costliest.length === 0 | |
| 392 | ? html`<p class="muted small">Nothing has cost anything yet.</p>` | |
| 393 | : html`<ol class="ms-rows"> | |
| 394 | ${spend.costliest.map( | |
| 395 | (c) => | |
| 396 | html`<li> | |
| 397 | ${issue(c.issue.number, c.issue.title)} | |
| 398 | <span class="ms-state small">${dollars(c.usd)}</span> | |
| 399 | </li>`, | |
| 400 | )} | |
| 401 | </ol>`} | |
| 402 | </section>`, | |
| 403 | )} | |
| 379 | 404 | <section class="ms-section" id="ms-merged"> |
| 380 | 405 | <h2>Merged in ${PILLAR_DAYS} days</h2> |
| 381 | 406 | ${m.merged.length === 0 |
| @@ -601,6 +626,7 @@ export const milestonesCss = /* css */ ` | ||
| 601 | 626 | .ms-section h2 { font-size: 13px; margin: 0 0 6px; } |
| 602 | 627 | .ms-rows { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; font-size: 14px; } |
| 603 | 628 | .ms-rows li > span { margin-left: 6px; } |
| 629 | .ms-figures { display: flex; flex-wrap: wrap; gap: 4px 20px; margin: 0 0 8px; font-variant-numeric: tabular-nums; } | |
| 604 | 630 | .ms-state { color: var(--muted-fg); } |
| 605 | 631 | .ms-rows li[data-state="building"] .ms-state { color: var(--accent); } |
| 606 | 632 | .ms-rows li[data-state="blocked"] .ms-state { color: var(--attn); } |
test/milestones.test.ts +54
| @@ -373,6 +373,14 @@ describe("milestones", () => { | ||
| 373 | 373 | expect(one).toContain(`href="/alice/engine/issues/${network.number}"`); |
| 374 | 374 | expect(one).toContain(`href="/alice/engine/pulls/${landed}"`); |
| 375 | 375 | expect(one).toContain("$1.50"); |
| 376 | // What it cost and landed, with its costliest: the shell, $1.50, one change. | |
| 377 | const spend = one.slice( | |
| 378 | one.indexOf('id="ms-spend"'), | |
| 379 | one.indexOf("</section>", one.indexOf('id="ms-spend"')), | |
| 380 | ); | |
| 381 | expect(spend).toMatch(/<b>\$1\.50<\/b> spent/); | |
| 382 | expect(spend).toMatch(/<b>1<\/b> change\s+landed/); | |
| 383 | expect(spend).toContain("Sandbox the shell"); | |
| 376 | 384 | // Each issue once: the network is under Building now and the disk under |
| 377 | 385 | // Blocked, so Issues lists neither again, only what closed. |
| 378 | 386 | const issues = one.slice( |
| @@ -390,6 +398,7 @@ describe("milestones", () => { | ||
| 390 | 398 | ).text(); |
| 391 | 399 | expect(theirs).toMatch(/<b>1<\/b> of 3 done/); |
| 392 | 400 | expect(theirs).not.toContain("$1.50"); |
| 401 | expect(theirs).not.toContain('id="ms-spend"'); | |
| 393 | 402 | expect(theirs).not.toContain("mason"); |
| 394 | 403 | expect(theirs).toMatch(/the crew · since/); |
| 395 | 404 | |
| @@ -484,6 +493,12 @@ describe("milestones", () => { | ||
| 484 | 493 | }) |
| 485 | 494 | ).json()) as Record<string, any>; |
| 486 | 495 | expect("spentUsd" in theirs).toBe(false); |
| 496 | expect("spend" in theirs).toBe(false); | |
| 497 | expect(one["spend"]).toMatchObject({ | |
| 498 | usd: 1.5, | |
| 499 | landed: 1, | |
| 500 | costliest: [{ number: shell.number, usd: 1.5 }], | |
| 501 | }); | |
| 487 | 502 | expect(theirs["buildingNow"][0].who).toBe("the crew"); |
| 488 | 503 | expect(theirs["done"]).toBe(1); |
| 489 | 504 | }); |
| @@ -515,4 +530,43 @@ describe("milestones", () => { | ||
| 515 | 530 | expect(page).toMatch(/<b>0<\/b> of 2 done/); |
| 516 | 531 | expect(page).toContain("A profit slider"); |
| 517 | 532 | }); |
| 533 | ||
| 534 | test("a milestone's spend adds up: its dollars are its issues' dollars, the three dearest are named, and a reader sees none", () => { | |
| 535 | const top = issue(plain, "Cost 1.0"); | |
| 536 | const parts = [0.5, 3, 1, 2.25].map((usd, n) => { | |
| 537 | const part = issue(plain, `Costed ${n + 1}`); | |
| 538 | link(plain, top, part); | |
| 539 | const run = createRun(app.db, { | |
| 540 | repoId: plain.id, | |
| 541 | kind: "agent", | |
| 542 | issueId: part.id, | |
| 543 | actorId: mason.id, | |
| 544 | reason: "assigned", | |
| 545 | }); | |
| 546 | claimRun(app.db, run.id); | |
| 547 | finishRun(app.db, run.id, { state: "ok", summary: "x", costUsd: usd }); | |
| 548 | return part; | |
| 549 | }); | |
| 550 | const free = issue(plain, "Costed nothing"); | |
| 551 | link(plain, top, free); | |
| 552 | const m = readPlan(app.db, fresh(plain), { spend: true }).milestone( | |
| 553 | top.number, | |
| 554 | )!; | |
| 555 | expect(m.spend!.usd).toBe(6.75); | |
| 556 | expect(m.spend!.usd).toBe(m.weight.spentUsd!); | |
| 557 | expect(m.spend!.landed).toBe(0); | |
| 558 | expect(m.spend!.costliest.map((c) => [c.issue.number, c.usd])).toEqual([ | |
| 559 | [parts[1]!.number, 3], | |
| 560 | [parts[3]!.number, 2.25], | |
| 561 | [parts[2]!.number, 1], | |
| 562 | ]); | |
| 563 | // The three named are never more than the whole. | |
| 564 | expect(m.spend!.costliest.reduce((sum, c) => sum + c.usd, 0)).toBeLessThanOrEqual( | |
| 565 | m.spend!.usd, | |
| 566 | ); | |
| 567 | const reader = readPlan(app.db, fresh(plain), { spend: false }).milestone( | |
| 568 | top.number, | |
| 569 | )!; | |
| 570 | expect(reader.spend).toBeUndefined(); | |
| 571 | }); | |
| 518 | 572 | }); |
new above
offline