711b86f15634a4c05d2c07731d25090125edfb83
[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     producers$: Observable<EIProducer[]>;
55     @ViewChild(MatSort, { static: true }) sort: MatSort;
56     @ViewChild('producersTable', { static: true }) table: MatTable<Element>;
57
58     eiJobInfo = new Map<string, EIJobInfo>();
59     darkMode: boolean;
60     searchString: string;
61     formGroup: FormGroup;
62     jobsDataSource: MatTableDataSource<EIJob>;
63     producersDataSource: MatTableDataSource<EIProducer>;
64
65     readonly jobsFormControl: AbstractControl;
66     readonly producersFormControl: AbstractControl;
67
68     constructor(
69         private eiJobsDataSource: EIJobDataSource,
70         private eiProducersDataSource: EIProducerDataSource,
71         private ui: UiService,
72         private formBuilder: FormBuilder) {
73             this.formGroup = formBuilder.group({ filter: [""] });
74
75             this.jobsFormControl = formBuilder.group({
76                 id: '',
77                 typeId: '',
78                 owner: '',
79                 targetUri:''
80             });
81             this.producersFormControl = formBuilder.group({
82                 ei_producer_id: '',
83                 ei_producer_types: '',
84                 status: ''
85             });
86     }
87
88     ngOnInit() {
89         this.eiJobsDataSource.getJobs();
90         this.producers$ = this.eiProducersDataSource.loadProducers();
91         this.jobsDataSource = new MatTableDataSource(this.eiJobsDataSource.eiJobsSubject.value);
92         this.producersDataSource = new MatTableDataSource(this.eiProducersDataSource.producerSubject.value)
93
94         this.jobsFormControl.valueChanges.subscribe(value => {
95             const filter = {...value, id: value.id.trim().toLowerCase()} as string;
96             this.jobsDataSource.filter = filter;
97         });
98         this.producersFormControl.valueChanges.subscribe(value => {
99             const filter = {...value, ei_producer_id: value.ei_producer_id.trim().toLowerCase()} as string;
100             this.producersDataSource.filter = filter;
101         });
102
103         this.jobsDataSource.filterPredicate = ((data, filter) => {
104             return this.isDataIncluding(data.ei_job_identity, filter.id)
105                 && this.isDataIncluding(data.target_uri, filter.target_uri)
106                 && this.isDataIncluding(data.owner, filter.owner)
107                 && this.isDataIncluding(data.ei_type_identity, filter.typeId);
108           }) as (EIJob, string) => boolean;
109
110         this.producersDataSource.filterPredicate = ((data, filter) => {
111             return this.isDataIncluding(data.ei_producer_id, filter.ei_producer_id)
112                 && this.isDataIncluding(data.ei_producer_types.join(','), filter.ei_producer_types)
113                 && this.isDataIncluding(data.status, filter.status);
114           }) as (EIProducer, string) => boolean;
115
116         this.ui.darkModeState.subscribe((isDark) => {
117             this.darkMode = isDark;
118         });
119     }
120
121     isDataIncluding(data: string, filter: string) : boolean {
122         return !filter || data.toLowerCase().includes(filter);
123     }
124
125     getEIJobInfo(eiJob: EIJob): EIJobInfo {
126         let info: EIJobInfo = this.eiJobInfo.get(eiJob.ei_job_data);
127         if (!info) {
128             info = new EIJobInfo(eiJob);
129             this.eiJobInfo.set(eiJob.ei_job_data, info);
130         }
131         return info;
132     }
133
134     getDisplayName(eiJob: EIJob): string {
135         if (eiJob.ei_job_identity) {
136             return eiJob.ei_job_identity;
137         }
138         return '< No id >';
139     }
140
141     getEITypeId(eiJob: EIJob): string {
142         if (eiJob.ei_type_identity) {
143             return eiJob.ei_type_identity;
144         }
145         return '< No type >';
146     }
147
148     getTargetUri(eiJob: EIJob): string {
149         if (eiJob.target_uri) {
150             return eiJob.target_uri;
151         }
152         return '< No target URI >';
153     }
154
155     isInstancesShown(eiJob: EIJob): boolean {
156         return this.getEIJobInfo(eiJob).isExpanded.getValue();
157     }
158
159     getExpandedObserver(eiJob: EIJob): Observable<boolean> {
160         return this.getEIJobInfo(eiJob).isExpanded.asObservable();
161     }
162
163     getEIProducerId(eiProducer: EIProducer): string {
164         if (eiProducer.ei_producer_id) {
165             return eiProducer.ei_producer_id;
166         }
167         return '< No id>';
168     }
169
170     getEIProducerTypes(eiProducer: EIProducer): string[] {
171         if (eiProducer.ei_producer_types) {
172             return eiProducer.ei_producer_types;
173         }
174         return ['< No types >'];
175     }
176
177     getEIProducerStatus(eiProducer: EIProducer): string {
178         if (eiProducer.status) {
179             return eiProducer.status;
180         }
181         return '< No status >';
182     }
183
184     refreshTables() {
185         this.eiJobsDataSource.getJobs();
186         this.eiProducersDataSource.loadProducers();
187     }
188 }