Files
WebApp/src/app/gobernanza/tea/preview.ts
T
alberto b291885b97 Gobernanza preview saved as PDF; Playwright TEA page tests
- The EEG preview dialog gets "Guardar como PDF": previewPdfContent is a
  pure function (verdict, checks, metrics with scores per electrode and
  zone, ADOS-2 classification/CSS and items by their Core titles,
  warnings), downloadPreviewPdf a thin wrapper over export/pdf; the file
  is tea-gobernanza-<day>.pdf. Unit-tested without mocks.
- e2e/pages.spec.ts now holds only the TEA page tests (the CaCu/CaMa
  cases needed fixtures P7 removed): every indicator tab, the follow-up
  xlsx export, the patient record and risk page with their PDFs, and
  every Gobernanza section in sync mode including the preview PDF. Each
  download is checked for a file and a name without a record number.
- Fixes the page tests found: the TEA xlsx name ('seguimiento-tea') failed
  validateName, so the export always threw; it is now tea-<page>-<day>.
  ng serve crashed on every Plotly page (has-hover reads 'global'): the
  build defines global as globalThis. Record numbers repeated once per
  source identifier; they are deduplicated. A refused bundle export showed
  '{}': the blob error body is now read for the API's detail.
2026-11-30 19:17:46 -06:00

204 lines
7.4 KiB
TypeScript

import { KeyValuePipe } from '@angular/common';
import { Component, inject, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { Api } from '../../api/api';
import { previewStatus } from '../../api/fn/preview/preview-status';
import { startPreview } from '../../api/fn/preview/start-preview';
import type { PreviewResult } from '../../api/models/preview-result';
import type { Run } from '../../api/models/run';
import { RunMonitor, errorText } from '../run-monitor';
import { downloadPreviewPdf } from './preview-pdf';
@Component({
selector: 'app-eeg-preview',
imports: [FormsModule, KeyValuePipe, MatButtonModule, RunMonitor],
template: `
<section>
<h3>Vista previa</h3>
<form (ngSubmit)="start(edf.files?.[0], ados.files?.[0], birth.value)">
<label>EDF <input #edf type="file" accept=".edf" required /></label>
<label>ADOS-2 (PDF) <input #ados type="file" accept=".pdf" /></label>
<label>Fecha de nacimiento <input #birth type="date" /></label>
<button mat-stroked-button type="submit" [disabled]="busy()">Vista previa</button>
</form>
@if (error()) {
<p role="alert">{{ error() }}</p>
}
@if (runId(); as id) {
<app-run-monitor [runId]="id" (finished)="finish($event, dialog)" />
}
<dialog #dialog aria-label="Vista previa del estudio">
@if (result(); as r) {
<h3>Resultado de la vista previa</h3>
<p>
{{ r.eeg.level }} ·
{{ r.eeg.valid === true ? 'Válido' : r.eeg.valid === false ? 'No válido' : '—' }}
</p>
@if (r.eeg.error) {
<p role="alert">{{ r.eeg.error }}</p>
}
<h4>Comprobaciones</h4>
<table class="plain">
<thead>
<tr>
<th>Comprobación</th>
<th>Electrodo</th>
<th>Resultado</th>
<th>Rango</th>
<th>Anormal</th>
</tr>
</thead>
<tbody>
@for (check of r.eeg.checks; track $index) {
<tr>
<td>{{ check['code'] }}</td>
<td>{{ check['electrode'] ?? '—' }}</td>
<td>{{ check['value'] ?? check['text'] ?? '—' }} {{ check['unit'] }}</td>
<td>{{ check['low'] ?? '—' }} / {{ check['high'] ?? '—' }}</td>
<td>
{{
check['abnormal'] === true ? 'Sí' : check['abnormal'] === false ? 'No' : '—'
}}
</td>
</tr>
}
</tbody>
</table>
<h4>Métricas</h4>
<table class="plain">
<thead>
<tr>
<th>Métrica</th>
<th>Valor</th>
<th>z</th>
<th>p5 / mediana / p95</th>
<th>Fuera de rango</th>
</tr>
</thead>
<tbody>
@for (metric of r.eeg.metrics; track metric.code) {
<tr>
<th>{{ metric.code }}</th>
<td>{{ metric.value ?? '—' }}</td>
<td>{{ metric.score?.z ?? '—' }}</td>
<td>
{{ metric.score?.p5 ?? '—' }} / {{ metric.score?.median ?? '—' }} /
{{ metric.score?.p95 ?? '—' }}
</td>
<td>{{ metric.score?.outside ?? '—' }}</td>
</tr>
@for (site of metric.electrodes | keyvalue; track site.key) {
<tr>
<td>{{ site.key }}</td>
<td>{{ site.value.value ?? '—' }}</td>
<td>{{ site.value.score?.z ?? '—' }}</td>
<td>
{{ site.value.score?.p5 ?? '—' }} / {{ site.value.score?.median ?? '—' }} /
{{ site.value.score?.p95 ?? '—' }}
</td>
<td>{{ site.value.score?.outside ?? '—' }}</td>
</tr>
}
@for (site of metric.zones | keyvalue; track site.key) {
<tr>
<td>{{ site.key }}</td>
<td>{{ site.value.value ?? '—' }}</td>
<td>{{ site.value.score?.z ?? '—' }}</td>
<td>
{{ site.value.score?.p5 ?? '—' }} / {{ site.value.score?.median ?? '—' }} /
{{ site.value.score?.p95 ?? '—' }}
</td>
<td>{{ site.value.score?.outside ?? '—' }}</td>
</tr>
}
}
</tbody>
</table>
@if (r.ados2; as ados2) {
<h4>ADOS-2</h4>
<p>{{ ados2.classification }} · CSS {{ ados2.css ?? '—' }}</p>
<dl>
@for (item of ados2.items | keyvalue: unsorted; track item.key) {
<dt>{{ ados2.titles[item.key] ?? item.key }}</dt>
<dd>{{ item.value ?? '—' }}</dd>
}
</dl>
}
@if (r.warnings.length) {
<h4>Advertencias</h4>
<ul>
@for (warning of r.warnings; track $index) {
<li>{{ warning }}</li>
}
</ul>
}
}
@if (pdfError()) {
<p role="alert">{{ pdfError() }}</p>
}
<form method="dialog">
@if (result(); as r) {
<button mat-stroked-button type="button" [disabled]="exporting()" (click)="pdf(r)">
Guardar como PDF
</button>
}
<button mat-stroked-button>Cerrar</button>
</form>
</dialog>
</section>
`,
})
export class EegPreview {
private readonly api = inject(Api);
protected readonly busy = signal(false);
protected readonly error = signal('');
protected readonly runId = signal('');
protected readonly result = signal<PreviewResult | null>(null);
protected readonly unsorted = () => 0;
protected readonly exporting = signal(false);
protected readonly pdfError = signal('');
protected async pdf(result: PreviewResult): Promise<void> {
this.exporting.set(true);
this.pdfError.set('');
try {
await downloadPreviewPdf(result);
} catch (error) {
this.pdfError.set(errorText(error));
} finally {
this.exporting.set(false);
}
}
protected async start(edf?: File, ados2?: File, birth_date?: string): Promise<void> {
if (!edf) return;
this.busy.set(true);
this.error.set('');
this.result.set(null);
try {
const { run_id } = await this.api.invoke(startPreview, {
body: { edf, ados2, birth_date: birth_date || undefined },
});
this.runId.set(run_id);
} catch (error) {
this.error.set(errorText(error));
this.busy.set(false);
}
}
protected async finish(run: Run, dialog: HTMLDialogElement): Promise<void> {
try {
if (run.state !== 'succeeded') return;
const { result } = await this.api.invoke(previewStatus, { run_id: run.id });
this.result.set(result);
if (result) dialog.showModal();
else this.error.set('La vista previa no devolvió un resultado.');
} catch (error) {
this.error.set(errorText(error));
} finally {
this.busy.set(false);
}
}
}