2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
20 import { Component, OnInit, ViewChild } from '@angular/core';
21 import { FormControl, FormGroup } from '@angular/forms';
22 import { MatPaginator } from '@angular/material/paginator';
23 import { Sort } from '@angular/material/sort';
24 import { MatTableDataSource } from '@angular/material/table';
25 import { forkJoin } from 'rxjs';
26 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
27 import { mergeMap, finalize, tap } from 'rxjs/operators';
28 import { EIJob } from '@interfaces/ei.types';
29 import { EIService } from '@services/ei/ei.service';
30 import { UiService } from '@services/ui/ui.service';
32 export interface Job {
42 selector: 'nrcp-jobs-list',
43 templateUrl: './jobs-list.component.html',
44 styleUrls: ['./jobs-list.component.scss']
46 export class JobsListComponent implements OnInit {
48 @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
51 jobsDataSource: MatTableDataSource<Job>;
53 private loadingSubject = new BehaviorSubject<boolean>(false);
54 private jobsSubject = new BehaviorSubject<Job[]>([]);
55 public loading$ = this.loadingSubject.asObservable();
58 private eiSvc: EIService,
61 this.jobForm = new FormGroup({
62 jobId: new FormControl(''),
63 typeId: new FormControl(''),
64 owner: new FormControl(''),
65 targetUri: new FormControl(''),
66 prodId: new FormControl('')
72 this.jobsSubject.subscribe((data) => {
73 this.jobsDataSource = new MatTableDataSource<Job>(data);
74 this.jobsDataSource.paginator = this.paginator;
76 this.jobsDataSource.filterPredicate = ((data: Job, filter) => {
77 let searchTerms = JSON.parse(filter);
78 return this.isDataIncluding(data.targetUri, searchTerms.targetUri)
79 && this.isDataIncluding(data.jobId, searchTerms.jobId)
80 && this.isDataIncluding(data.owner, searchTerms.owner)
81 && this.isDataIncluding(data.typeId, searchTerms.typeId)
82 && this.isDataIncluding(data.prodId, searchTerms.prodId);
83 }) as (data: Job, filter: any) => boolean;
86 this.jobForm.valueChanges.subscribe(value => {
87 this.jobsDataSource.filter = JSON.stringify(value);
90 this.ui.darkModeState.subscribe((isDark) => {
91 this.darkMode = isDark;
96 if (!this.jobsSubject) this.jobsSubject.unsubscribe();
97 if (!this.loadingSubject) this.loadingSubject.unsubscribe();
98 if (!this.ui.darkModeState) this.ui.darkModeState.unsubscribe();
102 this.jobForm.get('jobId').setValue('');
103 this.jobForm.get('typeId').setValue('');
104 this.jobForm.get('owner').setValue('');
105 this.jobForm.get('targetUri').setValue('');
106 this.jobForm.get('prodId').setValue('');
109 sortJobs(sort: Sort) {
110 const data = this.jobsDataSource.data
111 data.sort((a: Job, b: Job) => {
112 const isAsc = sort.direction === 'asc';
113 switch (sort.active) {
114 case 'jobId': return this.compare(a.jobId, b.jobId, isAsc);
115 case 'typeId': return this.compare(a.typeId, b.typeId, isAsc);
116 case 'owner': return this.compare(a.owner, b.owner, isAsc);
117 case 'targetUri': return this.compare(a.targetUri, b.targetUri, isAsc);
118 case 'prodId': return this.compare(a.prodId, b.prodId, isAsc);
122 this.jobsDataSource.data = data;
125 compare(a: any, b: any, isAsc: boolean) {
126 return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
129 stopSort(event: any) {
130 event.stopPropagation();
133 isDataIncluding(data: string, filter: string): boolean {
134 const transformedFilter = filter.trim().toLowerCase();
135 return data.toLowerCase().includes(transformedFilter);
138 getJobTypeId(eiJob: Job): string {
142 return '< No type >';
145 getJobOwner(eiJob: Job): string {
149 return '< No owner >';
152 public jobs(): Job[] {
153 return this.jobsSubject.value;
157 this.loadingSubject.next(true);
160 this.eiSvc.getProducerIds().pipe(
161 tap(data => prodId = data),
163 forkJoin(prodIds.map(id => this.eiSvc.getJobsForProducer(id)))),
164 finalize(() => this.loadingSubject.next(false))
165 ).subscribe(result => {
166 jobs = this.createJobList(prodId, result);
167 this.jobsSubject.next(jobs);
170 createJobList(prodId: any[], result: EIJob[][]) {
172 prodId.forEach((element, index) => {
173 let jobs = result[index];
174 jobList = jobList.concat(jobs.map(job => this.createJob(element, job)));
178 createJob(element: any, job: EIJob): any {
180 eiJob.jobId = job.ei_job_identity;
181 eiJob.typeId = job.ei_type_identity;
182 eiJob.owner = job.owner;
183 eiJob.targetUri = job.target_uri;
184 eiJob.prodId = element;