87 lines
3.4 KiB
TypeScript
87 lines
3.4 KiB
TypeScript
import { DatePipe, DecimalPipe, KeyValuePipe } from '@angular/common';
|
|
import { Component, DestroyRef, OnInit, inject, signal } from '@angular/core';
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatToolbarModule } from '@angular/material/toolbar';
|
|
import { EMPTY, catchError, forkJoin, interval, startWith, switchMap } from 'rxjs';
|
|
import { Domain, apiErrorMessage } from '../connect/api/api.types';
|
|
import { DOMAINS } from '../connect/state/connect-workspace.store';
|
|
import { ImportAttempt, RecomputeStatus, ReportsApi } from './reportes.api';
|
|
|
|
const STATUS_LABELS: Record<string, string> = {
|
|
sin_correr: 'Pendiente de cálculo', en_curso: 'En proceso de cálculo', ok: 'Actualizado', error: 'Error en el cálculo',
|
|
};
|
|
|
|
const OUTCOME_LABELS: Record<string, string> = {
|
|
completada: 'Completada', solicitud_invalida: 'Solicitud inválida',
|
|
rechazada_por_la_base: 'Rechazada por la base', fallida: 'Fallida',
|
|
};
|
|
|
|
/** The import log page (`/bitacora`): import attempts in aggregate and the indicator status per domain. */
|
|
@Component({
|
|
selector: 'app-reportes-page',
|
|
imports: [DatePipe, DecimalPipe, KeyValuePipe, MatButtonModule, MatIconModule, MatToolbarModule],
|
|
templateUrl: './reportes-page.html',
|
|
styleUrl: './reportes-page.scss',
|
|
})
|
|
export class ReportsPage implements OnInit {
|
|
private readonly api = inject(ReportsApi);
|
|
private readonly destroyRef = inject(DestroyRef);
|
|
|
|
readonly domains = DOMAINS;
|
|
readonly statuses = signal<Record<Domain, RecomputeStatus> | null>(null);
|
|
readonly attempts = signal<ImportAttempt[]>([]);
|
|
readonly expanded = signal<string | null>(null);
|
|
readonly error = signal<string | null>(null);
|
|
|
|
ngOnInit(): void {
|
|
interval(15000)
|
|
.pipe(
|
|
startWith(0),
|
|
// Errors are handled per poll so one failed request doesn't stop the refresh.
|
|
switchMap(() =>
|
|
forkJoin({ statuses: this.api.indicatorStatus(), log: this.api.importLog() }).pipe(
|
|
catchError((err) => {
|
|
this.error.set(apiErrorMessage(err, 'No se pudo leer la bitácora.'));
|
|
return EMPTY;
|
|
}),
|
|
),
|
|
),
|
|
takeUntilDestroyed(this.destroyRef),
|
|
)
|
|
.subscribe(({ statuses, log }) => {
|
|
this.statuses.set(statuses);
|
|
this.attempts.set(log.intentos);
|
|
this.error.set(null);
|
|
});
|
|
}
|
|
|
|
statusLabel(status: RecomputeStatus): string {
|
|
return status.pendiente && status.estado !== 'en_curso' ? 'En espera de cálculo' : STATUS_LABELS[status.estado] ?? status.estado;
|
|
}
|
|
|
|
outcomeLabel(outcome: string): string {
|
|
return OUTCOME_LABELS[outcome] ?? outcome;
|
|
}
|
|
|
|
recompute(domain: Domain): void {
|
|
this.api.recompute(domain).subscribe({
|
|
next: () => this.statuses.update((s) => (s ? { ...s, [domain]: { ...s[domain], pendiente: true } } : s)),
|
|
error: (err) => this.error.set(apiErrorMessage(err, 'No se pudo solicitar el recálculo.')),
|
|
});
|
|
}
|
|
|
|
toggle(id: string): void {
|
|
this.expanded.update((current) => (current === id ? null : id));
|
|
}
|
|
|
|
fieldsWithProblems(attempt: ImportAttempt) {
|
|
return attempt.fuentes.flatMap((source) =>
|
|
Object.entries(source.campos)
|
|
.filter(([, field]) => field.errores || field.vacios)
|
|
.map(([name, field]) => ({ target: source.destino, name, field })),
|
|
);
|
|
}
|
|
}
|