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

185 lines
4.8 KiB
TypeScript
Raw 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-08 11:37:13 +00:00
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';
2018-06-12 05:45:21 +00:00
import { TreeNode, MenuItem, ContextMenu } from 'primeng/primeng';
2018-06-08 11:37:13 +00:00
import { PageParams, Page } from '@overflow/commons-typescript/core/model';
import { ProbeHost } from '@overflow/commons-typescript';
import { InfraService } 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;
infras: Infra[];
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-08 11:37:13 +00:00
private infraService: InfraService,
2018-06-07 06:07:45 +00:00
) {
}
2018-06-08 11:37:13 +00:00
ngOnInit(): void {
this.zoneNode.push({
label: this.probeHost.probe.cidr,
type: 'ZONE',
data: {
},
children: this.hostNode,
expanded: true
});
2018-06-12 05:45:21 +00:00
this.initContextMenu();
}
initContextMenu() {
this.contextMenuZone = [
{ label: 'Zone Menu', command: (event) => this.cmZone.hide() },
{ separator: true },
{ label: 'Discovery', icon: 'fa-plus', command: (event) => alert('Discovery') },
];
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-12 05:45:21 +00:00
console.log(changes);
if (changes['probeHost'].isFirstChange ) {
this.zoneNode = [];
this.hostNode = [];
this.getInfras();
}
2018-06-08 11:37:13 +00:00
}
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;
2018-06-12 05:45:21 +00:00
console.log(this.infras);
2018-06-08 11:37:13 +00:00
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
2018-06-12 05:45:21 +00:00
this.addService(infra);
2018-06-08 11:37:13 +00:00
break;
default:
break;
}
});
}
addHost(infraHost: InfraTypeHost) {
this.hostNode.push({
type: 'HOST',
2018-06-12 05:45:21 +00:00
label: infraHost.infraHostIPs[0].address,
2018-06-08 11:37:13 +00:00
data: {
target: infraHost
},
expanded: true,
children: []
});
}
addService(infraService: InfraTypeService) {
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
findHostIndex(infraService: InfraTypeService): number {
const idx = -1;
this.hostNode.forEach((node, index) => {
// if (node.data.target.id === infraService.infraHost.id) {
// idx = index;
// return;
// }
});
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
}