Add column sorting on ANR page
[portal/ric-dashboard.git] / webapp-frontend / src / app / anr-xapp / anr-xapp.datasource.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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 { CollectionViewer, DataSource } from '@angular/cdk/collections';
22 import { MatSort } from '@angular/material';
23 import { merge } from 'rxjs';
24 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
25 import { Observable } from 'rxjs/Observable';
26 import { of } from 'rxjs/observable/of';
27 import { catchError, finalize, map } from 'rxjs/operators';
28 import { ANRNeighborCellRelation } from '../interfaces/anr-xapp.types';
29 import { ANRXappService } from '../services/anr-xapp/anr-xapp.service';
30
31 // https://blog.angular-university.io/angular-material-data-table/
32 export class ANRXappDataSource extends DataSource<ANRNeighborCellRelation> {
33
34     private relationsSubject = new BehaviorSubject<ANRNeighborCellRelation[]>([]);
35
36     private loadingSubject = new BehaviorSubject<boolean>(false);
37
38     public loading$ = this.loadingSubject.asObservable();
39
40   constructor(private anrXappService: ANRXappService, private sort: MatSort) {
41         super();
42     }
43
44     loadTable(ggnodeb = '', servingCellNrcgi = '', neighborCellNrpci = '') {
45         this.loadingSubject.next(true);
46         this.anrXappService.getNcrtInfo(ggnodeb, servingCellNrcgi, neighborCellNrpci)
47             .pipe(
48                 catchError(() => of([])),
49                 finalize(() => this.loadingSubject.next(false))
50             )
51             .subscribe(ncrt => this.relationsSubject.next(ncrt));
52     }
53
54     connect(collectionViewer: CollectionViewer): Observable<ANRNeighborCellRelation[]> {
55       const dataMutations = [
56         this.relationsSubject.asObservable(),
57         this.sort.sortChange
58       ];
59       return merge(...dataMutations).pipe(map(() => {
60         return this.getSortedData([...this.relationsSubject.getValue()]);
61       }));
62     }
63
64     disconnect(collectionViewer: CollectionViewer): void {
65         this.relationsSubject.complete();
66         this.loadingSubject.complete();
67     }
68
69   private getSortedData(data: ANRNeighborCellRelation[]) {
70     if (!this.sort.active || this.sort.direction === '') {
71       return data;
72     }
73
74     return data.sort((a, b) => {
75       const isAsc = this.sort.direction === 'asc';
76       switch (this.sort.active) {
77         case 'cellIdentifierNrcgi': return compare(a.servingCellNrcgi, b.servingCellNrcgi, isAsc);
78         case 'neighborCellNrpci': return compare(a.neighborCellNrpci, b.neighborCellNrpci, isAsc);
79         case 'neighborCellNrcgi': return compare(a.neighborCellNrcgi, b.neighborCellNrcgi, isAsc);
80         case 'flagNoHo': return compare(a.flagNoHo, b.flagNoHo, isAsc);
81         case 'flagNoXn': return compare(a.flagNoXn, b.flagNoXn, isAsc);
82         case 'flagNoRemove': return compare(a.flagNoRemove, b.flagNoRemove, isAsc);
83         default: return 0;
84       }
85     });
86   }
87
88 }
89
90 function compare(a, b, isAsc) {
91   return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
92 }