import { Reading } from './measures'; /** One step of a route: its name and the measure that times it. A step with no measure is * not published yet. */ export interface RouteStep { label: string; measure?: string; /** the step (its position) this one starts after; the previous one by default */ after?: number; } /** One row of a duration table: the median days, how many measurements and the quartiles. * `empty` (published, nothing measured in the period) is not `unpublished` (no measure), and * neither is a zero: a median of 0 days is a value. */ export interface DurationRow { label: string; status: 'value' | 'empty' | 'unpublished'; median?: number; observations?: number; low?: number; high?: number; after?: number; /** where its bar starts, in days, when the steps are chained */ start?: number; } /** Where each step's bar starts when drawn as a schematic sequence (a Gantt chart): where the * step it follows ends. A step without a median moves nothing after it. */ export function chainStarts(steps: { median?: number; after?: number }[]): number[] { const starts: number[] = []; steps.forEach(({ after }, i) => { const j = after ?? i - 1; starts.push(j < 0 ? 0 : starts[j] + (steps[j].median ?? 0)); }); return starts; } export function chained(rows: DurationRow[]): DurationRow[] { const starts = chainStarts(rows); return rows.map((row, i) => ({ ...row, start: starts[i] })); } /** The rows of a route in one reading; every step keeps its row. `published` says which * measures the catalog has. */ export function durationRows( steps: RouteStep[], read: (measure: string) => Reading | undefined, published: (measure: string) => boolean, ): DurationRow[] { return steps.map(({ label, measure, after }) => { if (!measure || !published(measure)) return { label, after, status: 'unpublished' }; const r = read(measure); if (r?.score === undefined) return { label, after, status: 'empty' }; return { label, after, status: 'value', median: r.score, observations: r.populations['measure-observation'], low: r.low, high: r.high, }; }); } /** The scale of a table of durations, from zero: the farthest median or third quartile shown. */ export const durationMax = (rows: DurationRow[]): number => Math.max(0, ...rows.map((r) => (r.start ?? 0) + Math.max(r.median ?? 0, r.high ?? 0)));