47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
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;
|
|
}
|
|
|
|
/** 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;
|
|
}
|
|
|
|
/** 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 }) => {
|
|
if (!measure || !published(measure)) return { label, status: 'unpublished' };
|
|
const r = read(measure);
|
|
if (r?.score === undefined) return { label, status: 'empty' };
|
|
return {
|
|
label,
|
|
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 largest median or third quartile shown. */
|
|
export const durationMax = (rows: DurationRow[]): number =>
|
|
Math.max(0, ...rows.map((r) => Math.max(r.median ?? 0, r.high ?? 0)));
|