Reorganize dashboard into subfolders
[portal/ric-dashboard.git] / dashboard / webapp-frontend / src / app / catalog / catalog.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 { merge } from 'rxjs';
27 import { of } from 'rxjs/observable/of';
28 import { catchError, finalize, map } from 'rxjs/operators';
29 import { AppMgrService } from '../services/app-mgr/app-mgr.service';
30 import { XMDeployableApp } from '../interfaces/app-mgr.types';
31 import { NotificationService } from '../services/ui/notification.service';
32
33 export class CatalogDataSource extends DataSource<XMDeployableApp> {
34
35   private catalogSubject = new BehaviorSubject<XMDeployableApp[]>([]);
36   private loadingSubject = new BehaviorSubject<boolean>(false);
37   public loading$ = this.loadingSubject.asObservable();
38   public rowCount = 1; // hide footer during intial load
39
40   constructor(
41     private appMgrSvc: AppMgrService,
42     private sort: MatSort,
43     private notificationService: NotificationService) {
44     super();
45   }
46
47   loadTable(instanceKey: string) {
48     this.loadingSubject.next(true);
49     this.appMgrSvc.getDeployable(instanceKey)
50       .pipe(
51         catchError((her: HttpErrorResponse) => {
52           console.log('CatalogDataSource failed: ' + her.message);
53           this.notificationService.error('Failed to get applications: ' + her.message);
54           return of([]);
55         }),
56         finalize(() => this.loadingSubject.next(false))
57       )
58       .subscribe((xApps: XMDeployableApp[]) => {
59         this.rowCount = xApps.length;
60         this.catalogSubject.next(xApps);
61       });
62   }
63
64   connect(collectionViewer: CollectionViewer): Observable<XMDeployableApp[]> {
65     const dataMutations = [
66       this.catalogSubject.asObservable(),
67       this.sort.sortChange
68     ];
69     return merge(...dataMutations).pipe(map(() => {
70       return this.getSortedData([...this.catalogSubject.getValue()]);
71     }));
72   }
73
74   disconnect(collectionViewer: CollectionViewer): void {
75     this.catalogSubject.complete();
76     this.loadingSubject.complete();
77   }
78
79   private getSortedData(data: XMDeployableApp[]) {
80     if (!this.sort.active || this.sort.direction === '') {
81       return data;
82     }
83     return data.sort((a: XMDeployableApp, b: XMDeployableApp) => {
84       const isAsc = this.sort.direction === 'asc';
85       switch (this.sort.active) {
86         case 'name': return this.compare(a.name, b.name, isAsc);
87         case 'version': return this.compare(a.version, b.version, isAsc);
88         default: return 0;
89       }
90     });
91   }
92
93   private compare(a: any, b: any, isAsc: boolean) {
94     return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
95   }
96
97 }