2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 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 } from '@angular/core';
21 import { Sort } from '@angular/material/sort';
22 import { FormBuilder, FormGroup, AbstractControl } from '@angular/forms';
23 import { MatTableDataSource } from '@angular/material/table';
25 import { EIJob, EIProducer } from '../interfaces/ei.types';
26 import { EIJobDataSource } from './ei-job.datasource';
27 import { EIProducerDataSource } from './ei-producer.datasource';
28 import { UiService } from '../services/ui/ui.service';
29 import { Observable } from 'rxjs/Observable';
32 selector: 'nrcp-ei-coordinator',
33 templateUrl: './ei-coordinator.component.html',
34 styleUrls: ['./ei-coordinator.component.scss']
36 export class EICoordinatorComponent implements OnInit {
41 jobsDataSource: MatTableDataSource<EIJob> = new MatTableDataSource<EIJob>();
42 producersDataSource: MatTableDataSource<EIProducer>= new MatTableDataSource<EIProducer>();
44 readonly jobsFormControl: AbstractControl;
45 readonly producersFormControl: AbstractControl;
48 private eiJobsDataSource: EIJobDataSource,
49 private eiProducersDataSource: EIProducerDataSource,
50 private ui: UiService,
51 private formBuilder: FormBuilder) {
52 this.formGroup = formBuilder.group({ filter: [""] });
54 this.jobsFormControl = formBuilder.group({
60 this.producersFormControl = formBuilder.group({
62 ei_producer_types: '',
68 this.eiJobsDataSource.loadJobs();
69 this.eiProducersDataSource.loadProducers();
71 this.eiJobsDataSource.eiJobsSubject().subscribe((data) => {
72 this.jobsDataSource.data = data;
74 this.eiProducersDataSource.eiProducersSubject().subscribe((data) => {
75 this.producersDataSource.data = data;
78 this.jobsFormControl.valueChanges.subscribe(value => {
79 const filter = {...value, id: value.id.trim().toLowerCase()} as string;
80 this.jobsDataSource.filter = filter;
82 this.producersFormControl.valueChanges.subscribe(value => {
83 const filter = {...value, ei_producer_id: value.ei_producer_id.trim().toLowerCase()} as string;
84 this.producersDataSource.filter = filter;
87 this.jobsDataSource.filterPredicate = ((data: EIJob, filter) => {
88 return this.isDataIncluding(data.ei_job_identity, filter.id)
89 && this.isDataIncluding(data.target_uri, filter.targetUri)
90 && this.isDataIncluding(data.owner, filter.owner)
91 && this.isDataIncluding(data.ei_type_identity, filter.typeId);
92 }) as (data: EIJob, filter: any) => boolean;
94 this.producersDataSource.filterPredicate = ((data, filter) => {
95 return this.isDataIncluding(data.ei_producer_id, filter.ei_producer_id)
96 && this.isDataIncluding(data.ei_producer_types.join(','), filter.ei_producer_types)
97 && this.isDataIncluding(data.status, filter.status);
98 }) as (data: EIProducer, filter: any) => boolean;
100 this.ui.darkModeState.subscribe((isDark) => {
101 this.darkMode = isDark;
105 sortJobs(sort: Sort){
106 const data = this.jobsDataSource.data
107 data.sort((a: EIJob, b: EIJob) => {
108 const isAsc = sort.direction === 'asc';
109 switch (sort.active) {
110 case 'id': return this.compare(a.ei_job_identity, b.ei_job_identity, isAsc);
111 case 'typeId': return this.compare(a.ei_type_identity, b.ei_type_identity, isAsc);
112 case 'owner': return this.compare(a.owner, b.owner, isAsc);
113 case 'targetUri': return this.compare(a.target_uri, b.owner, isAsc);
117 this.jobsDataSource.data = data;
120 sortProducers(sort: Sort){
121 const data = this.producersDataSource.data
122 data.sort((a: EIProducer, b: EIProducer) => {
123 const isAsc = sort.direction === 'asc';
124 switch (sort.active) {
125 case 'id': return this.compare(a.ei_producer_id, b.ei_producer_id, isAsc);
126 case 'types': return this.compare(a.ei_producer_types, b.ei_producer_types, isAsc);
127 case 'status': return this.compare(a.status, b.status, isAsc);
131 this.producersDataSource.data = data;
134 compare(a: any, b: any, isAsc: boolean) {
135 return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
138 stopSort(event: any){
139 event.stopPropagation();
142 isDataIncluding(data: string, filter: string) : boolean {
143 return !filter || data.toLowerCase().includes(filter);
146 getJobTypeId(eiJob: EIJob): string {
147 if (eiJob.ei_type_identity) {
148 return eiJob.ei_type_identity;
150 return '< No type >';
153 getJobOwner(eiJob: EIJob): string {
157 return '< No owner >';
160 getProducerTypes(eiProducer: EIProducer): string[] {
161 if (eiProducer.ei_producer_types) {
162 return eiProducer.ei_producer_types;
164 return ['< No types >'];
167 getProducerStatus(eiProducer: EIProducer): string {
168 if (eiProducer.status) {
169 return eiProducer.status;
171 return '< No status >';
175 this.eiJobsDataSource.loadJobs();
176 this.eiProducersDataSource.loadProducers();
178 this.eiJobsDataSource.eiJobsSubject().subscribe((data) => {
179 this.jobsDataSource.data = data;
181 this.eiProducersDataSource.eiProducersSubject().subscribe((data) => {
182 this.producersDataSource.data = data;