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";
30 import { BehaviorSubject } from "rxjs/BehaviorSubject";
37 } from "rxjs/operators";
38 import { EIJob } from "@interfaces/ei.types";
39 import { EIService } from "@services/ei/ei.service";
40 import { UiService } from "@services/ui/ui.service";
42 export interface Job {
52 selector: "nrcp-jobs-list",
53 templateUrl: "./jobs-list.component.html",
54 styleUrls: ["./jobs-list.component.scss"],
56 export class JobsListComponent implements OnInit {
57 @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
58 jobsDataSource: MatTableDataSource<Job>;
62 private jobsSubject = new BehaviorSubject<Job[]>([]);
63 private refresh$ = new BehaviorSubject("");
64 private loadingSubject = new BehaviorSubject<boolean>(false);
65 public loading$ = this.loadingSubject.asObservable();
66 subscription: Subscription;
68 constructor(private eiSvc: EIService, private ui: UiService) {
69 this.jobForm = new FormGroup({
70 jobId: new FormControl(""),
71 typeId: new FormControl(""),
72 owner: new FormControl(""),
73 targetUri: new FormControl(""),
74 prodId: new FormControl(""),
79 this.subscription = this.dataSubscription();
81 this.jobsSubject.subscribe((data) => {
82 this.jobsDataSource = new MatTableDataSource<Job>(data);
83 this.jobsDataSource.paginator = this.paginator;
85 this.jobsDataSource.filterPredicate = ((data: Job, filter) => {
86 let searchTerms = JSON.parse(filter);
88 this.isDataIncluding(data.targetUri, searchTerms.targetUri) &&
89 this.isDataIncluding(data.jobId, searchTerms.jobId) &&
90 this.isDataIncluding(data.owner, searchTerms.owner) &&
91 this.isDataIncluding(data.typeId, searchTerms.typeId) &&
92 this.isDataIncluding(data.prodId, searchTerms.prodId)
94 }) as (data: Job, filter: any) => boolean;
97 this.jobForm.valueChanges.subscribe((value) => {
98 this.jobsDataSource.filter = JSON.stringify(value);
101 this.ui.darkModeState.subscribe((isDark) => {
102 this.darkMode = isDark;
106 dataSubscription(): Subscription {
108 const jobs$ = this.eiSvc.getProducerIds().pipe(
109 tap((data) => (prodId = data)),
110 mergeMap((prodIds) =>
111 forkJoin(prodIds.map((id) => this.eiSvc.getJobsForProducer(id)))
113 finalize(() => this.loadingSubject.next(false))
119 timer(0, 10000).pipe(
121 this.loadingSubject.next(true);
123 switchMap((_) => jobs$),
124 map((response) => this.extractJobs(prodId, response))
132 this.subscription.unsubscribe();
136 this.jobForm.get("jobId").setValue("");
137 this.jobForm.get("typeId").setValue("");
138 this.jobForm.get("owner").setValue("");
139 this.jobForm.get("targetUri").setValue("");
140 this.jobForm.get("prodId").setValue("");
143 sortJobs(sort: Sort) {
144 const data = this.jobsDataSource.data;
145 data.sort((a: Job, b: Job) => {
146 const isAsc = sort.direction === "asc";
147 switch (sort.active) {
149 return this.compare(a.jobId, b.jobId, isAsc);
151 return this.compare(a.typeId, b.typeId, isAsc);
153 return this.compare(a.owner, b.owner, isAsc);
155 return this.compare(a.targetUri, b.targetUri, isAsc);
157 return this.compare(a.prodId, b.prodId, isAsc);
162 this.jobsDataSource.data = data;
165 compare(a: any, b: any, isAsc: boolean) {
166 return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
169 stopSort(event: any) {
170 event.stopPropagation();
173 isDataIncluding(data: string, filter: string): boolean {
174 const transformedFilter = filter.trim().toLowerCase();
175 return data.toLowerCase().includes(transformedFilter);
178 getJobTypeId(eiJob: Job): string {
182 return "< No type >";
185 getJobOwner(eiJob: Job): string {
189 return "< No owner >";
192 public jobs(): Job[] {
193 return this.jobsSubject.value;
196 private extractJobs(prodId: number[], res: EIJob[][]) {
197 console.log(res.length);
199 prodId.forEach((element, index) => {
200 let jobs = res[index];
201 jobList = jobList.concat(jobs.map((job) => this.createJob(element, job)));
203 this.jobsSubject.next(jobList);
207 createJobList(prodId: any[], result: EIJob[][]) {
209 prodId.forEach((element, index) => {
210 let jobs = result[index];
211 jobList = jobList.concat(jobs.map((job) => this.createJob(element, job)));
216 createJob(element: any, job: EIJob): any {
218 eiJob.jobId = job.ei_job_identity;
219 eiJob.typeId = job.ei_type_identity;
220 eiJob.owner = job.owner;
221 eiJob.targetUri = job.target_uri;
222 eiJob.prodId = element;
227 this.refresh$.next("");