blob: 64955aef31085250d484973afce32239e2fd1771 [file] [log] [blame]
Matteo Scandolo75171782017-03-08 14:17:01 -08001import {IXosServiceGraphStore} from '../../services/graph.store';
2import './fine-grained.component.scss';
3import * as d3 from 'd3';
4import * as $ from 'jquery';
Matteo Scandolobafd8d62017-03-29 23:23:00 -07005import * as _ from 'lodash';
Matteo Scandolo75171782017-03-08 14:17:01 -08006import {Subscription} from 'rxjs';
7import {XosServiceGraphConfig as config} from '../../graph.config';
8import {IXosDebouncer} from '../../../core/services/helpers/debounce.helper';
9import {IXosServiceGraph, IXosServiceGraphLink, IXosServiceGraphNode} from '../../interfaces';
Matteo Scandolo520a8a12017-03-10 17:31:37 -080010import {IXosModelDiscovererService} from '../../../datasources/helpers/model-discoverer.service';
11import {IXosSidePanelService} from '../../../core/side-panel/side-panel.service';
Matteo Scandolo7629cc42017-03-13 14:12:15 -070012import {IXosGraphHelpers} from '../../services/d3-helpers/graph.helpers';
Matteo Scandolobafd8d62017-03-29 23:23:00 -070013import {IXosServiceGraphExtender, IXosServiceGraphReducer} from '../../services/graph.extender';
Matteo Scandolo75171782017-03-08 14:17:01 -080014
15class XosFineGrainedTenancyGraphCtrl {
16 static $inject = [
17 '$log',
18 'XosServiceGraphStore',
Matteo Scandolo520a8a12017-03-10 17:31:37 -080019 'XosDebouncer',
20 'XosModelDiscoverer',
Matteo Scandolo7629cc42017-03-13 14:12:15 -070021 'XosSidePanel',
Matteo Scandolobafd8d62017-03-29 23:23:00 -070022 'XosGraphHelpers',
23 'XosServiceGraphExtender'
Matteo Scandolo75171782017-03-08 14:17:01 -080024 ];
25
26 public graph: IXosServiceGraph;
27
28 private GraphSubscription: Subscription;
29 private svg;
30 private forceLayout;
31 private linkGroup;
32 private nodeGroup;
Simon Huntc8f23142017-03-14 14:11:13 -070033 private defs;
Matteo Scandolo6a7435f2017-03-24 18:07:17 -070034 private textSize = 20;
35 private textOffset = this.textSize / 4;
Matteo Scandolo75171782017-03-08 14:17:01 -080036
37 // debounced functions
38 private renderGraph;
39
40 constructor(
41 private $log: ng.ILogService,
42 private XosServiceGraphStore: IXosServiceGraphStore,
Matteo Scandolo520a8a12017-03-10 17:31:37 -080043 private XosDebouncer: IXosDebouncer,
44 private XosModelDiscoverer: IXosModelDiscovererService,
Matteo Scandolo7629cc42017-03-13 14:12:15 -070045 private XosSidePanel: IXosSidePanelService,
Matteo Scandolobafd8d62017-03-29 23:23:00 -070046 private XosGraphHelpers: IXosGraphHelpers,
47 private XosServiceGraphExtender: IXosServiceGraphExtender
Matteo Scandolo75171782017-03-08 14:17:01 -080048 ) {
49 this.handleSvg();
Simon Huntc8f23142017-03-14 14:11:13 -070050 this.loadDefs();
Matteo Scandolo75171782017-03-08 14:17:01 -080051 this.setupForceLayout();
Matteo Scandolobafd8d62017-03-29 23:23:00 -070052 this.renderGraph = this.XosDebouncer.debounce(this._renderGraph, 1000, this);
Matteo Scandolo75171782017-03-08 14:17:01 -080053
54 $(window).on('resize', () => {
55 this.setupForceLayout();
56 this.renderGraph();
57 });
58
59 this.GraphSubscription = this.XosServiceGraphStore.get()
60 .subscribe(
61 (graph) => {
Matteo Scandolo98b5f5d2017-03-17 17:09:05 -070062 this.$log.debug(`[XosFineGrainedTenancyGraphCtrl] Fine-Grained Event and render`, graph);
Matteo Scandolo75171782017-03-08 14:17:01 -080063
Matteo Scandolo265c2042017-03-20 10:15:40 -070064 if (!graph || !graph.nodes || !graph.links) {
Matteo Scandolo75171782017-03-08 14:17:01 -080065 return;
66 }
67
Matteo Scandolobafd8d62017-03-29 23:23:00 -070068 _.forEach(this.XosServiceGraphExtender.getFinegrained(), (r: IXosServiceGraphReducer) => {
69 graph = r.reducer(graph);
70 });
71
Matteo Scandolo75171782017-03-08 14:17:01 -080072 this.graph = graph;
73 this.renderGraph();
74 },
75 (err) => {
76 this.$log.error(`[XosFineGrainedTenancyGraphCtrl] Error: `, err);
77 }
78 );
79 }
80
81 $onDestroy() {
82 this.GraphSubscription.unsubscribe();
83 }
84
85 private _renderGraph() {
86 this.addNodeLinksToForceLayout(this.graph);
87 this.renderNodes(this.graph.nodes);
88 this.renderLinks(this.graph.links);
89 }
90
91 private getSvgDimensions(): {width: number, heigth: number} {
92 return {
93 width: $('xos-fine-grained-tenancy-graph svg').width(),
94 heigth: $('xos-fine-grained-tenancy-graph svg').height()
95 };
96 }
97
98 private handleSvg() {
99 this.svg = d3.select('svg');
100
Simon Huntc8f23142017-03-14 14:11:13 -0700101 this.defs = this.svg.append('defs');
102
Matteo Scandolo75171782017-03-08 14:17:01 -0800103 this.linkGroup = this.svg.append('g')
104 .attr({
105 class: 'link-group'
106 });
107
108 this.nodeGroup = this.svg.append('g')
109 .attr({
110 class: 'node-group'
111 });
112 }
113
Simon Huntc8f23142017-03-14 14:11:13 -0700114 private loadDefs() {
115 const cloud = {
116 vbox: '0 0 303.8 185.8',
117 path: `M88.6,44.3c31.7-45.5,102.1-66.7,135-3
118 M37.8,142.9c-22.5,3.5-60.3-32.4-16.3-64.2
119 M101.8,154.2c-15.6,59.7-121.4,18.8-77.3-13
120 M194.6,150c-35.4,51.8-85.7,34.3-98.8-9.5
121 M274.4,116.4c29.4,73.2-81.9,80.3-87.7,44.3
122 M28.5,89.2C3.7,77.4,55.5,4.8,95.3,36.1
123 M216.1,28.9C270.9-13,340.8,91,278.4,131.1`,
124 bgpath: `M22,78.3C21.5,55.1,62.3,10.2,95.2,36
125 h0c31.9-33.4,88.1-50.5,120.6-7.2l0.3,0.2
126 C270.9-13,340.8,91,278.4,131.1v-0.5
127 c10.5,59.8-86.4,63.7-91.8,30.1h-0.4
128 c-30.2,33.6-67.6,24-84.6-6v-0.4
129 c-15.6,59.7-121.4,18.8-77.3-13
130 l-0.2-.2c-20.2-7.9-38.6-36.5-2.8-62.3Z`
131 };
132
133 this.defs.append('symbol')
134 .attr({ id: 'cloud', viewBox: cloud.vbox })
135 .append('path').attr('d', cloud.path);
136
137 this.defs.append('symbol')
138 .attr({ id: 'cloud_bg', viewBox: cloud.vbox })
139 .append('path').attr('d', cloud.bgpath);
140 }
141
Matteo Scandolo75171782017-03-08 14:17:01 -0800142 private setupForceLayout() {
Matteo Scandolo0e8a8422017-03-25 14:55:40 -0700143 this.$log.debug(`[XosFineGrainedTenancyGraphCtrl] Setup Force Layout`);
Matteo Scandolo75171782017-03-08 14:17:01 -0800144 const tick = () => {
145 this.nodeGroup.selectAll('g.node')
146 .attr({
147 transform: d => `translate(${d.x}, ${d.y})`
148 });
149
150 this.linkGroup.selectAll('line')
151 .attr({
152 x1: l => l.source.x || 0,
153 y1: l => l.source.y || 0,
154 x2: l => l.target.x || 0,
155 y2: l => l.target.y || 0,
156 });
157 };
Matteo Scandolo0e8a8422017-03-25 14:55:40 -0700158 const getLinkStrenght = (l: IXosServiceGraphLink) => {
159 if (l.id.indexOf('service') > -1) {
160 return 0.1;
161 }
162 return 1;
163 };
Matteo Scandolo75171782017-03-08 14:17:01 -0800164 const svgDim = this.getSvgDimensions();
165 this.forceLayout = d3.layout.force()
166 .size([svgDim.width, svgDim.heigth])
167 .linkDistance(config.force.linkDistance)
Matteo Scandolo0e8a8422017-03-25 14:55:40 -0700168 .linkStrength(l => getLinkStrenght(l))
Matteo Scandolo75171782017-03-08 14:17:01 -0800169 .charge(config.force.charge)
170 .gravity(config.force.gravity)
171 .on('tick', tick);
172 }
173
174 private addNodeLinksToForceLayout(data: IXosServiceGraph) {
175 this.forceLayout
176 .nodes(data.nodes)
177 .links(data.links)
178 .start();
179 }
180
Matteo Scandolo75171782017-03-08 14:17:01 -0800181 private renderServiceNodes(nodes: any) {
182
183 const self = this;
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700184
Matteo Scandolo75171782017-03-08 14:17:01 -0800185 nodes.append('rect')
186 .attr({
187 rx: config.node.radius,
188 ry: config.node.radius
189 });
190
191 nodes.append('text')
192 .attr({
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700193 'text-anchor': 'middle',
194 'transform': `translate(0,${this.textOffset})`
Matteo Scandolo75171782017-03-08 14:17:01 -0800195 })
196 .text(n => n.label);
197 // .text(n => `${n.id} - ${n.label}`);
198
199 const existing = nodes.selectAll('rect');
200
201 // resize node > rect as contained text
202 existing.each(function() {
Matteo Scandolo7629cc42017-03-13 14:12:15 -0700203 const textBBox = self.XosGraphHelpers.getSiblingTextBBox(this);
Matteo Scandolo75171782017-03-08 14:17:01 -0800204 const rect = d3.select(this);
205 rect.attr({
206 width: textBBox.width + config.node.padding,
207 height: textBBox.height + config.node.padding,
208 x: textBBox.x - (config.node.padding / 2),
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700209 y: (textBBox.y + self.textOffset) - (config.node.padding / 2)
Matteo Scandolo75171782017-03-08 14:17:01 -0800210 });
211 });
212 }
213
214 private renderTenantNodes(nodes: any) {
215 nodes.append('rect')
216 .attr({
217 width: 40,
218 height: 40,
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700219 x: -20,
220 y: -20,
Matteo Scandolo75171782017-03-08 14:17:01 -0800221 transform: `rotate(45)`
222 });
223
224 nodes.append('text')
225 .attr({
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700226 'text-anchor': 'middle',
227 'transform': `translate(0,${this.textOffset})`
Matteo Scandolo75171782017-03-08 14:17:01 -0800228 })
229 .text(n => n.label);
230 }
231
232 private renderNetworkNodes(nodes: any) {
233 const self = this;
Simon Huntc8f23142017-03-14 14:11:13 -0700234
235 nodes.append('use')
236 .attr({
237 class: 'symbol-bg',
238 'xlink:href': '#cloud_bg'
239 });
240
241 nodes.append('use')
242 .attr({
243 class: 'symbol',
244 'xlink:href': '#cloud'
245 });
Matteo Scandolo75171782017-03-08 14:17:01 -0800246
247 nodes.append('text')
248 .attr({
Simon Huntc8f23142017-03-14 14:11:13 -0700249 'text-anchor': 'middle',
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700250 'transform': `translate(0,${this.textOffset})`
Matteo Scandolo75171782017-03-08 14:17:01 -0800251 })
252 .text(n => n.label);
253
Simon Huntc8f23142017-03-14 14:11:13 -0700254 const existing = nodes.selectAll('use');
Matteo Scandolo75171782017-03-08 14:17:01 -0800255
256 // resize node > rect as contained text
257 existing.each(function() {
Matteo Scandolo7629cc42017-03-13 14:12:15 -0700258 const textBBox = self.XosGraphHelpers.getSiblingTextBBox(this);
Simon Huntc8f23142017-03-14 14:11:13 -0700259 const useElem = d3.select(this);
260 const w = textBBox.width + config.node.padding * 2;
261 const h = w;
262 const xoff = -(w / 2);
263 const yoff = -(h / 2);
264
265 useElem.attr({
266 width: w,
267 height: h,
268 transform: 'translate(' + xoff + ',' + yoff + ')'
Matteo Scandolo75171782017-03-08 14:17:01 -0800269 });
270 });
271 }
272
273 private renderSubscriberNodes(nodes: any) {
274 const self = this;
275 nodes.append('rect');
276
277 nodes.append('text')
278 .attr({
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700279 'text-anchor': 'middle',
280 'transform': `translate(0,${this.textOffset})`
Matteo Scandolo75171782017-03-08 14:17:01 -0800281 })
282 .text(n => n.label);
283
284 const existing = nodes.selectAll('rect');
285
286 // resize node > rect as contained text
287 existing.each(function() {
Matteo Scandolo7629cc42017-03-13 14:12:15 -0700288 const textBBox = self.XosGraphHelpers.getSiblingTextBBox(this);
Matteo Scandolo75171782017-03-08 14:17:01 -0800289 const rect = d3.select(this);
290 rect.attr({
291 width: textBBox.width + config.node.padding,
292 height: textBBox.height + config.node.padding,
293 x: textBBox.x - (config.node.padding / 2),
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700294 y: (textBBox.y + self.textOffset) - (config.node.padding / 2)
Matteo Scandolo75171782017-03-08 14:17:01 -0800295 });
296 });
297 }
298
299 private renderNodes(nodes: IXosServiceGraphNode[]) {
300 const node = this.nodeGroup
301 .selectAll('g.node')
302 .data(nodes, n => n.id);
303
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800304 let mouseEventsTimer, selectedModel;
Matteo Scandolo75171782017-03-08 14:17:01 -0800305 const svgDim = this.getSvgDimensions();
306 const hStep = svgDim.width / (nodes.length - 1);
307 const vStep = svgDim.heigth / (nodes.length - 1);
308 const entering = node.enter()
309 .append('g')
310 .attr({
Matteo Scandolo7629cc42017-03-13 14:12:15 -0700311 class: n => `node ${n.type} ${this.XosGraphHelpers.parseElemClasses(n.d3Class)}`,
Matteo Scandolo75171782017-03-08 14:17:01 -0800312 transform: (n, i) => `translate(${hStep * i}, ${vStep * i})`
313 })
314 .call(this.forceLayout.drag)
315 .on('mousedown', () => {
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800316 mouseEventsTimer = new Date().getTime();
Matteo Scandolo75171782017-03-08 14:17:01 -0800317 d3.event.stopPropagation();
318 })
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800319 .on('mouseup', (n) => {
320 mouseEventsTimer = new Date().getTime() - mouseEventsTimer;
321 n.fixed = true;
322 })
323 .on('click', (n: IXosServiceGraphNode) => {
324 if (mouseEventsTimer > 100) {
325 // it is a drag
326 return;
327 }
328 if (selectedModel === n.id) {
329 // this model is already selected, so close the panel
330 this.XosSidePanel.removeInjectedComponents();
331 selectedModel = null;
332 return;
333 }
334 selectedModel = n.id;
335 const modelName = n.model['class_names'].split(',')[0];
336 const formConfig = this.XosModelDiscoverer.get(modelName).formCfg;
337 const model = angular.copy(n.model);
338 delete model.d3Id;
339 this.XosSidePanel.injectComponent('xosForm', {config: formConfig, ngModel: model});
Matteo Scandolo75171782017-03-08 14:17:01 -0800340 });
341
342 this.renderServiceNodes(entering.filter('.service'));
343 this.renderTenantNodes(entering.filter('.tenant'));
344 this.renderNetworkNodes(entering.filter('.network'));
345 this.renderSubscriberNodes(entering.filter('.subscriber'));
Matteo Scandolo0e8a8422017-03-25 14:55:40 -0700346 this.renderSubscriberNodes(entering.filter('.tenantroot'));
Matteo Scandolo75171782017-03-08 14:17:01 -0800347 }
348
349 private renderLinks(links: IXosServiceGraphLink[]) {
350 const link = this.linkGroup
351 .selectAll('line')
352 .data(links, l => l.id);
353
354 const entering = link.enter();
355
356 entering.append('line')
357 .attr({
Matteo Scandolo7629cc42017-03-13 14:12:15 -0700358 class: n => `link ${this.XosGraphHelpers.parseElemClasses(n.d3Class)}`,
Matteo Scandolo75171782017-03-08 14:17:01 -0800359 'marker-start': 'url(#arrow)'
360 });
361 }
362}
363
364export const XosFineGrainedTenancyGraph: angular.IComponentOptions = {
365 template: require('./fine-grained.component.html'),
366 controllerAs: 'vm',
367 controller: XosFineGrainedTenancyGraphCtrl,
368};