d57f9e19c7859df0189a57b6aa82fc33aaaccdf8
[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
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 { 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 { of } from 'rxjs/observable/of';
27 import { merge } from 'rxjs';
28 import { catchError, finalize, map } from 'rxjs/operators';
29 import { ANRNeighborCellRelation } from '../interfaces/anr-xapp.types';
30 import { ANRXappService } from '../services/anr-xapp/anr-xapp.service';
31 import { NotificationService } from '../services/ui/notification.service';
32
33 export class ANRXappDataSource extends DataSource<ANRNeighborCellRelation> {
34
35   private relationsSubject = new BehaviorSubject<ANRNeighborCellRelation[]>([]);
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(private anrXappService: ANRXappService,
44     private sort: MatSort,
45     private notificationService: NotificationService) {
46     super();
47   }
48
49   loadTable(ggnodeb: string = '', servingCellNrcgi: string = '', neighborCellNrpci: string = '') {
50     this.loadingSubject.next(true);
51     this.anrXappService.getNcrtInfo(ggnodeb, servingCellNrcgi, neighborCellNrpci)
52       .pipe(
53         catchError( (her: HttpErrorResponse) => {
54           console.log('ANRXappDataSource failed: ' + her.message);
55           this.notificationService.error('Failed to get data: ' + her.message);
56           return of([]);
57         }),
58         finalize(() => this.loadingSubject.next(false))
59       )
60       .subscribe( (ncrt: ANRNeighborCellRelation[]) => {
61         this.rowCount = ncrt.length;
62         this.relationsSubject.next(ncrt);
63       });
64   }
65
66   connect(collectionViewer: CollectionViewer): Observable<ANRNeighborCellRelation[]> {
67     const dataMutations = [
68       this.relationsSubject.asObservable(),
69       this.sort.sortChange
70     ];
71     return merge(...dataMutations).pipe(map(() => {
72       return this.getSortedData([...this.relationsSubject.getValue()]);
73     }));
74   }
75
76   disconnect(collectionViewer: CollectionViewer): void {
77     this.relationsSubject.complete();
78     this.loadingSubject.complete();
79   }
80
81   private getSortedData(data: ANRNeighborCellRelation[]) {
82     if (!this.sort.active || this.sort.direction === '') {
83       return data;
84     }
85     return data.sort((a: ANRNeighborCellRelation, b: ANRNeighborCellRelation) => {
86       const isAsc = this.sort.direction === 'asc';
87       switch (this.sort.active) {
88         case 'cellIdentifierNrcgi': return compare(a.servingCellNrcgi, b.servingCellNrcgi, isAsc);
89         case 'neighborCellNrpci': return compare(a.neighborCellNrpci, b.neighborCellNrpci, isAsc);
90         case 'neighborCellNrcgi': return compare(a.neighborCellNrcgi, b.neighborCellNrcgi, isAsc);
91         case 'flagNoHo': return compare(a.flagNoHo, b.flagNoHo, isAsc);
92         case 'flagNoXn': return compare(a.flagNoXn, b.flagNoXn, isAsc);
93         case 'flagNoRemove': return compare(a.flagNoRemove, b.flagNoRemove, isAsc);
94         default: return 0;
95       }
96     });
97   }
98 }
99
100 function compare(a: any, b: any, isAsc: boolean) {
101   return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
102 }