Matteo Scandolo | 594dfbc | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 1 | (function () { |
Matteo Scandolo | 4b3d872 | 2016-02-24 11:22:48 -0800 | [diff] [blame] | 2 | angular.module('xos.diagnostic') |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 3 | .service('RackHelper', function(serviceTopologyConfig, _){ |
Matteo Scandolo | 594dfbc | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 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 | |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 14 | this.getComputeNodeSize = _.memoize((instances) => { |
Matteo Scandolo | 594dfbc | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 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 | |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 35 | _.forEach(nodes, (node) => { |
Matteo Scandolo | db8a185 | 2016-02-23 10:04:36 -0800 | [diff] [blame] | 36 | let [nodeWidth, nodeHeight] = this.getComputeNodeSize(node.instances); |
Matteo Scandolo | 594dfbc | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 37 | |
Matteo Scandolo | db8a185 | 2016-02-23 10:04:36 -0800 | [diff] [blame] | 38 | width = nodeWidth + (serviceTopologyConfig.computeNode.margin * 2); |
| 39 | height += (nodeHeight + serviceTopologyConfig.computeNode.margin); |
Matteo Scandolo | 594dfbc | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 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 | |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 72 | let previousElEight = _.reduce(nodes.slice(0, position), (val, node) => { |
Matteo Scandolo | 594dfbc | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 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 | })(); |