Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | angular.module('xos.serviceTopology') |
| 3 | .service('RackHelper', function(serviceTopologyConfig, lodash){ |
| 4 | |
| 5 | this.getComputeNodeLabelSize = () => { |
| 6 | return serviceTopologyConfig.computeNode.labelHeight + (serviceTopologyConfig.instance.margin * 2) |
| 7 | } |
| 8 | |
| 9 | /** |
| 10 | * Given a list of instance should get the Compute Node size. |
| 11 | * They are placed in rows of 2 with 5px margin on each side. |
| 12 | */ |
| 13 | |
| 14 | this.getComputeNodeSize = lodash.memoize((instances) => { |
| 15 | const width = (serviceTopologyConfig.instance.margin * 3) + (serviceTopologyConfig.instance.width *2); |
| 16 | |
| 17 | const rows = Math.round(instances.length / 2); |
| 18 | |
| 19 | const labelSpace = this.getComputeNodeLabelSize(); |
| 20 | |
| 21 | const height = (serviceTopologyConfig.instance.height * rows) + (serviceTopologyConfig.instance.margin * (rows + 1)) + labelSpace; |
| 22 | return [width, height]; |
| 23 | }); |
| 24 | |
| 25 | /** |
| 26 | * Give a list on Compute Node should calculate the Rack Size. |
| 27 | * Compute nodes are placed in a single column with 5px margin on each side. |
| 28 | */ |
| 29 | this.getRackSize = (nodes) => { |
| 30 | |
| 31 | let width = 0; |
| 32 | let height = serviceTopologyConfig.computeNode.margin; |
| 33 | |
| 34 | lodash.forEach(nodes, (node) => { |
| 35 | let [instanceWidth, instanceHeight] = this.getComputeNodeSize(node.instances); |
| 36 | |
| 37 | width = instanceWidth + (serviceTopologyConfig.computeNode.margin * 2); |
| 38 | height += (instanceHeight + serviceTopologyConfig.computeNode.margin); |
| 39 | }); |
| 40 | |
| 41 | return [width, height]; |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * Given an instance index, return the coordinates |
| 46 | */ |
| 47 | |
| 48 | this.getInstancePosition = (position) => { |
| 49 | const row = Math.floor(position / 2); |
| 50 | const column = (position % 2) ? 1 : 0; |
| 51 | |
| 52 | // add ComputeNode label size |
| 53 | const labelSpace = this.getComputeNodeLabelSize(); |
| 54 | |
| 55 | // x = margin + (width * column) + ( maring * column) |
| 56 | const x = serviceTopologyConfig.instance.margin + (serviceTopologyConfig.instance.width * column) + (serviceTopologyConfig.instance.margin * column); |
| 57 | |
| 58 | // y = label + margin + (height * row) + ( maring * row) |
| 59 | const y = labelSpace + serviceTopologyConfig.instance.margin + (serviceTopologyConfig.instance.height * row) + (serviceTopologyConfig.instance.margin * row); |
| 60 | return [x, y]; |
| 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * Given an Compute Node index, return the coordinates |
| 65 | */ |
| 66 | |
| 67 | this.getComputeNodePosition = (nodes, position) => { |
| 68 | const x = serviceTopologyConfig.computeNode.margin; |
| 69 | |
| 70 | let previousElEight = lodash.reduce(nodes.slice(0, position), (val, node) => { |
| 71 | return val + this.getComputeNodeSize(node.instances)[1] |
| 72 | }, 0); |
| 73 | |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 74 | const y = |
| 75 | serviceTopologyConfig.computeNode.margin |
| 76 | + (serviceTopologyConfig.computeNode.margin * position) |
| 77 | + previousElEight; |
| 78 | |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 79 | return [x, y]; |
| 80 | }; |
| 81 | |
| 82 | }); |
| 83 | })(); |