Make ei-coordinator tests work
[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, ViewChild } from '@angular/core';
21 import { MatSort } from '@angular/material/sort';
22 import { animate, state, style, transition, trigger } from '@angular/animations';
23 import { FormBuilder, FormGroup, AbstractControl } from '@angular/forms';
24 import { MatTableDataSource, MatTable } from '@angular/material';
25
26 import { BehaviorSubject, Observable } from 'rxjs';
27
28 import { EIJob, EIProducer } from '../interfaces/ei.types';
29 import { EIJobDataSource } from './ei-job.datasource';
30 import { EIProducerDataSource } from './ei-producer.datasource';
31 import { UiService } from '../services/ui/ui.service';
32
33 class EIJobInfo {
34     constructor(public eiJob: EIJob) { }
35
36     isExpanded: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
37 }
38
39 @Component({
40     selector: 'nrcp-ei-coordinator',
41     templateUrl: './ei-coordinator.component.html',
42     styleUrls: ['./ei-coordinator.component.scss'],
43     animations: [
44         trigger('detailExpand', [
45             state('collapsed, void', style({ height: '0px', minHeight: '0', display: 'none' })),
46             state('expanded', style({ height: '*' })),
47             transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
48             transition('expanded <=> void', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)'))
49         ]),
50     ],
51 })
52 export class EICoordinatorComponent implements OnInit {
53
54     @ViewChild(MatSort, { static: true }) sort: MatSort;
55     @ViewChild('producersTable', { static: true }) table: MatTable<Element>;
56
57     eiJobInfo = new Map<string, EIJobInfo>();
58     darkMode: boolean;
59     searchString: string;
60     formGroup: FormGroup;
61     jobsDataSource: MatTableDataSource<EIJob>;
62     producersDataSource: MatTableDataSource<EIProducer>;
63
64     readonly jobsFormControl: AbstractControl;
65     readonly producersFormControl: AbstractControl;
66
67     constructor(
68         private eiJobsDataSource: EIJobDataSource,
69         private eiProducersDataSource: EIProducerDataSource,
70         private ui: UiService,
71         private formBuilder: FormBuilder) {
72             this.formGroup = formBuilder.group({ filter: [""] });
73
74             this.jobsFormControl = formBuilder.group({
75                 id: '',
76                 typeId: '',
77                 owner: '',
78                 targetUri:''
79             });
80             this.producersFormControl = formBuilder.group({
81                 ei_producer_id: '',
82                 ei_producer_types: '',
83                 status: ''
84             });
85     }
86
87     ngOnInit() {
88         this.eiJobsDataSource.loadJobs();
89         this.eiProducersDataSource.loadProducers();
90         this.jobsDataSource = new MatTableDataSource(this.eiJobsDataSource.eiJobs());
91         this.producersDataSource = new MatTableDataSource(this.eiProducersDataSource.eiProducers());
92
93         this.jobsFormControl.valueChanges.subscribe(value => {
94             const filter = {...value, id: value.id.trim().toLowerCase()} as string;
95             this.jobsDataSource.filter = filter;
96         });
97         this.producersFormControl.valueChanges.subscribe(value => {
98             const filter = {...value, ei_producer_id: value.ei_producer_id.trim().toLowerCase()} as string;
99             this.producersDataSource.filter = filter;
100         });
101
102         this.jobsDataSource.filterPredicate = ((data, filter) => {
103             return this.isDataIncluding(data.ei_job_identity, filter.id)
104                 && this.isDataIncluding(data.target_uri, filter.target_uri)
105                 && this.isDataIncluding(data.owner, filter.owner)
106                 && this.isDataIncluding(data.ei_type_identity, filter.typeId);
107           }) as (EIJob, string) => boolean;
108
109         this.producersDataSource.filterPredicate = ((data, filter) => {
110             return this.isDataIncluding(data.ei_producer_id, filter.ei_producer_id)
111                 && this.isDataIncluding(data.ei_producer_types.join(','), filter.ei_producer_types)
112                 && this.isDataIncluding(data.status, filter.status);
113           }) as (EIProducer, string) => boolean;
114
115         this.ui.darkModeState.subscribe((isDark) => {
116             this.darkMode = isDark;
117         });
118     }
119
120     isDataIncluding(data: string, filter: string) : boolean {
121         return !filter || data.toLowerCase().includes(filter);
122     }
123
124     getEIJobInfo(eiJob: EIJob): EIJobInfo {
125         let info: EIJobInfo = this.eiJobInfo.get(eiJob.ei_job_data);
126         if (!info) {
127             info = new EIJobInfo(eiJob);
128             this.eiJobInfo.set(eiJob.ei_job_data, info);
129         }
130         return info;
131     }
132
133     getDisplayName(eiJob: EIJob): string {
134         if (eiJob.ei_job_identity) {
135             return eiJob.ei_job_identity;
136         }
137         return '< No id >';
138     }
139
140     getEITypeId(eiJob: EIJob): string {
141         if (eiJob.ei_type_identity) {
142             return eiJob.ei_type_identity;
143         }
144         return '< No type >';
145     }
146
147     getTargetUri(eiJob: EIJob): string {
148         if (eiJob.target_uri) {
149             return eiJob.target_uri;
150         }
151         return '< No target URI >';
152     }
153
154     isInstancesShown(eiJob: EIJob): boolean {
155         return this.getEIJobInfo(eiJob).isExpanded.getValue();
156     }
157
158     getExpandedObserver(eiJob: EIJob): Observable<boolean> {
159         return this.getEIJobInfo(eiJob).isExpanded.asObservable();
160     }
161
162     getEIProducerId(eiProducer: EIProducer): string {
163         if (eiProducer.ei_producer_id) {
164             return eiProducer.ei_producer_id;
165         }
166         return '< No id>';
167     }
168
169     getEIProducerTypes(eiProducer: EIProducer): string[] {
170         if (eiProducer.ei_producer_types) {
171             return eiProducer.ei_producer_types;
172         }
173         return ['< No types >'];
174     }
175
176     getEIProducerStatus(eiProducer: EIProducer): string {
177         if (eiProducer.status) {
178             return eiProducer.status;
179         }
180         return '< No status >';
181     }
182
183     refreshTables() {
184         this.eiJobsDataSource.loadJobs();
185         this.eiProducersDataSource.loadProducers();
186     }
187 }