179 lines
7.1 KiB
TypeScript
179 lines
7.1 KiB
TypeScript
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';
|
|
import { MatTooltipModule } from '@angular/material/tooltip';
|
|
import { DOMAINS } from '../../../connect/state/connect-workspace.store';
|
|
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, TrainableScenario, TrainingJob } from '../../api/governance.types';
|
|
import { ModelStatusChip } from '../model-status-chip/model-status-chip';
|
|
import { ModelVersionDialog } from '../model-version-dialog/model-version-dialog';
|
|
|
|
/** 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 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',
|
|
imports: [
|
|
DatePipe,
|
|
MatButtonModule,
|
|
MatButtonToggleModule,
|
|
MatChipsModule,
|
|
MatIconModule,
|
|
MatProgressBarModule,
|
|
MatSelectModule,
|
|
MatFormFieldModule,
|
|
MatSnackBarModule,
|
|
MatTableModule,
|
|
MatTooltipModule,
|
|
ModelStatusChip,
|
|
],
|
|
templateUrl: './model-versions-panel.html',
|
|
styleUrl: './model-versions-panel.scss',
|
|
})
|
|
export class ModelVersionsPanel implements OnInit {
|
|
private readonly api = inject(GovernanceApi);
|
|
private readonly auth = inject(AuthService);
|
|
private readonly snackBar = inject(MatSnackBar);
|
|
private readonly dialog = inject(MatDialog);
|
|
private readonly destroyRef = inject(DestroyRef);
|
|
|
|
/** Only the account's domains; «Todos» only when it reaches all of them. */
|
|
readonly domains = computed<{ id: string | null; label: string }[]>(() => [
|
|
...(alcanzaTodos(this.auth, DOMAINS) ? [{ id: null, label: 'Todos' }] : []),
|
|
...dominiosPermitidos(this.auth, DOMAINS).map((d) => ({ id: d.id as string | null, label: d.label })),
|
|
]);
|
|
readonly domain = signal<string | null>(dominioInicial(this.auth, DOMAINS));
|
|
readonly versions = signal<ModelVersion[]>([]);
|
|
readonly loading = signal(false);
|
|
readonly loadError = signal<string | null>(null);
|
|
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 {
|
|
if (domain !== null && !this.auth.puedeVer(domain)) return;
|
|
this.domain.set(domain);
|
|
this.reload();
|
|
}
|
|
|
|
reload(): void {
|
|
this.loading.set(true);
|
|
this.loadError.set(null);
|
|
this.api
|
|
.listModelVersions(this.domain() ? { domain: this.domain()! } : {})
|
|
.pipe(takeUntilDestroyed(this.destroyRef))
|
|
.subscribe({
|
|
next: ({ model_versions }) => {
|
|
this.loading.set(false);
|
|
this.versions.set(model_versions);
|
|
},
|
|
error: (error) => {
|
|
this.loading.set(false);
|
|
this.loadError.set(apiErrorMessage(error, 'No se pudieron cargar las versiones de modelo.'));
|
|
},
|
|
});
|
|
}
|
|
|
|
open(version: ModelVersion): void {
|
|
this.dialog
|
|
.open<ModelVersionDialog, ModelVersion, boolean>(ModelVersionDialog, { data: version, width: '880px', maxWidth: '96vw', autoFocus: false })
|
|
.afterClosed()
|
|
.subscribe((changed) => {
|
|
if (!changed) return;
|
|
this.snackBar.open(`${version.name}:${version.version} actualizada.`, 'OK', { duration: 3000 });
|
|
this.reload();
|
|
});
|
|
}
|
|
|
|
metrics(version: ModelVersion): { key: string; value: unknown }[] {
|
|
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 {
|
|
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
|
|
.startTrainingJob(scenario.domain, scenario.scenario)
|
|
.pipe(takeUntilDestroyed(this.destroyRef))
|
|
.subscribe({
|
|
next: () => {
|
|
this.starting.set(false);
|
|
this.snackBar.open(`Entrenamiento de ${key} iniciado.`, 'OK', { duration: 3000 });
|
|
this.refreshJobs();
|
|
},
|
|
error: (error) => {
|
|
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}`;
|
|
}
|
|
}
|