integration layer; merge work on indicators from forks with existing ingestion pipeline
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
<div class="ocr">
|
||||
<div class="ocr__toolbar">
|
||||
@if (jobs().length) {
|
||||
<mat-button-toggle-group
|
||||
class="ocr__jobs"
|
||||
hideSingleSelectionIndicator
|
||||
[value]="selectedJob()"
|
||||
(change)="selectJob($event.value)"
|
||||
aria-label="Tipo de extracción OCR"
|
||||
>
|
||||
@for (job of jobs(); track job.entity) {
|
||||
<mat-button-toggle [value]="job">{{ job.label }}</mat-button-toggle>
|
||||
}
|
||||
</mat-button-toggle-group>
|
||||
}
|
||||
|
||||
<span class="ocr__spacer"></span>
|
||||
|
||||
<button mat-stroked-button type="button" [disabled]="!selectedJob() || running()" (click)="runOcr()">
|
||||
<mat-icon aria-hidden="true">auto_fix_high</mat-icon>Ejecutar OCR automático
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<mat-button-toggle-group class="ocr__status" hideSingleSelectionIndicator [value]="status()" (change)="setStatus($event.value)" aria-label="Filtro de estatus">
|
||||
@for (option of statusOptions; track option.value) {
|
||||
<mat-button-toggle [value]="option.value">{{ option.label }}</mat-button-toggle>
|
||||
}
|
||||
</mat-button-toggle-group>
|
||||
|
||||
@if (loading() || running()) {
|
||||
<mat-progress-bar mode="indeterminate"></mat-progress-bar>
|
||||
}
|
||||
@if (loadError(); as error) {
|
||||
<p class="ocr__alert" role="alert"><mat-icon aria-hidden="true">error</mat-icon>{{ error }}</p>
|
||||
}
|
||||
|
||||
<table mat-table [dataSource]="rows()" class="ocr__table">
|
||||
<ng-container matColumnDef="pac_id_hospital">
|
||||
<th mat-header-cell *matHeaderCellDef>Paciente</th>
|
||||
<td mat-cell *matCellDef="let row">{{ row.pac_id_hospital ?? '—' }}</td>
|
||||
</ng-container>
|
||||
|
||||
<ng-container matColumnDef="values">
|
||||
<th mat-header-cell *matHeaderCellDef>Valores extraídos</th>
|
||||
<td mat-cell *matCellDef="let row">
|
||||
<mat-chip-set>
|
||||
@for (entry of rowValueEntries(row); track entry.field) {
|
||||
<mat-chip>{{ entry.field }}: {{ entry.value }}</mat-chip>
|
||||
}
|
||||
</mat-chip-set>
|
||||
</td>
|
||||
</ng-container>
|
||||
|
||||
<ng-container matColumnDef="confianza">
|
||||
<th mat-header-cell *matHeaderCellDef>Confianza</th>
|
||||
<td mat-cell *matCellDef="let row">{{ row.confianza === null ? '—' : (row.confianza | number: '1.0-2') }}</td>
|
||||
</ng-container>
|
||||
|
||||
<ng-container matColumnDef="estatus">
|
||||
<th mat-header-cell *matHeaderCellDef>Estatus</th>
|
||||
<td mat-cell *matCellDef="let row">
|
||||
<span class="ocr__status-chip" [class]="'ocr__status-chip--' + (row.estatus ?? 'pendiente').toLowerCase()">
|
||||
{{ row.estatus ?? 'PENDIENTE' }}
|
||||
</span>
|
||||
@if (row.requiere_revision) {
|
||||
<mat-icon aria-hidden="true" matTooltip="Requiere revisión manual" class="ocr__review-flag">flag</mat-icon>
|
||||
}
|
||||
</td>
|
||||
</ng-container>
|
||||
|
||||
<ng-container matColumnDef="acciones">
|
||||
<th mat-header-cell *matHeaderCellDef></th>
|
||||
<td mat-cell *matCellDef="let row">
|
||||
<button mat-button type="button" [disabled]="correctingId() === row.id" (click)="correct(row)">
|
||||
<mat-icon aria-hidden="true">crop</mat-icon>Corregir
|
||||
</button>
|
||||
</td>
|
||||
</ng-container>
|
||||
|
||||
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
|
||||
<tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
|
||||
</table>
|
||||
|
||||
@if (!loading() && rows().length === 0) {
|
||||
<p class="ocr__empty"><mat-icon aria-hidden="true">task_alt</mat-icon>No hay filas en este filtro.</p>
|
||||
}
|
||||
|
||||
<p class="ocr__total">{{ total() }} fila(s) en total.</p>
|
||||
</div>
|
||||
@@ -0,0 +1,69 @@
|
||||
.ocr {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--space-3);
|
||||
}
|
||||
|
||||
.ocr__toolbar {
|
||||
align-items: center;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: var(--space-2);
|
||||
}
|
||||
|
||||
.ocr__spacer {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.ocr__status {
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.ocr__alert {
|
||||
align-items: center;
|
||||
background: var(--mat-sys-error-container);
|
||||
color: var(--mat-sys-on-error-container);
|
||||
display: flex;
|
||||
gap: var(--space-2);
|
||||
margin: 0;
|
||||
padding: var(--space-2) var(--space-3);
|
||||
}
|
||||
|
||||
.ocr__table {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.ocr__status-chip {
|
||||
background: var(--mat-sys-surface-variant);
|
||||
border-radius: 999px;
|
||||
color: var(--mat-sys-on-surface-variant);
|
||||
font: var(--mat-sys-label-small);
|
||||
padding: 2px 10px;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.ocr__status-chip--realizado {
|
||||
background: #d8f0da;
|
||||
color: #1e5c25;
|
||||
}
|
||||
|
||||
.ocr__status-chip--fallido {
|
||||
background: var(--mat-sys-error-container);
|
||||
color: var(--mat-sys-on-error-container);
|
||||
}
|
||||
|
||||
.ocr__review-flag {
|
||||
color: #b8860b;
|
||||
margin-left: var(--space-1);
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.ocr__empty,
|
||||
.ocr__total {
|
||||
align-items: center;
|
||||
color: var(--mat-sys-on-surface-variant);
|
||||
display: flex;
|
||||
font: var(--mat-sys-body-small);
|
||||
gap: var(--space-2);
|
||||
margin: 0;
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
import { DecimalPipe } 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 { MatButtonToggleModule } from '@angular/material/button-toggle';
|
||||
import { MatChipsModule } from '@angular/material/chips';
|
||||
import { MatDialog } from '@angular/material/dialog';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
||||
import { MatTableModule } from '@angular/material/table';
|
||||
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
|
||||
import { MatTooltipModule } from '@angular/material/tooltip';
|
||||
import { apiErrorMessage } from '../../../connect/api/api.types';
|
||||
import { GovernanceApi } from '../../api/governance.api';
|
||||
import { OcrJob, OcrRow, OcrRowStatus } from '../../api/governance.types';
|
||||
import {
|
||||
OcrCorrectionDialog,
|
||||
OcrCorrectionDialogData,
|
||||
OcrCorrectionResult,
|
||||
} from '../ocr-correction-dialog/ocr-correction-dialog';
|
||||
|
||||
const STATUS_OPTIONS: { value: OcrRowStatus; label: string }[] = [
|
||||
{ value: 'review', label: 'Requieren revisión' },
|
||||
{ value: 'pending', label: 'Pendientes' },
|
||||
{ value: 'failed', label: 'Fallidas' },
|
||||
{ value: 'done', label: 'Completadas' },
|
||||
{ value: 'all', label: 'Todas' },
|
||||
];
|
||||
|
||||
/**
|
||||
* Review queue for OCR-derived fields. The toggle group picks the OCR job,
|
||||
* the run button starts the automatic batch (rows already done are
|
||||
* skipped), and "Corregir" opens `OcrCorrectionDialog` for one row.
|
||||
*/
|
||||
@Component({
|
||||
selector: 'app-ocr-review-panel',
|
||||
imports: [
|
||||
DecimalPipe,
|
||||
MatButtonModule,
|
||||
MatButtonToggleModule,
|
||||
MatChipsModule,
|
||||
MatIconModule,
|
||||
MatProgressBarModule,
|
||||
MatSnackBarModule,
|
||||
MatTableModule,
|
||||
MatTooltipModule,
|
||||
],
|
||||
templateUrl: './ocr-review-panel.html',
|
||||
styleUrl: './ocr-review-panel.scss',
|
||||
})
|
||||
export class OcrReviewPanel implements OnInit {
|
||||
private readonly api = inject(GovernanceApi);
|
||||
private readonly dialog = inject(MatDialog);
|
||||
private readonly snackBar = inject(MatSnackBar);
|
||||
private readonly destroyRef = inject(DestroyRef);
|
||||
|
||||
readonly statusOptions = STATUS_OPTIONS;
|
||||
readonly jobs = signal<OcrJob[]>([]);
|
||||
readonly selectedJob = signal<OcrJob | null>(null);
|
||||
readonly status = signal<OcrRowStatus>('review');
|
||||
readonly rows = signal<OcrRow[]>([]);
|
||||
readonly total = signal(0);
|
||||
readonly loading = signal(false);
|
||||
readonly running = signal(false);
|
||||
readonly correctingId = signal<number | null>(null);
|
||||
readonly loadError = signal<string | null>(null);
|
||||
|
||||
/** The same for every job: each job's result fields render as chips in the "values" column. */
|
||||
readonly displayedColumns = ['pac_id_hospital', 'values', 'confianza', 'estatus', 'acciones'];
|
||||
|
||||
rowValueEntries(row: OcrRow): { field: string; value: string }[] {
|
||||
return Object.entries(row.values).map(([field, value]) => ({ field, value: this.valueOf(row, field) }));
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.api
|
||||
.listOcrJobs()
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe({
|
||||
next: ({ jobs }) => {
|
||||
this.jobs.set(jobs);
|
||||
if (jobs.length) this.selectJob(jobs[0]);
|
||||
},
|
||||
error: (error) => this.loadError.set(apiErrorMessage(error, 'No se pudieron cargar los tipos de extracción OCR.')),
|
||||
});
|
||||
}
|
||||
|
||||
selectJob(job: OcrJob): void {
|
||||
this.selectedJob.set(job);
|
||||
this.reload();
|
||||
}
|
||||
|
||||
setStatus(status: OcrRowStatus): void {
|
||||
this.status.set(status);
|
||||
this.reload();
|
||||
}
|
||||
|
||||
reload(): void {
|
||||
const job = this.selectedJob();
|
||||
if (!job) return;
|
||||
this.loading.set(true);
|
||||
this.loadError.set(null);
|
||||
this.api.listOcrRows(job.domain, job.entity, this.status()).subscribe({
|
||||
next: ({ rows, total }) => {
|
||||
this.loading.set(false);
|
||||
this.rows.set(rows);
|
||||
this.total.set(total);
|
||||
},
|
||||
error: (error) => {
|
||||
this.loading.set(false);
|
||||
this.loadError.set(apiErrorMessage(error, 'No se pudieron cargar las filas.'));
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
runOcr(): void {
|
||||
const job = this.selectedJob();
|
||||
if (!job) return;
|
||||
this.running.set(true);
|
||||
this.api.runOcr(job.domain, job.entity).subscribe({
|
||||
next: (summary) => {
|
||||
this.running.set(false);
|
||||
this.snackBar.open(
|
||||
`OCR: ${summary.processed} leída(s), ${summary.needs_review} por revisar, ${summary.failed} fallida(s), ${summary.skipped_already_done} ya hechas.`,
|
||||
'OK',
|
||||
{ duration: 5000 },
|
||||
);
|
||||
this.reload();
|
||||
},
|
||||
error: (error) => {
|
||||
this.running.set(false);
|
||||
const message = apiErrorMessage(error, 'No se pudo correr la extracción.');
|
||||
this.snackBar.open(message, 'OK', { duration: 5000 });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
correct(row: OcrRow): void {
|
||||
const job = this.selectedJob();
|
||||
if (!job) return;
|
||||
this.correctingId.set(row.id);
|
||||
this.api.getOcrBlob(job.domain, job.entity, row.id).subscribe({
|
||||
next: (blob) => {
|
||||
this.correctingId.set(null);
|
||||
const file = new File([blob], `${job.entity}-${row.id}.pdf`, { type: 'application/pdf' });
|
||||
this.dialog
|
||||
.open<OcrCorrectionDialog, OcrCorrectionDialogData, OcrCorrectionResult>(OcrCorrectionDialog, {
|
||||
width: '1200px',
|
||||
maxWidth: '98vw',
|
||||
autoFocus: false,
|
||||
disableClose: true,
|
||||
data: {
|
||||
title: `Corregir · ${job.label} · ${row.pac_id_hospital ?? 'fila ' + row.id}`,
|
||||
file,
|
||||
fields: job.result_fields.map((name) => ({ name, label: name })),
|
||||
currentValues: row.values,
|
||||
},
|
||||
})
|
||||
.afterClosed()
|
||||
.subscribe((result) => {
|
||||
if (!result || Object.keys(result.values).length === 0) return;
|
||||
this.api.correctOcrRow(job.domain, job.entity, row.id, result.values).subscribe({
|
||||
next: () => {
|
||||
this.snackBar.open('Corrección guardada.', 'OK', { duration: 3000 });
|
||||
this.reload();
|
||||
},
|
||||
error: (error) => this.snackBar.open(apiErrorMessage(error, 'No se pudo guardar la corrección.'), 'OK', { duration: 5000 }),
|
||||
});
|
||||
});
|
||||
},
|
||||
error: (error) => {
|
||||
this.correctingId.set(null);
|
||||
this.snackBar.open(apiErrorMessage(error, 'No se pudo cargar el PDF de esta fila.'), 'OK', { duration: 5000 });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
valueOf(row: OcrRow, field: string): string {
|
||||
const value = row.values[field];
|
||||
return value === null || value === undefined || value === '' ? '—' : String(value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user