Files
WebApp/src/app/governance/components/model-versions-panel/model-versions-panel.ts
T
Erick ivan 7f17681409 feat(auth): login screen and domain-scoped navigation
Session login against /auth/*, with a guard on every route and an interceptor that returns to the login on 401. The header, the import workspace and both governance panels only offer the account's own domains (auth/dominios.ts), and a batch opens on the account's database.

The sign-in screen follows the app's palette: logo above the title, the action button in the shared tokens, and no register or password-recovery rows.
2026-11-19 09:36:54 -06:00

130 lines
5.1 KiB
TypeScript

import { DatePipe } from '@angular/common';
import { Component, DestroyRef, OnInit, computed, inject, signal } from '@angular/core';
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 { 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 } 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' };
/**
* 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.
*/
@Component({
selector: 'app-model-versions-panel',
imports: [
DatePipe,
MatButtonModule,
MatButtonToggleModule,
MatChipsModule,
MatIconModule,
MatProgressBarModule,
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 training = signal(false);
readonly displayedColumns = ['version', 'scenario', 'environment', 'armado', 'status', 'created_at', 'acciones'];
ngOnInit(): void {
this.reload();
}
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 }));
}
train(): void {
this.training.set(true);
this.api
.trainModelVersion(TRAINABLE.domain, TRAINABLE.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();
},
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();
},
});
}
}