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

152 lines
3.6 KiB
TypeScript
Raw Normal View History

2018-05-30 09:39:26 +00:00
import {
AfterContentInit, Component, Input,
EventEmitter,
2018-05-30 14:05:58 +00:00
Output,
2018-06-01 11:25:51 +00:00
SimpleChanges,
OnInit,
2018-05-30 09:39:26 +00:00
} from '@angular/core';
2018-05-30 14:05:58 +00:00
import { Anim } from './animation';
2018-06-01 07:32:16 +00:00
import { TreeNode } from 'primeng/primeng';
2018-06-01 11:25:51 +00:00
import { DiscoveryNotify } from '../subscriber/discovery.subscriber';
import { DiscoverZone, Zone, Host, Port, Service } from '@overflow/commons-typescript/model/discovery';
import { Subscription } from 'rxjs';
import { ProbeHost } from '@overflow/commons-typescript/model/probe';
2018-05-30 09:39:26 +00:00
@Component({
selector: 'of-discovery-result',
templateUrl: './search-result.component.html',
2018-05-30 14:05:58 +00:00
animations: Anim
2018-05-30 09:39:26 +00:00
})
2018-06-01 11:25:51 +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-05-30 09:39:26 +00:00
@Output() stop = new EventEmitter();
2018-06-01 11:25:51 +00:00
@Input() started: boolean; // Temporary
2018-05-30 14:05:58 +00:00
2018-06-01 11:25:51 +00:00
discoverySubscription: Subscription;
2018-06-01 07:32:16 +00:00
zoneNode: TreeNode[] = [];
hostNode: TreeNode[] = [];
2018-06-01 11:25:51 +00:00
selectedItems = [];
tempHostId = 0; // Temporary
2018-05-30 09:39:26 +00:00
constructor(
) {
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-05-30 09:39:26 +00:00
}
2018-05-30 14:05:58 +00:00
2018-06-01 11:25:51 +00:00
tempHost() {
const idx = Math.floor(Math.random() * (255));
const host: Host = {
id: this.tempHostId++,
ipv4: '192.168.1.' + idx,
};
this.addHostNode(host);
2018-05-30 14:05:58 +00:00
}
2018-06-01 11:25:51 +00:00
tempService() {
const idx = Math.floor(Math.random() * (255));
const hostId = Math.floor(Math.random() * (this.tempHostId - 1));
const service: Service = {
id: idx,
serviceName: 'Service',
port: {
portNumber: idx,
portType: idx % 2 === 0 ? 'TCP' : 'UDP',
host: {
id: hostId
}
}
};
this.addServiceNode(service);
2018-05-30 14:05:58 +00:00
}
2018-06-01 11:25:51 +00:00
addHostNode(host: Host) {
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',
label: host.ipv4,
data: {
id: host.id,
ip: this.convertIPtoNumber(host.ipv4),
},
2018-06-01 07:32:16 +00:00
expanded: true,
children: []
});
}
2018-06-01 11:25:51 +00:00
addServiceNode(service: Service) {
const targetHostNode = this.findHostNodeByService(service);
const idx = this.findServiceIndex(targetHostNode.children, service);
targetHostNode.children[idx] = {
type: 'SERVICE',
label: service.serviceName,
data: {
id: service.id,
portType: service.port.portType,
portNumber: service.port.portNumber,
},
};
}
onTargetSelect(e, data) {
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 => {
if (node.data.ip < this.convertIPtoNumber(host.ipv4)) {
index++;
}
});
return index;
}
findServiceIndex(serviceNodes: TreeNode[], service: Service) {
let index = 0;
serviceNodes.forEach(node => {
if (node.data.portNumber < service.port.portNumber) {
index++;
}
});
return index;
}
findHostNodeByService(service: Service) {
let targetHost = null;
this.hostNode.forEach((value, i) => {
if (value.data.id === service.port.host.id) {
targetHost = this.hostNode[i];
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) => {
// tslint:disable-next-line:radix
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-05-30 09:39:26 +00:00
}