integration layer; merge work on indicators from forks with existing ingestion pipeline
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
import { DecimalPipe } from '@angular/common';
|
||||
import { Component, OnInit, computed, inject, signal } from '@angular/core';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
import { MatCheckboxModule } from '@angular/material/checkbox';
|
||||
import { MatDialogModule, MatDialogRef } from '@angular/material/dialog';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
||||
import { MatRadioModule } from '@angular/material/radio';
|
||||
import { MatTableModule } from '@angular/material/table';
|
||||
import { ConflictStrategy } from '../../api/api.types';
|
||||
import { ImportFlow } from '../../domain/import-flow';
|
||||
import { ConnectWorkspaceStore } from '../../state/connect-workspace.store';
|
||||
import { FlowStatusChip } from '../shared/flow-status';
|
||||
|
||||
export type LoadDialogResult = 'explore' | undefined;
|
||||
|
||||
/**
|
||||
* "Cargar a la base": validates every flow, shows what will be written,
|
||||
* sets the batch options (conflict policy, merging a patient's data spread
|
||||
* across sources) and loads the batch in one request and one transaction.
|
||||
* Rows failing validation are skipped and reported, never written.
|
||||
*/
|
||||
@Component({
|
||||
selector: 'app-load-dialog',
|
||||
imports: [DecimalPipe, FlowStatusChip, MatButtonModule, MatCheckboxModule, MatDialogModule, MatIconModule, MatProgressBarModule, MatRadioModule, MatTableModule],
|
||||
templateUrl: './load-dialog.html',
|
||||
styleUrl: './load-dialog.scss',
|
||||
})
|
||||
export class LoadDialog implements OnInit {
|
||||
readonly store = inject(ConnectWorkspaceStore);
|
||||
private readonly dialogRef = inject<MatDialogRef<LoadDialog, LoadDialogResult>>(MatDialogRef);
|
||||
|
||||
readonly columns = ['flow', 'status', 'rows', 'valid', 'errors', 'outcome'];
|
||||
readonly validating = signal(true);
|
||||
/** Whether merging is even possible here; independent of the checkbox itself. */
|
||||
readonly mergeAvailable = this.store.mergeAvailable;
|
||||
/** With merging, rows incomplete on their own may be completed by another source. */
|
||||
readonly canLoad = computed(() => this.totals().valid > 0 || (this.store.mergeDuplicates() && this.totals().rows > 0));
|
||||
|
||||
/**
|
||||
* The flows the next batch would write. Reactive to the merge checkbox
|
||||
* while nothing has loaded yet, so ticking it reveals the flow it
|
||||
* completes; frozen the moment a batch starts, so the results table
|
||||
* keeps listing exactly what was sent.
|
||||
*/
|
||||
private readonly _frozenFlows = signal<ImportFlow[] | null>(null);
|
||||
readonly flows = computed(() => this._frozenFlows() ?? this.store.submittableFlows());
|
||||
readonly skipped = computed(() => this.store.flows().filter((flow) => !this.flows().includes(flow) && !flow.canSubmit));
|
||||
|
||||
readonly totals = computed(() =>
|
||||
this.flows().reduce(
|
||||
(acc, flow) => {
|
||||
const validation = flow.validation();
|
||||
acc.rows += validation?.total_rows ?? 0;
|
||||
acc.valid += validation?.valid_count ?? 0;
|
||||
acc.errors += validation?.error_count ?? 0;
|
||||
return acc;
|
||||
},
|
||||
{ rows: 0, valid: 0, errors: 0 },
|
||||
),
|
||||
);
|
||||
|
||||
readonly batch = this.store.lastBatch;
|
||||
readonly outcome = computed(() => {
|
||||
const batch = this.batch();
|
||||
if (!batch) return null;
|
||||
return batch.results.reduce(
|
||||
(acc, result) => ({
|
||||
created: acc.created + result.created,
|
||||
updated: acc.updated + result.updated,
|
||||
rejected: acc.rejected + result.rejected,
|
||||
merged: acc.merged + (result.merged ?? 0),
|
||||
warnings: acc.warnings + (result.warnings?.length ?? 0),
|
||||
}),
|
||||
{ created: 0, updated: 0, rejected: 0, merged: 0, warnings: 0 },
|
||||
);
|
||||
});
|
||||
|
||||
async ngOnInit(): Promise<void> {
|
||||
await Promise.all(this.flows().filter((flow) => !flow.validation()).map((flow) => this.store.validate(flow)));
|
||||
this.validating.set(false);
|
||||
}
|
||||
|
||||
setStrategy(strategy: ConflictStrategy): void {
|
||||
this.store.setOnConflict(strategy);
|
||||
}
|
||||
|
||||
/** Toggling may bring an until-now-skipped flow into `flows()`; preview its rows too. */
|
||||
toggleMerge(merge: boolean): void {
|
||||
this.store.setMergeDuplicates(merge);
|
||||
Promise.all(this.flows().filter((flow) => !flow.validation()).map((flow) => this.store.validate(flow)));
|
||||
}
|
||||
|
||||
load(): void {
|
||||
this._frozenFlows.set(this.store.submittableFlows());
|
||||
this.store.commit().subscribe();
|
||||
}
|
||||
|
||||
explore(): void {
|
||||
this.dialogRef.close('explore');
|
||||
}
|
||||
|
||||
resultOf(flow: ImportFlow) {
|
||||
return flow.result();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user