CaMa development view on the survival tab; governance trains any scenario in the background

This commit is contained in:
rockdrigo
2026-11-21 03:28:13 -06:00
parent cc802cbb57
commit c5cbaaab13
13 changed files with 544 additions and 20 deletions
@@ -1,11 +1,14 @@
import { DatePipe } from '@angular/common';
import { Component, DestroyRef, OnInit, computed, inject, signal } from '@angular/core';
import { timer } from 'rxjs';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { MatButtonModule } from '@angular/material/button';
import { MatDialog } from '@angular/material/dialog';
import { MatButtonToggleModule } from '@angular/material/button-toggle';
import { MatChipsModule } from '@angular/material/chips';
import { MatIconModule } from '@angular/material/icon';
import { MatSelectModule } from '@angular/material/select';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatProgressBarModule } from '@angular/material/progress-bar';
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
import { MatTableModule } from '@angular/material/table';
@@ -15,18 +18,27 @@ import { AuthService } from '../../../auth/auth.service';
import { alcanzaTodos, dominioInicial, dominiosPermitidos } from '../../../auth/dominios';
import { apiErrorMessage } from '../../../connect/api/api.types';
import { GovernanceApi } from '../../api/governance.api';
import { ModelVersion } from '../../api/governance.types';
import { ModelVersion, TrainableScenario, TrainingJob } from '../../api/governance.types';
import { ModelStatusChip } from '../model-status-chip/model-status-chip';
import { ModelVersionDialog } from '../model-version-dialog/model-version-dialog';
/** The only scenario with a trainer today (ML-cin2_logistic, CaCu): `entrenar`'s own domain and scenario. */
const TRAINABLE = { domain: 'cacu', scenario: 'cin2_logistic' };
/** Milliseconds between asking for the state of a training that is still running. */
export const SONDEO_MS = 3000;
export const ESTADOS: Record<TrainingJob['estado'], string> = {
en_curso: 'En curso',
completado: 'Entrenado y promovido',
entrenado_sin_promover: 'Entrenado; faltan sus informes para promoverlo',
fallido: 'Falló',
interrumpido: 'Interrumpido',
};
/**
* Every registered model version with its status (desarrollo, candidata,
* en producción, retirada). The detail dialog holds the promotion
* checklist, lineage, artifacts and reports. The toolbar also offers
* training the one scenario with a trainer, over HTTP instead of a shell.
* checklist, lineage, artifacts and reports. The toolbar trains any scenario
* that has a trainer, in the background, and lists the latest trainings with
* their state while they run.
*/
@Component({
selector: 'app-model-versions-panel',
@@ -37,6 +49,8 @@ const TRAINABLE = { domain: 'cacu', scenario: 'cin2_logistic' };
MatChipsModule,
MatIconModule,
MatProgressBarModule,
MatSelectModule,
MatFormFieldModule,
MatSnackBarModule,
MatTableModule,
MatTooltipModule,
@@ -61,12 +75,28 @@ export class ModelVersionsPanel implements OnInit {
readonly versions = signal<ModelVersion[]>([]);
readonly loading = signal(false);
readonly loadError = signal<string | null>(null);
readonly training = signal(false);
readonly scenarios = signal<TrainableScenario[]>([]);
readonly selected = signal<string | null>(null);
readonly jobs = signal<TrainingJob[]>([]);
readonly starting = signal(false);
readonly estados = ESTADOS;
/** The scenarios of the domain being shown, and only of the account's domains. */
readonly offered = computed(() =>
this.scenarios().filter((s) => this.auth.puedeVer(s.domain) && (this.domain() === null || s.domain === this.domain())));
readonly running = computed(() => this.jobs().some((j) => j.estado === 'en_curso'));
readonly displayedColumns = ['version', 'scenario', 'environment', 'armado', 'status', 'created_at', 'acciones'];
ngOnInit(): void {
this.reload();
this.api.trainableScenarios().pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
next: ({ scenarios }) => this.scenarios.set(scenarios),
error: () => this.scenarios.set([]),
});
this.refreshJobs();
timer(SONDEO_MS, SONDEO_MS).pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => {
if (this.running()) this.refreshJobs();
});
}
setDomain(domain: string | null): void {
@@ -108,22 +138,41 @@ export class ModelVersionsPanel implements OnInit {
return Object.entries(version.metrics ?? {}).map(([key, value]) => ({ key, value }));
}
/** Asks for the latest trainings; when one that was running has ended, the versions are read again. */
refreshJobs(): void {
const before = this.running();
this.api.trainingJobs().pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
next: ({ jobs }) => {
this.jobs.set(jobs.slice(0, 8));
if (before && !this.running()) this.reload();
},
error: () => undefined,
});
}
train(): void {
this.training.set(true);
const key = this.selected() ?? (this.offered().length === 1 ? this.keyOf(this.offered()[0]) : null);
const scenario = this.offered().find((s) => this.keyOf(s) === key);
if (!scenario) return;
this.starting.set(true);
this.api
.trainModelVersion(TRAINABLE.domain, TRAINABLE.scenario)
.startTrainingJob(scenario.domain, scenario.scenario)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe({
next: () => {
this.training.set(false);
this.snackBar.open(`El modelo ${TRAINABLE.domain}/${TRAINABLE.scenario} se entrenó correctamente.`, 'OK', { duration: 3000 });
this.reload();
this.starting.set(false);
this.snackBar.open(`Entrenamiento de ${key} iniciado.`, 'OK', { duration: 3000 });
this.refreshJobs();
},
error: (error) => {
this.training.set(false);
this.snackBar.open(apiErrorMessage(error, 'No se pudo entrenar el modelo.'), 'OK', { duration: 5000 });
if (error?.status === 409) this.reload();
this.starting.set(false);
this.snackBar.open(apiErrorMessage(error, 'No se pudo iniciar el entrenamiento.'), 'OK', { duration: 5000 });
if (error?.status === 409) this.refreshJobs();
},
});
}
keyOf(s: { domain: string; scenario: string }): string {
return `${s.domain}/${s.scenario}`;
}
}