Matteo Scandolo | 594dfbc | 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; |
Matteo Scandolo | b785b57 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 22 | |
Matteo Scandolo | 594dfbc | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 23 | return [width, height]; |
| 24 | }); |
| 25 | |
| 26 | /** |
| 27 | * Give a list on Compute Node should calculate the Rack Size. |
| 28 | * Compute nodes are placed in a single column with 5px margin on each side. |
| 29 | */ |
| 30 | this.getRackSize = (nodes) => { |
| 31 | |
| 32 | let width = 0; |
| 33 | let height = serviceTopologyConfig.computeNode.margin; |
| 34 | |
| 35 | lodash.forEach(nodes, (node) => { |
| 36 | let [instanceWidth, instanceHeight] = this.getComputeNodeSize(node.instances); |
| 37 | |
| 38 | width = instanceWidth + (serviceTopologyConfig.computeNode.margin * 2); |
| 39 | height += (instanceHeight + serviceTopologyConfig.computeNode.margin); |
| 40 | }); |
| 41 | |
| 42 | return [width, height]; |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * Given an instance index, return the coordinates |
| 47 | */ |
| 48 | |
| 49 | this.getInstancePosition = (position) => { |
| 50 | const row = Math.floor(position / 2); |
| 51 | const column = (position % 2) ? 1 : 0; |
| 52 | |
| 53 | // add ComputeNode label size |
| 54 | const labelSpace = this.getComputeNodeLabelSize(); |
| 55 | |
| 56 | // x = margin + (width * column) + ( maring * column) |
| 57 | const x = serviceTopologyConfig.instance.margin + (serviceTopologyConfig.instance.width * column) + (serviceTopologyConfig.instance.margin * column); |
| 58 | |
| 59 | // y = label + margin + (height * row) + ( maring * row) |
| 60 | const y = labelSpace + serviceTopologyConfig.instance.margin + (serviceTopologyConfig.instance.height * row) + (serviceTopologyConfig.instance.margin * row); |
| 61 | return [x, y]; |
| 62 | }; |
| 63 | |
| 64 | /** |
| 65 | * Given an Compute Node index, return the coordinates |
| 66 | */ |
| 67 | |
| 68 | this.getComputeNodePosition = (nodes, position) => { |
Matteo Scandolo | b785b57 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 69 | |
Matteo Scandolo | 594dfbc | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 70 | const x = serviceTopologyConfig.computeNode.margin; |
| 71 | |
| 72 | let previousElEight = lodash.reduce(nodes.slice(0, position), (val, node) => { |
| 73 | return val + this.getComputeNodeSize(node.instances)[1] |
| 74 | }, 0); |
| 75 | |
Matteo Scandolo | 594dfbc | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 76 | const y = |
| 77 | serviceTopologyConfig.computeNode.margin |
| 78 | + (serviceTopologyConfig.computeNode.margin * position) |
| 79 | + previousElEight; |
| 80 | |
Matteo Scandolo | 594dfbc | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 81 | return [x, y]; |
| 82 | }; |
| 83 | |
| 84 | }); |
| 85 | })(); |