member_webapp/@overflow/discovery/component/search-result.component.ts

349 lines
9.3 KiB
TypeScript
Raw Permalink Normal View History

2018-05-30 09:39:26 +00:00
import {
2018-06-14 03:11:56 +00:00
Component, Input,
2018-06-01 11:25:51 +00:00
OnInit,
2018-05-30 09:39:26 +00:00
} from '@angular/core';
2018-06-14 03:11:56 +00:00
import { Host, Port, Service } from '@overflow/commons-typescript/model/discovery';
2018-06-05 06:07:12 +00:00
import { TreeNode, Message, Tree } from 'primeng/primeng';
2018-06-01 11:25:51 +00:00
import { ProbeHost } from '@overflow/commons-typescript/model/probe';
2018-06-04 08:55:47 +00:00
import { Anim } from './animation';
2018-06-06 03:19:44 +00:00
import { TargetService } from '@overflow/target/service/target.service';
2018-06-20 10:17:38 +00:00
import { InfraService, InfraHost, MetaTargetHostTypeEnum, toMetaTargetHostType, Infra, MetaInfraTypeEnum, toMetaInfraTypeEnum, toMetaInfraType, InfraZone, Target, Page, PageParams } from '@overflow/commons-typescript';
2018-06-15 10:18:50 +00:00
import { InfraService as InfraRegistService } from '../../infra/service/infra.service';
import { Observable, of, Subscription } from 'rxjs';
2018-06-20 10:17:38 +00:00
import { catchError, map, tap, take } from 'rxjs/operators';
2018-06-04 08:55:47 +00:00
2018-05-30 09:39:26 +00:00
@Component({
selector: 'of-discovery-result',
templateUrl: './search-result.component.html',
2018-06-06 03:19:44 +00:00
animations: Anim,
2018-06-15 10:18:50 +00:00
providers: [
TargetService,
InfraRegistService
]
2018-05-30 09:39:26 +00:00
})
2018-06-21 09:35:24 +00:00
export class SearchResultComponent implements OnInit {
2018-05-30 09:39:26 +00:00
2018-06-01 11:25:51 +00:00
@Input() probeHost: ProbeHost;
2018-06-04 08:31:00 +00:00
@Input() filterWord: string;
2018-06-05 02:56:45 +00:00
@Input() filterServices: Map<string, boolean>;
2018-05-30 14:05:58 +00:00
2018-06-20 10:17:38 +00:00
infras: Infra[];
existTargets: Target[];
2018-06-01 11:25:51 +00:00
discoverySubscription: Subscription;
2018-06-01 07:32:16 +00:00
zoneNode: TreeNode[] = [];
hostNode: TreeNode[] = [];
2018-06-05 13:01:01 +00:00
selectedItems = [];
2018-06-05 02:56:45 +00:00
msgs: Message[];
2018-06-05 13:01:01 +00:00
error$: Observable<any>;
2018-06-05 06:07:12 +00:00
2018-06-15 10:18:50 +00:00
discoveredHosts: Host[] = [];
discoveredServices: Service[] = [];
2018-06-20 10:17:38 +00:00
infraSaved: boolean;
targetSaved: boolean;
2018-06-15 10:18:50 +00:00
pending$: Observable<boolean>;
2018-05-30 09:39:26 +00:00
constructor(
2018-06-20 10:17:38 +00:00
private infraRegistService: InfraRegistService,
private targetService: TargetService
2018-05-30 09:39:26 +00:00
) {
2018-06-01 11:25:51 +00:00
}
ngOnInit(): void {
2018-06-01 07:32:16 +00:00
this.zoneNode.push({
2018-06-01 11:25:51 +00:00
label: this.probeHost.probe.cidr,
type: 'ZONE',
data: {
},
2018-06-01 07:32:16 +00:00
children: this.hostNode,
expanded: true
});
2018-06-21 09:35:24 +00:00
// this.getExistTarget();
}
discoveryStarted(startedAt: Date) {
console.log('Discovery Started at: ' + startedAt);
}
discoveryStopped(stoppedAt: Date) {
console.log('Discovery Stopped at: ' + stoppedAt);
this.saveDiscoveredInfras();
2018-06-20 10:17:38 +00:00
}
getExistTarget() {
const pageParams: PageParams = {
pageNo: 0,
countPerPage: 99999,
sortCol: 'id',
sortDirection: 'descending'
};
this.targetService.readAllByProbeID(this.probeHost.probe.id, pageParams)
.pipe(
tap(() => {
}),
map((targetPage: Page<Target>) => {
this.existTargets = targetPage.content;
}),
catchError(error => {
this.error$ = of(error);
return of();
}),
tap(() => {
}),
take(1),
).subscribe();
2018-05-30 09:39:26 +00:00
}
2018-05-30 14:05:58 +00:00
2018-06-21 09:35:24 +00:00
// ngOnChanges(changes: SimpleChanges): void {
// if (changes['finished'] && changes['finished'].currentValue === true) {
// this.saveDiscoveredInfras();
// }
// }
2018-06-20 10:17:38 +00:00
displayInform() {
this.msgs = [];
this.msgs.push({
severity: 'success',
summary: 'Discovery가 완료되었습니다. 모니터링 대상(들)을 선택 후 저장하세요.',
});
}
2018-06-15 10:18:50 +00:00
2018-06-20 10:17:38 +00:00
rerenderInfras() {
this.hostNode = [];
for (const infra of this.infras) {
switch (infra.metaInfraType) {
2018-06-21 09:35:24 +00:00
// case toMetaInfraType(MetaInfraTypeEnum.ZONE):
// const infraZone: InfraZone = infra;
// break;
2018-06-20 10:17:38 +00:00
case toMetaInfraType(MetaInfraTypeEnum.HOST):
const infraHost: InfraHost = infra;
this.addInfraHost(infraHost);
break;
case toMetaInfraType(MetaInfraTypeEnum.SERVICE):
const infraService: InfraService = infra;
this.addInfraService(infraService)
break;
default:
break;
}
2018-06-05 02:56:45 +00:00
}
2018-05-30 14:05:58 +00:00
}
2018-06-15 10:18:50 +00:00
saveDiscoveredInfras() {
this.infraRegistService.registDiscoverd(
this.probeHost.probe.id,
this.discoveredHosts,
this.discoveredServices)
.pipe(
tap(() => {
2018-06-20 10:17:38 +00:00
this.infraSaved = false;
2018-06-15 10:18:50 +00:00
this.pending$ = of(true);
}),
map((infras: Infra[]) => {
2018-06-20 10:17:38 +00:00
if (infras) {
this.infras = infras;
this.rerenderInfras();
this.displayInform();
}
2018-06-15 10:18:50 +00:00
}),
catchError(error => {
this.error$ = of(error);
return of();
}),
tap(() => {
2018-06-20 10:17:38 +00:00
this.infraSaved = true;
2018-06-15 10:18:50 +00:00
this.pending$ = of(false);
}),
take(1),
).subscribe();
}
2018-06-04 08:31:00 +00:00
addHost(host: Host) {
2018-06-01 11:25:51 +00:00
const idx = this.findHostIndex(host);
2018-06-01 07:32:16 +00:00
this.hostNode.splice(idx, 0, {
2018-06-01 11:25:51 +00:00
type: 'HOST',
2018-06-12 05:45:21 +00:00
label: host.address,
2018-06-01 11:25:51 +00:00
data: {
2018-06-12 05:45:21 +00:00
ip: this.convertIPtoNumber(host.address),
2018-06-05 06:07:12 +00:00
mac: host.mac,
2018-06-05 09:32:36 +00:00
target: host
2018-06-01 11:25:51 +00:00
},
2018-06-01 07:32:16 +00:00
expanded: true,
children: []
});
2018-06-15 10:18:50 +00:00
this.discoveredHosts.push(host);
2018-06-01 07:32:16 +00:00
}
2018-06-04 12:50:46 +00:00
2018-06-04 08:31:00 +00:00
addService(service: Service) {
2018-06-20 10:17:38 +00:00
const targetHostNode = this.findHostNodeByService(service.port.host.address);
const idx = this.findServiceIndex(targetHostNode.children, service.name);
2018-06-04 12:50:46 +00:00
targetHostNode.children.splice(idx, 0, {
2018-06-01 11:25:51 +00:00
type: 'SERVICE',
2018-06-12 05:45:21 +00:00
label: service.name + ' (' + service.port.portNumber + ')',
2018-06-01 11:25:51 +00:00
data: {
2018-06-12 05:45:21 +00:00
name: service.name,
2018-06-15 11:03:25 +00:00
portType: service.port.metaPortType.key,
2018-06-01 11:25:51 +00:00
portNumber: service.port.portNumber,
2018-06-05 09:32:36 +00:00
target: service
2018-06-01 11:25:51 +00:00
},
2018-06-04 12:50:46 +00:00
});
2018-06-15 10:18:50 +00:00
this.discoveredServices.push(service);
2018-06-01 11:25:51 +00:00
}
2018-06-15 11:03:25 +00:00
2018-06-20 10:17:38 +00:00
addInfraHost(infraHost: InfraHost) {
const host: Host = {
address: infraHost.infraHostIPs[0].address,
}
const idx = this.findHostIndex(host);
this.hostNode.splice(idx, 0, {
type: 'HOST',
label: host.address,
data: {
ip: this.convertIPtoNumber(host.address),
target: infraHost
},
expanded: true,
children: []
});
2018-06-04 08:31:00 +00:00
}
2018-06-01 11:25:51 +00:00
2018-06-20 10:17:38 +00:00
addInfraService(infraService: InfraService) {
const targetHostNode = this.findHostNodeByService(infraService.infraHostPort.infraHostIP.address);
const idx = this.findServiceIndex(targetHostNode.children, infraService.metaTargetServiceType.name);
targetHostNode.children.splice(idx, 0, {
type: 'SERVICE',
label: infraService.metaTargetServiceType.name + ' (' + infraService.infraHostPort.port + ')',
data: {
name: infraService.metaTargetServiceType.name,
portType: infraService.infraHostPort.metaPortType.name,
portNumber: infraService.infraHostPort.port,
target: infraService
},
});
}
2018-06-06 03:19:44 +00:00
onTargetSelect(e, node: TreeNode) {
const data = node.data.target;
2018-06-01 11:25:51 +00:00
if (e.checked) {
this.selectedItems.push(data);
} else {
const index = this.selectedItems.indexOf(data);
this.selectedItems.splice(index, 1);
}
}
findHostIndex(host: Host): number {
let index = 0;
this.hostNode.forEach(node => {
2018-06-12 05:45:21 +00:00
if (node.data.ip < this.convertIPtoNumber(host.address)) {
2018-06-01 11:25:51 +00:00
index++;
}
});
return index;
}
2018-06-20 10:17:38 +00:00
findServiceIndex(serviceNodes: TreeNode[], serviceName: string) {
2018-06-01 11:25:51 +00:00
let index = 0;
serviceNodes.forEach(node => {
2018-06-04 12:50:46 +00:00
// if (node.data.portNumber < service.port.portNumber) {
2018-06-04 09:25:48 +00:00
// index++;
// }
2018-06-20 10:17:38 +00:00
if (node.data.name.toUpperCase().localeCompare(serviceName.toUpperCase()) === -1) {
2018-06-04 12:50:46 +00:00
index++;
}
2018-06-01 11:25:51 +00:00
});
return index;
}
2018-06-20 10:17:38 +00:00
findHostNodeByService(serviceAddress: string) {
2018-06-01 11:25:51 +00:00
let targetHost = null;
this.hostNode.forEach((value, i) => {
2018-06-20 10:17:38 +00:00
if (value.data.ip === this.convertIPtoNumber(serviceAddress)) {
2018-06-04 12:50:46 +00:00
targetHost = value;
2018-06-04 04:13:06 +00:00
return;
2018-06-01 11:25:51 +00:00
}
});
return targetHost;
}
convertIPtoNumber(ip: string) {
return ip.split('.').map((octet, index, array) => {
return parseInt(octet) * Math.pow(256, (array.length - index - 1));
}).reduce((prev, curr) => {
return prev + curr;
2018-06-01 07:32:16 +00:00
});
}
2018-06-04 08:31:00 +00:00
2018-06-06 03:19:44 +00:00
checkHighlight(label: string, type: number) {
2018-06-05 09:32:36 +00:00
let highlight = true;
if (this.filterWord && (label.toUpperCase().indexOf(this.filterWord.toUpperCase()) < 0)) {
highlight = false;
2018-06-05 02:56:45 +00:00
}
2018-06-06 03:19:44 +00:00
if (type === 1 && this.filterServices[label] === false) {
2018-06-05 09:32:36 +00:00
highlight = false;
2018-06-04 09:25:48 +00:00
}
2018-06-05 09:32:36 +00:00
return highlight;
2018-06-04 08:31:00 +00:00
}
2018-06-04 12:50:46 +00:00
2018-06-20 10:17:38 +00:00
checkExistTarget(infra: Infra): boolean {
if (!this.infraSaved) {
return false;
}
for (const target of this.existTargets) {
if (target.infra.id === infra.id) {
return true;
}
}
return false;
}
2018-06-05 06:07:12 +00:00
2018-06-05 02:56:45 +00:00
saveTargets() {
2018-06-20 10:17:38 +00:00
const targets: Target[] = [];
this.selectedItems.forEach(value => {
const infra: Infra = value;
let name: string;
if (value.metaInfraType === toMetaInfraType(MetaInfraTypeEnum.ZONE)) {
const infraZone: InfraZone = value;
name = infraZone.network;
} else if (value.metaInfraType === toMetaInfraType(MetaInfraTypeEnum.HOST)) {
const infraHost: InfraHost = value;
name = infraHost.infraHostIPs[0].address;
} else if (value.metaInfraType === toMetaInfraType(MetaInfraTypeEnum.SERVICE)) {
const infraService: InfraService = value;
name = infraService.metaInfraType.name;
}
const target: Target = {
infra: {
id: infra.id
},
name: name,
sensorCount: 0,
};
targets.push(target);
});
this.targetService.registAll(targets, this.probeHost.probe.id)
.pipe(
tap(() => {
}),
map((targets: Target[]) => {
if (targets) {
this.targetSaved = true;
}
}),
catchError(error => {
this.error$ = of(error);
return of();
}),
tap(() => {
}),
take(1),
).subscribe();
2018-06-04 12:50:46 +00:00
}
2018-06-06 08:57:19 +00:00
2018-05-30 09:39:26 +00:00
}