import { Component, computed, inject, signal } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { MatButtonModule } from '@angular/material/button'; import { MatButtonToggleModule } from '@angular/material/button-toggle'; import { MAT_DIALOG_DATA, 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 { MatSelectModule } from '@angular/material/select'; import { Domain, apiErrorMessage } from '../../../connect/api/api.types'; import { DOMAINS } from '../../../connect/state/connect-workspace.store'; import { GovernanceApi } from '../../api/governance.api'; import { CohortBuildFile, ExperimentDetail, ModelVersion, NotebookTemplate } from '../../api/governance.types'; /** What the dialog starts with: a version's dialog passes its own domain, scenario and version. */ export interface NotebookRunDefaults { domain?: Domain; scenario?: string; version?: string; } const SCENARIO = /^[\w-]{1,100}$/; /** Where the notebook comes from: one kept in the code (a template) or an upload. */ export type NotebookSource = 'plantilla' | 'archivo'; /** * Runs a Jupyter notebook on a cohort build's file, optionally with a registered * version to try: one kept in the code (`models///notebooks/`, * for the scenarios it serves) or an upload. Closes with the run once it has started. */ @Component({ selector: 'app-notebook-run-dialog', imports: [ FormsModule, MatButtonModule, MatButtonToggleModule, MatDialogModule, MatFormFieldModule, MatIconModule, MatInputModule, MatProgressBarModule, MatSelectModule, ], templateUrl: './notebook-run-dialog.html', styleUrl: './notebook-run-dialog.scss', }) export class NotebookRunDialog { private readonly api = inject(GovernanceApi); private readonly dialogRef = inject>(MatDialogRef); private readonly defaults = inject(MAT_DIALOG_DATA, { optional: true }) ?? {}; readonly domains = DOMAINS; readonly domain = signal(this.defaults.domain ?? 'cacu'); readonly scenario = signal(this.defaults.scenario ?? ''); readonly version = signal(this.defaults.version ?? ''); readonly source = signal('plantilla'); readonly file = signal(null); readonly templates = signal([]); readonly template = signal(null); readonly builds = signal([]); readonly build = signal(null); readonly versions = signal([]); readonly busy = signal(false); readonly error = signal(null); readonly scenarioValid = computed(() => SCENARIO.test(this.scenario())); /** The template serves the scenario typed, or the upload is chosen. */ readonly notebookReady = computed(() => { if (this.source() === 'archivo') return !!this.file(); const template = this.template(); return !!template && template.scenarios.includes(this.scenario()); }); readonly ready = computed(() => this.notebookReady() && !!this.build() && this.scenarioValid() && !this.busy()); constructor() { this.loadTemplates(); this.loadBuilds(); this.loadVersions(); } templateLabel(template: NotebookTemplate): string { return `${template.scenario}/${template.name} · ${template.title}`; } sameTemplate(a: NotebookTemplate | null, b: NotebookTemplate | null): boolean { return !!a && !!b && a.id === b.id; } /** Choosing a template puts its scenario when the one typed isn't one it serves. */ setTemplate(template: NotebookTemplate | null): void { this.template.set(template); if (template && !template.scenarios.includes(this.scenario())) this.setScenario(template.scenario); } buildLabel(build: CohortBuildFile): string { const members = build.n_members ? ` · ${build.n_members} miembros` : ''; const asOf = build.datos_as_of ? ` · datos al ${build.datos_as_of}` : ''; return `${build.url} · ${build.source} ${build.build}${members}${asOf}`; } sameBuild(a: CohortBuildFile | null, b: CohortBuildFile | null): boolean { return !!a && !!b && a.url === b.url && a.source === b.source && a.build === b.build; } setDomain(domain: Domain): void { this.domain.set(domain); this.version.set(''); this.loadTemplates(); this.loadBuilds(); this.loadVersions(); } setScenario(scenario: string): void { this.scenario.set(scenario.trim()); this.version.set(''); this.loadVersions(); } pick(event: Event): void { const input = event.target as HTMLInputElement; this.file.set(input.files?.[0] ?? null); input.value = ''; } run(): void { const build = this.build(); if (!build || !this.ready()) return; const notebook = this.source() === 'plantilla' ? { template: this.template()!.id } : { file: this.file() }; this.busy.set(true); this.error.set(null); this.api .runExperiment({ ...notebook, domain: this.domain(), scenario: this.scenario(), build, version: this.version() || null }) .subscribe({ next: (run) => this.dialogRef.close(run), error: (error) => { this.busy.set(false); this.error.set(apiErrorMessage(error, 'No se pudo iniciar el notebook.')); }, }); } private loadTemplates(): void { this.templates.set([]); this.template.set(null); this.api.notebookTemplates(this.domain()).subscribe({ next: ({ templates }) => { this.templates.set(templates); if (!templates.length) this.source.set('archivo'); }, error: () => this.templates.set([]), }); } private loadBuilds(): void { this.builds.set([]); this.build.set(null); this.api.cohortFiles(this.domain()).subscribe({ next: ({ builds }) => { this.builds.set(builds); this.build.set(builds[0] ?? null); }, error: (error) => this.error.set(apiErrorMessage(error, 'No se pudieron cargar las cohortes exportadas.')), }); } private loadVersions(): void { this.versions.set([]); if (!this.scenarioValid()) return; this.api.listModelVersions({ domain: this.domain(), scenario: this.scenario() }).subscribe({ next: ({ model_versions }) => this.versions.set(model_versions), error: () => this.versions.set([]), }); } }