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, |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 6 | IXosServiceGraphNode, IXosServiceGraphLink, IXosFineGrainedGraphData |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 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 |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 22 | private graphData: BehaviorSubject<IXosFineGrainedGraphData> = new BehaviorSubject({ |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 23 | services: [], |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 24 | tenants: [], |
| 25 | networks: [], |
| 26 | subscribers: [] |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 27 | }); |
| 28 | |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 29 | // representation of the graph as D3 requires |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 30 | private d3CoarseGraph = new BehaviorSubject({}); |
| 31 | private d3FineGrainedGraph = new BehaviorSubject({}); |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 32 | |
| 33 | // storing locally reference to the data model |
| 34 | private services; |
| 35 | private tenants; |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 36 | private subscribers; |
| 37 | private networks; |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 38 | |
| 39 | // debounced functions |
| 40 | private handleData; |
| 41 | |
| 42 | // datastore |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 43 | private ServiceSubscription: Subscription; |
| 44 | private TenantSubscription: Subscription; |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 45 | private SubscriberSubscription: Subscription; |
| 46 | private NetworkSubscription: Subscription; |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 47 | |
| 48 | constructor ( |
| 49 | private $log: ng.ILogService, |
| 50 | private XosModelStore: IXosModelStoreService, |
| 51 | private XosDebouncer: IXosDebouncer |
| 52 | ) { |
| 53 | |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 54 | this.$log.info(`[XosServiceGraphStore] Setup`); |
| 55 | |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 56 | // 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] | 57 | this.handleData = this.XosDebouncer.debounce(this._handleData, 500, this, false); |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 58 | |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 59 | // observe models and populate graphData |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 60 | // TODO get Nodes (model that represent compute nodes in a pod) |
| 61 | // TODO get Instances (model that represent deployed VMs) |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 62 | this.ServiceSubscription = this.XosModelStore.query('Service', '/core/services') |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 63 | .subscribe( |
| 64 | (res) => { |
| 65 | this.combineData(res, 'services'); |
| 66 | }, |
| 67 | (err) => { |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 68 | this.$log.error(`[XosServiceGraphStore] Service Observable: `, err); |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 69 | } |
| 70 | ); |
| 71 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 72 | this.TenantSubscription = this.XosModelStore.query('Tenant', '/core/tenants') |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 73 | .subscribe( |
| 74 | (res) => { |
| 75 | this.combineData(res, 'tenants'); |
| 76 | }, |
| 77 | (err) => { |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 78 | this.$log.error(`[XosServiceGraphStore] Tenant Observable: `, err); |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 79 | } |
| 80 | ); |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 81 | |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 82 | this.SubscriberSubscription = this.XosModelStore.query('Subscriber', '/core/subscribers') |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 83 | .subscribe( |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 84 | (res) => { |
| 85 | this.combineData(res, 'subscribers'); |
| 86 | }, |
| 87 | (err) => { |
| 88 | this.$log.error(`[XosServiceGraphStore] Subscriber Observable: `, err); |
| 89 | } |
| 90 | ); |
| 91 | |
| 92 | this.NetworkSubscription = this.XosModelStore.query('Network', '/core/networks') |
| 93 | .subscribe( |
| 94 | (res) => { |
| 95 | this.combineData(res, 'networks'); |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 96 | }, |
| 97 | (err) => { |
| 98 | this.$log.error(`[XosServiceGraphStore] graphData Observable: `, err); |
| 99 | } |
| 100 | ); |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 101 | |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 102 | // observe graphData and build Coarse and FineGrained graphs |
| 103 | this.graphData |
| 104 | .subscribe( |
| 105 | (res: IXosFineGrainedGraphData) => { |
| 106 | this.graphDataToCoarseGraph(res); |
| 107 | this.graphDataToFineGrainedGraph(res); |
| 108 | }, |
| 109 | (err) => { |
| 110 | this.$log.error(`[XosServiceGraphStore] graphData Observable: `, err); |
| 111 | } |
| 112 | ); |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 115 | public get() { |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 116 | return this.d3FineGrainedGraph.asObservable(); |
| 117 | } |
| 118 | |
| 119 | public getCoarse() { |
| 120 | return this.d3CoarseGraph.asObservable(); |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 123 | private combineData(data: any, type: 'services'|'tenants'|'subscribers'|'networks') { |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 124 | switch (type) { |
| 125 | case 'services': |
| 126 | this.services = data; |
| 127 | break; |
| 128 | case 'tenants': |
| 129 | this.tenants = data; |
| 130 | break; |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 131 | case 'subscribers': |
| 132 | this.subscribers = data; |
| 133 | break; |
| 134 | case 'networks': |
| 135 | this.networks = data; |
| 136 | break; |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 137 | } |
| 138 | this.handleData(this.services, this.tenants); |
| 139 | } |
| 140 | |
| 141 | private _handleData(services: IXosServiceModel[], tenants: IXosTenantModel[]) { |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 142 | this.graphData.next({ |
| 143 | services: this.services, |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 144 | tenants: this.tenants, |
| 145 | subscribers: this.subscribers, |
| 146 | networks: this.networks |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 147 | }); |
| 148 | } |
| 149 | |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 150 | private getNodeIndexById(id: number | string, nodes: IXosServiceModel[]) { |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 151 | return _.findIndex(nodes, {id: id}); |
| 152 | } |
| 153 | |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 154 | private d3Id(type: string, id: number) { |
| 155 | return `${type.toLowerCase()}~${id}`; |
| 156 | } |
| 157 | |
| 158 | private getTargetId(tenant: IXosTenantModel) { |
| 159 | |
| 160 | let targetId; |
| 161 | if (tenant.subscriber_service_id) { |
| 162 | targetId = this.d3Id('service', tenant.subscriber_service_id); |
| 163 | } |
| 164 | else if (tenant.subscriber_tenant_id) { |
| 165 | targetId = this.d3Id('tenant', tenant.subscriber_tenant_id); |
| 166 | } |
| 167 | else if (tenant.subscriber_network_id) { |
| 168 | targetId = this.d3Id('network', tenant.subscriber_network_id); |
| 169 | } |
| 170 | else if (tenant.subscriber_root_id) { |
| 171 | targetId = this.d3Id('subscriber', tenant.subscriber_root_id); |
| 172 | } |
| 173 | return targetId; |
| 174 | } |
| 175 | |
| 176 | private getSourceId(tenant: IXosTenantModel) { |
| 177 | return this.d3Id('service', tenant.provider_service_id); |
| 178 | } |
| 179 | |
| 180 | private getNodeType(n: any) { |
| 181 | return n.class_names.split(',')[0].toLowerCase(); |
| 182 | } |
| 183 | |
| 184 | private getNodeLabel(n: any) { |
| 185 | if (this.getNodeType(n) === 'tenant') { |
| 186 | return n.id; |
| 187 | } |
| 188 | return n.humanReadableName ? n.humanReadableName : n.name; |
| 189 | } |
| 190 | |
| 191 | private removeUnwantedFineGrainedData(data: IXosFineGrainedGraphData): IXosFineGrainedGraphData { |
| 192 | data.tenants = _.filter(data.tenants, t => t.kind !== 'coarse'); |
| 193 | data.networks = _.filter(data.networks, n => { |
| 194 | const subscriber = _.findIndex(data.tenants, {subscriber_network_id: n.id}); |
| 195 | return subscriber > -1; |
| 196 | }); |
| 197 | return data; |
| 198 | } |
| 199 | |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 200 | private graphDataToCoarseGraph(data: IXosCoarseGraphData) { |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 201 | |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 202 | const links: IXosServiceGraphLink[] = _.chain(data.tenants) |
| 203 | .filter((t: IXosTenantModel) => t.kind === 'coarse') |
| 204 | .map((t: IXosTenantModel) => { |
| 205 | return { |
| 206 | id: t.id, |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 207 | source: this.getNodeIndexById(t.provider_service_id, data.services), |
| 208 | target: this.getNodeIndexById(t.subscriber_service_id, data.services), |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 209 | model: t |
| 210 | }; |
| 211 | }) |
| 212 | .value(); |
| 213 | |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 214 | const nodes: IXosServiceGraphNode[] = _.map(data.services, (s: IXosServiceModel) => { |
| 215 | return { |
| 216 | id: s.id, |
| 217 | label: s.name, |
| 218 | model: s |
| 219 | }; |
| 220 | }); |
| 221 | |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 222 | this.d3CoarseGraph.next({ |
| 223 | nodes: nodes, |
| 224 | links: links |
| 225 | }); |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 228 | private graphDataToFineGrainedGraph(data: IXosFineGrainedGraphData) { |
| 229 | |
| 230 | data = this.removeUnwantedFineGrainedData(data); |
| 231 | |
| 232 | let nodes = _.reduce(Object.keys(data), (list: any[], k: string) => { |
| 233 | return list.concat(data[k]); |
| 234 | }, []); |
| 235 | |
| 236 | nodes = _.chain(nodes) |
| 237 | .map(n => { |
| 238 | n.d3Id = this.d3Id(this.getNodeType(n), n.id); |
| 239 | return n; |
| 240 | }) |
| 241 | .map(n => { |
| 242 | let node: IXosServiceGraphNode = { |
| 243 | id: n.d3Id, |
| 244 | label: this.getNodeLabel(n), |
| 245 | model: n, |
| 246 | type: this.getNodeType(n) |
| 247 | }; |
| 248 | return node; |
| 249 | }) |
| 250 | .value(); |
| 251 | |
| 252 | const links = _.reduce(data.tenants, (links: IXosServiceGraphLink[], tenant: IXosTenantModel) => { |
| 253 | const sourceId = this.getSourceId(tenant); |
| 254 | const targetId = this.getTargetId(tenant); |
| 255 | |
Matteo Scandolo | 6d3e80e | 2017-03-10 11:34:43 -0800 | [diff] [blame] | 256 | if (!angular.isDefined(targetId)) { |
| 257 | // if the tenant is not pointing to anything, don't draw links |
| 258 | return links; |
| 259 | } |
| 260 | |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 261 | const tenantToProvider = { |
| 262 | id: `${sourceId}_${tenant.d3Id}`, |
| 263 | source: this.getNodeIndexById(sourceId, nodes), |
| 264 | target: this.getNodeIndexById(tenant.d3Id, nodes), |
| 265 | model: tenant |
| 266 | }; |
| 267 | |
| 268 | const tenantToSubscriber = { |
| 269 | id: `${tenant.d3Id}_${targetId}`, |
| 270 | source: this.getNodeIndexById(tenant.d3Id, nodes), |
| 271 | target: this.getNodeIndexById(targetId, nodes), |
| 272 | model: tenant |
| 273 | }; |
| 274 | |
| 275 | links.push(tenantToProvider); |
| 276 | links.push(tenantToSubscriber); |
| 277 | return links; |
| 278 | }, []); |
| 279 | |
| 280 | if (nodes.length === 0 || links.length === 0) { |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | this.d3FineGrainedGraph.next({ |
| 285 | nodes: nodes, |
| 286 | links: links |
| 287 | }); |
| 288 | } |
| 289 | |
Matteo Scandolo | a62adbc | 2017-03-02 15:37:34 -0800 | [diff] [blame] | 290 | } |