import { Component, inject, OnInit, signal } from '@angular/core'; import { MatProgressBarModule } from '@angular/material/progress-bar'; import { forkJoin, map, of, switchMap } from 'rxjs'; import { PlotlyChart } from '../../shared/plotly/plotly-chart'; import { PlotlyFigure } from './indicadores.models'; import { compositionFigure, durationFigure, proportionFigure } from './measure-report'; import { KpiEntry, MeasureReportService } from './measure-report.service'; const PERIODOS = ['2025-S1', '2025-S2']; /** Every measure the WebApi answers on request, drawn from its FHIR `MeasureReport`s: one contract for all. */ @Component({ selector: 'app-kpis-page', imports: [MatProgressBarModule, PlotlyChart], template: `

Indicadores

@if (status() === 'loading') { } @if (status() === 'error') {

No se pudieron cargar los indicadores.

} @for (item of figures(); track item.id) {
} `, }) export class KpisPage implements OnInit { readonly status = signal<'loading' | 'ready' | 'error'>('loading'); readonly figures = signal<{ id: string; figure: PlotlyFigure }[]>([]); private readonly service = inject(MeasureReportService); ngOnInit() { this.service.list('cacu').pipe( switchMap(kpis => kpis.length === 0 ? of([]) : forkJoin(kpis.map(kpi => this.figureOf(kpi)))), ).subscribe({ next: figures => { this.figures.set(figures); this.status.set('ready'); }, error: () => this.status.set('error'), }); } /** Drawn by its scoring, from the one contract: nothing here is per measure. */ private figureOf(kpi: KpiEntry) { const drawn = (figure: PlotlyFigure) => ({ id: kpi.id, figure }); switch (kpi.scoring) { case 'composition': return this.service.composition('cacu', kpi.id, PERIODOS).pipe(map(s => drawn(compositionFigure(s, kpi.id)))); case 'continuous-variable': return this.service.duration('cacu', kpi.id, PERIODOS).pipe(map(s => drawn(durationFigure(s, kpi.id)))); default: return this.service.series('cacu', kpi.id, PERIODOS).pipe(map(s => drawn(proportionFigure(s, kpi.id)))); } } }