diff --git a/pm-stack/aggregator/src/flow-analytics.ts b/pm-stack/aggregator/src/flow-analytics.ts new file mode 100644 index 0000000000..267a09c785 --- /dev/null +++ b/pm-stack/aggregator/src/flow-analytics.ts @@ -0,0 +1,270 @@ +// Flow analytics computation (#4829, step 2 of the #1281 split): +// time-in-status and weekly throughput per project/epic, computed +// from the #3981 item-history store and exposed machine-first as the +// JSON twin the step-3 charts (#4830) will render. +// +// The history seam is `item-history.ts` (#3981) — this module adds no +// store and no collector. It consumes the same weekly cutoffs the +// forecasting derivations consume and follows the same honesty rules: +// +// - Only complete, available cutoffs take part. An interval between +// two consecutive stored cutoffs accrues time-in-status only when +// BOTH bounding cutoffs are complete: an incomplete cutoff breaks +// the chain exactly as it does in `weeklyTransitions`, the spanning +// interval is attributed to no status and no week, and the cutoff +// is named in the window's `gaps` — never silently zero-filled. +// - A complete week with no completions is a real observation: it +// appears in every throughput series with `completed: 0`. +// - Throughput counts issue items only (the #3981 design SS1 unit): +// a PR closing or merging is not a completed unit. Time-in-status +// covers every recorded item, issues and PRs alike — their status +// is a real board observation — with `kind` carried on each item +// row so a consumer can filter. +// - Grouping is the forecast subject model (forecast.ts): an item's +// group is its `parentRef` when present (direct membership), +// otherwise its `epicRef` (ancestor membership), otherwise the +// explicit unassigned group (`groupRef: null`). Membership is read +// from the observation at the START of each interval, so an item +// that changes parent mid-window accrues each interval to the +// group it was observed in at that interval's start. +// - Time-in-status accrues an interval's whole span to the status +// observed at the interval's first cutoff (the status the item was +// seen in while that time passed). Transitions are observed +// between cutoffs, never exact event times — the same lower-bound +// honesty as the history module itself. +// +// Every result carries its computation window and the gap list, so +// no number is ever presented without its window and coverage state +// (#4829 acceptance). + +import { + weeklyTransitions, + type ItemHistoryCutoff, + type ItemHistoryItem, +} from "./item-history.ts"; +import type { RefKey } from "./model.ts"; + +export const FLOW_ANALYTICS_SCHEMA_VERSION = 1; + +/** Status label used for an item whose Project Status is unset. */ +export const UNSET_STATUS = "(unset)"; + +export interface FlowWindowGap { + cutoffAt: string; + complete: boolean; + unavailable: boolean; +} + +export interface FlowWindow { + /** First stored cutoff in the window, or null when history is empty. */ + fromCutoff: string | null; + /** Last stored cutoff in the window, or null when history is empty. */ + toCutoff: string | null; + /** All stored cutoffs in the window. */ + cutoffCount: number; + /** Cutoffs usable as interval bounds (complete and available). */ + completeCutoffs: number; + /** Incomplete or unavailable cutoffs, in order — the visible gap signal. */ + gaps: FlowWindowGap[]; +} + +export interface ItemStatusTime { + ref: RefKey; + kind: string; + /** Days accrued per observed status over the window's complete intervals. */ + byStatus: { status: string; days: number }[]; + totalDays: number; +} + +export interface FlowGroup { + /** The project/epic subject ref, or null for the unassigned group. */ + groupRef: RefKey | null; + /** Distinct items observed in this group at complete cutoffs. */ + itemCount: number; + /** Aggregate item-days per status across the group's items. */ + timeInStatus: { status: string; itemDays: number }[]; + /** Per-item time-in-status, sorted by ref. */ + items: ItemStatusTime[]; + /** One entry per complete week in the window (the same weeks for + * every group), in order; `completed` counts this group's issue + * items observed completed that week. Zero weeks are preserved. */ + throughput: { cutoffAt: string; completed: number; completedRefs: RefKey[] }[]; +} + +export interface FlowAnalytics { + schemaVersion: number; + window: FlowWindow; + groups: FlowGroup[]; +} + +function isUsable(cutoff: ItemHistoryCutoff): boolean { + return cutoff.coverage.complete && !cutoff.coverage.unavailable; +} + +function groupRefOf(item: ItemHistoryItem): RefKey | null { + return item.parentRef ?? item.epicRef ?? null; +} + +function statusOf(item: ItemHistoryItem): string { + return item.status ?? UNSET_STATUS; +} + +const DAY_MS = 86_400_000; + +/** + * Compute flow analytics over stored item-history cutoffs. Pure: + * the caller supplies the cutoffs (the server reads them from the + * ItemHistoryStore); no I/O, no clock. + */ +export function computeFlowAnalytics(cutoffs: ItemHistoryCutoff[]): FlowAnalytics { + const ordered = [...cutoffs].sort((a, b) => a.cutoffAt.localeCompare(b.cutoffAt)); + + const gaps: FlowWindowGap[] = ordered + .filter((c) => !isUsable(c)) + .map((c) => ({ + cutoffAt: c.cutoffAt, + complete: c.coverage.complete, + unavailable: c.coverage.unavailable, + })); + + // Per (group, ref): accrued days per status, plus the item kind. + interface ItemAcc { + kind: string; + byStatus: Map; + } + const perGroup = new Map>(); + const groupItems = new Map>(); + + const accFor = (group: RefKey | null, item: ItemHistoryItem): ItemAcc => { + let items = perGroup.get(group); + if (!items) { + items = new Map(); + perGroup.set(group, items); + } + let acc = items.get(item.ref); + if (!acc) { + acc = { kind: item.kind ?? "issue", byStatus: new Map() }; + items.set(item.ref, acc); + } + return acc; + }; + + // Item membership per group, from complete cutoffs only. + for (const cutoff of ordered) { + if (!isUsable(cutoff)) continue; + for (const item of cutoff.items) { + const group = groupRefOf(item); + let set = groupItems.get(group); + if (!set) { + set = new Set(); + groupItems.set(group, set); + } + set.add(item.ref); + accFor(group, item); // ensure the item row exists even with no accrued interval yet + } + } + + // Time-in-status: an interval accrues only when both bounding + // cutoffs are usable (the weeklyTransitions chain rule). + for (let i = 1; i < ordered.length; i++) { + const prev = ordered[i - 1]!; + const cur = ordered[i]!; + if (!isUsable(prev) || !isUsable(cur)) continue; + const days = (Date.parse(cur.cutoffAt) - Date.parse(prev.cutoffAt)) / DAY_MS; + if (!(days > 0)) continue; + for (const item of prev.items) { + const acc = accFor(groupRefOf(item), item); + const status = statusOf(item); + acc.byStatus.set(status, (acc.byStatus.get(status) ?? 0) + days); + } + } + + // Throughput per group, reusing weeklyTransitions so the week + // definition (chain rule, issues only, zero weeks preserved) is + // identical to the forecasting series. + const weeks = weeklyTransitions(ordered); + // Returns undefined when the item is not observed at the cutoff, + // so a missing observation stays distinguishable from an explicit + // unassigned membership (groupRefOf -> null) at that cutoff. + const groupAt = ( + cutoff: ItemHistoryCutoff | undefined, + ref: RefKey, + ): { group: RefKey | null } | undefined => { + const item = cutoff?.items.find((i) => i.ref === ref); + return item ? { group: groupRefOf(item) } : undefined; + }; + const byCutoff = new Map(ordered.map((c) => [c.cutoffAt, c])); + const prevCutoffOf = (cutoffAt: string): ItemHistoryCutoff | undefined => { + const idx = ordered.findIndex((c) => c.cutoffAt === cutoffAt); + return idx > 0 ? ordered[idx - 1] : undefined; + }; + const throughputByGroup = new Map>(); + for (const week of weeks) { + const closing = byCutoff.get(week.cutoffAt); + const previous = prevCutoffOf(week.cutoffAt); + for (const ref of week.completedRefs) { + const group = (groupAt(closing, ref) ?? groupAt(previous, ref))?.group ?? null; + let perWeek = throughputByGroup.get(group); + if (!perWeek) { + perWeek = new Map(); + throughputByGroup.set(group, perWeek); + } + const refs = perWeek.get(week.cutoffAt) ?? []; + refs.push(ref); + perWeek.set(week.cutoffAt, refs); + } + } + + const groupRefs: (RefKey | null)[] = [...new Set([ + ...groupItems.keys(), + ...perGroup.keys(), + ...throughputByGroup.keys(), + ])].sort((a, b) => (a ?? "").localeCompare(b ?? "")); + + const groups: FlowGroup[] = groupRefs.map((groupRef) => { + const items: ItemStatusTime[] = [...(perGroup.get(groupRef)?.entries() ?? [])] + .map(([ref, acc]) => { + const byStatus = [...acc.byStatus.entries()] + .map(([status, days]) => ({ status, days })) + .sort((a, b) => a.status.localeCompare(b.status)); + return { + ref, + kind: acc.kind, + byStatus, + totalDays: byStatus.reduce((sum, s) => sum + s.days, 0), + }; + }) + .sort((a, b) => a.ref.localeCompare(b.ref)); + const aggregate = new Map(); + for (const item of items) { + for (const s of item.byStatus) { + aggregate.set(s.status, (aggregate.get(s.status) ?? 0) + s.days); + } + } + const perWeek = throughputByGroup.get(groupRef); + return { + groupRef, + itemCount: groupItems.get(groupRef)?.size ?? items.length, + timeInStatus: [...aggregate.entries()] + .map(([status, itemDays]) => ({ status, itemDays })) + .sort((a, b) => a.status.localeCompare(b.status)), + items, + throughput: weeks.map((week) => { + const completedRefs = [...(perWeek?.get(week.cutoffAt) ?? [])].sort(); + return { cutoffAt: week.cutoffAt, completed: completedRefs.length, completedRefs }; + }), + }; + }); + + return { + schemaVersion: FLOW_ANALYTICS_SCHEMA_VERSION, + window: { + fromCutoff: ordered[0]?.cutoffAt ?? null, + toCutoff: ordered[ordered.length - 1]?.cutoffAt ?? null, + cutoffCount: ordered.length, + completeCutoffs: ordered.filter(isUsable).length, + gaps, + }, + groups, + }; +} diff --git a/pm-stack/aggregator/src/server.ts b/pm-stack/aggregator/src/server.ts index ac33e1811a..b96722f82f 100644 --- a/pm-stack/aggregator/src/server.ts +++ b/pm-stack/aggregator/src/server.ts @@ -151,6 +151,7 @@ import { dispatchStateFor, fetchRfaState, RFA_NOT_PICKED_UP } from "./sources/re import { TodayCache, TODAY_CACHE_TTL_MS } from "./today-cache.ts"; import { boardJsonBody, boardJsonViewBody } from "./board-json.ts"; import { ItemHistoryStore } from "./item-history.ts"; +import { computeFlowAnalytics } from "./flow-analytics.ts"; import { FORECAST_CONTRACT_VERSION, quotaForecastContract, @@ -1771,6 +1772,18 @@ async function handler(req: Request): Promise { ); } + if (path === "/api/flow-analytics.json") { + // #4829 (step 2 of #1281): the flow-analytics JSON twin — the + // exact numbers the step-3 charts (#4830) will render, computed + // from the #3981 item-history store. Every group carries its + // computation window and gap signal via the shared window block. + const cutoffs = await itemHistoryStore.readAll().catch(() => []); + return json({ + generatedAt: new Date().toISOString(), + ...computeFlowAnalytics(cutoffs), + }); + } + if (path === "/api/forecasts.json") { // #3984: the shared forecast contract — the identical objects the // Roadmap and Projects pages render as one-line summaries. diff --git a/pm-stack/aggregator/test/flow-analytics-4829.test.ts b/pm-stack/aggregator/test/flow-analytics-4829.test.ts new file mode 100644 index 0000000000..c904236798 --- /dev/null +++ b/pm-stack/aggregator/test/flow-analytics-4829.test.ts @@ -0,0 +1,184 @@ +// Fixture-snapshot tests for flow analytics (#4829): a complete +// window, a gappy window (gap flagged, not zero-filled), a +// zero-completion week preserved only when coverage is complete, +// and the #3981 throughput unit rule (issues only). +import { describe, expect, test } from "bun:test"; +import { computeFlowAnalytics, UNSET_STATUS } from "../src/flow-analytics.ts"; +import type { + ItemHistoryCoverage, + ItemHistoryCutoff, + ItemHistoryItem, +} from "../src/item-history.ts"; + +const REPO = "developing-today/code"; +const W1 = "2026-10-05T00:00:00Z"; +const W2 = "2026-10-12T00:00:00Z"; +const W3 = "2026-10-19T00:00:00Z"; +const W4 = "2026-10-26T00:00:00Z"; +const EPIC = `${REPO}#100`; + +function coverage(partial: Partial = {}): ItemHistoryCoverage { + return { source: "projects-pool", fetchedAt: W1, complete: true, unavailable: false, ...partial }; +} + +function item( + number: number, + partial: Partial = {}, +): ItemHistoryItem { + return { + ref: `${REPO}#${number}`, + repo: REPO, + number, + kind: "issue", + state: "open", + status: "In Progress", + effort: null, + blockedBy: [], + ...partial, + }; +} + +function cutoff(cutoffAt: string, items: ItemHistoryItem[], cov?: Partial): ItemHistoryCutoff { + return { schemaVersion: 1, cutoffAt, coverage: coverage(cov), items }; +} + +describe("flow analytics (#4829)", () => { + test("complete window: time-in-status and throughput per epic, zero week preserved", () => { + const a = (p: Partial) => item(1, { parentRef: EPIC, ...p }); + const b = (p: Partial) => item(2, { parentRef: EPIC, ...p }); + const cutoffs = [ + cutoff(W1, [a({}), b({ status: "Todo" }), item(3, {})]), + cutoff(W2, [a({}), b({ state: "closed", status: "Done" }), item(3, {})]), + cutoff(W3, [a({ state: "closed", status: "Done" }), b({ state: "closed", status: "Done" }), item(3, {})]), + cutoff(W4, [a({ state: "closed", status: "Done" }), b({ state: "closed", status: "Done" }), item(3, {})]), + ]; + const out = computeFlowAnalytics(cutoffs); + + expect(out.window).toEqual({ + fromCutoff: W1, + toCutoff: W4, + cutoffCount: 4, + completeCutoffs: 4, + gaps: [], + }); + + const epic = out.groups.find((g) => g.groupRef === EPIC)!; + expect(epic.itemCount).toBe(2); + // A: In Progress over W1->W2 and W2->W3 (14), Done over W3->W4 (7). + // B: Todo over W1->W2 (7), Done over W2->W3 and W3->W4 (14). + expect(epic.timeInStatus).toEqual([ + { status: "Done", itemDays: 21 }, + { status: "In Progress", itemDays: 14 }, + { status: "Todo", itemDays: 7 }, + ]); + const itemA = epic.items.find((i) => i.ref === `${REPO}#1`)!; + expect(itemA.byStatus).toEqual([ + { status: "Done", days: 7 }, + { status: "In Progress", days: 14 }, + ]); + expect(itemA.totalDays).toBe(21); + // B completes in week W2, A in week W3, week W4 is a preserved zero. + expect(epic.throughput).toEqual([ + { cutoffAt: W2, completed: 1, completedRefs: [`${REPO}#2`] }, + { cutoffAt: W3, completed: 1, completedRefs: [`${REPO}#1`] }, + { cutoffAt: W4, completed: 0, completedRefs: [] }, + ]); + + const unassigned = out.groups.find((g) => g.groupRef === null)!; + expect(unassigned.itemCount).toBe(1); + expect(unassigned.timeInStatus).toEqual([{ status: "In Progress", itemDays: 21 }]); + expect(unassigned.throughput.every((w) => w.completed === 0)).toBe(true); + }); + + test("gappy window: incomplete cutoff breaks the chain, gap is flagged, nothing zero-filled", () => { + const a = (p: Partial) => item(1, { parentRef: EPIC, ...p }); + const cutoffs = [ + cutoff(W1, [a({})]), + cutoff(W2, [a({})], { complete: false }), + cutoff(W3, [a({})]), + cutoff(W4, [a({ state: "closed", status: "Done" })]), + ]; + const out = computeFlowAnalytics(cutoffs); + + expect(out.window.cutoffCount).toBe(4); + expect(out.window.completeCutoffs).toBe(3); + expect(out.window.gaps).toEqual([{ cutoffAt: W2, complete: false, unavailable: false }]); + + const epic = out.groups.find((g) => g.groupRef === EPIC)!; + // Only the W3->W4 interval accrues; the two intervals touching + // the incomplete cutoff accrue nothing. + expect(epic.timeInStatus).toEqual([{ status: "In Progress", itemDays: 7 }]); + // The only week is W4 (W1->W2, W2->W3 form no week at all). + expect(epic.throughput).toEqual([ + { cutoffAt: W4, completed: 1, completedRefs: [`${REPO}#1`] }, + ]); + }); + + test("unavailable cutoff is a gap, and items seen only there create no group", () => { + const cutoffs = [ + cutoff(W1, [item(1, { parentRef: EPIC })]), + cutoff(W2, [item(9, { parentRef: `${REPO}#999` })], { complete: false, unavailable: true }), + cutoff(W3, [item(1, { parentRef: EPIC })]), + ]; + const out = computeFlowAnalytics(cutoffs); + expect(out.window.gaps).toEqual([{ cutoffAt: W2, complete: false, unavailable: true }]); + expect(out.groups.map((g) => g.groupRef)).toEqual([EPIC]); + expect(out.groups[0]!.throughput).toEqual([]); + }); + + test("zero-completion week is preserved when coverage is complete", () => { + const cutoffs = [cutoff(W1, [item(1, {})]), cutoff(W2, [item(1, {})])]; + const out = computeFlowAnalytics(cutoffs); + const group = out.groups.find((g) => g.groupRef === null)!; + expect(group.throughput).toEqual([{ cutoffAt: W2, completed: 0, completedRefs: [] }]); + expect(group.timeInStatus).toEqual([{ status: "In Progress", itemDays: 7 }]); + }); + + test("throughput counts issues only; PRs still accrue time-in-status", () => { + const pr = (p: Partial) => item(7, { kind: "pr", parentRef: EPIC, ...p }); + const cutoffs = [ + cutoff(W1, [pr({})]), + cutoff(W2, [pr({ state: "merged", status: "Done" })]), + ]; + const out = computeFlowAnalytics(cutoffs); + const epic = out.groups.find((g) => g.groupRef === EPIC)!; + expect(epic.throughput).toEqual([{ cutoffAt: W2, completed: 0, completedRefs: [] }]); + const prRow = epic.items.find((i) => i.ref === `${REPO}#7`)!; + expect(prRow.kind).toBe("pr"); + expect(prRow.byStatus).toEqual([{ status: "In Progress", days: 7 }]); + }); + + test("unset status is labelled, and empty history yields an empty window", () => { + const cutoffs = [ + cutoff(W1, [item(1, { status: null })]), + cutoff(W2, [item(1, { status: null })]), + ]; + const out = computeFlowAnalytics(cutoffs); + expect(out.groups[0]!.timeInStatus).toEqual([{ status: UNSET_STATUS, itemDays: 7 }]); + + const empty = computeFlowAnalytics([]); + expect(empty.window).toEqual({ + fromCutoff: null, + toCutoff: null, + cutoffCount: 0, + completeCutoffs: 0, + gaps: [], + }); + expect(empty.groups).toEqual([]); + }); + test("throughput credits explicit unassigned membership at the closing cutoff, not the previous parent", () => { + const cutoffs = [ + cutoff(W1, [item(1, { parentRef: EPIC })]), + cutoff(W2, [item(1, { state: "closed", status: "Done" })]), + ]; + const out = computeFlowAnalytics(cutoffs); + const epic = out.groups.find((g) => g.groupRef === EPIC)!; + expect(epic.throughput.every((w) => w.completed === 0)).toBe(true); + expect(epic.throughput.flatMap((w) => w.completedRefs)).toEqual([]); + const unassigned = out.groups.find((g) => g.groupRef === null)!; + expect(unassigned.throughput).toEqual([ + { cutoffAt: W2, completed: 1, completedRefs: [`${REPO}#1`] }, + ]); + }); + +});