From 780d3b5b9317e4a0be9dbc0d338d4c3093f7f16a Mon Sep 17 00:00:00 2001 From: insanity Date: Wed, 20 Jun 2018 19:17:38 +0900 Subject: [PATCH] discovery in progress --- .../component/discovery.component.ts | 2 +- .../component/search-result.component.html | 28 +-- .../component/search-result.component.ts | 218 +++++++++++++----- .../infra/component/infra-tree.component.ts | 4 +- tsconfig.json | 4 +- tslint.json | 5 +- 6 files changed, 179 insertions(+), 82 deletions(-) diff --git a/@overflow/discovery/component/discovery.component.ts b/@overflow/discovery/component/discovery.component.ts index 4f6f1a1..a2a887d 100644 --- a/@overflow/discovery/component/discovery.component.ts +++ b/@overflow/discovery/component/discovery.component.ts @@ -93,7 +93,7 @@ export class DiscoveryComponent implements OnDestroy { } case 'DiscoveryService.discoveredPort': { const port = discoveryNotify.params as Port; - this.discoveryResult.addPort(port); + // this.discoveryResult.addPort(port); break; } case 'DiscoveryService.discoveredService': { diff --git a/@overflow/discovery/component/search-result.component.html b/@overflow/discovery/component/search-result.component.html index 77d91fd..7e17698 100644 --- a/@overflow/discovery/component/search-result.component.html +++ b/@overflow/discovery/component/search-result.component.html @@ -16,13 +16,13 @@
- +
- +
@@ -31,12 +31,12 @@
-
- @@ -50,12 +50,12 @@ - \ No newline at end of file + + + + + + \ No newline at end of file diff --git a/@overflow/discovery/component/search-result.component.ts b/@overflow/discovery/component/search-result.component.ts index bd0f34d..9fbcdd9 100644 --- a/@overflow/discovery/component/search-result.component.ts +++ b/@overflow/discovery/component/search-result.component.ts @@ -9,10 +9,10 @@ import { TreeNode, Message, Tree } from 'primeng/primeng'; import { ProbeHost } from '@overflow/commons-typescript/model/probe'; import { Anim } from './animation'; import { TargetService } from '@overflow/target/service/target.service'; -import { InfraService, InfraHost, MetaTargetHostTypeEnum, toMetaTargetHostType, Infra } from '@overflow/commons-typescript'; +import { InfraService, InfraHost, MetaTargetHostTypeEnum, toMetaTargetHostType, Infra, MetaInfraTypeEnum, toMetaInfraTypeEnum, toMetaInfraType, InfraZone, Target, Page, PageParams } from '@overflow/commons-typescript'; import { InfraService as InfraRegistService } from '../../infra/service/infra.service'; import { Observable, of, Subscription } from 'rxjs'; -import { catchError, exhaustMap, map, tap, take } from 'rxjs/operators'; +import { catchError, map, tap, take } from 'rxjs/operators'; @Component({ selector: 'of-discovery-result', @@ -30,6 +30,9 @@ export class SearchResultComponent implements OnInit, OnChanges { @Input() filterServices: Map; @Input() finished: boolean; + infras: Infra[]; + existTargets: Target[]; + discoverySubscription: Subscription; zoneNode: TreeNode[] = []; hostNode: TreeNode[] = []; @@ -37,20 +40,17 @@ export class SearchResultComponent implements OnInit, OnChanges { msgs: Message[]; error$: Observable; - targetSaveSucceed: boolean; - displayTargetDone: boolean; - discoveredHosts: Host[] = []; discoveredServices: Service[] = []; + infraSaved: boolean; + targetSaved: boolean; pending$: Observable; constructor( - private targetService: TargetService, - private infraRegistService: InfraRegistService + private infraRegistService: InfraRegistService, + private targetService: TargetService ) { - this.targetSaveSucceed = false; - this.displayTargetDone = false; } ngOnInit(): void { @@ -62,17 +62,65 @@ export class SearchResultComponent implements OnInit, OnChanges { children: this.hostNode, expanded: true }); + this.getExistTarget(); + } + + 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) => { + this.existTargets = targetPage.content; + }), + catchError(error => { + this.error$ = of(error); + return of(); + }), + tap(() => { + }), + take(1), + ).subscribe(); } ngOnChanges(changes: SimpleChanges): void { if (changes['finished'] && changes['finished'].currentValue === true) { - this.msgs = []; - this.msgs.push({ - severity: 'success', - summary: 'Discovery가 완료되었습니다. 모니터링 대상(들)을 선택 후 저장하세요.', - }); this.saveDiscoveredInfras(); + } + } + displayInform() { + this.msgs = []; + this.msgs.push({ + severity: 'success', + summary: 'Discovery가 완료되었습니다. 모니터링 대상(들)을 선택 후 저장하세요.', + }); + } + + rerenderInfras() { + this.hostNode = []; + for (const infra of this.infras) { + switch (infra.metaInfraType) { + case toMetaInfraType(MetaInfraTypeEnum.ZONE): + const infraZone: InfraZone = infra; + break; + 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; + } } } @@ -83,33 +131,36 @@ export class SearchResultComponent implements OnInit, OnChanges { this.discoveredServices) .pipe( tap(() => { + this.infraSaved = false; this.pending$ = of(true); }), map((infras: Infra[]) => { - console.log(infras); + if (infras) { + this.infras = infras; + this.rerenderInfras(); + this.displayInform(); + } }), catchError(error => { this.error$ = of(error); return of(); }), tap(() => { + this.infraSaved = true; this.pending$ = of(false); }), take(1), ).subscribe(); } - addHost(host: Host) { const idx = this.findHostIndex(host); this.hostNode.splice(idx, 0, { type: 'HOST', label: host.address, data: { - exist: false, ip: this.convertIPtoNumber(host.address), mac: host.mac, - openPorts: [], target: host }, expanded: true, @@ -120,8 +171,8 @@ export class SearchResultComponent implements OnInit, OnChanges { } addService(service: Service) { - const targetHostNode = this.findHostNodeByService(service); - const idx = this.findServiceIndex(targetHostNode.children, service); + const targetHostNode = this.findHostNodeByService(service.port.host.address); + const idx = this.findServiceIndex(targetHostNode.children, service.name); targetHostNode.children.splice(idx, 0, { type: 'SERVICE', label: service.name + ' (' + service.port.portNumber + ')', @@ -135,15 +186,39 @@ export class SearchResultComponent implements OnInit, OnChanges { this.discoveredServices.push(service); } - addPort(port: Port) { - // this.hostNode.forEach(node => { - // if (node.data.id === port.host.id) { - // node.data.openPorts.push(port); - // return; - // } - // }); + 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: [] + }); } + 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 + }, + }); + } + + onTargetSelect(e, node: TreeNode) { const data = node.data.target; if (e.checked) { @@ -165,23 +240,23 @@ export class SearchResultComponent implements OnInit, OnChanges { return index; } - findServiceIndex(serviceNodes: TreeNode[], service: Service) { + findServiceIndex(serviceNodes: TreeNode[], serviceName: string) { let index = 0; serviceNodes.forEach(node => { // if (node.data.portNumber < service.port.portNumber) { // index++; // } - if (node.data.name.toUpperCase().localeCompare(service.name.toUpperCase()) === -1) { + if (node.data.name.toUpperCase().localeCompare(serviceName.toUpperCase()) === -1) { index++; } }); return index; } - findHostNodeByService(service: Service) { + findHostNodeByService(serviceAddress: string) { let targetHost = null; this.hostNode.forEach((value, i) => { - if (value.data.ip === this.convertIPtoNumber(service.port.host.address)) { + if (value.data.ip === this.convertIPtoNumber(serviceAddress)) { targetHost = value; return; } @@ -191,7 +266,6 @@ export class SearchResultComponent implements OnInit, OnChanges { convertIPtoNumber(ip: string) { return ip.split('.').map((octet, index, array) => { - // tslint:disable-next-line:radix return parseInt(octet) * Math.pow(256, (array.length - index - 1)); }).reduce((prev, curr) => { return prev + curr; @@ -209,36 +283,60 @@ export class SearchResultComponent implements OnInit, OnChanges { return highlight; } + 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; + } saveTargets() { - // const hosts: Host[] = []; - // const services: Service[] = []; - // this.selectedItems.forEach(value => { - // if (!value.port) { - // hosts.push(value); - // } else { - // services.push(value); - // } - // }); - // this.targetService.registDiscoveredTargets(this.probeHost.probe.id, hosts, services) - // .pipe( - // tap(() => { - // this.targetSaveSucceed = false; - // }), - // map((targets: Target[]) => { - // if (targets) { - // this.displayTargetDone = true; - // } - // }), - // catchError(error => { - // this.error$ = of(error); - // return of(); - // }), - // tap(() => { - // this.targetSaveSucceed = true; - // }), - // take(1), - // ).subscribe(); + 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(); } } diff --git a/@overflow/infra/component/infra-tree.component.ts b/@overflow/infra/component/infra-tree.component.ts index 210eb51..8ec9d22 100644 --- a/@overflow/infra/component/infra-tree.component.ts +++ b/@overflow/infra/component/infra-tree.component.ts @@ -7,7 +7,7 @@ import { catchError, exhaustMap, map, tap, take } from 'rxjs/operators'; import { TreeNode, MenuItem, ContextMenu } from 'primeng/primeng'; import { PageParams, Page } from '@overflow/commons-typescript/core/model'; import { ProbeHost, MetaInfraTypeEnum } from '@overflow/commons-typescript'; -import { InfraService as InfraCRUDService } from '../service/infra.service'; +import { InfraService as InfraManageService } from '../service/infra.service'; @Component({ selector: 'of-infra-tree', @@ -34,7 +34,7 @@ export class InfraTreeComponent implements OnInit, OnChanges { @ViewChild('cmService') cmService: ContextMenu; constructor( - private infraService: InfraCRUDService, + private infraService: InfraManageService, ) { } diff --git a/tsconfig.json b/tsconfig.json index af2a405..20c1331 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -32,17 +32,15 @@ "strictPropertyInitialization": true /* Enable strict checking of property initialization in classes. */, // "noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */, // "alwaysStrict": true /* Parse in strict mode and emit "use strict" for each source file. */, - /* Additional Checks */ // "noUnusedLocals": true /* Report errors on unused locals. */, // "noUnusedParameters": true /* Report errors on unused parameters. */, "noImplicitReturns": true /* Report error when not all code paths in function return a value. */, "noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */, - /* Debugging Options */ "traceResolution": false /* Report module resolution log messages. */, "listEmittedFiles": false /* Print names of generated files part of the compilation. */, "listFiles": false /* Print names of files part of the compilation. */, "pretty": true /* Stylize errors and messages using color and context. */, } -} +} \ No newline at end of file diff --git a/tslint.json b/tslint.json index 3ea984c..610aa1a 100644 --- a/tslint.json +++ b/tslint.json @@ -125,6 +125,7 @@ "use-life-cycle-interface": true, "use-pipe-transform-interface": true, "component-class-suffix": true, - "directive-class-suffix": true + "directive-class-suffix": true, + "no-unused-variable": true } -} +} \ No newline at end of file