blob: 4ff551625f8f9819f9edf4563e1d6990af2bb216 [file] [log] [blame]
Matteo Scandolo219b1a72016-02-09 11:19:22 -08001(function () {
2 'use strict';
3
4 angular.module('xos.serviceTopology')
Matteo Scandolo35d53c82016-02-16 14:44:51 -08005 .service('ServiceTopologyHelper', function($rootScope, $window, $log, lodash, ServiceRelation, serviceTopologyConfig, d3){
Matteo Scandolo219b1a72016-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({
122 class: d => `node ${d.type}`,
123 transform: `translate(${source.y0}, ${source.x0})`
124 });
125
126 const subscriberNodes = nodeEnter.filter('.subscriber');
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800127 const internetNodes = nodeEnter.filter('.router');
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800128 const serviceNodes = nodeEnter.filter('.service');
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800129
130 subscriberNodes.append('rect')
131 .attr(serviceTopologyConfig.square);
132
133 internetNodes.append('rect')
134 .attr(serviceTopologyConfig.square);
135
136 serviceNodes.append('circle')
137 .attr('r', 1e-6)
138 .style('fill', d => d._children ? 'lightsteelblue' : '#fff')
139 .on('click', serviceClick);
140
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800141 nodeEnter.append('text')
142 .attr({
143 x: d => d.children ? -serviceTopologyConfig.circle.selectedRadius -3 : serviceTopologyConfig.circle.selectedRadius + 3,
144 dy: '.35em',
145 transform: d => {
146 if (d.children && d.parent){
147 if(d.parent.x < d.x){
148 return 'rotate(-30)';
149 }
150 return 'rotate(30)';
151 }
152 },
153 'text-anchor': d => d.children ? 'end' : 'start'
154 })
155 .text(d => d.name)
156 .style('fill-opacity', 1e-6);
157
158 // Transition nodes to their new position.
159 var nodeUpdate = node.transition()
160 .duration(serviceTopologyConfig.duration)
161 .attr({
162 'transform': d => `translate(${d.y},${d.x})`
163 });
164
165 nodeUpdate.select('circle')
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800166 .attr('r', d => {
167 return d.selected ? serviceTopologyConfig.circle.selectedRadius : serviceTopologyConfig.circle.radius
168 })
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800169 .style('fill', d => d.selected ? 'lightsteelblue' : '#fff');
170
171 nodeUpdate.select('text')
172 .style('fill-opacity', 1);
173
174 // Transition exiting nodes to the parent's new position.
175 var nodeExit = node.exit().transition()
176 .duration(serviceTopologyConfig.duration)
177 .remove();
178
179 nodeExit.select('circle')
180 .attr('r', 1e-6);
181
182 nodeExit.select('text')
183 .style('fill-opacity', 1e-6);
184
185 // Update the links…
186 var link = svg.selectAll('path.link')
187 .data(links, function(d) { return d.target.id; });
188
189 // Enter any new links at the parent's previous position.
190 link.enter().insert('path', 'g')
191 .attr('class', d => `link ${d.target.type} ${d.target.active ? '' : 'active'}`)
192 .attr('d', function(d) {
193 var o = {x: source.x0, y: source.y0};
194 return diagonal({source: o, target: o});
195 });
196
197 // Transition links to their new position.
198 link.transition()
199 .duration(serviceTopologyConfig.duration)
200 .attr('d', diagonal);
201
202 // Transition exiting nodes to the parent's new position.
203 link.exit().transition()
204 .duration(serviceTopologyConfig.duration)
205 .attr('d', function(d) {
206 var o = {x: source.x, y: source.y};
207 return diagonal({source: o, target: o});
208 })
209 .remove();
210
211 // Stash the old positions for transition.
212 nodes.forEach(function(d) {
213 d.x0 = d.x;
214 d.y0 = d.y;
215 });
216 };
217
218 const serviceClick = function(d) {
219
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800220 $log.info('TODO emit an event to highlight VMs', d);
221
222 if(d.service.service_specific_attribute && d.service.service_specific_attribute.instance_id){
223 $rootScope.$emit('instance.detail', {id: d.service.service_specific_attribute.instance_id});
224 }
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800225
226 if(!d.service){
227 return;
228 }
229
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800230 // unselect all
231 _svg.selectAll('circle')
232 .each(d => d.selected = false);
233
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800234 // toggling selected status
235 d.selected = !d.selected;
236
237 var selectedNode = d3.select(this);
238
239 selectedNode
240 .transition()
241 .duration(serviceTopologyConfig.duration)
242 .attr('r', serviceTopologyConfig.circle.selectedRadius);
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800243
244 updateTree(_svg, _layout, _source);
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800245 };
246
247 this.updateTree = updateTree;
248 this.drawLegend = drawLegend;
249 });
250
251}());