KPIs page draws durations as boxes and reads a composition's categories from its first stratifier
This commit is contained in:
@@ -6,10 +6,11 @@ export interface CodeableConcept { coding?: { system?: string; code: string }[];
|
||||
export interface ReportPopulation { code: CodeableConcept; count: number }
|
||||
export interface ReportStratum { value: CodeableConcept; population: ReportPopulation[]; measureScore?: { value: number } }
|
||||
export interface ReportStratifier { code: CodeableConcept[]; stratum: ReportStratum[] }
|
||||
export interface Score { value: number; unit?: string; extension?: { url: string; valueDecimal?: number }[] }
|
||||
export interface ReportGroup {
|
||||
id?: string;
|
||||
population: ReportPopulation[];
|
||||
measureScore?: { value: number };
|
||||
measureScore?: Score;
|
||||
stratifier?: ReportStratifier[];
|
||||
}
|
||||
export interface MeasureReport {
|
||||
@@ -112,10 +113,11 @@ export function proportionFigure(series: Series, title: string, stratum?: string
|
||||
}
|
||||
|
||||
|
||||
/** A composition: per period, how many units fall in each category of its `resultado` stratifier. */
|
||||
/** A composition: per period, how many units fall in each category. The categories are the report's first
|
||||
* stratifier (the WebApi puts them first, named after the category's column), or the one named `stratifier`. */
|
||||
export interface CompositionSeries { measure: string; periods: string[]; categories: Record<string, number[]> }
|
||||
|
||||
export function compositionFrom(bundle: MeasureReportBundle, stratifier = 'resultado'): CompositionSeries {
|
||||
export function compositionFrom(bundle: MeasureReportBundle, stratifier?: string): CompositionSeries {
|
||||
if (bundle?.resourceType !== 'Bundle' || !Array.isArray(bundle.entry) || bundle.entry.length === 0) {
|
||||
throw new InvalidMeasureReport('Respuesta de KPI inválida');
|
||||
}
|
||||
@@ -123,8 +125,9 @@ export function compositionFrom(bundle: MeasureReportBundle, stratifier = 'resul
|
||||
const periods = reports.map(r => periodLabel(r.period));
|
||||
const categories: Record<string, number[]> = {};
|
||||
reports.forEach((report, i) => {
|
||||
const found = report.group[0]?.stratifier?.find(s => s.code[0]?.text === stratifier);
|
||||
if (!found) throw new InvalidMeasureReport(`Falta el estratificador ${stratifier}`);
|
||||
const stratifiers = report.group[0]?.stratifier ?? [];
|
||||
const found = stratifier === undefined ? stratifiers[0] : stratifiers.find(s => s.code[0]?.text === stratifier);
|
||||
if (!found) throw new InvalidMeasureReport(`Falta el estratificador ${stratifier ?? 'de categorías'}`);
|
||||
for (const stratum of found.stratum) {
|
||||
const name = stratum.value.text ?? '';
|
||||
(categories[name] ??= periods.map(() => 0))[i] = count(stratum.population, 'initial-population');
|
||||
@@ -145,3 +148,45 @@ export function compositionFigure(series: CompositionSeries, title: string): Plo
|
||||
xaxis: { type: 'category', title: { text: 'Periodo' } }, yaxis: { title: { text: 'Pacientes' }, rangemode: 'tozero' } },
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/** A duration: per period, the units measurable and measured and, if any was, the quartiles of their days. */
|
||||
export interface DurationPoint { period: string; initial: number; n: number; q1: number | null; median: number | null; q3: number | null }
|
||||
export interface DurationSeries { measure: string; lowerIsBetter: boolean; points: DurationPoint[] }
|
||||
|
||||
const quartile = (score: Score | undefined, name: string) =>
|
||||
score?.extension?.find(e => e.url.endsWith(`/${name}`))?.valueDecimal ?? null;
|
||||
|
||||
export function durationFrom(bundle: MeasureReportBundle): DurationSeries {
|
||||
if (bundle?.resourceType !== 'Bundle' || !Array.isArray(bundle.entry) || bundle.entry.length === 0) {
|
||||
throw new InvalidMeasureReport('Respuesta de KPI inválida');
|
||||
}
|
||||
const reports = bundle.entry.map(e => e.resource);
|
||||
const points = reports.map(report => {
|
||||
const [group] = report.group;
|
||||
const n = count(group.population, 'measure-observation');
|
||||
const score = group.measureScore;
|
||||
if (n > 0 && score === undefined) throw new InvalidMeasureReport('Falta la mediana');
|
||||
return { period: periodLabel(report.period), initial: count(group.population, 'initial-population'), n,
|
||||
q1: quartile(score, 'q1'), median: score?.value ?? null, q3: quartile(score, 'q3') };
|
||||
});
|
||||
const lowerIsBetter = reports[0].improvementNotation?.coding?.some(c => c.code === 'decrease') ?? false;
|
||||
return { measure: reports[0].measure, lowerIsBetter, points };
|
||||
}
|
||||
|
||||
/** One box per period, from the served quartiles: first to third quartile and the median, no whiskers. */
|
||||
export function durationFigure(series: DurationSeries, title: string): PlotlyFigure {
|
||||
const measured = series.points.filter(p => p.median !== null);
|
||||
const data = [{
|
||||
type: 'box', name: 'Días', x: measured.map(p => p.period),
|
||||
q1: measured.map(p => p.q1), median: measured.map(p => p.median), q3: measured.map(p => p.q3),
|
||||
lowerfence: measured.map(p => p.q1), upperfence: measured.map(p => p.q3),
|
||||
text: measured.map(p => `${p.period}<br>n = ${p.n} de ${p.initial}<br>mediana ${p.median} días`), hoverinfo: 'text',
|
||||
}];
|
||||
const notes = series.lowerIsBetter ? [{ text: 'Menos días es mejor.', showarrow: false, xref: 'paper', yref: 'paper', x: 0, y: -0.2 }] : [];
|
||||
return {
|
||||
data,
|
||||
layout: { title: { text: title }, showlegend: false, annotations: notes,
|
||||
xaxis: { type: 'category', title: { text: 'Periodo' } }, yaxis: { title: { text: 'Días' }, rangemode: 'tozero' } },
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user