From 29baedcf5bb6d200995472b92be92ac179352591 Mon Sep 17 00:00:00 2001 From: snoop Date: Thu, 22 Mar 2018 18:58:06 +0900 Subject: [PATCH] add discvoery saveAllTarget --- .../service/discovery.service.spec.ts | 2 +- src/packages/discovery/service/index.ts | 2 + .../service/target-discovery.service.spec.ts | 15 ++++ .../service/target-discovery.service.ts | 31 +++++++ .../store/discovery/discovery.action.ts | 84 ------------------- .../store/discovery/discovery.effect.spec.ts | 0 .../store/discovery/discovery.effect.ts | 0 .../store/discovery/discovery.reducer.ts | 0 .../store/discovery/discovery.state.ts | 0 .../discovery/store/discovery/index.ts | 0 src/packages/discovery/store/regist/index.ts | 4 + .../discovery/store/regist/regist.action.ts | 46 ++++++++++ .../store/regist/regist.effect.spec.ts | 15 ++++ .../discovery/store/regist/regist.effect.ts | 51 +++++++++++ .../discovery/store/regist/regist.reducer.ts | 47 +++++++++++ .../discovery/store/regist/regist.state.ts | 18 ++++ 16 files changed, 230 insertions(+), 85 deletions(-) create mode 100644 src/packages/discovery/service/target-discovery.service.spec.ts create mode 100644 src/packages/discovery/service/target-discovery.service.ts delete mode 100644 src/packages/discovery/store/discovery/discovery.action.ts delete mode 100644 src/packages/discovery/store/discovery/discovery.effect.spec.ts delete mode 100644 src/packages/discovery/store/discovery/discovery.effect.ts delete mode 100644 src/packages/discovery/store/discovery/discovery.reducer.ts delete mode 100644 src/packages/discovery/store/discovery/discovery.state.ts delete mode 100644 src/packages/discovery/store/discovery/index.ts create mode 100644 src/packages/discovery/store/regist/index.ts create mode 100644 src/packages/discovery/store/regist/regist.action.ts create mode 100644 src/packages/discovery/store/regist/regist.effect.spec.ts create mode 100644 src/packages/discovery/store/regist/regist.effect.ts create mode 100644 src/packages/discovery/store/regist/regist.reducer.ts create mode 100644 src/packages/discovery/store/regist/regist.state.ts diff --git a/src/packages/discovery/service/discovery.service.spec.ts b/src/packages/discovery/service/discovery.service.spec.ts index 5f2d867..7859d24 100644 --- a/src/packages/discovery/service/discovery.service.spec.ts +++ b/src/packages/discovery/service/discovery.service.spec.ts @@ -2,7 +2,7 @@ import { TestBed, inject } from '@angular/core/testing'; import { DiscoveryService } from './discovery.service'; -describe('MemberService', () => { +describe('DiscoveryService', () => { beforeEach(() => { TestBed.configureTestingModule({ providers: [DiscoveryService] diff --git a/src/packages/discovery/service/index.ts b/src/packages/discovery/service/index.ts index f19161b..d227ad7 100644 --- a/src/packages/discovery/service/index.ts +++ b/src/packages/discovery/service/index.ts @@ -1,5 +1,7 @@ import { DiscoveryService } from './discovery.service'; +import { TargetDiscoveryService } from './target-discovery.service'; export const SERVICES = [ DiscoveryService, + TargetDiscoveryService, ]; diff --git a/src/packages/discovery/service/target-discovery.service.spec.ts b/src/packages/discovery/service/target-discovery.service.spec.ts new file mode 100644 index 0000000..6e58f64 --- /dev/null +++ b/src/packages/discovery/service/target-discovery.service.spec.ts @@ -0,0 +1,15 @@ +import { TestBed, inject } from '@angular/core/testing'; + +import { TargetDiscoveryService } from './target-discovery.service'; + +describe('TargetDiscoveryService', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [TargetDiscoveryService] + }); + }); + + it('should be created', inject([TargetDiscoveryService], (service: TargetDiscoveryService) => { + expect(service).toBeTruthy(); + })); +}); diff --git a/src/packages/discovery/service/target-discovery.service.ts b/src/packages/discovery/service/target-discovery.service.ts new file mode 100644 index 0000000..ba3f504 --- /dev/null +++ b/src/packages/discovery/service/target-discovery.service.ts @@ -0,0 +1,31 @@ +import { Injectable } from '@angular/core'; +import { Observable } from 'rxjs/Observable'; +import { RPCClient } from 'packages/core/rpc/client/RPCClient'; +import { + DiscoveryStartInfo, + DiscoveryZone, + DiscoveryHost, + DiscoveryPort, + DiscoveryService as M_DiscoveryService, + Zone, + Host, + Port, + Service, +} from '../model'; +import { Probe } from 'packages/probe/model'; + +@Injectable() +export class TargetDiscoveryService { + + public constructor( + private rpcClient: RPCClient, + ) { + + } + + public saveAllTarget(hosts: Host[], probe: Probe): Observable { + + return this.rpcClient.call('TargetDiscoveryService.saveAllTarget', hosts, probe); + } + +} diff --git a/src/packages/discovery/store/discovery/discovery.action.ts b/src/packages/discovery/store/discovery/discovery.action.ts deleted file mode 100644 index 52ac83c..0000000 --- a/src/packages/discovery/store/discovery/discovery.action.ts +++ /dev/null @@ -1,84 +0,0 @@ -// import { Action } from '@ngrx/store'; - -// import { notificationActionEnum } from 'packages/core/rpc/store/notification'; -// import { RPCError } from 'packages/core/rpc/error'; - -// import { -// Zone, -// Host, -// Port, -// Service, -// DiscoveryZone, -// DiscoveryHost, -// DiscoveryPort, -// DiscoveryService as MDiscoveryService, -// } from '../../model'; - -// export enum ActionType { -// DiscoverZone = '[@@NOTIFICATION] DiscoveryService.discoverZone', -// DiscoverHost = '[@@NOTIFICATION] DiscoveryService.discoverHost', -// DiscoverPort = '[@@NOTIFICATION] DiscoveryService.discoverPort', -// DiscoverService = '[@@NOTIFICATION] DiscoveryService.discoverService', -// DiscoveredZone = '[@@NOTIFICATION] DiscoveryService.discoveredZone', -// DiscoveredHost = '[@@NOTIFICATION] DiscoveryService.discoveredHost', -// DiscoveredPort = '[@@NOTIFICATION] DiscoveryService.discoveredPort', -// DiscoveredService = '[@@NOTIFICATION] DiscoveryService.discoveredService', -// } - -// export class DiscoverZone implements Action { -// readonly type = ActionType.DiscoverZone; - -// constructor(public payload: {probeID: string, discoveryZone: DiscoveryZone}) {} -// } - -// export class DiscoverHost implements Action { -// readonly type = ActionType.DiscoverHost; - -// constructor(public payload: {probeID: string, zone: Zone, discoveryHost: DiscoveryHost}) {} -// } - -// export class DiscoverPort implements Action { -// readonly type = ActionType.DiscoverPort; - -// constructor(public payload: {probeID: string, host: Host, discoveryPort: DiscoveryPort}) {} -// } - -// export class DiscoverService implements Action { -// readonly type = ActionType.DiscoverService; -// constructor(public payload: {probeID: string, port: Port, discoveryService: MDiscoveryService}) {} -// } - - -// export class DiscoveredZone implements Action { -// readonly type = ActionType.DiscoveredZone; - -// constructor(public payload: Zone) {} -// } - -// export class DiscoveredHost implements Action { -// readonly type = ActionType.DiscoveredHost; - -// constructor(public payload: Host) {} -// } - -// export class DiscoveredPort implements Action { -// readonly type = ActionType.DiscoveredPort; - -// constructor(public payload: Port) {} -// } - -// export class DiscoveredService implements Action { -// readonly type = ActionType.DiscoveredService; -// constructor(public payload: Service) {} -// } - -// export type Actions = -// | DiscoverZone -// | DiscoverHost -// | DiscoverPort -// | DiscoverService -// | DiscoveredZone -// | DiscoveredHost -// | DiscoveredPort -// | DiscoveredService -// ; diff --git a/src/packages/discovery/store/discovery/discovery.effect.spec.ts b/src/packages/discovery/store/discovery/discovery.effect.spec.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/packages/discovery/store/discovery/discovery.effect.ts b/src/packages/discovery/store/discovery/discovery.effect.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/packages/discovery/store/discovery/discovery.reducer.ts b/src/packages/discovery/store/discovery/discovery.reducer.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/packages/discovery/store/discovery/discovery.state.ts b/src/packages/discovery/store/discovery/discovery.state.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/packages/discovery/store/discovery/index.ts b/src/packages/discovery/store/discovery/index.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/packages/discovery/store/regist/index.ts b/src/packages/discovery/store/regist/index.ts new file mode 100644 index 0000000..f6da3fb --- /dev/null +++ b/src/packages/discovery/store/regist/index.ts @@ -0,0 +1,4 @@ +export * from './regist.action'; +export * from './regist.effect'; +export * from './regist.reducer'; +export * from './regist.state'; diff --git a/src/packages/discovery/store/regist/regist.action.ts b/src/packages/discovery/store/regist/regist.action.ts new file mode 100644 index 0000000..32c2085 --- /dev/null +++ b/src/packages/discovery/store/regist/regist.action.ts @@ -0,0 +1,46 @@ +import { Action } from '@ngrx/store'; + +import { notificationActionEnum } from 'packages/core/rpc/store/notification'; +import { RPCError } from 'packages/core/rpc/error'; + +import { + Zone, + Host, + Port, + Service, + DiscoveryZone, + DiscoveryHost, + DiscoveryPort, + DiscoveryService as MDiscoveryService, +} from '../../model'; +import { Probe } from 'packages/probe/model'; + + export enum ActionType { + SaveAllTarget = '[@@REGIST] TargetDiscoveryService.saveAllTarget', + SaveAllTargetSuccess = '[@@REGIST] TargetDiscoveryService.SaveAllTargetSuccess', + SaveAllTargetFailure = '[@@REGIST] TargetDiscoveryService.SaveAllTargetFailure', +} + +export class DiscoverySaveAllTarget implements Action { + readonly type = ActionType.SaveAllTarget; + + constructor(public payload: {hosts: Host[], probe: Probe}) {} +} + +export class DiscoverySaveAllTargetSuccess implements Action { + readonly type = ActionType.SaveAllTargetSuccess; + + constructor(public payload: Boolean) {} +} + +export class DiscoverySaveAllTargetFailure implements Action { + readonly type = ActionType.SaveAllTargetFailure; + + constructor(public payload: RPCError) {} +} + +export type Actions = + | DiscoverySaveAllTarget + | DiscoverySaveAllTargetSuccess + | DiscoverySaveAllTargetFailure +; diff --git a/src/packages/discovery/store/regist/regist.effect.spec.ts b/src/packages/discovery/store/regist/regist.effect.spec.ts new file mode 100644 index 0000000..2b298f3 --- /dev/null +++ b/src/packages/discovery/store/regist/regist.effect.spec.ts @@ -0,0 +1,15 @@ +import { TestBed, inject } from '@angular/core/testing'; + +import { Effects } from './regist.effect'; + +describe('Regist.Effects', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [Effects] + }); + }); + + it('should be created', inject([Effects], (effects: Effects) => { + expect(effects).toBeTruthy(); + })); +}); diff --git a/src/packages/discovery/store/regist/regist.effect.ts b/src/packages/discovery/store/regist/regist.effect.ts new file mode 100644 index 0000000..87389ee --- /dev/null +++ b/src/packages/discovery/store/regist/regist.effect.ts @@ -0,0 +1,51 @@ +import { Injectable } from '@angular/core'; +import { Router } from '@angular/router'; + +import { Effect, Actions, ofType } from '@ngrx/effects'; +import { Action } from '@ngrx/store'; + +import { Observable } from 'rxjs/Observable'; +import { of } from 'rxjs/observable/of'; + +import 'rxjs/add/operator/catch'; +import 'rxjs/add/operator/do'; +import 'rxjs/add/operator/exhaustMap'; +import 'rxjs/add/operator/switchMap'; +import 'rxjs/add/operator/map'; +import 'rxjs/add/operator/take'; + +import { RPCError } from 'packages/core/rpc/error'; + +import { DiscoveryStartInfo } from '../../model'; +import { TargetDiscoveryService } from '../../service/target-discovery.service'; + +import { + DiscoverySaveAllTarget, + DiscoverySaveAllTargetSuccess, + DiscoverySaveAllTargetFailure, + ActionType +} from './regist.action'; + +@Injectable() +export class Effects { + + constructor( + private actions$: Actions, + private targetDiscoveryService: TargetDiscoveryService, + private router: Router + ) { } + + @Effect() + saveAllTargetResult$: Observable = this.actions$ + .ofType(ActionType.SaveAllTarget) + .map((action: DiscoverySaveAllTarget) => action.payload) + .switchMap(payload => this.targetDiscoveryService.saveAllTarget(payload.hosts, payload.probe)) + .map(result => { + return new DiscoverySaveAllTargetSuccess(result); + }) + .catch((error: RPCError) => { + console.log(error.response.message); + return of(new DiscoverySaveAllTargetFailure(error)); + }); + +} diff --git a/src/packages/discovery/store/regist/regist.reducer.ts b/src/packages/discovery/store/regist/regist.reducer.ts new file mode 100644 index 0000000..9e93ec7 --- /dev/null +++ b/src/packages/discovery/store/regist/regist.reducer.ts @@ -0,0 +1,47 @@ +import { RPCError } from 'packages/core/rpc/error'; + +import { + Actions, + ActionType, +} from './regist.action'; + +import { + State, + initialState, +} from './regist.state'; + +import { DiscoveryStartInfo } from '../../model'; + +export function reducer(state = initialState, action: Actions): State { + switch (action.type) { + case ActionType.SaveAllTarget: { + return { + ...state, + error: null, + isPending: true, + }; + } + + case ActionType.SaveAllTargetSuccess: { + return { + ...state, + error: null, + isPending: false, + isSuccess: action.payload, + }; + } + + case ActionType.SaveAllTargetFailure: { + return { + ...state, + error: action.payload, + isPending: false, + isSuccess: null, + }; + } + + default: { + return state; + } + } +} diff --git a/src/packages/discovery/store/regist/regist.state.ts b/src/packages/discovery/store/regist/regist.state.ts new file mode 100644 index 0000000..12ea68d --- /dev/null +++ b/src/packages/discovery/store/regist/regist.state.ts @@ -0,0 +1,18 @@ +import { RPCError } from 'packages/core/rpc/error'; + +import { DiscoveryStartInfo } from '../../model'; + +export interface State { + error: RPCError | null; + isPending: boolean; + isSuccess: Boolean | null; +} + +export const initialState: State = { + error: null, + isPending: false, + isSuccess: null, +}; + +export const getSuccess = (state: State) => state.isSuccess; +