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";

Reid Givens
committed
import {Epoch} from "../model/epoch";
import {
faExclamationTriangle,
faFastBackward,
faFastForward,
faStepBackward,
faStepForward,
faSyncAlt

Reid Givens
committed
} from "@fortawesome/free-solid-svg-icons";
@Component({
selector: 'app-executions',
templateUrl: './executions.component.html',
styleUrls: ['./executions.component.scss']
})
export class ExecutionsComponent implements OnInit, OnDestroy {

Reid Givens
committed
public epochs: Array<Epoch>;
public queues: Array<JobQueue>;
public statuses: Array<string>;
public jobs$: Subscription;
public jobs: Array<Job>;
public pattern: string = "";

Reid Givens
committed
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>;

Reid Givens
committed
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
) {

Reid Givens
committed
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);
}

Reid Givens
committed
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;

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

Reid Givens
committed
this.getPageInfoAndJob();
});
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;
});
}

Reid Givens
committed
setEpoch(epoch: Epoch) {
this.filterService.setCurrentSetting('EPOCH', epoch);
}
setQueue(queue: JobQueue): void {
this.filterService.setCurrentSetting('JOB_QUEUE', queue);
}

Reid Givens
committed
setStatus(status: string): void {
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);
}

Reid Givens
committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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;

Reid Givens
committed
const epoch = this.filterService.getCurrentSetting('EPOCH');
const queue = this.filterService.getCurrentSetting('JOB_QUEUE');
const status = this.filterService.getCurrentSetting('JOB_STATUS');

Reid Givens
committed
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;

Reid Givens
committed
this.alertService.error('No ' + queue.label + ' found.')

Reid Givens
committed
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();
}

Reid Givens
committed
if (this.pages$) {
this.pages$.unsubscribe();
}