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:
Roberto Treviño
2026-11-25 10:21:30 -06:00
parent 70b6d6c646
commit cd7bbaf11a
70 changed files with 5459 additions and 182 deletions
+176
View File
@@ -0,0 +1,176 @@
import { DecimalPipe } from '@angular/common';
import { Component, computed, inject, input, resource, signal } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatIconModule } from '@angular/material/icon';
import { RouterLink } from '@angular/router';
import type {
Condition,
DiagnosticReport,
Observation,
Patient,
Procedure,
RiskAssessment,
} from 'fhir/r4';
import { code, ext, Fhir, label, resources } from '../fhir';
import { name } from '../indicadores/measures';
import { Figure, PALETTE, Plot } from '../plot';
/** One patient: her history (conditions, procedures), her tests and results, and the
* champion models' risks with their curves. Everything read from the FHIR API. */
@Component({
selector: 'app-paciente',
imports: [DecimalPipe, MatButtonModule, MatCardModule, MatIconModule, RouterLink, Plot],
templateUrl: './paciente.html',
styleUrl: './paciente.scss',
})
export class Paciente {
private readonly fhir = inject(Fhir);
readonly domain = input.required<string>();
readonly id = input.required<string>();
private readonly data = resource({
params: () => ({ domain: this.domain(), id: this.id() }),
loader: async ({ params: { domain, id } }) => {
const subject = { subject: `Patient/${id}`, _count: 200 };
const [patient, conditions, procedures, reports, observations, risks] = await Promise.all([
this.fhir.read<Patient>(domain, 'Patient', id),
this.fhir.all(domain, 'Condition', subject),
this.fhir.all(domain, 'Procedure', subject),
this.fhir.all(domain, 'DiagnosticReport', subject),
this.fhir.all(domain, 'Observation', subject),
this.fhir.all(domain, 'RiskAssessment', subject),
]);
return {
patient,
conditions: resources<Condition>(conditions, 'Condition'),
procedures: resources<Procedure>(procedures, 'Procedure'),
reports: resources<DiagnosticReport>(reports, 'DiagnosticReport').reverse(),
observations: new Map(
resources<Observation>(observations, 'Observation').map((o) => [o.id, o]),
),
risks: resources<RiskAssessment>(risks, 'RiskAssessment'),
};
},
});
protected readonly patient = computed(() => this.data.value()?.patient);
protected readonly error = computed(() => this.data.error());
protected readonly loading = computed(() => this.data.isLoading());
protected readonly history = computed(() => {
const d = this.data.value();
if (!d) return [];
return [
...d.conditions.map((c) => ({
date: c.recordedDate ?? '',
kind: 'Diagnóstico',
text: label(c.code),
code: code(c.code),
})),
...d.procedures.map((p) => ({
date: p.performedDateTime ?? '',
kind: 'Procedimiento',
text: label(p.code),
code: undefined,
})),
].sort((a, b) => b.date.localeCompare(a.date));
});
protected readonly tests = computed(() => {
const d = this.data.value();
if (!d) return [];
return d.reports.map((r) => {
const results = (r.result ?? [])
.map((x) => d.observations.get(x.reference?.split('/')[1]))
.filter((o) => !!o) as Observation[];
return {
date: r.effectiveDateTime ?? '',
reported: ext(r, 'report-date')?.valueDate,
kind: label(r.code),
status: r.status,
results: [
...results.map(
(o) =>
`${label(o.code)}: ${o.valueBoolean !== undefined ? (o.valueBoolean ? 'positivo' : 'negativo') : name(code(o.valueCodeableConcept) ?? '')}`,
),
...(r.conclusionCode ?? []).map((c) => `Histología: ${name(code(c) ?? '')}`),
],
};
});
});
/** One model's latest risk assessment per model. */
protected readonly models = computed(() => {
const latest = new Map<string, RiskAssessment>();
for (const r of this.data.value()?.risks ?? []) {
const key = code(r.method) ?? '';
if (
!latest.has(key) ||
(latest.get(key)!.occurrenceDateTime ?? '') < (r.occurrenceDateTime ?? '')
)
latest.set(key, r);
}
return [...latest.values()].map((r) => ({
model: code(r.method) ?? '',
text: r.method?.text ?? '',
index: r.occurrenceDateTime ?? '',
predictions: (r.prediction ?? []).map((p) => ({
years: p.whenRange?.high?.value ?? 0,
p: p.probabilityDecimal ?? 0,
low: ext(p, 'ic-bajo')?.valueDecimal,
high: ext(p, 'ic-alto')?.valueDecimal,
})),
}));
});
protected readonly shown = signal<string | undefined>(undefined);
protected readonly curve = computed<Figure>(() => {
const models = this.models().filter((m) => m.predictions.length > 1);
return {
data: models.flatMap((m, i) => {
const x = m.predictions.map((p) => p.years);
const color = PALETTE[i % PALETTE.length];
return [
{
x,
y: m.predictions.map((p) => (p.high ?? p.p) * 100),
mode: 'lines',
line: { width: 0 },
showlegend: false,
hoverinfo: 'skip',
},
{
x,
y: m.predictions.map((p) => (p.low ?? p.p) * 100),
mode: 'lines',
line: { width: 0 },
fill: 'tonexty',
fillcolor: color + '33',
showlegend: false,
hoverinfo: 'skip',
},
{
x,
y: m.predictions.map((p) => p.p * 100),
mode: 'lines+markers',
name: m.model.replace('cacu.', ''),
line: { color },
},
];
}),
layout: {
xaxis: { title: { text: 'Años desde la fecha índice' }, dtick: 1 },
// At least 0–5 %: a near-zero risk reads as near zero, not as a full axis.
yaxis: {
title: { text: 'Probabilidad (%)' },
range: [0, Math.max(5, this.top() * 110)],
tickformat: '.1f',
},
},
};
});
private top(): number {
return Math.max(0, ...this.models().flatMap((m) => m.predictions.map((p) => p.high ?? p.p)));
}
protected readonly name = name;
protected readonly status = computed(() => {
const p = this.patient();
return p ? name(ext(p, 'follow-up-status')?.valueCoding?.code ?? '') : '';
});
}