2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
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';
31 // https://blog.angular-university.io/angular-material-data-table/
32 export class ANRXappDataSource extends DataSource<ANRNeighborCellRelation> {
34 private relationsSubject = new BehaviorSubject<ANRNeighborCellRelation[]>([]);
36 private loadingSubject = new BehaviorSubject<boolean>(false);
38 public loading$ = this.loadingSubject.asObservable();
40 constructor(private anrXappService: ANRXappService, private sort: MatSort) {
44 loadTable(ggnodeb = '', servingCellNrcgi = '', neighborCellNrpci = '') {
45 this.loadingSubject.next(true);
46 this.anrXappService.getNcrtInfo(ggnodeb, servingCellNrcgi, neighborCellNrpci)
48 catchError(() => of([])),
49 finalize(() => this.loadingSubject.next(false))
51 .subscribe(ncrt => this.relationsSubject.next(ncrt));
54 connect(collectionViewer: CollectionViewer): Observable<ANRNeighborCellRelation[]> {
55 const dataMutations = [
56 this.relationsSubject.asObservable(),
59 return merge(...dataMutations).pipe(map(() => {
60 return this.getSortedData([...this.relationsSubject.getValue()]);
64 disconnect(collectionViewer: CollectionViewer): void {
65 this.relationsSubject.complete();
66 this.loadingSubject.complete();
69 private getSortedData(data: ANRNeighborCellRelation[]) {
70 if (!this.sort.active || this.sort.direction === '') {
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);
90 function compare(a, b, isAsc) {
91 return (a < b ? -1 : 1) * (isAsc ? 1 : -1);