import { DOCUMENT } from '@angular/common'; import { Component, computed, effect, inject, model, resource, signal } from '@angular/core'; import { Api } from '../api/api'; import { executionHealth } from '../api/fn/execution/execution-health'; import { executionOptions } from '../api/fn/execution/execution-options'; import type { Execution as Choice } from '../api/models/execution'; import type { Health } from '../api/models/health'; @Component({ selector: 'app-execution', template: `

Ejecución

@if (options.hasValue()) { @if (choice().mode === 'remote') { @for (host of options.value().schedulers; track host) { } } } @if (health.hasValue()) { @let h = health.value();

{{ h.reachable ? 'Disponible' : 'No disponible' }} @if (h.workers != null) { · Trabajadores: {{ h.workers }} } @if (h.memory_per_worker != null) { · Memoria por trabajador: {{ h.memory_per_worker }} } @if (h.error) { · {{ h.error }} }

@if (h.dashboard_url && h.dashboard_ok !== false) { Panel de Dask } } @else if (health.error() || options.error()) {

No se pudo consultar la ejecución.

}
`, }) export class Execution { private readonly api = inject(Api); private readonly document = inject(DOCUMENT); private readonly visible = signal(!this.document.hidden); readonly execution = model(undefined); protected readonly options = resource({ loader: () => this.api.invoke(executionOptions) }); protected readonly mode = signal(undefined); protected readonly address = signal(undefined); protected readonly choice = computed(() => { const options = this.options.hasValue() ? this.options.value() : undefined; const mode = this.mode() ?? options?.default_mode; return { mode, scheduler: mode === 'remote' ? (this.address() ?? options?.schedulers[0] ?? '').trim() || null : null, }; }); protected readonly health = resource({ params: () => (this.visible() && this.choice().mode ? this.choice() : undefined), loader: async ({ params }) => { const health: Health & { memory_per_worker?: string | number } = await this.api.invoke( executionHealth, params, ); return health; }, }); constructor() { effect(() => { const choice = this.choice(); this.execution.set(choice.mode ? choice : undefined); }); effect((cleanup) => { const refresh = () => { if (!this.document.hidden) this.health.reload(); }; const visibilityChanged = () => this.visible.set(!this.document.hidden); const timer = setInterval(refresh, 30_000); this.document.addEventListener('visibilitychange', visibilityChanged); cleanup(() => { clearInterval(timer); this.document.removeEventListener('visibilitychange', visibilityChanged); }); }); } protected select(mode: NonNullable): void { this.mode.set(mode); if (mode === 'remote' && this.address() === undefined && this.options.hasValue()) { this.address.set(this.options.value().schedulers[0] ?? ''); } } }