9698f55ae728b15b0026e53791a854443d31d665
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ei-coordinator / ei-coordinator.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
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';
24
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';
30
31 @Component({
32     selector: 'nrcp-ei-coordinator',
33     templateUrl: './ei-coordinator.component.html',
34     styleUrls: ['./ei-coordinator.component.scss']
35 })
36 export class EICoordinatorComponent implements OnInit {
37
38     darkMode: boolean;
39     searchString: string;
40     formGroup: FormGroup;
41     jobsDataSource: MatTableDataSource<EIJob> = new MatTableDataSource<EIJob>();
42     producersDataSource: MatTableDataSource<EIProducer>= new MatTableDataSource<EIProducer>();
43
44     readonly jobsFormControl: AbstractControl;
45     readonly producersFormControl: AbstractControl;
46
47     constructor(
48         private eiJobsDataSource: EIJobDataSource,
49         private eiProducersDataSource: EIProducerDataSource,
50         private ui: UiService,
51         private formBuilder: FormBuilder) {
52             this.formGroup = formBuilder.group({ filter: [""] });
53
54             this.jobsFormControl = formBuilder.group({
55                 id: '',
56                 typeId: '',
57                 owner: '',
58                 targetUri:''
59             });
60             this.producersFormControl = formBuilder.group({
61                 ei_producer_id: '',
62                 ei_producer_types: '',
63                 status: ''
64             });
65     }
66
67     ngOnInit() {
68         this.eiJobsDataSource.loadJobs();
69         this.eiProducersDataSource.loadProducers();
70
71         this.eiJobsDataSource.eiJobsSubject().subscribe((data) => {
72             this.jobsDataSource.data = data;
73         });
74         this.eiProducersDataSource.eiProducersSubject().subscribe((data) => {
75             this.producersDataSource.data = data;
76         });
77
78         this.jobsFormControl.valueChanges.subscribe(value => {
79             const filter = {...value, id: value.id.trim().toLowerCase()} as string;
80             this.jobsDataSource.filter = filter;
81         });
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;
85         });
86
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;
93
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;
99
100         this.ui.darkModeState.subscribe((isDark) => {
101             this.darkMode = isDark;
102         });
103     }
104
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);
114               default: return 0;
115             }
116           });
117           this.jobsDataSource.data = data;
118     }
119
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);
128               default: return 0;
129             }
130           });
131           this.producersDataSource.data = data;
132     }
133     
134     compare(a: any, b: any, isAsc: boolean) {
135       return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
136     }
137
138     stopSort(event: any){
139         event.stopPropagation();
140     }
141
142     isDataIncluding(data: string, filter: string) : boolean {
143         return !filter || data.toLowerCase().includes(filter);
144     }
145
146     getJobTypeId(eiJob: EIJob): string {
147         if (eiJob.ei_type_identity) {
148             return eiJob.ei_type_identity;
149         }
150         return '< No type >';
151     }
152
153     getJobOwner(eiJob: EIJob): string {
154         if (eiJob.owner) {
155             return eiJob.owner;
156         }
157         return '< No owner >';
158     }
159
160     getProducerTypes(eiProducer: EIProducer): string[] {
161         if (eiProducer.ei_producer_types) {
162             return eiProducer.ei_producer_types;
163         }
164         return ['< No types >'];
165     }
166
167     getProducerStatus(eiProducer: EIProducer): string {
168         if (eiProducer.status) {
169             return eiProducer.status;
170         }
171         return '< No status >';
172     }
173
174     refreshTables() {
175         this.eiJobsDataSource.loadJobs();
176         this.eiProducersDataSource.loadProducers();
177         
178         this.eiJobsDataSource.eiJobsSubject().subscribe((data) => {
179             this.jobsDataSource.data = data;
180         });
181         this.eiProducersDataSource.eiProducersSubject().subscribe((data) => {
182             this.producersDataSource.data = data;
183         });
184     }
185 }