fixed discovery result

This commit is contained in:
snoop 2018-03-27 19:10:57 +09:00
parent 4196a124c2
commit 319efebe05
4 changed files with 102 additions and 20 deletions

View File

@ -1,4 +1,4 @@
import { Component, OnInit, AfterContentInit } from '@angular/core'; import { Component, OnInit, AfterContentInit, Host } from '@angular/core';
import { MatCheckboxChange } from '@angular/material'; import { MatCheckboxChange } from '@angular/material';
import { Store, select } from '@ngrx/store'; import { Store, select } from '@ngrx/store';
import { RPCClientError } from '@loafer/ng-rpc/protocol'; import { RPCClientError } from '@loafer/ng-rpc/protocol';
@ -9,9 +9,10 @@ import {
} from '../../model'; } from '../../model';
import * as CIDR from 'ip-cidr'; import * as CIDR from 'ip-cidr';
import * as DiscoveredStore from '../../store/setting'; import * as DiscoveredStore from '../../store/setting';
import * as DiscoverStore from '../../store/discover'; // import * as DiscoverStore from '../../store/discover';
import { SettingSelector, DiscoverSelector } from '../../store'; import { SettingSelector, DiscoverSelector } from '../../store';
import * as DiscoverStore from '../../store/discover';
@Component({ @Component({
selector: 'of-setting', selector: 'of-setting',
@ -20,8 +21,8 @@ import { SettingSelector, DiscoverSelector } from '../../store';
}) })
export class SettingComponent implements OnInit, AfterContentInit { export class SettingComponent implements OnInit, AfterContentInit {
settingSucceed$ = this.discoverdstore.pipe(select(SettingSelector.select('isStart'))); settingSucceed$: any;
discoveryResult$ = this.discoverstore.pipe(select(DiscoverSelector.select('getDiscoveryResult'))); discoveryResult$: any;
started = false; started = false;
@ -42,7 +43,7 @@ export class SettingComponent implements OnInit, AfterContentInit {
{ name: 'Nginx' }, { name: 'Nginx' },
]; ];
hosts = nodes; hosts = [];
checkedSet = new Set(); checkedSet = new Set();
@ -50,6 +51,8 @@ export class SettingComponent implements OnInit, AfterContentInit {
private discoverdstore: Store<DiscoveredStore.State>, private discoverdstore: Store<DiscoveredStore.State>,
private discoverstore: Store<DiscoverStore.State>, private discoverstore: Store<DiscoverStore.State>,
) { ) {
this.settingSucceed$ = discoverdstore.pipe(select(SettingSelector.select('isStart')));
this.discoveryResult$ = this.discoverstore.pipe(select(DiscoverSelector.select('zones')));
} }
ngOnInit() { ngOnInit() {
@ -65,15 +68,58 @@ export class SettingComponent implements OnInit, AfterContentInit {
); );
this.discoveryResult$.subscribe( this.discoveryResult$.subscribe(
(zone: Zone) => { (zones: Map<string, Zone>) => {
this.convertTreeView(zone); console.log('ZoneZoneZoneZoneZoneZoneZone');
// console.log(JSON.stringify(zones));
this.convertTreeView2(zones);
}, },
(error: RPCClientError) => { (error: RPCClientError) => {
console.log(error.response.message); console.log(error.response.message);
} }
); );
}
isZone(zone: Zone): boolean {
for (let i = 0 ; i < this.hosts.length; ++i) {
if (zone.iface === this.hosts[i].iface) {
return true;
}
}
return false;
}
convertTreeView2(zones: Map<string, Zone>) {
if (zones === undefined || zones === null) {
return;
}
console.log('convertTreeView2');
const treeNodes: any[] = [];
zones.forEach((value: Zone, key: string, map) => {
if (this.isZone(value)) {
return;
}
const jZone: any = {
title: 'Zone - ' + value.iface,
className : 'cn' + value.ip
};
jZone.obj = value;
jZone.children = this.convertViewHost(value.hosts);
treeNodes.push(jZone);
});
this.hosts = treeNodes;
} }
ngAfterContentInit() { ngAfterContentInit() {
@ -163,14 +209,19 @@ export class SettingComponent implements OnInit, AfterContentInit {
return; return;
} }
const treeNodes = this.convertViewHost(zone.hosts); // const treeNodes = this.convertViewHost(zone.hosts);
console.log(treeNodes); console.log(JSON.stringify(zone));
} }
convertViewHost(hosts): any[] { convertViewHost(hosts): any[] {
const treeNodes: any[] = []; if (hosts === undefined || hosts === null) {
return null;
}
const hostNodes: any[] = [];
hosts.forEach((host, hostKey) => { hosts.forEach((host, hostKey) => {
@ -182,17 +233,17 @@ export class SettingComponent implements OnInit, AfterContentInit {
jHost.children = this.convertViewPort(host.ports); jHost.children = this.convertViewPort(host.ports);
treeNodes.push(jHost); hostNodes.push(jHost);
}); });
return treeNodes; return hostNodes;
} }
convertViewPort(ports): any[] { convertViewPort(ports): any[] {
if (ports === undefined && ports.size <= 0) { if (ports === undefined || ports === null) {
return null; return null;
} }

View File

@ -15,6 +15,8 @@ import {
Service, Service,
} from '../../model'; } from '../../model';
// import * as _ 'lodash';
export function reducer(state = initialState, action: Actions): State { export function reducer(state = initialState, action: Actions): State {
switch (action.type) { switch (action.type) {
case ActionType.DiscoveredZone: { case ActionType.DiscoveredZone: {
@ -23,20 +25,51 @@ export function reducer(state = initialState, action: Actions): State {
zones.set(zone.network, zone); zones.set(zone.network, zone);
return state; const newZones: Map<string, Zone> = new Map();
zones.forEach(function(value, key) {
newZones.set(key, value);
});
return {
...state,
zones : newZones,
};
} }
case ActionType.DiscoveredHost: { case ActionType.DiscoveredHost: {
const host: Host = <Host>action.payload; const host: Host = <Host>action.payload;
const zone = state.zones.get(host.zone.network); let zone = null;
let zones: Map<string, Zone> = state.zones;
if (zones === undefined || zones === null) {
zones = new Map();
zone = host.zone;
// zones.set(zone.network, zone);
} else {
zone = zones.get(host.zone.network);
}
if (undefined === zone) { if (undefined === zone) {
console.error(`Discovery.discoveredHost: Zone[${host.zone.network}] is not exist`); console.error(`Discovery.discoveredHost: Zone[${host.zone.network}] is not exist`);
} }
if (null === zone.hosts) { if (null === zone.hosts || undefined === zone.hosts) {
zone.hosts = new Map(); zone.hosts = new Map();
} }
zone.hosts.set(host.ip, host); zone.hosts.set(host.ip, host);
return state; zones.set(zone.network, zone);
const newZones: Map<string, Zone> = new Map();
zones.forEach(function(value, key) {
newZones.set(key, value);
});
return {
...state,
zones: newZones
};
} }
case ActionType.DiscoveredPort: { case ActionType.DiscoveredPort: {

View File

@ -14,5 +14,3 @@ export const initialState: State = {
zones: null, zones: null,
}; };
export const getDiscoveryResult = (state: State) => state.zones;

View File

@ -31,7 +31,7 @@ export class DiscoverySubscriber {
} }
@RPCSubscriber({method: 'DiscoveryService.discoveredHost'}) @RPCSubscriber({method: 'DiscoveryService.discoveredHost'})
public discoveredHost(host: Host): void { public discoveredHost(host: Host): void {
this.loggerService.debug('DiscoverySubscriber.discoveredHost host:', host); // this.loggerService.debug('DiscoverySubscriber.discoveredHost host:', host);
this.store.dispatch(new DiscoverStore.DiscoveredHost(host)); this.store.dispatch(new DiscoverStore.DiscoveredHost(host));
} }