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