blob: b82ee753eedc0049cef5f5f0c63748ff771ec3f8 [file] [log] [blame]
PatrikBuhra2bc79c2019-10-29 13:39:00 +01001/*-
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
21import { DataSource } from '@angular/cdk/collections';
22import { HttpErrorResponse } from '@angular/common/http';
23import { MatSort } from '@angular/material';
24import { Observable } from 'rxjs/Observable';
25import { BehaviorSubject } from 'rxjs/BehaviorSubject';
26import { merge } from 'rxjs';
27import { of } from 'rxjs/observable/of';
28import { catchError, finalize, map } from 'rxjs/operators';
29import { PolicyInstance } from '../interfaces/policy.types';
30import { PolicyService } from '../services/policy/policy.service';
31import { NotificationService } from '../services/ui/notification.service';
32import { PolicyType } from '../interfaces/policy.types';
33
34export class PolicyInstanceDataSource extends DataSource<PolicyInstance> {
35
36 private policyInstanceSubject = new BehaviorSubject<PolicyInstance[]>([]);
37
38 private loadingSubject = new BehaviorSubject<boolean>(false);
39
40 public loading$ = this.loadingSubject.asObservable();
41
42 public rowCount = 1; // hide footer during intial load
43
44 constructor(
PatrikBuhr47120052019-11-19 09:56:44 +010045 private policySvc: PolicyService,
PatrikBuhra2bc79c2019-10-29 13:39:00 +010046 public sort: MatSort,
PatrikBuhr47120052019-11-19 09:56:44 +010047 private notificationService: NotificationService,
PatrikBuhra2bc79c2019-10-29 13:39:00 +010048 private policyType: PolicyType) {
49 super();
50 }
51
52 loadTable() {
53 this.loadingSubject.next(true);
elinuxhenrikb033eaf2020-02-03 16:02:21 +010054 this.policySvc.getPolicyInstances(this.policyType.name)
PatrikBuhra2bc79c2019-10-29 13:39:00 +010055 .pipe(
56 catchError((her: HttpErrorResponse) => {
57 this.notificationService.error('Failed to get policy instances: ' + her.message);
58 return of([]);
59 }),
60 finalize(() => this.loadingSubject.next(false))
61 )
62 .subscribe((instances: PolicyInstance[]) => {
63 this.rowCount = instances.length;
64 this.policyInstanceSubject.next(instances);
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) {
elinuxhenrikb033eaf2020-02-03 16:02:21 +010091 case 'instanceId': return compare(a.id, b.id, isAsc);
92 case 'ric': return compare(a.ric, b.ric, isAsc);
93 case 'service': return compare(a.service, b.service, isAsc);
94 case 'lastModified': return compare(a.lastModified, b.lastModified, isAsc)
PatrikBuhra2bc79c2019-10-29 13:39:00 +010095 default: return 0;
96 }
97 });
98 }
99}
100
PatrikBuhr47120052019-11-19 09:56:44 +0100101function compare(a: string, b: string, isAsc: boolean) {
PatrikBuhra2bc79c2019-10-29 13:39:00 +0100102 return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
103}