member_webapp/@overflow/infra/component/infra-tree.component.ts

216 lines
5.8 KiB
TypeScript
Raw Permalink Normal View History

2018-06-07 06:07:45 +00:00
import {
2018-06-12 05:45:21 +00:00
Component, Input, OnChanges, SimpleChanges, OnInit, ViewChild
2018-06-07 06:07:45 +00:00
} from '@angular/core';
2018-06-15 12:31:44 +00:00
import { InfraService, InfraHost, InfraZone, Infra } from '@overflow/commons-typescript/model/infra';
2018-06-08 11:37:13 +00:00
import { Observable, of } from 'rxjs';
2018-06-21 09:35:24 +00:00
import { catchError, map, tap, take } from 'rxjs/operators';
2018-06-12 05:45:21 +00:00
import { TreeNode, MenuItem, ContextMenu } from 'primeng/primeng';
2018-06-21 09:35:24 +00:00
import { ProbeHost, MetaInfraTypeEnum, toMetaInfraType } from '@overflow/commons-typescript';
2018-06-20 10:17:38 +00:00
import { InfraService as InfraManageService } from '../service/infra.service';
2018-06-07 06:07:45 +00:00
@Component({
selector: 'of-infra-tree',
templateUrl: './infra-tree.component.html',
})
2018-06-08 11:37:13 +00:00
export class InfraTreeComponent implements OnInit, OnChanges {
@Input() probeHost: ProbeHost;
2018-06-21 09:35:24 +00:00
infraZones: InfraZone[];
infraHosts: InfraHost[];
infraServices: InfraService[];
2018-06-08 11:37:13 +00:00
pending$: Observable<boolean>;
error$: Observable<any>;
2018-06-12 05:45:21 +00:00
zoneNode: TreeNode[];
hostNode: TreeNode[];
contextMenuZone: MenuItem[];
contextMenuHost: MenuItem[];
contextMenuService: MenuItem[];
selectedNode: TreeNode;
@ViewChild('cmZone') cmZone: ContextMenu;
@ViewChild('cmHost') cmHost: ContextMenu;
@ViewChild('cmService') cmService: ContextMenu;
2018-06-07 06:07:45 +00:00
constructor(
2018-06-20 10:17:38 +00:00
private infraService: InfraManageService,
2018-06-07 06:07:45 +00:00
) {
}
2018-06-08 11:37:13 +00:00
ngOnInit(): void {
2018-06-12 05:45:21 +00:00
this.initContextMenu();
}
initContextMenu() {
this.contextMenuZone = [
{ label: 'Zone Menu', command: (event) => this.cmZone.hide() },
{ separator: true },
2018-06-21 09:35:24 +00:00
{
label: 'Discovery', icon: 'fa-plus', command: (event) => {
alert('discovery');
}
},
2018-06-12 05:45:21 +00:00
];
this.contextMenuHost = [
{ label: 'Host Menu', command: (event) => this.cmHost.hide() },
{ separator: true },
{ label: 'Add sensor', icon: 'fa-plus', command: (event) => alert('Add sensor') },
{ label: 'Traceroute', icon: 'fa-plus' },
{ label: 'ARP Test', icon: 'fa-plus' },
];
this.contextMenuService = [
{ label: 'Service Menu', command: (event) => this.cmService.hide() },
{ separator: true },
{ label: 'Add sensor', icon: 'fa-plus', command: (event) => alert('Add sensor') },
];
2018-06-08 11:37:13 +00:00
}
ngOnChanges(changes: SimpleChanges): void {
2018-06-15 12:31:44 +00:00
if (changes['probeHost'].isFirstChange) {
2018-06-12 05:45:21 +00:00
this.zoneNode = [];
this.hostNode = [];
2018-06-21 09:35:24 +00:00
this.getInfraWithInfraTypeKey(toMetaInfraType(MetaInfraTypeEnum.ZONE).key);
this.getInfraWithInfraTypeKey(toMetaInfraType(MetaInfraTypeEnum.HOST).key);
this.getInfraWithInfraTypeKey(toMetaInfraType(MetaInfraTypeEnum.SERVICE).key);
2018-06-12 05:45:21 +00:00
}
2018-06-08 11:37:13 +00:00
}
2018-06-21 09:35:24 +00:00
getInfraWithInfraTypeKey(metaInfraTypeKey: string) {
this.infraService.readAllByMetaInfraTypeKeyAndProbeID(
metaInfraTypeKey, this.probeHost.probe.id)
2018-06-08 11:37:13 +00:00
.pipe(
tap(() => {
this.pending$ = of(true);
}),
2018-06-21 09:35:24 +00:00
map((infras: Infra[]) => {
switch (metaInfraTypeKey) {
case toMetaInfraType(MetaInfraTypeEnum.ZONE).key:
this.infraZones = infras;
break;
case toMetaInfraType(MetaInfraTypeEnum.HOST).key:
this.infraHosts = infras;
break;
case toMetaInfraType(MetaInfraTypeEnum.SERVICE).key:
this.infraServices = infras;
break;
default:
break;
}
if (this.infraZones && this.infraHosts && this.infraServices) {
this.generateTreeData();
}
2018-06-08 11:37:13 +00:00
}),
catchError(error => {
this.error$ = of(error);
return of();
}),
tap(() => {
this.pending$ = of(false);
}),
take(1),
).subscribe();
}
2018-06-21 09:35:24 +00:00
generateTreeData() {
if (null === this.infraZones) {
return;
}
this.infraZones.forEach(infraZone => {
this.addZone(infraZone);
});
if (null === this.infraHosts) {
return;
}
this.infraHosts.forEach(infraHost => {
this.addHost(infraHost);
});
if (null === this.infraServices) {
return;
}
this.infraServices.forEach(infraService => {
this.addService(infraService);
2018-06-08 11:37:13 +00:00
});
}
2018-06-15 12:31:44 +00:00
addZone(infraZone: InfraZone) {
this.zoneNode.push({
label: infraZone.network + '(' + infraZone.iface + ')',
type: 'ZONE',
data: {
target: infraZone,
subLabel: 'Something to display'
},
children: this.hostNode,
expanded: true
});
}
addHost(infraHost: InfraHost) {
let ipAddr = infraHost.infraHostIPs[0].address.split('/')[0];
if (infraHost.infraHostOS && infraHost.infraHostOS.name) {
ipAddr += '(' + infraHost.infraHostOS.name + ')';
}
2018-06-08 11:37:13 +00:00
this.hostNode.push({
type: 'HOST',
2018-06-15 12:31:44 +00:00
label: ipAddr,
2018-06-08 11:37:13 +00:00
data: {
2018-06-15 12:31:44 +00:00
target: infraHost,
2018-06-08 11:37:13 +00:00
},
expanded: true,
children: []
});
}
2018-06-15 12:31:44 +00:00
addService(infraService: InfraService) {
2018-06-12 05:45:21 +00:00
const idx = this.findHostIndex(infraService);
if (idx === -1) {
// this.addHost(infraService.infraHost);
// this.addService(infraService);
return;
}
this.hostNode[idx].children.push({
2018-06-08 11:37:13 +00:00
type: 'SERVICE',
label: 'TODO',
data: {
target: infraService
},
});
}
2018-06-12 05:45:21 +00:00
2018-06-15 12:31:44 +00:00
findHostIndex(infraService: InfraService): number {
let idx = -1;
2018-06-12 05:45:21 +00:00
this.hostNode.forEach((node, index) => {
2018-06-15 12:31:44 +00:00
const infraHost: InfraHost = node.data.target;
for (const infraHostIP of infraHost.infraHostIPs) {
if (infraHostIP.id === infraService.infraHostPort.infraHostIP.id) {
idx = index;
return;
}
}
2018-06-12 05:45:21 +00:00
});
return idx;
}
showContextMenu(event: MouseEvent, node: any) {
this.selectedNode = node;
this.cmZone.hide();
this.cmHost.hide();
this.cmService.hide();
if (node.type === 'ZONE') {
this.cmZone.show(event);
} else if (node.type === 'HOST') {
this.cmHost.show(event);
} else if (node.type === 'SERVICE') {
this.cmService.show(event);
}
return false;
}
2018-06-07 06:07:45 +00:00
}