225aabb3c05072dafc7d4f635db73063cbc649a0
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / policy-instance / 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 { MatSort } from '@angular/material/sort';
23 import { Observable } from 'rxjs/Observable';
24 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
25 import { merge } from 'rxjs';
26 import { map } from 'rxjs/operators';
27 import { PolicyInstance } from '@interfaces/policy.types';
28 import { PolicyService } from '@services/policy/policy.service';
29 import { NotificationService } from '@services/ui/notification.service';
30
31 export class PolicyInstanceDataSource extends DataSource<PolicyInstance> {
32
33     policyInstances: 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 policyTypeSchemaId: string) {
48         super();
49     }
50
51     public getPolicyInstances() {
52         this.policyInstances = [] as PolicyInstance[];
53         this.policySvc.getPolicyInstancesByType(this.policyTypeSchemaId).subscribe(policies => {
54             if (policies.policy_ids.length != 0) {
55                 policies.policy_ids.forEach(policyId => {
56                     this.policySvc.getPolicyInstance(policyId).subscribe(policyInstance => {
57                         this.policySvc.getPolicyStatus(policyId).subscribe(policyStatus => {
58                             policyInstance.lastModified = policyStatus.last_modified;
59                         })
60                         this.policyInstances.push(policyInstance);
61                     })
62                     this.policyInstanceSubject.next(this.policyInstances);
63                 })
64             }
65         })
66     }
67
68     connect(): Observable<PolicyInstance[]> {
69         const dataMutations = [
70             this.policyInstanceSubject.asObservable(),
71             this.sort.sortChange
72         ];
73         return merge(...dataMutations).pipe(map(() => {
74             return this.getSortedData([...this.policyInstanceSubject.getValue()]);
75         }));
76     }
77
78     disconnect(): void {
79         this.policyInstanceSubject.complete();
80         this.loadingSubject.complete();
81     }
82
83     private getSortedData(data: PolicyInstance[]) {
84         if (!this.sort || !this.sort.active || this.sort.direction === '') {
85             return data;
86         }
87
88         return data.sort((a, b) => {
89             const isAsc = this.sort.direction === 'asc';
90             switch (this.sort.active) {
91                 case 'instanceId': return compare(a.policy_id, b.policy_id, isAsc);
92                 case 'ric': return compare(a.ric_id, b.ric_id, isAsc);
93                 case 'service': return compare(a.service_id, b.service_id, isAsc);
94                 case 'lastModified': return compare(a.lastModified, b.lastModified, isAsc);
95                 default: return 0;
96             }
97         });
98     }
99 }
100
101 function compare(a: string, b: string, isAsc: boolean) {
102     return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
103 }