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