blob: d0aea8374cdc0679c6231ea5899097d0759597e5 [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() {
Matteo Scandolo9b460042017-04-14 16:24:45 -070086 if (!angular.isDefined(this.graph) || !angular.isDefined(this.graph.nodes) || !angular.isDefined(this.graph.links)) {
87 return;
88 }
Matteo Scandolo75171782017-03-08 14:17:01 -080089 this.addNodeLinksToForceLayout(this.graph);
90 this.renderNodes(this.graph.nodes);
91 this.renderLinks(this.graph.links);
92 }
93
94 private getSvgDimensions(): {width: number, heigth: number} {
95 return {
96 width: $('xos-fine-grained-tenancy-graph svg').width(),
97 heigth: $('xos-fine-grained-tenancy-graph svg').height()
98 };
99 }
100
101 private handleSvg() {
102 this.svg = d3.select('svg');
103
Simon Huntc8f23142017-03-14 14:11:13 -0700104 this.defs = this.svg.append('defs');
105
Matteo Scandolo75171782017-03-08 14:17:01 -0800106 this.linkGroup = this.svg.append('g')
107 .attr({
108 class: 'link-group'
109 });
110
111 this.nodeGroup = this.svg.append('g')
112 .attr({
113 class: 'node-group'
114 });
115 }
116
Simon Huntc8f23142017-03-14 14:11:13 -0700117 private loadDefs() {
118 const cloud = {
119 vbox: '0 0 303.8 185.8',
120 path: `M88.6,44.3c31.7-45.5,102.1-66.7,135-3
121 M37.8,142.9c-22.5,3.5-60.3-32.4-16.3-64.2
122 M101.8,154.2c-15.6,59.7-121.4,18.8-77.3-13
123 M194.6,150c-35.4,51.8-85.7,34.3-98.8-9.5
124 M274.4,116.4c29.4,73.2-81.9,80.3-87.7,44.3
125 M28.5,89.2C3.7,77.4,55.5,4.8,95.3,36.1
126 M216.1,28.9C270.9-13,340.8,91,278.4,131.1`,
127 bgpath: `M22,78.3C21.5,55.1,62.3,10.2,95.2,36
128 h0c31.9-33.4,88.1-50.5,120.6-7.2l0.3,0.2
129 C270.9-13,340.8,91,278.4,131.1v-0.5
130 c10.5,59.8-86.4,63.7-91.8,30.1h-0.4
131 c-30.2,33.6-67.6,24-84.6-6v-0.4
132 c-15.6,59.7-121.4,18.8-77.3-13
133 l-0.2-.2c-20.2-7.9-38.6-36.5-2.8-62.3Z`
134 };
135
136 this.defs.append('symbol')
137 .attr({ id: 'cloud', viewBox: cloud.vbox })
138 .append('path').attr('d', cloud.path);
139
140 this.defs.append('symbol')
141 .attr({ id: 'cloud_bg', viewBox: cloud.vbox })
142 .append('path').attr('d', cloud.bgpath);
143 }
144
Matteo Scandolo75171782017-03-08 14:17:01 -0800145 private setupForceLayout() {
Matteo Scandolo0e8a8422017-03-25 14:55:40 -0700146 this.$log.debug(`[XosFineGrainedTenancyGraphCtrl] Setup Force Layout`);
Matteo Scandolo75171782017-03-08 14:17:01 -0800147 const tick = () => {
148 this.nodeGroup.selectAll('g.node')
149 .attr({
150 transform: d => `translate(${d.x}, ${d.y})`
151 });
152
153 this.linkGroup.selectAll('line')
154 .attr({
155 x1: l => l.source.x || 0,
156 y1: l => l.source.y || 0,
157 x2: l => l.target.x || 0,
158 y2: l => l.target.y || 0,
159 });
160 };
Matteo Scandolo0e8a8422017-03-25 14:55:40 -0700161 const getLinkStrenght = (l: IXosServiceGraphLink) => {
162 if (l.id.indexOf('service') > -1) {
163 return 0.1;
164 }
165 return 1;
166 };
Matteo Scandolo75171782017-03-08 14:17:01 -0800167 const svgDim = this.getSvgDimensions();
168 this.forceLayout = d3.layout.force()
169 .size([svgDim.width, svgDim.heigth])
170 .linkDistance(config.force.linkDistance)
Matteo Scandolo0e8a8422017-03-25 14:55:40 -0700171 .linkStrength(l => getLinkStrenght(l))
Matteo Scandolo75171782017-03-08 14:17:01 -0800172 .charge(config.force.charge)
173 .gravity(config.force.gravity)
174 .on('tick', tick);
175 }
176
177 private addNodeLinksToForceLayout(data: IXosServiceGraph) {
178 this.forceLayout
179 .nodes(data.nodes)
180 .links(data.links)
181 .start();
182 }
183
Matteo Scandolo75171782017-03-08 14:17:01 -0800184 private renderServiceNodes(nodes: any) {
185
186 const self = this;
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700187
Matteo Scandolo75171782017-03-08 14:17:01 -0800188 nodes.append('rect')
189 .attr({
190 rx: config.node.radius,
191 ry: config.node.radius
192 });
193
194 nodes.append('text')
195 .attr({
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700196 'text-anchor': 'middle',
197 'transform': `translate(0,${this.textOffset})`
Matteo Scandolo75171782017-03-08 14:17:01 -0800198 })
199 .text(n => n.label);
200 // .text(n => `${n.id} - ${n.label}`);
201
202 const existing = nodes.selectAll('rect');
203
204 // resize node > rect as contained text
205 existing.each(function() {
Matteo Scandolo7629cc42017-03-13 14:12:15 -0700206 const textBBox = self.XosGraphHelpers.getSiblingTextBBox(this);
Matteo Scandolo75171782017-03-08 14:17:01 -0800207 const rect = d3.select(this);
208 rect.attr({
209 width: textBBox.width + config.node.padding,
210 height: textBBox.height + config.node.padding,
211 x: textBBox.x - (config.node.padding / 2),
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700212 y: (textBBox.y + self.textOffset) - (config.node.padding / 2)
Matteo Scandolo75171782017-03-08 14:17:01 -0800213 });
214 });
215 }
216
217 private renderTenantNodes(nodes: any) {
218 nodes.append('rect')
219 .attr({
220 width: 40,
221 height: 40,
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700222 x: -20,
223 y: -20,
Matteo Scandolo75171782017-03-08 14:17:01 -0800224 transform: `rotate(45)`
225 });
226
227 nodes.append('text')
228 .attr({
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700229 'text-anchor': 'middle',
230 'transform': `translate(0,${this.textOffset})`
Matteo Scandolo75171782017-03-08 14:17:01 -0800231 })
232 .text(n => n.label);
233 }
234
235 private renderNetworkNodes(nodes: any) {
236 const self = this;
Simon Huntc8f23142017-03-14 14:11:13 -0700237
238 nodes.append('use')
239 .attr({
240 class: 'symbol-bg',
241 'xlink:href': '#cloud_bg'
242 });
243
244 nodes.append('use')
245 .attr({
246 class: 'symbol',
247 'xlink:href': '#cloud'
248 });
Matteo Scandolo75171782017-03-08 14:17:01 -0800249
250 nodes.append('text')
251 .attr({
Simon Huntc8f23142017-03-14 14:11:13 -0700252 'text-anchor': 'middle',
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700253 'transform': `translate(0,${this.textOffset})`
Matteo Scandolo75171782017-03-08 14:17:01 -0800254 })
255 .text(n => n.label);
256
Simon Huntc8f23142017-03-14 14:11:13 -0700257 const existing = nodes.selectAll('use');
Matteo Scandolo75171782017-03-08 14:17:01 -0800258
259 // resize node > rect as contained text
260 existing.each(function() {
Matteo Scandolo7629cc42017-03-13 14:12:15 -0700261 const textBBox = self.XosGraphHelpers.getSiblingTextBBox(this);
Simon Huntc8f23142017-03-14 14:11:13 -0700262 const useElem = d3.select(this);
263 const w = textBBox.width + config.node.padding * 2;
264 const h = w;
265 const xoff = -(w / 2);
266 const yoff = -(h / 2);
267
268 useElem.attr({
269 width: w,
270 height: h,
271 transform: 'translate(' + xoff + ',' + yoff + ')'
Matteo Scandolo75171782017-03-08 14:17:01 -0800272 });
273 });
274 }
275
276 private renderSubscriberNodes(nodes: any) {
277 const self = this;
278 nodes.append('rect');
279
280 nodes.append('text')
281 .attr({
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700282 'text-anchor': 'middle',
283 'transform': `translate(0,${this.textOffset})`
Matteo Scandolo75171782017-03-08 14:17:01 -0800284 })
285 .text(n => n.label);
286
287 const existing = nodes.selectAll('rect');
288
289 // resize node > rect as contained text
290 existing.each(function() {
Matteo Scandolo7629cc42017-03-13 14:12:15 -0700291 const textBBox = self.XosGraphHelpers.getSiblingTextBBox(this);
Matteo Scandolo75171782017-03-08 14:17:01 -0800292 const rect = d3.select(this);
293 rect.attr({
294 width: textBBox.width + config.node.padding,
295 height: textBBox.height + config.node.padding,
296 x: textBBox.x - (config.node.padding / 2),
Matteo Scandolo6a7435f2017-03-24 18:07:17 -0700297 y: (textBBox.y + self.textOffset) - (config.node.padding / 2)
Matteo Scandolo75171782017-03-08 14:17:01 -0800298 });
299 });
300 }
301
302 private renderNodes(nodes: IXosServiceGraphNode[]) {
303 const node = this.nodeGroup
304 .selectAll('g.node')
305 .data(nodes, n => n.id);
306
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800307 let mouseEventsTimer, selectedModel;
Matteo Scandolo75171782017-03-08 14:17:01 -0800308 const svgDim = this.getSvgDimensions();
309 const hStep = svgDim.width / (nodes.length - 1);
310 const vStep = svgDim.heigth / (nodes.length - 1);
311 const entering = node.enter()
312 .append('g')
313 .attr({
Matteo Scandolo7629cc42017-03-13 14:12:15 -0700314 class: n => `node ${n.type} ${this.XosGraphHelpers.parseElemClasses(n.d3Class)}`,
Matteo Scandolo75171782017-03-08 14:17:01 -0800315 transform: (n, i) => `translate(${hStep * i}, ${vStep * i})`
316 })
317 .call(this.forceLayout.drag)
318 .on('mousedown', () => {
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800319 mouseEventsTimer = new Date().getTime();
Matteo Scandolo75171782017-03-08 14:17:01 -0800320 d3.event.stopPropagation();
321 })
Matteo Scandolo520a8a12017-03-10 17:31:37 -0800322 .on('mouseup', (n) => {
323 mouseEventsTimer = new Date().getTime() - mouseEventsTimer;
324 n.fixed = true;
325 })
326 .on('click', (n: IXosServiceGraphNode) => {
327 if (mouseEventsTimer > 100) {
328 // it is a drag
329 return;
330 }
331 if (selectedModel === n.id) {
332 // this model is already selected, so close the panel
333 this.XosSidePanel.removeInjectedComponents();
334 selectedModel = null;
335 return;
336 }
337 selectedModel = n.id;
338 const modelName = n.model['class_names'].split(',')[0];
339 const formConfig = this.XosModelDiscoverer.get(modelName).formCfg;
340 const model = angular.copy(n.model);
341 delete model.d3Id;
342 this.XosSidePanel.injectComponent('xosForm', {config: formConfig, ngModel: model});
Matteo Scandolo75171782017-03-08 14:17:01 -0800343 });
344
345 this.renderServiceNodes(entering.filter('.service'));
346 this.renderTenantNodes(entering.filter('.tenant'));
347 this.renderNetworkNodes(entering.filter('.network'));
348 this.renderSubscriberNodes(entering.filter('.subscriber'));
Matteo Scandolo0e8a8422017-03-25 14:55:40 -0700349 this.renderSubscriberNodes(entering.filter('.tenantroot'));
Matteo Scandolo75171782017-03-08 14:17:01 -0800350 }
351
352 private renderLinks(links: IXosServiceGraphLink[]) {
353 const link = this.linkGroup
354 .selectAll('line')
355 .data(links, l => l.id);
356
357 const entering = link.enter();
358
359 entering.append('line')
360 .attr({
Matteo Scandolo7629cc42017-03-13 14:12:15 -0700361 class: n => `link ${this.XosGraphHelpers.parseElemClasses(n.d3Class)}`,
Matteo Scandolo75171782017-03-08 14:17:01 -0800362 'marker-start': 'url(#arrow)'
363 });
364 }
365}
366
367export const XosFineGrainedTenancyGraph: angular.IComponentOptions = {
368 template: require('./fine-grained.component.html'),
369 controllerAs: 'vm',
370 controller: XosFineGrainedTenancyGraphCtrl,
371};