diff --git a/webapp/src/app/modules/jobs/jobs.component.html b/webapp/src/app/modules/jobs/jobs.component.html index dd974e7b..4909b602 100644 --- a/webapp/src/app/modules/jobs/jobs.component.html +++ b/webapp/src/app/modules/jobs/jobs.component.html @@ -7,6 +7,14 @@ ausführen oder per Cron-Zeitplan automatisieren.

+ diff --git a/webapp/src/app/modules/jobs/jobs.component.ts b/webapp/src/app/modules/jobs/jobs.component.ts index 3c0058cf..9a97abfb 100644 --- a/webapp/src/app/modules/jobs/jobs.component.ts +++ b/webapp/src/app/modules/jobs/jobs.component.ts @@ -41,6 +41,7 @@ import { JobLogsDialogComponent, JobLogsDialogData, } from './job-logs-dialog/job-logs-dialog.component'; +import { TaskCreateDialogComponent } from './task-create-dialog/task-create-dialog.component'; @Component({ selector: 'app-jobs', @@ -255,6 +256,29 @@ export class JobsComponent implements OnInit { ); } + protected openCreateDialog(): void { + const dialogRef = this.dialog.open< + TaskCreateDialogComponent, + void, + BackgroundJobTask | undefined + >(TaskCreateDialogComponent, { + width: '560px', + maxWidth: 'calc(100vw - 32px)', + }); + + dialogRef.afterClosed().subscribe((task) => { + if (!task) return; + + this.tasks.update((tasks) => [...tasks, task]); + this.snackBar.open(`„${task.name}“ wurde angelegt.`, 'OK', { + duration: 3000, + horizontalPosition: 'center', + verticalPosition: 'bottom', + }); + this.refreshTasks(); + }); + } + protected openScheduleDialog(task: BackgroundJobTask): void { const dialogRef = this.dialog.open< TaskScheduleDialogComponent, diff --git a/webapp/src/app/modules/jobs/task-create-dialog/task-create-dialog.component.html b/webapp/src/app/modules/jobs/task-create-dialog/task-create-dialog.component.html new file mode 100644 index 00000000..b58885f6 --- /dev/null +++ b/webapp/src/app/modules/jobs/task-create-dialog/task-create-dialog.component.html @@ -0,0 +1,74 @@ +

Neuer Job

+ + +
+ + Name + + @if (form.controls.name.invalid) { + Bitte einen Namen eingeben. + } + + + + Befehl (optional) + + Der Befehl, den der Container ausführt. Leer = Standard-Entrypoint des + Images. + + + + Image (optional) + + Docker-Image (Standard: + docker.nes.uni-due.de/inse/background-jobs:latest). + + + + Beschreibung + + + + + Ziel-Index (optional) + + Elasticsearch-Index, in den der Job schreibt (Anzeige). + + + + Umgebungsvariablen (optional) + + Eine Variable pro Zeile im Format KEY=value. + @if (form.controls.env.hasError('invalidEnv')) { + Ungültige Zeile — Format: KEY=value. + } + +
+ + @if (serverError()) { +

{{ serverError() }}

+ } +
+ + + + + diff --git a/webapp/src/app/modules/jobs/task-create-dialog/task-create-dialog.component.scss b/webapp/src/app/modules/jobs/task-create-dialog/task-create-dialog.component.scss new file mode 100644 index 00000000..dc8f0bda --- /dev/null +++ b/webapp/src/app/modules/jobs/task-create-dialog/task-create-dialog.component.scss @@ -0,0 +1,32 @@ +.create-form { + display: flex; + flex-direction: column; + gap: 12px; + min-width: 400px; +} + +.commands-loading { + display: flex; + align-items: center; + gap: 8px; + color: #64748b; + font-size: 14px; +} + +.field-hint { + margin: -6px 0 0; + color: #64748b; + font-size: 12px; +} + +.server-error { + margin: 8px 0 0; + color: #b3261e; + font-size: 14px; +} + +@media (max-width: 640px) { + .create-form { + min-width: 0; + } +} diff --git a/webapp/src/app/modules/jobs/task-create-dialog/task-create-dialog.component.ts b/webapp/src/app/modules/jobs/task-create-dialog/task-create-dialog.component.ts new file mode 100644 index 00000000..a48d43ba --- /dev/null +++ b/webapp/src/app/modules/jobs/task-create-dialog/task-create-dialog.component.ts @@ -0,0 +1,112 @@ +import { CommonModule } from '@angular/common'; +import { Component, inject, signal } from '@angular/core'; +import { + FormControl, + FormGroup, + ReactiveFormsModule, + Validators, +} from '@angular/forms'; +import { MatButtonModule } from '@angular/material/button'; +import { 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 { BackgroundJobTask, BackgroundJobsService } from '../../../api'; + +const ENV_LINE_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*=.*$/; + +@Component({ + selector: 'app-task-create-dialog', + imports: [ + CommonModule, + ReactiveFormsModule, + MatButtonModule, + MatDialogModule, + MatFormFieldModule, + MatIconModule, + MatInputModule, + ], + templateUrl: './task-create-dialog.component.html', + styleUrl: './task-create-dialog.component.scss', + standalone: true, +}) +export class TaskCreateDialogComponent { + private readonly dialogRef = + inject< + MatDialogRef + >(MatDialogRef); + private readonly backgroundJobsService = inject(BackgroundJobsService); + + protected readonly creating = signal(false); + protected readonly serverError = signal(''); + + protected readonly form = new FormGroup({ + name: new FormControl('', { + nonNullable: true, + validators: [Validators.required], + }), + description: new FormControl('', { nonNullable: true }), + targetIndex: new FormControl('', { nonNullable: true }), + command: new FormControl('', { nonNullable: true }), + image: new FormControl('', { nonNullable: true }), + env: new FormControl('', { nonNullable: true }), + }); + + protected submit(): void { + this.serverError.set(''); + const value = this.form.getRawValue(); + + const name = value.name.trim(); + if (!name) { + this.form.controls.name.markAsTouched(); + return; + } + + const command = value.command.trim() || null; + const env = this.parseEnv(value.env); + if (env === undefined) return; + + this.creating.set(true); + this.backgroundJobsService + .createBackgroundJobTask({ + name, + description: value.description.trim(), + target_index: value.targetIndex.trim(), + image: value.image.trim() || null, + command, + env, + }) + .subscribe({ + next: (task) => this.dialogRef.close(task), + error: (error) => { + this.creating.set(false); + const detail = error?.error?.detail; + this.serverError.set( + typeof detail === 'string' + ? detail + : 'Der Job konnte nicht angelegt werden.', + ); + }, + }); + } + + protected close(): void { + this.dialogRef.close(); + } + + private parseEnv(text: string): { [key: string]: string } | undefined { + const env: { [key: string]: string } = {}; + for (const line of text.split('\n')) { + const trimmed = line.trim(); + if (!trimmed) continue; + if (!ENV_LINE_PATTERN.test(trimmed)) { + this.form.controls.env.setErrors({ invalidEnv: true }); + this.form.controls.env.markAsTouched(); + return undefined; + } + const separatorIndex = trimmed.indexOf('='); + env[trimmed.slice(0, separatorIndex)] = trimmed.slice(separatorIndex + 1); + } + return env; + } +}