member_webapp/@overflow/infra/component/infra-tree.component.ts
2018-06-08 20:37:13 +09:00

113 lines
2.7 KiB
TypeScript

import {
Component, Input, OnChanges, SimpleChanges, OnInit
} from '@angular/core';
import { Infra } from '@overflow/commons-typescript/model/infra';
import { InfraHost as InfraTypeHost } from '@overflow/commons-typescript/model/infra';
import { InfraService as InfraTypeService } from '@overflow/commons-typescript/model/infra';
import { Observable, of } from 'rxjs';
import { catchError, exhaustMap, map, tap, take } from 'rxjs/operators';
import { TreeNode } from 'primeng/primeng';
import { PageParams, Page } from '@overflow/commons-typescript/core/model';
import { ProbeHost } from '@overflow/commons-typescript';
import { InfraService } from '../service/infra.service';
@Component({
selector: 'of-infra-tree',
templateUrl: './infra-tree.component.html',
})
export class InfraTreeComponent implements OnInit, OnChanges {
@Input() probeHost: ProbeHost;
infras: Infra[];
pending$: Observable<boolean>;
error$: Observable<any>;
zoneNode: TreeNode[] = [];
hostNode: TreeNode[] = [];
constructor(
private infraService: InfraService,
) {
}
ngOnInit(): void {
this.zoneNode.push({
label: this.probeHost.probe.cidr,
type: 'ZONE',
data: {
},
children: this.hostNode,
expanded: true
});
}
ngOnChanges(changes: SimpleChanges): void {
this.getInfras();
}
getInfras() {
const pageParams: PageParams = {
pageNo: 0,
countPerPage: 99999,
sortCol: 'id',
sortDirection: 'descending'
};
this.infraService.readAllByProbeID(this.probeHost.probe.id, pageParams)
.pipe(
tap(() => {
this.pending$ = of(true);
}),
map((infraPage: Page<Infra>) => {
this.infras = infraPage.content;
this.generateTreeData(this.infras);
}),
catchError(error => {
this.error$ = of(error);
return of();
}),
tap(() => {
this.pending$ = of(false);
}),
take(1),
).subscribe();
}
generateTreeData(infras: Infra[]) {
infras.forEach(infra => {
switch (infra.metaInfraType.id) { // TODO: fix to enum
case 2: // InfraHost
this.addHost(infra);
break;
case 7: // InfraService
// this.addService(infra);
break;
default:
break;
}
});
}
addHost(infraHost: InfraTypeHost) {
this.hostNode.push({
type: 'HOST',
label: infraHost.ipv4,
data: {
target: infraHost
},
expanded: true,
children: []
});
}
addService(infraService: InfraTypeService) {
this.hostNode[0].children.push({
type: 'SERVICE',
label: 'TODO',
data: {
target: infraService
},
});
}
}