4bf2aad0ca7caea54a847cbc141cb3864f6c3afb
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy-control / policy-instance.datasource.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
21 import { DataSource } from '@angular/cdk/collections';
22 import { HttpErrorResponse } from '@angular/common/http';
23 import { MatSort } from '@angular/material';
24 import { Observable } from 'rxjs/Observable';
25 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
26 import { merge } from 'rxjs';
27 import { of } from 'rxjs/observable/of';
28 import { catchError, finalize, map } from 'rxjs/operators';
29 import { PolicyInstance, PolicyTypeSchema } from '../interfaces/policy.types';
30 import { PolicyService } from '../services/policy/policy.service';
31 import { NotificationService } from '../services/ui/notification.service';
32
33 export class PolicyInstanceDataSource extends DataSource<PolicyInstance> {
34
35     private policyInstanceSubject = new BehaviorSubject<PolicyInstance[]>([]);
36
37     private loadingSubject = new BehaviorSubject<boolean>(false);
38
39     public loading$ = this.loadingSubject.asObservable();
40
41     public rowCount = 1; // hide footer during intial load
42
43     constructor(
44         private policySvc: PolicyService,
45         public sort: MatSort,
46         private notificationService: NotificationService,
47         private policyTypeSchema: PolicyTypeSchema) {
48         super();
49     }
50
51     loadTable() {
52         this.loadingSubject.next(true);
53         this.policySvc.getPolicyInstances(this.policyTypeSchema.name)
54             .pipe(
55                 catchError((her: HttpErrorResponse) => {
56                     this.notificationService.error('Failed to get policy instances: ' + her.error);
57                     return of([]);
58                 }),
59                 finalize(() => this.loadingSubject.next(false))
60             )
61             .subscribe((instances: PolicyInstance[]) => {
62                 this.rowCount = instances.length;
63                 this.policyInstanceSubject.next(instances);
64             });
65     }
66
67     connect(): Observable<PolicyInstance[]> {
68         const dataMutations = [
69             this.policyInstanceSubject.asObservable(),
70             this.sort.sortChange
71         ];
72         return merge(...dataMutations).pipe(map(() => {
73             return this.getSortedData([...this.policyInstanceSubject.getValue()]);
74         }));
75     }
76
77     disconnect(): void {
78         this.policyInstanceSubject.complete();
79         this.loadingSubject.complete();
80     }
81
82     private getSortedData(data: PolicyInstance[]) {
83         if (!this.sort || !this.sort.active || this.sort.direction === '') {
84             return data;
85         }
86
87         return data.sort((a, b) => {
88             const isAsc = this.sort.direction === 'asc';
89             switch (this.sort.active) {
90                 case 'instanceId': return compare(a.id, b.id, isAsc);
91                 case 'ric': return compare(a.ric, b.ric, isAsc);
92                 case 'service': return compare(a.service, b.service, isAsc);
93                 case 'lastModified': return compare(a.lastModified, b.lastModified, isAsc);
94                 default: return 0;
95             }
96         });
97     }
98 }
99
100 function compare(a: string, b: string, isAsc: boolean) {
101     return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
102 }