68f4f3f1846fc86b1f704c132fdb8a528fe9a2ee
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ei-coordinator / producers-list / producers-list.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 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 { FormControl, FormGroup } from '@angular/forms';
22 import { MatSort, Sort } from '@angular/material/sort';
23 import { MatTableDataSource } from '@angular/material/table';
24 import { forkJoin, of } from 'rxjs';
25 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
26 import { mergeMap, finalize, catchError } from 'rxjs/operators';
27 import { EIService } from 'src/app/services/ei/ei.service';
28 import { EIProducer } from '../../interfaces/ei.types';
29 import { UiService } from '../../services/ui/ui.service';
30
31 @Component({
32   selector: 'nrcp-producers-list',
33   templateUrl: './producers-list.component.html',
34   styleUrls: ['./producers-list.component.scss']
35 })
36 export class ProducersListComponent implements OnInit {
37
38   @ViewChild(MatSort) sort: MatSort;
39
40   producersDataSource: MatTableDataSource<EIProducer> = new MatTableDataSource<EIProducer>();
41   producerForm: FormGroup;
42   darkMode: boolean;
43
44   private loadingSubject = new BehaviorSubject<boolean>(false);
45   private producerSubject = new BehaviorSubject<EIProducer[]>([]);
46   public loading$ = this.loadingSubject.asObservable();
47
48   constructor(
49     private eiSvc: EIService,
50     private ui: UiService) {
51
52     this.producerForm = new FormGroup({
53       ei_producer_id: new FormControl(''),
54       ei_producer_types: new FormControl(''),
55       status: new FormControl('')
56     });
57   }
58
59   ngOnInit(): void {
60     this.loadProducers();
61     this.producerSubject.subscribe((data) => {
62       this.producersDataSource = new MatTableDataSource<EIProducer>(data);
63       //this.producersDataSource.data = data;
64     });
65
66     this.producerForm.valueChanges.subscribe(value => {
67       const filter = { ...value, ei_producer_id: value.ei_producer_id.trim().toLowerCase() } as string;
68       this.producersDataSource.filter = filter;
69     });
70
71     this.producersDataSource.filterPredicate = ((data, filter) => {
72       return this.isDataIncluding(data.ei_producer_id, filter.ei_producer_id)
73         && this.isDataIncluding(data.ei_producer_types.join(','), filter.ei_producer_types)
74         && this.isDataIncluding(data.status, filter.status);
75     }) as (data: EIProducer, filter: any) => boolean;
76
77     this.ui.darkModeState.subscribe((isDark) => {
78       this.darkMode = isDark;
79     });
80   }
81
82   ngOnDestroy() {
83     if (!this.producerSubject) this.producerSubject.unsubscribe();
84     if (!this.loadingSubject) this.loadingSubject.unsubscribe();
85     if (!this.ui.darkModeState) this.ui.darkModeState.unsubscribe();
86   }
87
88   isDataIncluding(data: string, filter: string): boolean {
89     return !filter || data.toLowerCase().includes(filter);
90   }
91
92   sortProducers(sort: Sort) {
93     const data = this.producersDataSource.data
94     data.sort((a: EIProducer, b: EIProducer) => {
95       const isAsc = sort.direction === 'asc';
96       switch (sort.active) {
97         case 'id': return this.compare(a.ei_producer_id, b.ei_producer_id, isAsc);
98         case 'types': return this.compare(a.ei_producer_types, b.ei_producer_types, isAsc);
99         case 'status': return this.compare(a.status, b.status, isAsc);
100         default: return 0;
101       }
102     });
103     this.producersDataSource.data = data;
104   }
105
106   compare(a: any, b: any, isAsc: boolean) {
107     return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
108   }
109
110   stopSort(event: any) {
111     event.stopPropagation();
112   }
113
114   getProducerTypes(eiProducer: EIProducer): string[] {
115     if (eiProducer.ei_producer_types) {
116       return eiProducer.ei_producer_types;
117     }
118     return ['< No types >'];
119   }
120
121   getProducerStatus(eiProducer: EIProducer): string {
122     if (eiProducer.status) {
123       return eiProducer.status;
124     }
125     return '< No status >';
126   }
127
128   public eiProducers(): EIProducer[] {
129     return this.producerSubject.value;
130   }
131
132   loadProducers() {
133     this.loadingSubject.next(true);
134     let producers = [];
135
136     this.eiSvc.getProducerIds().pipe(
137       mergeMap(prodIds =>
138         forkJoin(prodIds.map(id => {
139           return forkJoin([
140             of(id),
141             this.eiSvc.getProducer(id),
142             this.eiSvc.getProducerStatus(id)
143           ])
144         })
145         )),
146       finalize(() => this.loadingSubject.next(false)),
147     ).subscribe(result => {
148       producers = result.map(producer => {
149         let eiProducer = <EIProducer>{};
150         eiProducer.ei_producer_id = producer[0];
151         if (producer[1].supported_ei_types) {
152           eiProducer.ei_producer_types = producer[1].supported_ei_types;
153         }
154         if (producer[2].operational_state) {
155           eiProducer.status = producer[2].operational_state.toString();
156         }
157         return eiProducer;
158       });
159       this.producerSubject.next(producers);
160     }, err => {
161       console.error("Subscribe function error:" + err);
162     });
163   }
164 }