import { DatePipe, DecimalPipe, JsonPipe } from '@angular/common'; import { Component, inject, signal } from '@angular/core'; import { FormBuilder, ReactiveFormsModule } from '@angular/forms'; import { MatButtonModule } from '@angular/material/button'; import { MAT_DIALOG_DATA, MatDialog, MatDialogModule, MatDialogRef } from '@angular/material/dialog'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatIconModule } from '@angular/material/icon'; import { MatInputModule } from '@angular/material/input'; import { MatProgressBarModule } from '@angular/material/progress-bar'; import { MatTooltipModule } from '@angular/material/tooltip'; import { Observable } from 'rxjs'; import { apiErrorMessage } from '../../../connect/api/api.types'; import { GovernanceApi } from '../../api/governance.api'; import { ArtifactFile, ModelLineage, ModelVersion, PromotionCheck, RoutineKind } from '../../api/governance.types'; import { ModelStatusChip } from '../model-status-chip/model-status-chip'; import { NotebookRunDefaults, NotebookRunDialog } from '../notebook-run-dialog/notebook-run-dialog'; /** The routines training records as it runs (ML-01a), in the order they happen. */ export const ROUTINES: { kind: RoutineKind; label: string }[] = [ { kind: 'MUESTREO', label: 'Muestreo' }, { kind: 'DATOS_FALTANTES', label: 'Datos faltantes' }, { kind: 'METODO_ANALITICO', label: 'Método analítico' }, { kind: 'BALANCE_CLASES', label: 'Balance de clases' }, { kind: 'CONFIANZA_SALIDA', label: 'Confianza de la salida' }, ]; /** * One model version: what it was trained on (the kit's cohort build and its attrition), * what each routine did as it trained, the files its run stored, its validation * reports, and the checklist that must pass before it can serve predictions. * Closes with `true` when the version changed. */ @Component({ selector: 'app-model-version-dialog', imports: [ DatePipe, DecimalPipe, JsonPipe, MatButtonModule, MatDialogModule, MatFormFieldModule, MatIconModule, MatInputModule, MatProgressBarModule, MatTooltipModule, ModelStatusChip, ReactiveFormsModule, ], templateUrl: './model-version-dialog.html', styleUrl: './model-version-dialog.scss', }) export class ModelVersionDialog { private readonly api = inject(GovernanceApi); private readonly dialogRef = inject>(MatDialogRef); private readonly version = inject(MAT_DIALOG_DATA); private readonly dialog = inject(MatDialog); readonly routineKinds = ROUTINES; readonly lineage = signal(null); readonly busy = signal(false); readonly error = signal(null); private changed = false; readonly reports = inject(FormBuilder).nonNullable.group({ technical_validation_report_uri: [''], clinical_validation_report_uri: [''], interpretability_report_uri: [''], }); constructor() { this.load(); } private get domain(): string { return this.version.tags.domain!; } private get scenario(): string { return this.version.tags.scenario!; } get title(): string { const v = this.lineage()?.model_version ?? this.version; return `${v.name}:${v.version}`; } ready(checks: PromotionCheck[]): boolean { return checks.every((check) => check.passed); } metrics(version: ModelVersion): [string, unknown][] { return Object.entries(version.metrics ?? {}); } /** The run's files, with the saved model as one folder (it downloads as a zip). */ files(artifacts: ArtifactFile[]): { path: string; label: string; size: number | null }[] { const model = artifacts.filter((file) => file.path.startsWith('model/')); const rest = artifacts.filter((file) => !file.path.startsWith('model/')); const folder = model.length ? [{ path: 'model', label: 'model (modelo guardado, .zip)', size: null }] : []; return [...folder, ...rest.map((file) => ({ ...file, label: file.path }))]; } artifactUrl(path: string): string { return this.api.artifactUrl(this.domain, this.scenario, this.version.version, path); } tryInNotebook(): void { this.dialog.open(NotebookRunDialog, { data: { domain: this.domain as NotebookRunDefaults['domain'], scenario: this.scenario, version: this.version.version }, width: '640px', maxWidth: '96vw', autoFocus: false, }); } saveReports(): void { const values = this.reports.getRawValue(); this.run(this.api.attachReports(this.domain, this.scenario, this.version.version, { technical_validation_report_uri: values.technical_validation_report_uri.trim() || null, clinical_validation_report_uri: values.clinical_validation_report_uri.trim() || null, interpretability_report_uri: values.interpretability_report_uri.trim() || null, })); } promote(): void { this.run(this.api.promoteModelVersion(this.domain, this.scenario, this.version.version)); } retire(): void { this.run(this.api.retireModelVersion(this.domain, this.scenario, this.version.version)); } close(): void { this.dialogRef.close(this.changed); } private run(request: Observable): void { this.busy.set(true); this.error.set(null); request.subscribe({ next: () => { this.changed = true; this.load(); }, error: (error) => { this.busy.set(false); this.error.set(apiErrorMessage(error, 'La operación no se pudo completar.')); }, }); } private load(): void { this.busy.set(true); this.api.lineage(this.domain, this.scenario, this.version.version).subscribe({ next: (lineage) => { this.lineage.set(lineage); const tags = lineage.model_version.tags; this.reports.setValue({ technical_validation_report_uri: tags.technical_validation_report_uri ?? '', clinical_validation_report_uri: tags.clinical_validation_report_uri ?? '', interpretability_report_uri: tags.interpretability_report_uri ?? '', }); this.reports.markAsPristine(); this.busy.set(false); }, error: (error) => { this.busy.set(false); this.error.set(apiErrorMessage(error, 'No se pudo cargar la versión.')); }, }); } }