Misc improvements in front end
[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 { MatSort } from '@angular/material/sort';
22 import { animate, state, style, transition, trigger } from '@angular/animations';
23 import { FormBuilder, FormGroup } from '@angular/forms';
24 import { MatTableDataSource } from '@angular/material';
25
26 import { defer, BehaviorSubject, Observable } from 'rxjs';
27 import { map, withLatestFrom, startWith, tap } from 'rxjs/operators';
28
29 import { EIService } from '../services/ei/ei.service';
30 import { EIJob, EIProducer } from '../interfaces/ei.jobs';
31 import { EIProducerDataSource } from './ei-producer.datasource';
32 import { EIJobDataSource } from './ei-job.datasource';
33 import { NotificationService } from '../services/ui/notification.service';
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     constructor(
70         private eiSvc: EIService,
71         private notificationService: NotificationService,
72         private ui: UiService,
73         private formBuilder: FormBuilder) {
74             this.formGroup = formBuilder.group({ filter: [""] });
75         }
76
77     ngOnInit() {
78         this.eiJobsDataSource = new EIJobDataSource(this.eiSvc, this.sort, this.notificationService);
79         this.eiProducersDataSource = new EIProducerDataSource(this.eiSvc, this.sort, this.notificationService);
80         this.eiJobsDataSource.loadTable();
81         //this.eiProducersDataSource.loadTable();
82
83         this.producers$= this.eiProducersDataSource.getProducers();
84         this.filteredProducers$ = defer(() => this.formGroup.get("filter")
85         .valueChanges.pipe(
86             startWith(""),
87             withLatestFrom(this.producers$),
88             map(([val, producers]) =>
89             !val ? producers : producers.filter((x) =>
90             x.ei_producer_id.toLowerCase().includes(val))))
91         );
92
93         this.ui.darkModeState.subscribe((isDark) => {
94             this.darkMode = isDark;
95         });
96     }
97
98     getEIJobInfo(eiJob: EIJob): EIJobInfo {
99         let info: EIJobInfo = this.eiJobInfo.get(eiJob.ei_job_data);
100         if (!info) {
101             info = new EIJobInfo(eiJob);
102             this.eiJobInfo.set(eiJob.ei_job_data, info);
103         }
104         return info;
105     }
106
107     getDisplayName(eiJob: EIJob): string {
108         if (eiJob.ei_job_identity) {
109             return eiJob.ei_job_identity;
110         }
111         return '< No id >';
112     }
113
114     getEITypeId(eiJob: EIJob): string {
115         if (eiJob.ei_type_identity) {
116             return eiJob.ei_type_identity;
117         }
118         return '< No type >';
119     }
120
121     getTargetUri(eiJob: EIJob): string {
122         if (eiJob.target_uri) {
123             return eiJob.target_uri;
124         }
125         return '< No target URI >';
126     }
127
128     isInstancesShown(eiJob: EIJob): boolean {
129         return this.getEIJobInfo(eiJob).isExpanded.getValue();
130     }
131
132     getExpandedObserver(eiJob: EIJob): Observable<boolean> {
133         return this.getEIJobInfo(eiJob).isExpanded.asObservable();
134     }
135
136     getEIProducerId(eiProducer: EIProducer): string {
137         if (eiProducer.ei_producer_id) {
138             return eiProducer.ei_producer_id;
139         }
140         return '< No id>';
141     }
142
143     getEIProducerTypes(eiProducer: EIProducer): string[] {
144         if (eiProducer.ei_producer_types) {
145             return eiProducer.ei_producer_types;
146         }
147         return ['< No types >'];
148     }
149
150     getEIProducerStatus(eiProducer: EIProducer): string {
151         if (eiProducer.status) {
152             return eiProducer.status;
153         }
154         return '< No status >';
155     }
156
157     refreshTables() {
158         this.eiJobsDataSource.loadTable();
159         this.eiProducersDataSource.loadTable();
160     }
161 }