2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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 { 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 { XappControlRow, XMDeployedApp, XMXappInstance } from '../interfaces/app-mgr.types';
30 import { AppMgrService } from '../services/app-mgr/app-mgr.service';
31 import { NotificationService } from '../services/ui/notification.service';
33 export class AppControlDataSource extends DataSource<XappControlRow> {
35 private appControlSubject = new BehaviorSubject<XappControlRow[]>([]);
37 private loadingSubject = new BehaviorSubject<boolean>(false);
39 public loading$ = this.loadingSubject.asObservable();
41 public rowCount = 1; // hide footer during intial load
43 private emptyInstances: XMXappInstance =
53 constructor(private appMgrSvc: AppMgrService,
54 private sort: MatSort,
55 private notificationService: NotificationService) {
59 loadTable(instanceKey: string) {
60 this.loadingSubject.next(true);
61 this.appMgrSvc.getDeployed(instanceKey)
63 catchError((her: HttpErrorResponse) => {
64 console.log('AppControlDataSource failed: ' + her.message);
65 this.notificationService.error('Failed to get applications: ' + her.message);
68 finalize(() => this.loadingSubject.next(false))
70 .subscribe((xApps: XMDeployedApp[]) => {
71 this.rowCount = xApps.length;
72 const flattenedApps = this.flatten(xApps);
73 this.appControlSubject.next(flattenedApps);
77 connect(collectionViewer: CollectionViewer): Observable<XappControlRow[]> {
78 const dataMutations = [
79 this.appControlSubject.asObservable(),
82 return merge(...dataMutations).pipe(map(() => {
83 return this.getSortedData([...this.appControlSubject.getValue()]);
87 disconnect(collectionViewer: CollectionViewer): void {
88 this.appControlSubject.complete();
89 this.loadingSubject.complete();
92 private flatten(allxappdata: XMDeployedApp[]): XappControlRow[] {
93 const xAppInstances: XappControlRow[] = [];
94 for (const xapp of allxappdata) {
95 if (!xapp.instances) {
96 const row: XappControlRow = {
98 instance: this.emptyInstances
100 xAppInstances.push(row);
102 for (const ins of xapp.instances) {
103 const row: XappControlRow = {
107 xAppInstances.push(row);
111 return xAppInstances;
114 private getSortedData(data: XappControlRow[]) {
115 if (!this.sort.active || this.sort.direction === '') {
119 return data.sort((a, b) => {
120 const isAsc = this.sort.direction === 'asc';
121 switch (this.sort.active) {
122 case 'xapp': return compare(a.xapp, b.xapp, isAsc);
123 case 'name': return compare(a.instance.name, b.instance.name, isAsc);
124 case 'status': return compare(a.instance.status, b.instance.status, isAsc);
125 case 'ip': return compare(a.instance.ip, b.instance.ip, isAsc);
126 case 'port': return compare(a.instance.port, b.instance.port, isAsc);
133 function compare(a: any, b: any, isAsc: boolean) {
134 return (a < b ? -1 : 1) * (isAsc ? 1 : -1);