Merge "Filtering the producer in the frontend"
[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, Version } from '@angular/core';
21 import { MatDialog } from '@angular/material/dialog';
22 import { MatSort } from '@angular/material/sort';
23 import { animate, state, style, transition, trigger } from '@angular/animations';
24 import { FormBuilder, FormGroup } from '@angular/forms';
25 import { MatTableDataSource } from '@angular/material';
26
27 import { EIService } from '../services/ei/ei.service';
28 import { EIJob, EIProducer } from '../interfaces/ei.jobs';
29 import { EIProducerDataSource } from './ei-producer.datasource';
30 import { EIJobDataSource } from './ei-job.datasource';
31 import { NotificationService } from '../services/ui/notification.service';
32 import { defer, BehaviorSubject, Observable } from 'rxjs';
33 import { map, withLatestFrom, startWith, tap } from 'rxjs/operators';
34 import { UiService } from '../services/ui/ui.service';
35
36 class EIJobInfo {
37     constructor(public eiJob: EIJob) { }
38
39     isExpanded: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
40 }
41
42 @Component({
43     selector: 'rd-ei-coordinator',
44     templateUrl: './ei-coordinator.component.html',
45     styleUrls: ['./ei-coordinator.component.scss'],
46     animations: [
47         trigger('detailExpand', [
48             state('collapsed, void', style({ height: '0px', minHeight: '0', display: 'none' })),
49             state('expanded', style({ height: '*' })),
50             transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
51             transition('expanded <=> void', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)'))
52         ]),
53     ],
54 })
55 export class EICoordinatorComponent implements OnInit {
56
57     eiJobsDataSource: EIJobDataSource;
58     eiProducersDataSource: EIProducerDataSource;
59     producers$: Observable<EIProducer[]>;
60     filteredProducers$: Observable<EIProducer[]>;
61     @ViewChild(MatSort, { static: true }) sort: MatSort;
62
63     eiJobInfo = new Map<string, EIJobInfo>();
64     darkMode: boolean;
65     searchString: string;
66     formGroup: FormGroup;
67     eiProducersData: MatTableDataSource<EIProducerDataSource>;
68
69
70     constructor(
71         private eiSvc: EIService,
72         private dialog: MatDialog,
73         private notificationService: NotificationService,
74         private ui: UiService,
75         private formBuilder: FormBuilder) {
76             this.formGroup = formBuilder.group({ filter: [""] });
77         }
78
79     ngOnInit() {
80         this.eiJobsDataSource = new EIJobDataSource(this.eiSvc, this.sort, this.notificationService);
81         this.eiProducersDataSource = new EIProducerDataSource(this.eiSvc, this.sort, this.notificationService);
82         this.eiJobsDataSource.loadTable();
83         //this.eiProducersDataSource.loadTable();
84
85         this.producers$= this.eiProducersDataSource.getProducers();
86         this.filteredProducers$ = defer(() => this.formGroup.get("filter")
87         .valueChanges.pipe(
88             startWith(""),
89             withLatestFrom(this.producers$),
90             map(([val, producers]) =>
91             !val ? producers : producers.filter((x) =>
92             x.ei_producer_id.toLowerCase().includes(val))))
93         );
94
95         this.ui.darkModeState.subscribe((isDark) => {
96             this.darkMode = isDark;
97         });
98     }
99
100     ngAfterViewInit() {
101         this.eiJobsDataSource.sort = this.sort;
102         this.eiProducersDataSource.sort = this.sort;
103
104       }
105
106     getEIJobInfo(eiJob: EIJob): EIJobInfo {
107         let info: EIJobInfo = this.eiJobInfo.get(eiJob.ei_job_data);
108         if (!info) {
109             info = new EIJobInfo(eiJob);
110             this.eiJobInfo.set(eiJob.ei_job_data, info);
111         }
112         return info;
113     }
114
115     getDisplayName(eiJob: EIJob): string {
116         if (eiJob.ei_job_identity) {
117             return eiJob.ei_job_identity;
118         }
119         return '< No id >';
120     }
121
122     getEITypeId(eiJob: EIJob): string {
123         if (eiJob.ei_type_identity) {
124             return eiJob.ei_type_identity;
125         }
126         return '< No type >';
127     }
128
129     getTargetUri(eiJob: EIJob): string {
130         if (eiJob.target_uri) {
131             return eiJob.target_uri;
132         }
133         return '< No target URI >';
134     }
135
136     isInstancesShown(eiJob: EIJob): boolean {
137         return this.getEIJobInfo(eiJob).isExpanded.getValue();
138     }
139
140     getExpandedObserver(eiJob: EIJob): Observable<boolean> {
141         return this.getEIJobInfo(eiJob).isExpanded.asObservable();
142     }
143
144     getEIProducerId(eiProducer: EIProducer): string {
145         if (eiProducer.ei_producer_id) {
146             return eiProducer.ei_producer_id;
147         }
148         return '< No id>';
149     }
150
151     getEIProducerTypes(eiProducer: EIProducer): string[] {
152         if (eiProducer.ei_producer_types) {
153             return eiProducer.ei_producer_types;
154         }
155         return ['< No types >'];
156     }
157
158     getEIProducerStatus(eiProducer: EIProducer): string {
159         if (eiProducer.status) {
160             return eiProducer.status;
161         }
162         return '< No status >';
163     }
164
165     refreshTables() {
166         this.eiJobsDataSource.loadTable();
167         this.eiProducersDataSource.loadTable();
168     }
169 }