79b7129a3feb4f804eff65a4d54b07216cac79b9
[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 } from '@angular/core';
21 import { Sort } from '@angular/material/sort';
22 import { FormBuilder, FormGroup, AbstractControl } from '@angular/forms';
23 import { MatTableDataSource } from '@angular/material/table';
24
25 import { EIJob } from '../interfaces/ei.types';
26 import { EIJobDataSource } from './ei-job.datasource';
27 import { UiService } from '../services/ui/ui.service';
28 import { ProducersListComponent } from './producers-list/producers-list.component';
29
30 @Component({
31     selector: 'nrcp-ei-coordinator',
32     templateUrl: './ei-coordinator.component.html',
33     styleUrls: ['./ei-coordinator.component.scss']
34 })
35 export class EICoordinatorComponent implements OnInit {
36
37     darkMode: boolean;
38     formGroup: FormGroup;
39     jobsDataSource: MatTableDataSource<EIJob> = new MatTableDataSource<EIJob>();
40
41     readonly jobsFormControl: AbstractControl;
42
43     constructor(
44         private producersList: ProducersListComponent,
45         private eiJobsDataSource: EIJobDataSource,
46         private ui: UiService,
47         private formBuilder: FormBuilder) {
48             this.formGroup = formBuilder.group({ filter: [""] });
49
50             this.jobsFormControl = formBuilder.group({
51                 id: '',
52                 typeId: '',
53                 owner: '',
54                 targetUri:''
55             });
56     }
57
58     ngOnInit() {
59         this.eiJobsDataSource.loadJobs();
60
61         this.eiJobsDataSource.eiJobsSubject().subscribe((data) => {
62             this.jobsDataSource.data = data;
63         });
64
65         this.jobsFormControl.valueChanges.subscribe(value => {
66             const filter = {...value, id: value.id.trim().toLowerCase()} as string;
67             this.jobsDataSource.filter = filter;
68         });
69
70         this.jobsDataSource.filterPredicate = ((data: EIJob, filter) => {
71             return this.isDataIncluding(data.ei_job_identity, filter.id)
72                 && this.isDataIncluding(data.target_uri, filter.targetUri)
73                 && this.isDataIncluding(data.owner, filter.owner)
74                 && this.isDataIncluding(data.ei_type_identity, filter.typeId);
75           }) as (data: EIJob, filter: any) => boolean;
76
77         this.ui.darkModeState.subscribe((isDark) => {
78             this.darkMode = isDark;
79         });
80     }
81
82     sortJobs(sort: Sort){
83         const data = this.jobsDataSource.data
84         data.sort((a: EIJob, b: EIJob) => {
85             const isAsc = sort.direction === 'asc';
86             switch (sort.active) {
87               case 'id': return this.compare(a.ei_job_identity, b.ei_job_identity, isAsc);
88               case 'typeId': return this.compare(a.ei_type_identity, b.ei_type_identity, isAsc);
89               case 'owner': return this.compare(a.owner, b.owner, isAsc);
90               case 'targetUri': return this.compare(a.target_uri, b.owner, isAsc);
91               default: return 0;
92             }
93           });
94           this.jobsDataSource.data = data;
95     }
96
97     compare(a: any, b: any, isAsc: boolean) {
98       return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
99     }
100
101     stopSort(event: any){
102         event.stopPropagation();
103     }
104
105     isDataIncluding(data: string, filter: string) : boolean {
106         return !filter || data.toLowerCase().includes(filter);
107     }
108
109     getJobTypeId(eiJob: EIJob): string {
110         if (eiJob.ei_type_identity) {
111             return eiJob.ei_type_identity;
112         }
113         return '< No type >';
114     }
115
116     getJobOwner(eiJob: EIJob): string {
117         if (eiJob.owner) {
118             return eiJob.owner;
119         }
120         return '< No owner >';
121     }
122     refreshTables() {
123         this.eiJobsDataSource.loadJobs();
124         this.eiJobsDataSource.eiJobsSubject().subscribe((data) => {
125             this.jobsDataSource.data = data;
126         });
127
128         this.producersList.refresh();
129     }
130 }