← Public packages
@kody/kody-factory
Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.
Files
import { round } from './shared.ts'
import type { Metrics } from './types.ts'
/** Look up the value a metric key names in one week's metrics (null when absent). */
export function metricValue(metrics: Metrics, key: string): number | null {
const strip = (prefix: string, suffix: string) => (key.startsWith(prefix) && key.endsWith(suffix) ? key.slice(prefix.length, key.length - suffix.length) : null)
let inner = strip('step:', ':p50')
if (inner !== null) {
const cut = inner.lastIndexOf(':')
const jobKey = inner.slice(0, cut)
const step = inner.slice(cut + 1)
return metrics.jobs.find((job) => job.key === jobKey)?.steps.find((entry) => entry.name === step)?.p50Min ?? null
}
inner = strip('job:', ':p50')
if (inner !== null) return metrics.jobs.find((job) => job.key === inner)?.durationMin.p50 ?? null
inner = strip('job:', ':flaky')
if (inner !== null) {
const job = metrics.jobs.find((entry) => entry.key === inner)
return job ? job.flakyFailures + job.soleFailuresPassedOnRerun : 0
}
inner = strip('path:', ':p50')
if (inner !== null) return metrics.criticalPath?.workflow === inner ? metrics.criticalPath.totalP50Min : null
inner = strip('workflow:', ':supersededMinutesPerWeek')
if (inner !== null) return metrics.workflows.find((workflow) => workflow.name === inner)?.supersededMinutesPerWeek ?? null
inner = strip('dup:', ':minutesPerRun')
if (inner !== null) {
const cut = inner.lastIndexOf(':')
return metrics.duplicates.find((entry) => entry.workflow === inner!.slice(0, cut) && entry.step === inner!.slice(cut + 1))?.minutesPerRun ?? 0
}
if (key === 'prs:docsOnlyShare') return metrics.docsOnlyPrs.sampled ? metrics.docsOnlyPrs.docsOnly / metrics.docsOnlyPrs.sampled : null
return null
}
export type WeekSummary = {
week: string
prTimeToGreenP50Min: number
prTimeToGreenP90Min: number
criticalPathP50Min: number | null
runnerMinutesPerWeek: number
billedMinutesPerWeek: number
flakyShas: number
rerunRate: number
}
export function summarizeWeek(week: string, metrics: Metrics): WeekSummary {
const runs = metrics.workflows.reduce((total, workflow) => total + workflow.analyzedRuns, 0)
const reruns = metrics.workflows.reduce((total, workflow) => total + workflow.rerunRate * workflow.analyzedRuns, 0)
return {
week,
prTimeToGreenP50Min: metrics.prTimeToGreenMin.p50,
prTimeToGreenP90Min: metrics.prTimeToGreenMin.p90,
criticalPathP50Min: metrics.criticalPath?.totalP50Min ?? null,
runnerMinutesPerWeek: metrics.runnerMinutesPerWeek.total,
billedMinutesPerWeek: metrics.billedMinutesPerWeek.total,
flakyShas: metrics.workflows.reduce((total, workflow) => total + workflow.flakyShas, 0),
rerunRate: round(runs ? reruns / runs : 0, 3),
}
}
/** Week-over-week trend plus the effect of changes shipped from earlier cards. Pure. */
export function buildTrends(input: {
week: string
metrics: Metrics
history: ReadonlyArray<{ week: string; metrics: Metrics }>
/** Shipped ci-review cards with the baseline recorded when each was filed. */
shipped: ReadonlyArray<{ cardId: string; title: string; prUrl: string | null; shippedAt: string; oppId: string; metricKey: string; baseline: number }>
}) {
const previous = input.history.filter((entry) => entry.week < input.week).sort((a, b) => b.week.localeCompare(a.week))
const weeks = [summarizeWeek(input.week, input.metrics), ...previous.slice(0, 7).map((entry) => summarizeWeek(entry.week, entry.metrics))]
const last = weeks[1] ?? null
const delta = last
? {
prTimeToGreenP50Min: round(weeks[0].prTimeToGreenP50Min - last.prTimeToGreenP50Min, 1),
runnerMinutesPerWeek: Math.round(weeks[0].runnerMinutesPerWeek - last.runnerMinutesPerWeek),
flakyShas: weeks[0].flakyShas - last.flakyShas,
}
: null
const shipped = input.shipped.map((card) => {
const now = metricValue(input.metrics, card.metricKey)
return { ...card, now, change: now === null ? null : round(now - card.baseline, 2) }
})
return { weeks, delta, shipped }
}