Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 1 | import * as _ from 'lodash'; |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 2 | import {Observable, BehaviorSubject, Subscription} from 'rxjs'; |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 3 | import {IXosModelStoreService} from '../../datasources/stores/model.store'; |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 4 | import { |
| 5 | IXosServiceGraph, IXosServiceModel, IXosTenantModel, IXosCoarseGraphData, |
| 6 | IXosServiceGraphNode, IXosServiceGraphLink |
| 7 | } from '../interfaces'; |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 8 | import {IXosDebouncer} from '../../core/services/helpers/debounce.helper'; |
| 9 | export interface IXosServiceGraphStore { |
| 10 | get(): Observable<IXosServiceGraph>; |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 11 | getCoarse(): Observable<IXosServiceGraph>; |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 12 | dispose(): void; |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | export class XosServiceGraphStore implements IXosServiceGraphStore { |
| 16 | static $inject = [ |
| 17 | '$log', |
| 18 | 'XosModelStore', |
| 19 | 'XosDebouncer' |
| 20 | ]; |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 21 | |
| 22 | // graph data store |
| 23 | private graphData: BehaviorSubject<IXosCoarseGraphData> = new BehaviorSubject({ |
| 24 | services: [], |
| 25 | tenants: [] |
| 26 | }); |
| 27 | |
| 28 | // reprentations of the graph as D3 requires |
| 29 | private d3CoarseGraph = new BehaviorSubject({}); |
| 30 | private d3FineGrainedGraph = new BehaviorSubject({}); |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 31 | |
| 32 | // storing locally reference to the data model |
| 33 | private services; |
| 34 | private tenants; |
| 35 | |
| 36 | // debounced functions |
| 37 | private handleData; |
| 38 | |
| 39 | // datastore |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 40 | private ServiceSubscription: Subscription; |
| 41 | private TenantSubscription: Subscription; |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 42 | |
| 43 | constructor ( |
| 44 | private $log: ng.ILogService, |
| 45 | private XosModelStore: IXosModelStoreService, |
| 46 | private XosDebouncer: IXosDebouncer |
| 47 | ) { |
| 48 | |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 49 | this.$log.info(`[XosServiceGraphStore] Setup`); |
| 50 | |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 51 | // we want to have a quiet period of 500ms from the last event before doing anything |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 52 | this.handleData = this.XosDebouncer.debounce(this._handleData, 500, this, false); |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 53 | |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 54 | |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 55 | // observe models and populate graphData |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 56 | this.ServiceSubscription = this.XosModelStore.query('Service', '/core/services') |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 57 | .subscribe( |
| 58 | (res) => { |
| 59 | this.combineData(res, 'services'); |
| 60 | }, |
| 61 | (err) => { |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 62 | this.$log.error(`[XosServiceGraphStore] graphData Observable: `, err); |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 63 | } |
| 64 | ); |
| 65 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 66 | this.TenantSubscription = this.XosModelStore.query('Tenant', '/core/tenants') |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 67 | .subscribe( |
| 68 | (res) => { |
| 69 | this.combineData(res, 'tenants'); |
| 70 | }, |
| 71 | (err) => { |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 72 | this.$log.error(`[XosServiceGraphStore] graphData Observable: `, err); |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 73 | } |
| 74 | ); |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 75 | |
| 76 | // observe graphData and build Coarse or FineGrained graphs (based on who's subscribed) |
| 77 | this.graphData |
| 78 | .subscribe( |
| 79 | (res: IXosCoarseGraphData) => { |
| 80 | if (this.d3CoarseGraph.observers.length > 0) { |
| 81 | this.graphDataToCoarseGraph(res); |
| 82 | } |
| 83 | if (this.d3FineGrainedGraph.observers.length > 0) { |
| 84 | // TODO graphDataToFineGrainedGraph |
| 85 | } |
| 86 | }, |
| 87 | (err) => { |
| 88 | this.$log.error(`[XosServiceGraphStore] graphData Observable: `, err); |
| 89 | } |
| 90 | ); |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 93 | public dispose() { |
| 94 | // cancel subscriptions from observables |
| 95 | this.ServiceSubscription.unsubscribe(); |
| 96 | this.TenantSubscription.unsubscribe(); |
| 97 | } |
| 98 | |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 99 | public get() { |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 100 | return this.d3FineGrainedGraph.asObservable(); |
| 101 | } |
| 102 | |
| 103 | public getCoarse() { |
| 104 | return this.d3CoarseGraph.asObservable(); |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private combineData(data: any, type: 'services'|'tenants') { |
| 108 | switch (type) { |
| 109 | case 'services': |
| 110 | this.services = data; |
| 111 | break; |
| 112 | case 'tenants': |
| 113 | this.tenants = data; |
| 114 | break; |
| 115 | } |
| 116 | this.handleData(this.services, this.tenants); |
| 117 | } |
| 118 | |
| 119 | private _handleData(services: IXosServiceModel[], tenants: IXosTenantModel[]) { |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 120 | this.graphData.next({ |
| 121 | services: this.services, |
| 122 | tenants: this.tenants |
| 123 | }); |
| 124 | } |
| 125 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 126 | private getCoarseNodeIndexById(id: number, nodes: IXosServiceModel[]) { |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 127 | return _.findIndex(nodes, {id: id}); |
| 128 | } |
| 129 | |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 130 | private graphDataToCoarseGraph(data: IXosCoarseGraphData) { |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 131 | // TODO find how to bind source/target by node ID and not by position in array (ask Simon?) |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 132 | const links: IXosServiceGraphLink[] = _.chain(data.tenants) |
| 133 | .filter((t: IXosTenantModel) => t.kind === 'coarse') |
| 134 | .map((t: IXosTenantModel) => { |
| 135 | return { |
| 136 | id: t.id, |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 137 | source: this.getCoarseNodeIndexById(t.provider_service_id, data.services), |
| 138 | target: this.getCoarseNodeIndexById(t.subscriber_service_id, data.services), |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 139 | model: t |
| 140 | }; |
| 141 | }) |
| 142 | .value(); |
| 143 | |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 144 | const nodes: IXosServiceGraphNode[] = _.map(data.services, (s: IXosServiceModel) => { |
| 145 | return { |
| 146 | id: s.id, |
| 147 | label: s.name, |
| 148 | model: s |
| 149 | }; |
| 150 | }); |
| 151 | |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 152 | this.d3CoarseGraph.next({ |
| 153 | nodes: nodes, |
| 154 | links: links |
| 155 | }); |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | } |