support-multiple-ric-instances
[portal/ric-dashboard.git] / webapp-frontend / src / app / caas-ingress / caas-ingress.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/sort';
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 { V1Pod, V1PodList } from '@kubernetes/client-node';
30 import { CaasIngressService } from '../services/caas-ingress/caas-ingress.service';
31 import { NotificationService } from '../services/ui/notification.service';
32
33 export class CaasIngressDataSource extends DataSource<V1Pod> {
34
35   private relationsSubject = new BehaviorSubject<V1Pod[]>([]);
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 caasIngressService: CaasIngressService,
44     private sort: MatSort,
45     private notificationService: NotificationService) {
46     super();
47   }
48
49   loadTable(instanceKey: string, cluster: string, namespace: string) {
50     this.loadingSubject.next(true);
51     this.caasIngressService.getPodList(instanceKey, cluster, namespace)
52       .pipe(
53         catchError((her: HttpErrorResponse) => {
54           console.log('CaasIngressDataSource 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((pl: V1PodList) => {
61         this.rowCount = pl.items.length;
62         // precompute container ready, restart counts to keep HTML simple
63         for (const v1pod of pl.items) {
64           v1pod['readyCount'] = 0;
65           v1pod['restartCount'] = 0;
66           for (const cs of v1pod.status.containerStatuses) {
67             if (cs.ready) {
68               v1pod['readyCount'] = v1pod['readyCount'] + 1;
69             }
70             v1pod['restartCount'] = v1pod['restartCount'] + cs.restartCount;
71           }
72         }
73         this.relationsSubject.next(pl.items);
74       });
75   }
76
77   connect(collectionViewer: CollectionViewer): Observable<V1Pod[]> {
78     const dataMutations = [
79       this.relationsSubject.asObservable(),
80       this.sort.sortChange
81     ];
82     return merge(...dataMutations).pipe(map(() => {
83       return this.getSortedData([...this.relationsSubject.getValue()]);
84     }));
85   }
86
87   disconnect(collectionViewer: CollectionViewer): void {
88     this.relationsSubject.complete();
89     this.loadingSubject.complete();
90   }
91
92   private getSortedData(data: V1Pod[]) {
93     if (!this.sort.active || this.sort.direction === '') {
94       return data;
95     }
96     return data.sort((a: V1Pod, b: V1Pod) => {
97       const isAsc = this.sort.direction === 'asc';
98       switch (this.sort.active) {
99         case 'namespace': return compare(a.metadata.namespace, b.metadata.namespace, isAsc);
100         case 'name': return compare(a.metadata.name, b.metadata.name, isAsc);
101         case 'status': return compare(a.status.phase, b.status.phase, isAsc);
102         case 'containers': return compare(a.spec.containers.length, b.spec.containers.length, isAsc);
103         case 'ip': return compare(a.status.podIP, b.status.podIP, isAsc);
104         case 'restartCount': return compare(a['restartCount'], b['restartCount'], isAsc);
105         case 'createTime': return compare(a.metadata.creationTimestamp, b.metadata.creationTimestamp, isAsc);
106         default: return 0;
107       }
108     });
109   }
110 }
111
112 function compare(a: any, b: any, isAsc: boolean) {
113   return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
114 }