Skip to content
Snippets Groups Projects
executions.component.ts 3.68 KiB
Newer Older
import {Component, OnDestroy, OnInit} from '@angular/core';
import {Subscription} from "rxjs";
import {Job, JobQueue} from "../model/job";
import {FormControl, FormGroup, Validators} from "@angular/forms";
import {JobsService} from "../services/jobs.service";
import {debounceTime, distinctUntilChanged, map} from "rxjs/operators";
import {ActivatedRoute} from "@angular/router";
import {AlertService} from "../services/alert.service";
import {FiltersService} from "../services/filters.service";
import {faExclamationTriangle} from "@fortawesome/free-solid-svg-icons";

@Component({
  selector: 'app-executions',
  templateUrl: './executions.component.html',
  styleUrls: ['./executions.component.scss']
})
export class ExecutionsComponent implements OnInit, OnDestroy {

  public queues: Array<JobQueue>;
  public statuses: Array<string>;
  public jobs$: Subscription;
  public jobs: Array<Job>;
  public pattern: string = "";

  private filters$: Subscription;
  public filters: object;

  public faExclamationTriangle = faExclamationTriangle;
  public alertAfterDays = 14;

  public formGroup: FormGroup;

  public alertThresholdForm: FormGroup;

  constructor(
    private jobService: JobsService,
    private route: ActivatedRoute,
    private alertService: AlertService,
    private filterService: FiltersService
  ) {
    this.queues = this.filterService.getFilter('JOB_QUEUE');
    this.statuses = this.filterService.getFilter('JOB_STATUS');
  }

  ngOnInit() {
    this.formGroup = new FormGroup({
      pattern: new FormControl()
    });

    this.route.queryParams.subscribe(params => {
      if (params.hasOwnProperty('pattern')) {
        this.pattern = params.pattern;
        this.formGroup.get('pattern').setValue(params.pattern);
      }
      if (params.hasOwnProperty('status')) {
        this.filterService.setCurrentSetting('JOB_STATUS', params.status);
      }
    });

    this.formGroup.get('pattern').valueChanges.pipe(
      debounceTime(400),
      distinctUntilChanged(),
      map(results => results)
    ).subscribe((val: string) => {
      this.pattern = val;
      this.getJobs();
    });

    this.filters$ = this.filterService.currentSettings$.subscribe((filters: object) => {
      this.filters = filters;
      this.getJobs();
    });

    this.alertThresholdForm = new FormGroup({
      threshold: new FormControl(this.alertAfterDays, Validators.required)
    });

    this.alertThresholdForm.get('threshold').valueChanges.pipe(
      debounceTime(200),
      distinctUntilChanged(),
      map(results => results)
    ).subscribe((val: number) => {
      this.alertAfterDays = val;
    });
  }

  setQueue(queue: JobQueue) {
    this.filterService.setCurrentSetting('JOB_QUEUE', queue);
  }

  setStatus(status: string) {
    this.filterService.setCurrentSetting('JOB_STATUS', status);
  }

  getJobs() {
    if (this.jobs$) {
      this.jobs$.unsubscribe();
    }
    this.jobs = null;
    var possibleId = parseInt(this.pattern);
    var id = "";
    if (!isNaN(possibleId)) {
      id = this.pattern;
    }
    const queue = this.filterService.getCurrentSetting('JOB_QUEUE');
    const status = this.filterService.getCurrentSetting('JOB_STATUS');
    this.alertService.info('Getting ' + queue.label + ' Jobs');
    this.jobs$ = this.jobService.getJobs(queue.name, id, this.pattern, status).subscribe((j: Array<Job>) => {
      if (j && j.length > 0) {
        this.alertService.success(queue.label + ' jobs retrieved');
        this.jobs = j;
      } else {
        this.jobs = [];
        this.alertService.error('No ' + queue.label + ' jobs found');
      }
    });
  }

  ngOnDestroy(): void {
    if (this.jobs$) {
      this.jobs$.unsubscribe();
    }
    if (this.filters$) {
      this.filters$.unsubscribe();
    }
  }
}