Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | const shapes = { |
| 5 | cloud: ' M 79.72 49.60 C 86.00 37.29 98.57 29.01 111.96 26.42 C 124.27 24.11 137.53 26.15 148.18 32.90 C 158.08 38.78 165.39 48.87 167.65 60.20 C 176.20 57.90 185.14 56.01 194.00 57.73 C 206.08 59.59 217.92 66.01 224.37 76.66 C 227.51 81.54 228.85 87.33 229.23 93.06 C 237.59 93.33 246.22 95.10 253.04 100.19 C 256.69 103.13 259.87 107.67 258.91 112.59 C 257.95 118.43 252.78 122.38 247.78 124.82 C 235.27 130.43 220.23 130.09 207.98 123.93 C 199.33 127.88 189.76 129.43 180.30 128.57 C 173.70 139.92 161.70 147.65 148.86 149.93 C 133.10 153.26 116.06 148.15 104.42 137.08 C 92.98 143.04 78.96 143.87 66.97 139.04 C 57.75 135.41 49.70 128.00 46.60 118.43 C 43.87 109.95 45.81 100.29 51.30 93.32 C 57.38 85.18 67.10 80.44 76.99 78.89 C 74.38 69.20 74.87 58.52 79.72 49.60 Z' |
| 6 | } |
| 7 | |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 8 | var computeNodeId = 0; |
| 9 | var instanceId = 0; |
| 10 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 11 | angular.module('xos.serviceTopology') |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 12 | .service('NodeDrawer', function(d3, serviceTopologyConfig, RackHelper){ |
Matteo Scandolo | 5103148 | 2016-02-17 13:54:11 -0800 | [diff] [blame] | 13 | |
| 14 | var _this = this; |
| 15 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 16 | this.addNetworks = (nodes) => { |
| 17 | nodes.append('path') |
| 18 | .attr({ |
| 19 | d: shapes.cloud, |
| 20 | transform: 'translate(-63, -52), scale(0.5)', |
| 21 | class: 'cloud' |
| 22 | }); |
| 23 | |
| 24 | nodes.append('text') |
| 25 | .attr({ |
| 26 | 'text-anchor': 'middle' |
| 27 | }) |
| 28 | .text(d => d.name) |
| 29 | } |
| 30 | |
| 31 | this.addRack = (nodes) => { |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 32 | |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 33 | // loop because of D3 |
| 34 | // rack will be only one |
| 35 | nodes.each(d => { |
| 36 | let [w, h] = RackHelper.getRackSize(d.computeNodes); |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 37 | |
| 38 | let rack = nodes |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 39 | .append('g'); |
| 40 | |
| 41 | rack |
| 42 | .attr({ |
| 43 | transform: `translate(0,0)` |
| 44 | }) |
| 45 | .transition() |
| 46 | .duration(serviceTopologyConfig.duration) |
| 47 | .attr({ |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 48 | transform: d => `translate(${- (w / 2)}, ${- (h / 2)})` |
| 49 | }); |
| 50 | |
| 51 | rack |
| 52 | .append('rect') |
| 53 | .attr({ |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 54 | width: 0, |
| 55 | height: 0 |
| 56 | }) |
| 57 | .transition() |
| 58 | .duration(serviceTopologyConfig.duration) |
| 59 | .attr({ |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 60 | width: w, |
| 61 | height: h |
| 62 | }); |
| 63 | |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 64 | rack.append('text') |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 65 | .attr({ |
| 66 | 'text-anchor': 'middle', |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 67 | y: - 10, |
| 68 | x: w / 2, |
| 69 | opacity: 0 |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 70 | }) |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 71 | .text(d => d.name) |
| 72 | .transition() |
| 73 | .duration(serviceTopologyConfig.duration) |
| 74 | .attr({ |
| 75 | opacity: 1 |
| 76 | }) |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 77 | |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 78 | this.drawComputeNodes(rack, d.computeNodes); |
| 79 | |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 80 | }); |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 81 | |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | this.drawComputeNodes = (container, nodes) => { |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 85 | |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 86 | let elements = container.selectAll('.compute-nodes') |
| 87 | .data(nodes, d => { |
| 88 | if(!angular.isString(d.d3Id)){ |
| 89 | d.d3Id = `compute-node-${++computeNodeId}`; |
| 90 | } |
| 91 | return d.d3Id; |
| 92 | }); |
| 93 | |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 94 | let {width, height} = container.node().getBoundingClientRect(); |
| 95 | |
| 96 | var nodeContainer = elements.enter().append('g'); |
| 97 | |
| 98 | nodeContainer |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 99 | .attr({ |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 100 | transform: `translate(${width / 2}, ${ height / 2})`, |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 101 | class: 'compute-node', |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 102 | }) |
| 103 | .transition() |
| 104 | .duration(serviceTopologyConfig.duration) |
| 105 | .attr({ |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 106 | transform: (d) => `translate(${RackHelper.getComputeNodePosition(nodes, d.d3Id.replace('compute-node-', '') - 1)})` |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 107 | }); |
| 108 | |
| 109 | nodeContainer.append('rect') |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 110 | .attr({ |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 111 | width: 0, |
| 112 | height: 0 |
| 113 | }) |
| 114 | .transition() |
| 115 | .duration(serviceTopologyConfig.duration) |
| 116 | .attr({ |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 117 | width: d => RackHelper.getComputeNodeSize(d.instances)[0], |
| 118 | height: d => RackHelper.getComputeNodeSize(d.instances)[1], |
| 119 | }); |
| 120 | |
| 121 | nodeContainer.append('text') |
| 122 | .attr({ |
| 123 | 'text-anchor': 'start', |
| 124 | y: 15, //FIXME |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 125 | x: 10, //FIXME |
| 126 | opacity: 0 |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 127 | }) |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 128 | .text(d => d.humanReadableName.split('.')[0]) |
| 129 | .transition() |
| 130 | .duration(serviceTopologyConfig.duration) |
| 131 | .attr({ |
| 132 | opacity: 1 |
| 133 | }) |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 134 | |
| 135 | // if there are Compute Nodes |
| 136 | if(nodeContainer.length > 0){ |
Matteo Scandolo | 5103148 | 2016-02-17 13:54:11 -0800 | [diff] [blame] | 137 | // draw instances for each compute node |
| 138 | nodeContainer.each(function(a){ |
| 139 | _this.drawInstances(d3.select(this), a.instances); |
| 140 | }) |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | }; |
| 144 | |
Matteo Scandolo | 7030ceb | 2016-02-16 13:29:26 -0800 | [diff] [blame] | 145 | // NOTE Stripping unuseful names to shorten labels. |
| 146 | // This is not elegant |
| 147 | const formatInstanceName = (name) => { |
| 148 | return name |
| 149 | .replace('app_', '') |
| 150 | .replace('service_', '') |
| 151 | .replace('ovs_', '') |
| 152 | .replace('mysite_', '') |
| 153 | .replace('_instance', ''); |
| 154 | }; |
| 155 | |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 156 | this.drawInstances = (container, instances) => { |
Matteo Scandolo | 5103148 | 2016-02-17 13:54:11 -0800 | [diff] [blame] | 157 | |
Matteo Scandolo | c49ff70 | 2016-02-17 15:11:33 -0800 | [diff] [blame] | 158 | // TODO check for stats field in instance and draw popup |
| 159 | |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 160 | let {width, height} = container.node().getBoundingClientRect(); |
| 161 | |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 162 | let elements = container.selectAll('.instances') |
| 163 | .data(instances, d => angular.isString(d.d3Id) ? d.d3Id : d.d3Id = `instance-${++instanceId}`) |
| 164 | |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 165 | var instanceContainer = elements.enter().append('g'); |
| 166 | |
| 167 | instanceContainer |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 168 | .attr({ |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 169 | transform: `translate(${width / 2}, ${ height / 2})`, |
Matteo Scandolo | 7e67a9a | 2016-02-16 16:33:26 -0800 | [diff] [blame] | 170 | class: d => `instance ${d.selected ? 'active' : ''}`, |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 171 | }) |
| 172 | .transition() |
| 173 | .duration(serviceTopologyConfig.duration) |
| 174 | .attr({ |
Matteo Scandolo | 5103148 | 2016-02-17 13:54:11 -0800 | [diff] [blame] | 175 | transform: (d, i) => `translate(${RackHelper.getInstancePosition(i)})` |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 176 | }); |
| 177 | |
| 178 | instanceContainer.append('rect') |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 179 | .attr({ |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 180 | width: 0, |
| 181 | height: 0 |
| 182 | }) |
| 183 | .transition() |
| 184 | .duration(serviceTopologyConfig.duration) |
| 185 | .attr({ |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 186 | width: serviceTopologyConfig.instance.width, |
| 187 | height: serviceTopologyConfig.instance.height |
| 188 | }); |
| 189 | |
| 190 | instanceContainer.append('text') |
| 191 | .attr({ |
Matteo Scandolo | 7030ceb | 2016-02-16 13:29:26 -0800 | [diff] [blame] | 192 | 'text-anchor': 'middle', |
| 193 | y: 23, //FIXME |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 194 | x: 40, //FIXME |
| 195 | opacity: 0 |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 196 | }) |
Matteo Scandolo | 35d53c8 | 2016-02-16 14:44:51 -0800 | [diff] [blame] | 197 | .text(d => formatInstanceName(d.name)) |
| 198 | .transition() |
| 199 | .duration(serviceTopologyConfig.duration) |
| 200 | .attr({ |
| 201 | opacity: 1 |
| 202 | }); |
Matteo Scandolo | 7030ceb | 2016-02-16 13:29:26 -0800 | [diff] [blame] | 203 | |
| 204 | instanceContainer |
| 205 | .on('click', d => { |
| 206 | console.log(d); |
| 207 | }); |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 208 | }; |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 209 | |
| 210 | this.addPhisical = (nodes) => { |
| 211 | nodes.append('rect') |
| 212 | .attr(serviceTopologyConfig.square); |
| 213 | |
| 214 | nodes.append('text') |
| 215 | .attr({ |
| 216 | 'text-anchor': 'middle', |
| 217 | y: serviceTopologyConfig.square.y - 10 |
| 218 | }) |
| 219 | .text(d => d.name); |
| 220 | } |
| 221 | |
| 222 | this.addDevice = (nodes) => { |
| 223 | nodes.append('circle') |
| 224 | .attr(serviceTopologyConfig.circle); |
| 225 | |
| 226 | nodes.append('text') |
| 227 | .attr({ |
| 228 | 'text-anchor': 'end', |
| 229 | x: - serviceTopologyConfig.circle.r - 10, |
| 230 | y: serviceTopologyConfig.circle.r / 2 |
| 231 | }) |
| 232 | .text(d => d.name); |
| 233 | } |
| 234 | }); |
| 235 | })(); |