Skip to content
Snippets Groups Projects
executions.component.ts 6.85 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 {Epoch} from "../model/epoch";
import {
  faExclamationTriangle,
  faFastBackward,
  faFastForward,
  faStepBackward,
} 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 = "";

  public resultsPerPage: number = 100;
  public currentPage: number = 1;
  public numResults: number = 0;
  public pages: number = 1;
  private pages$: Subscription;

  private filters$: Subscription;
  public filters: object;
  public sortColumns: Array<string>;
  public sortDirections: Array<string>;
  public faFastBackward = faFastBackward;
  public faStepBackward = faStepBackward;
  public faFastForward = faFastForward;
  public faStepForward = faStepForward;
  public faExclamationTriangle = faExclamationTriangle;
  public faSyncAlt = faSyncAlt;
  public alertAfterDays = 7;

  public formGroup: FormGroup;

  public alertThresholdForm: FormGroup;

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

  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);
      }
      if (params.hasOwnProperty('queue')) {
        const paramQueue = Job.getQueueFromName(params.queue);
        if (paramQueue) {
          this.filterService.setCurrentSetting('JOB_QUEUE', paramQueue);
        }
      }
      if (params.hasOwnProperty('epoch')) {
        const paramEpoch = Epoch.getEpochFromId(params.epoch);
        if (paramEpoch) {
          this.filterService.setCurrentSetting('EPOCHS', paramEpoch);
        }
      }
    });

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

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

    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;
    });
  }

  setEpoch(epoch: Epoch) {
    this.filterService.setCurrentSetting('EPOCH', epoch);
  }

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

    this.filterService.setCurrentSetting('JOB_STATUS', status);
  }

  setSortColumn(column: string): void {
    this.filterService.setCurrentSetting('JOB_SORT', column);
  }

  setSortDirection(direction: string): void {
    this.filterService.setCurrentSetting('SORT_DIRECTION', direction);
  }

  getPrettyName(name: string): string {
    return FiltersService.prettyName(name);
  }

  goToPage(page: number): boolean {
    if (page < 1) {
      page = 1;
    }
    if (page > this.pages) {
      page = this.pages;
    }
    if (this.currentPage == page) {
      return false;
    }
    this.currentPage = page;
    this.getJobs(page);
  }

  getPages(): Array<any> {
    return new Array(this.pages);
  }

  getPageCount(): void {
    const epoch = this.filterService.getCurrentSetting('EPOCH');
    const queue = this.filterService.getCurrentSetting('JOB_QUEUE');
    const status = this.filterService.getCurrentSetting('JOB_STATUS');
    this.pages$ = this.jobService.getJobRecordCount(epoch.id, queue.name, this.pattern, status).subscribe((jobNumber: number) => {
      this.numResults = jobNumber;
      this.pages = Math.ceil(jobNumber / this.resultsPerPage);
      if (this.currentPage > this.pages) {
        this.currentPage = this.pages;
      }
      if (this.currentPage < 1) {
        this.currentPage = 1;
      }
      this.getJobs(1);
    });
  }

  getJobs(pageId: number): void {
    // clear a previous call
    if (this.jobs$) {
      this.jobs$.unsubscribe();
    }
    this.jobs = null;
    const epoch = this.filterService.getCurrentSetting('EPOCH');
    const queue = this.filterService.getCurrentSetting('JOB_QUEUE');
    const status = this.filterService.getCurrentSetting('JOB_STATUS');
    this.alertService.info('Getting ' + queue.label);
    this.jobs$ = this.jobService.getJobPage(epoch.id, queue.name, pageId - 1, this.pattern, status).subscribe((jobs: Array<Job>) => {
      if (jobs && jobs.length > 0) {
        this.alertService.success(queue.label + ' loaded.');
        this.jobs = jobs;
      } else {
        this.jobs = [];
        this.alertService.error('No ' + queue.label + ' found.')
  getJobById(id: number): void {
    // clear a previous call
    if (this.jobs$) {
      this.jobs$.unsubscribe();
    }
    this.jobs$ = this.jobService.getJobById(id).subscribe((job: Job) => {
      this.jobs = [job];
      this.numResults = 1;
      this.pages = 1;
      this.currentPage = 1;
    });
  }

  getPageInfoAndJob(): void {
    let possible_id = parseInt(this.pattern);
    if (isNaN(possible_id)) {
      this.getPageCount();
      this.getJobs(1);
    } else {
      this.getJobById(possible_id);
    }
  }

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