blob: 05ddba7562162bbdd887eb21e68b1649282e1249 [file] [log] [blame]
Matteo Scandoloeeb9c082016-02-09 11:19:22 -08001(function () {
2 'use strict';
3
4 angular.module('xos.serviceTopology')
Matteo Scandolo7fd4d042016-02-16 14:44:51 -08005 .service('ServiceTopologyHelper', function($rootScope, $window, $log, lodash, ServiceRelation, serviceTopologyConfig, d3){
Matteo Scandoloeeb9c082016-02-09 11:19:22 -08006
7 const drawLegend = (svg) => {
8 const legendContainer = svg.append('g')
9 .attr({
10 class: 'legend'
11 });
12
13 legendContainer.append('rect')
14 .attr({
15 transform: d => `translate(10, 80)`,
16 width: 100,
17 height: 100
18 });
19
20 // service
21 const service = legendContainer.append('g')
22 .attr({
23 class: 'node service'
24 });
25
26 service.append('circle')
27 .attr({
28 r: serviceTopologyConfig.circle.radius,
29 transform: d => `translate(30, 100)`
30 });
31
32 service.append('text')
33 .attr({
34 transform: d => `translate(45, 100)`,
35 dy: '.35em'
36 })
37 .text('Service')
38 .style('fill-opacity', 1);
39
40 // slice
41 const slice = legendContainer.append('g')
42 .attr({
43 class: 'node slice'
44 });
45
46 slice.append('rect')
47 .attr({
48 width: 20,
49 height: 20,
50 x: -10,
51 y: -10,
52 transform: d => `translate(30, 130)`
53 });
54
55 slice.append('text')
56 .attr({
57 transform: d => `translate(45, 130)`,
58 dy: '.35em'
59 })
60 .text('Slices')
61 .style('fill-opacity', 1);
62
63 // instance
64 const instance = legendContainer.append('g')
65 .attr({
66 class: 'node instance'
67 });
68
69 instance.append('rect')
70 .attr({
71 width: 20,
72 height: 20,
73 x: -10,
74 y: -10,
75 transform: d => `translate(30, 160)`
76 });
77
78 instance.append('text')
79 .attr({
80 transform: d => `translate(45, 160)`,
81 dy: '.35em'
82 })
83 .text('Instances')
84 .style('fill-opacity', 1);
85 };
86
87 var _svg, _layout, _source;
88
89 var i = 0;
90
91 // given a canvas, a layout and a data source, draw a tree layout
92 const updateTree = (svg, layout, source) => {
93
94 //cache data
95 _svg = svg;
96 _layout = layout;
97 _source = source;
98
99 const maxDepth = ServiceRelation.depthOf(source);
100
101 const diagonal = d3.svg.diagonal()
102 .projection(d => [d.y, d.x]);
103
104 // Compute the new tree layout.
105 var nodes = layout.nodes(source).reverse(),
106 links = layout.links(nodes);
107
108 // Normalize for fixed-depth.
109 nodes.forEach(function(d) {
110 // position the child node horizontally
111 const step = (($window.innerWidth - (serviceTopologyConfig.widthMargin * 2)) / maxDepth);
112 d.y = d.depth * step;
113 });
114
115 // Update the nodes…
116 var node = svg.selectAll('g.node')
117 .data(nodes, function(d) { return d.id || (d.id = ++i); });
118
119 // Enter any new nodes at the parent's previous position.
120 var nodeEnter = node.enter().append('g')
121 .attr({
Matteo Scandolo657d1322016-02-16 17:43:00 -0800122 class: d => {
123 console.log(d);
124 return `node ${d.type}`
125 },
Matteo Scandoloeeb9c082016-02-09 11:19:22 -0800126 transform: `translate(${source.y0}, ${source.x0})`
127 });
128
129 const subscriberNodes = nodeEnter.filter('.subscriber');
Matteo Scandolo38ba3312016-02-09 16:01:49 -0800130 const internetNodes = nodeEnter.filter('.router');
Matteo Scandoloeeb9c082016-02-09 11:19:22 -0800131 const serviceNodes = nodeEnter.filter('.service');
Matteo Scandoloeeb9c082016-02-09 11:19:22 -0800132
133 subscriberNodes.append('rect')
134 .attr(serviceTopologyConfig.square);
135
136 internetNodes.append('rect')
137 .attr(serviceTopologyConfig.square);
138
139 serviceNodes.append('circle')
140 .attr('r', 1e-6)
141 .style('fill', d => d._children ? 'lightsteelblue' : '#fff')
142 .on('click', serviceClick);
143
Matteo Scandoloeeb9c082016-02-09 11:19:22 -0800144 nodeEnter.append('text')
145 .attr({
146 x: d => d.children ? -serviceTopologyConfig.circle.selectedRadius -3 : serviceTopologyConfig.circle.selectedRadius + 3,
147 dy: '.35em',
148 transform: d => {
149 if (d.children && d.parent){
150 if(d.parent.x < d.x){
151 return 'rotate(-30)';
152 }
153 return 'rotate(30)';
154 }
155 },
156 'text-anchor': d => d.children ? 'end' : 'start'
157 })
158 .text(d => d.name)
159 .style('fill-opacity', 1e-6);
160
161 // Transition nodes to their new position.
162 var nodeUpdate = node.transition()
163 .duration(serviceTopologyConfig.duration)
164 .attr({
165 'transform': d => `translate(${d.y},${d.x})`
166 });
167
168 nodeUpdate.select('circle')
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800169 .attr('r', d => {
170 return d.selected ? serviceTopologyConfig.circle.selectedRadius : serviceTopologyConfig.circle.radius
171 })
Matteo Scandoloeeb9c082016-02-09 11:19:22 -0800172 .style('fill', d => d.selected ? 'lightsteelblue' : '#fff');
173
174 nodeUpdate.select('text')
175 .style('fill-opacity', 1);
176
177 // Transition exiting nodes to the parent's new position.
178 var nodeExit = node.exit().transition()
179 .duration(serviceTopologyConfig.duration)
180 .remove();
181
182 nodeExit.select('circle')
183 .attr('r', 1e-6);
184
185 nodeExit.select('text')
186 .style('fill-opacity', 1e-6);
187
188 // Update the links…
189 var link = svg.selectAll('path.link')
190 .data(links, function(d) { return d.target.id; });
191
192 // Enter any new links at the parent's previous position.
193 link.enter().insert('path', 'g')
194 .attr('class', d => `link ${d.target.type} ${d.target.active ? '' : 'active'}`)
195 .attr('d', function(d) {
196 var o = {x: source.x0, y: source.y0};
197 return diagonal({source: o, target: o});
198 });
199
200 // Transition links to their new position.
201 link.transition()
202 .duration(serviceTopologyConfig.duration)
203 .attr('d', diagonal);
204
205 // Transition exiting nodes to the parent's new position.
206 link.exit().transition()
207 .duration(serviceTopologyConfig.duration)
208 .attr('d', function(d) {
209 var o = {x: source.x, y: source.y};
210 return diagonal({source: o, target: o});
211 })
212 .remove();
213
214 // Stash the old positions for transition.
215 nodes.forEach(function(d) {
216 d.x0 = d.x;
217 d.y0 = d.y;
218 });
219 };
220
221 const serviceClick = function(d) {
222
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800223 $log.info('TODO emit an event to highlight VMs', d);
224
Matteo Scandoloc2c6fb02016-02-16 16:03:43 -0800225 // TODO how I get the instance id???
226 $rootScope.$emit('instance.detail', {id: d.service.service_specific_attribute.instance_id || null});
Matteo Scandoloeeb9c082016-02-09 11:19:22 -0800227
228 if(!d.service){
229 return;
230 }
231
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800232 // unselect all
233 _svg.selectAll('circle')
234 .each(d => d.selected = false);
235
Matteo Scandoloeeb9c082016-02-09 11:19:22 -0800236 // toggling selected status
237 d.selected = !d.selected;
238
239 var selectedNode = d3.select(this);
240
241 selectedNode
242 .transition()
243 .duration(serviceTopologyConfig.duration)
244 .attr('r', serviceTopologyConfig.circle.selectedRadius);
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800245
246 updateTree(_svg, _layout, _source);
Matteo Scandoloeeb9c082016-02-09 11:19:22 -0800247 };
248
249 this.updateTree = updateTree;
250 this.drawLegend = drawLegend;
251 });
252
253}());