Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | angular.module('xos.serviceTopology') |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 5 | .service('LogicTopologyHelper', function($window, $log, lodash, serviceTopologyConfig, NodeDrawer){ |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 6 | |
| 7 | var diagonal, nodes, links, i = 0, svgWidth, svgHeight, layout; |
| 8 | |
| 9 | const baseData = { |
| 10 | name: 'Router', |
| 11 | type: 'router', |
| 12 | children: [ |
| 13 | { |
| 14 | name: 'WAN', |
| 15 | type: 'network', |
| 16 | children: [ |
| 17 | { |
| 18 | name: 'Rack', |
| 19 | type: 'rack', |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 20 | computeNodes: [], |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 21 | children: [ |
| 22 | { |
| 23 | name: 'LAN', |
| 24 | type: 'network', |
Matteo Scandolo | 7e67a9a | 2016-02-16 16:33:26 -0800 | [diff] [blame] | 25 | children: [{ |
| 26 | name: 'Subscriber', |
| 27 | type: 'subscriber' |
| 28 | }] //subscribers goes here |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 29 | } |
| 30 | ] |
| 31 | } |
| 32 | ] |
| 33 | } |
| 34 | ] |
| 35 | }; |
| 36 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 37 | /** |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 38 | * Calculate the horizontal position for each element. |
| 39 | * subsrcribers, devices and routers have the same fixed width 20 |
| 40 | * network have a fixed width 104 |
| 41 | * rack have a fixed width 105 |
| 42 | * build and array of 6 elements representing the position of each element in the svg |
| 43 | * to equally space them |
| 44 | */ |
| 45 | |
| 46 | this.computeElementPosition = (svgWidth) => { |
| 47 | |
| 48 | let xPos = []; |
| 49 | |
| 50 | let totalElWidth = lodash.reduce(serviceTopologyConfig.elWidths, (el, val) => val + el, 0); |
| 51 | |
Matteo Scandolo | bec0a6c | 2016-02-11 17:58:18 -0800 | [diff] [blame] | 52 | let remainingSpace = svgWidth - totalElWidth - (serviceTopologyConfig.widthMargin * 2); |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 53 | |
| 54 | let step = remainingSpace / (serviceTopologyConfig.elWidths.length - 1); |
| 55 | |
| 56 | lodash.forEach(serviceTopologyConfig.elWidths, (el, i) => { |
| 57 | |
| 58 | // get half of the previous elements width |
| 59 | let previousElWidth = 0; |
| 60 | if(i !== 0){ |
Matteo Scandolo | bec0a6c | 2016-02-11 17:58:18 -0800 | [diff] [blame] | 61 | previousElWidth = lodash.reduce(serviceTopologyConfig.elWidths.slice(0, i), (el, val) => val + el, 0); |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | let elPos = |
| 65 | serviceTopologyConfig.widthMargin // right margin |
| 66 | + (step * i) // space between elements |
| 67 | + (el / 2) // this el width |
| 68 | + previousElWidth; // previous elements width |
Matteo Scandolo | 77d8fa0 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 69 | |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 70 | xPos.push(svgWidth - elPos); |
| 71 | }) |
| 72 | |
| 73 | return xPos |
| 74 | }; |
| 75 | |
| 76 | /** |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 77 | * from a nested data structure, |
| 78 | * create nodes and links for a D3 Tree Layout |
| 79 | */ |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 80 | const computeLayout = (data) => { |
| 81 | let nodes = layout.nodes(data); |
| 82 | |
| 83 | // Normalize for fixed-depth. |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 84 | nodes.forEach((d) => { |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 85 | // position the child node horizontally |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 86 | d.y = this.computeElementPosition(svgWidth)[d.depth]; |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 87 | }); |
| 88 | |
| 89 | let links = layout.links(nodes); |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 90 | |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 91 | return [nodes, links]; |
| 92 | }; |
| 93 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Draw the containing group for any node or update the existing one |
| 96 | */ |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 97 | const drawNodes = (svg, nodes) => { |
| 98 | // Update the nodes… |
| 99 | var node = svg.selectAll('g.node') |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 100 | .data(nodes, d => { |
| 101 | if(!angular.isString(d.d3Id)){ |
| 102 | d.d3Id = `tree-${++i}`; |
| 103 | } |
| 104 | return d.d3Id; |
| 105 | }); |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 106 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 107 | // Enter any new nodes |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 108 | var nodeEnter = node.enter().append('g') |
| 109 | .attr({ |
| 110 | class: d => `node ${d.type}`, |
| 111 | transform: `translate(${svgWidth / 2}, ${svgHeight / 2})` |
| 112 | }); |
| 113 | |
Matteo Scandolo | fd46858 | 2016-02-16 16:03:43 -0800 | [diff] [blame] | 114 | // create Nodes |
Matteo Scandolo | 79de20a | 2016-02-16 15:06:11 -0800 | [diff] [blame] | 115 | NodeDrawer.addNetworks(node.filter('.network')); |
| 116 | NodeDrawer.addRack(node.filter('.rack')); |
| 117 | NodeDrawer.addPhisical(node.filter('.router')); |
| 118 | NodeDrawer.addPhisical(node.filter('.subscriber')); |
| 119 | NodeDrawer.addDevice(node.filter('.device')); |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 120 | |
Matteo Scandolo | fd46858 | 2016-02-16 16:03:43 -0800 | [diff] [blame] | 121 | //update nodes |
| 122 | // TODO if data change, only update them |
| 123 | // NodeDrawer.updateRack(node.filter('.rack')); |
| 124 | |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 125 | // Transition nodes to their new position. |
| 126 | var nodeUpdate = node.transition() |
| 127 | .duration(serviceTopologyConfig.duration) |
| 128 | .attr({ |
| 129 | 'transform': d => `translate(${d.y},${d.x})` |
| 130 | }); |
| 131 | |
| 132 | // TODO handle node remove |
| 133 | }; |
| 134 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 135 | /** |
| 136 | * Handle links in the tree layout |
| 137 | */ |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 138 | const drawLinks = (svg, links) => { |
| 139 | |
| 140 | diagonal = d3.svg.diagonal() |
| 141 | .projection(d => [d.y, d.x]); |
| 142 | |
| 143 | // Update the links… |
| 144 | var link = svg.selectAll('path.link') |
| 145 | .data(links, d => { |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 146 | return d.target.d3Id |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 147 | }); |
| 148 | |
| 149 | // Enter any new links at the parent's previous position. |
| 150 | link.enter().insert('path', 'g') |
| 151 | .attr('class', d => `link ${d.target.type}`) |
| 152 | .attr('d', function(d) { |
| 153 | var o = {x: svgHeight / 2, y: svgWidth / 2}; |
| 154 | return diagonal({source: o, target: o}); |
| 155 | }); |
| 156 | |
| 157 | // Transition links to their new position. |
| 158 | link.transition() |
| 159 | .duration(serviceTopologyConfig.duration) |
| 160 | .attr('d', diagonal); |
| 161 | }; |
| 162 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 163 | /** |
| 164 | * Calculate the svg size and setup tree layout |
| 165 | */ |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 166 | this.setupTree = (svg) => { |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 167 | |
| 168 | |
| 169 | svgWidth = svg.node().getBoundingClientRect().width; |
| 170 | svgHeight = svg.node().getBoundingClientRect().height; |
| 171 | |
| 172 | const width = svgWidth - (serviceTopologyConfig.widthMargin * 2); |
| 173 | const height = svgHeight - (serviceTopologyConfig.heightMargin * 2); |
| 174 | |
| 175 | layout = d3.layout.tree() |
| 176 | .size([height, width]); |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 177 | }; |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 178 | |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 179 | /** |
| 180 | * Update the tree layout |
| 181 | */ |
| 182 | |
| 183 | this.updateTree = (svg) => { |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 184 | // Compute the new tree layout. |
| 185 | [nodes, links] = computeLayout(baseData); |
| 186 | |
| 187 | drawNodes(svg, nodes); |
| 188 | drawLinks(svg, links); |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 189 | } |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 190 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 191 | /** |
| 192 | * Add Subscribers to the tree |
| 193 | */ |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 194 | this.addSubscribers = (subscribers) => { |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 195 | |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 196 | subscribers.map((subscriber) => { |
| 197 | subscriber.children = subscriber.devices; |
| 198 | }); |
| 199 | |
| 200 | // add subscriber to data tree |
| 201 | baseData.children[0].children[0].children[0].children = subscribers; |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 202 | |
| 203 | return baseData; |
| 204 | }; |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 205 | |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 206 | /** |
| 207 | * Add compute nodes to the rack element |
| 208 | */ |
| 209 | |
| 210 | this.addComputeNodes = (computeNodes) => { |
| 211 | baseData.children[0].children[0].computeNodes = computeNodes; |
Matteo Scandolo | 79de20a | 2016-02-16 15:06:11 -0800 | [diff] [blame] | 212 | }; |
| 213 | |
Matteo Scandolo | 5103148 | 2016-02-17 13:54:11 -0800 | [diff] [blame] | 214 | this.getInstanceStatus = (instances) => { |
Matteo Scandolo | 79de20a | 2016-02-16 15:06:11 -0800 | [diff] [blame] | 215 | |
| 216 | const computeNodes = baseData.children[0].children[0].computeNodes; |
| 217 | |
Matteo Scandolo | fd46858 | 2016-02-16 16:03:43 -0800 | [diff] [blame] | 218 | // unselect all |
| 219 | computeNodes.map((node) => { |
| 220 | node.instances.map((instance) => { |
| 221 | instance.selected = false |
| 222 | return instance; |
| 223 | }); |
| 224 | }); |
| 225 | |
Matteo Scandolo | c49ff70 | 2016-02-17 15:11:33 -0800 | [diff] [blame] | 226 | lodash.forEach(instances, (instance) => { |
Matteo Scandolo | 5103148 | 2016-02-17 13:54:11 -0800 | [diff] [blame] | 227 | computeNodes.map((node) => { |
Matteo Scandolo | c49ff70 | 2016-02-17 15:11:33 -0800 | [diff] [blame] | 228 | node.instances.map((d3instance) => { |
| 229 | if(d3instance.id === instance.id){ |
| 230 | d3instance.selected = true; |
Matteo Scandolo | 5103148 | 2016-02-17 13:54:11 -0800 | [diff] [blame] | 231 | } |
Matteo Scandolo | c49ff70 | 2016-02-17 15:11:33 -0800 | [diff] [blame] | 232 | return d3instance; |
Matteo Scandolo | 5103148 | 2016-02-17 13:54:11 -0800 | [diff] [blame] | 233 | }); |
| 234 | }); |
| 235 | }); |
Matteo Scandolo | 79de20a | 2016-02-16 15:06:11 -0800 | [diff] [blame] | 236 | |
Matteo Scandolo | af28637 | 2016-02-09 14:46:14 -0800 | [diff] [blame] | 237 | } |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 238 | }); |
| 239 | |
| 240 | }()); |