One shell with the current look: indicators, follow-up list and patient view from FHIR; /conectar canvas; /revision; Playwright page tests
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
import { Component, computed, effect, inject, input, resource, signal } from '@angular/core';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
import { MatButtonToggleModule } from '@angular/material/button-toggle';
|
||||
import { MatCardModule } from '@angular/material/card';
|
||||
import { MatSelectModule } from '@angular/material/select';
|
||||
import type { Measure, MeasureReport } from 'fhir/r4';
|
||||
import { Fhir, resources } from '../fhir';
|
||||
import { Figure, Plot } from '../plot';
|
||||
import { LAYOUTS } from './layout';
|
||||
import {
|
||||
detail,
|
||||
donut,
|
||||
formatScore,
|
||||
medians,
|
||||
name,
|
||||
periodLabel,
|
||||
periods,
|
||||
PeriodType,
|
||||
reading,
|
||||
Reading,
|
||||
scoring,
|
||||
STRATA,
|
||||
strataBars,
|
||||
} from './measures';
|
||||
|
||||
/** The period's indicators: the timeliness KPIs, the screening route, and a card per
|
||||
* clinical measure with its figures. */
|
||||
@Component({
|
||||
selector: 'app-tab-indicadores',
|
||||
imports: [
|
||||
FormsModule,
|
||||
MatButtonModule,
|
||||
MatButtonToggleModule,
|
||||
MatCardModule,
|
||||
MatSelectModule,
|
||||
Plot,
|
||||
],
|
||||
templateUrl: './tab-indicadores.html',
|
||||
styleUrl: './tab-indicadores.scss',
|
||||
})
|
||||
export class TabIndicadores {
|
||||
private readonly fhir = inject(Fhir);
|
||||
readonly domain = input.required<string>();
|
||||
readonly measures = input.required<Measure[]>();
|
||||
|
||||
protected readonly type = signal<PeriodType>('semester');
|
||||
protected readonly options = computed(() => periods(this.type()));
|
||||
protected readonly chosen = signal<string | undefined>(undefined);
|
||||
/** The period the figures show (set by Aplicar). */
|
||||
protected readonly applied = signal<string | undefined>(undefined);
|
||||
protected readonly routeView = signal<'sample' | 'step'>('sample');
|
||||
|
||||
protected readonly layout = computed(
|
||||
() => LAYOUTS[this.domain()] ?? { kpis: [], route: [], anchor: '' },
|
||||
);
|
||||
private readonly byName = computed(() => new Map(this.measures().map((m) => [m.name!, m])));
|
||||
protected readonly kpis = computed(() =>
|
||||
this.layout()
|
||||
.kpis.map((n) => this.byName().get(n))
|
||||
.filter((m) => !!m),
|
||||
);
|
||||
protected readonly clinical = computed(() => {
|
||||
const placed = new Set([
|
||||
...this.layout().kpis,
|
||||
...this.layout().route.flatMap((s) => [s.fromSample, s.fromStep]),
|
||||
]);
|
||||
return this.measures().filter((m) => !placed.has(m.name!));
|
||||
});
|
||||
|
||||
/** The latest period with data, the default. */
|
||||
private readonly latest = resource({
|
||||
params: () => ({
|
||||
domain: this.domain(),
|
||||
anchor: this.byName().get(this.layout().anchor) ?? this.measures()[0],
|
||||
all: this.options(),
|
||||
}),
|
||||
loader: async ({ params }) => {
|
||||
const pages = await this.fhir.all(params.domain, 'MeasureReport', {
|
||||
measure: params.anchor.id!,
|
||||
'reporting-period': params.all.join(','),
|
||||
});
|
||||
const found = resources<MeasureReport>(pages, 'MeasureReport');
|
||||
const index = found.findIndex(
|
||||
(r) => (reading(r)?.populations['initial-population'] ?? 0) > 0,
|
||||
);
|
||||
return params.all[Math.max(index, 0)];
|
||||
},
|
||||
});
|
||||
|
||||
protected readonly reports = resource({
|
||||
params: () => ({ domain: this.domain(), period: this.applied(), measures: this.measures() }),
|
||||
loader: async ({ params }) => {
|
||||
if (!params.period) return new Map<string, Reading | undefined>();
|
||||
const found = await Promise.all(
|
||||
params.measures.map(async (m) => {
|
||||
const page = await this.fhir.search(params.domain, 'MeasureReport', {
|
||||
measure: m.id!,
|
||||
'reporting-period': params.period,
|
||||
});
|
||||
return [m.name!, reading(resources<MeasureReport>(page, 'MeasureReport')[0])] as const;
|
||||
}),
|
||||
);
|
||||
return new Map(found);
|
||||
},
|
||||
});
|
||||
|
||||
constructor() {
|
||||
effect(() => {
|
||||
const latest = this.latest.value();
|
||||
if (latest && !this.applied()) {
|
||||
this.chosen.set(latest);
|
||||
this.applied.set(latest);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected readonly label = periodLabel;
|
||||
protected readonly strataNames = STRATA;
|
||||
protected r(m: Measure): Reading | undefined {
|
||||
return this.reports.value()?.get(m.name!);
|
||||
}
|
||||
protected score(m: Measure): string {
|
||||
return formatScore(m, this.r(m));
|
||||
}
|
||||
protected detail(m: Measure): string {
|
||||
return detail(m, this.r(m));
|
||||
}
|
||||
|
||||
protected setType(type: PeriodType): void {
|
||||
this.type.set(type);
|
||||
this.chosen.set(this.options()[0]);
|
||||
}
|
||||
|
||||
protected readonly route = computed<Figure>(() => {
|
||||
const view = this.routeView();
|
||||
return medians(
|
||||
this.layout().route.map((s) => {
|
||||
const m = this.byName().get(view === 'sample' ? s.fromSample : s.fromStep);
|
||||
return { label: view === 'sample' ? s.label : s.step, r: m && this.r(m) };
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
/** Each clinical card's figures, built once per loaded period. */
|
||||
protected readonly figureSets = computed(
|
||||
() =>
|
||||
new Map(
|
||||
this.clinical().map((m) => [m.name!, this.build(m, this.reports.value()?.get(m.name!))]),
|
||||
),
|
||||
);
|
||||
|
||||
/** A clinical card's figures: the overall split, then one per stratum. */
|
||||
private build(m: Measure, r: Reading | undefined): { title: string; figure: Figure }[] {
|
||||
if (!r || !(r.populations['initial-population'] ?? 0)) return [];
|
||||
const strata = Object.keys(r.strata).filter(
|
||||
(s) => s !== 'category' && Object.keys(r.strata[s]).length,
|
||||
);
|
||||
const kind = scoring(m);
|
||||
if (kind === 'continuous-variable')
|
||||
return [{ title: 'Mediana y cuartiles', figure: medians([{ label: m.title!, r }]) }];
|
||||
return [
|
||||
{ title: kind === 'proportion' ? 'Numerador y resto' : 'Por categoría', figure: donut(m, r) },
|
||||
...strata.map((s) => ({
|
||||
title: `Por ${STRATA[s]?.toLowerCase() ?? s}`,
|
||||
figure: strataBars(m, r, s),
|
||||
})),
|
||||
];
|
||||
}
|
||||
protected readonly name = name;
|
||||
}
|
||||
Reference in New Issue
Block a user