44af50a50a1a6f1f163a22c12cd80d74042a849e
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ei-coordinator / ei-producer.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 { 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 { merge } from 'rxjs';
27 import { of } from 'rxjs/observable/of';
28 import { catchError, finalize, map, tap } from 'rxjs/operators';
29 import { EIProducer } from '../interfaces/ei.jobs';
30 import { EIService } from '../services/ei/ei.service';
31 import { NotificationService } from '../services/ui/notification.service';
32
33 export class EIProducerDataSource extends DataSource<EIProducer> {
34
35     private producerSubject = new BehaviorSubject<EIProducer[]>([]);
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 eiSvc: EIService,
45         public sort: MatSort,
46         private notificationService: NotificationService) {
47         super();
48     }
49
50     loadTable() {
51         this.loadingSubject.next(true);
52         this.eiSvc.getEIProducers()
53             .pipe(
54                 catchError((her: HttpErrorResponse) => {
55                     this.notificationService.error('Failed to get producers: ' + her.error);
56                     return of([]);
57                 }),
58                 finalize(() => this.loadingSubject.next(false))
59             )
60             .subscribe((prods: EIProducer[]) => {
61                 console.log("Producers: " + prods);
62                 this.rowCount = prods.length;
63                 this.producerSubject.next(prods);
64             });
65             this.connect();
66     }
67
68     connect(): Observable<EIProducer[]> {
69         const dataMutations = [
70             this.producerSubject.asObservable(),
71             this.sort.sortChange
72         ];
73         return merge(...dataMutations).pipe(map(() => {
74             return this.getSortedData([...this.producerSubject.getValue()]);
75         }));
76     }
77
78     disconnect(): void {
79         this.producerSubject.complete();
80         this.loadingSubject.complete();
81     }
82
83     private getSortedData(data: EIProducer[]) {
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 'id': return compare(a.ei_producer_id, b.ei_producer_id, isAsc);
92                 case 'type': return compare(a.ei_producer_types[0], b.ei_producer_types[0], isAsc);
93                 case 'status': return compare(a.status, b.status, isAsc);
94                 default: return 0;
95             }
96         });
97     }
98
99     getProducers(): Observable<EIProducer[]> {
100         return this.eiSvc.getEIProducers()
101         .pipe(tap(console.log));
102     }
103 }
104
105 function compare(a: string, b: string, isAsc: boolean) {
106     return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
107 }