Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 19 | (function () { |
Matteo Scandolo | 0456495 | 2016-02-24 11:22:48 -0800 | [diff] [blame] | 20 | angular.module('xos.diagnostic') |
Matteo Scandolo | fe307b1 | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 21 | .service('RackHelper', function(serviceTopologyConfig, _){ |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 22 | |
| 23 | this.getComputeNodeLabelSize = () => { |
| 24 | return serviceTopologyConfig.computeNode.labelHeight + (serviceTopologyConfig.instance.margin * 2) |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Given a list of instance should get the Compute Node size. |
| 29 | * They are placed in rows of 2 with 5px margin on each side. |
| 30 | */ |
| 31 | |
Matteo Scandolo | fe307b1 | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 32 | this.getComputeNodeSize = _.memoize((instances) => { |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 33 | const width = (serviceTopologyConfig.instance.margin * 3) + (serviceTopologyConfig.instance.width *2); |
| 34 | |
| 35 | const rows = Math.round(instances.length / 2); |
| 36 | |
| 37 | const labelSpace = this.getComputeNodeLabelSize(); |
| 38 | |
| 39 | const height = (serviceTopologyConfig.instance.height * rows) + (serviceTopologyConfig.instance.margin * (rows + 1)) + labelSpace; |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 40 | |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 41 | return [width, height]; |
| 42 | }); |
| 43 | |
| 44 | /** |
| 45 | * Give a list on Compute Node should calculate the Rack Size. |
| 46 | * Compute nodes are placed in a single column with 5px margin on each side. |
| 47 | */ |
| 48 | this.getRackSize = (nodes) => { |
| 49 | |
| 50 | let width = 0; |
| 51 | let height = serviceTopologyConfig.computeNode.margin; |
| 52 | |
Matteo Scandolo | fe307b1 | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 53 | _.forEach(nodes, (node) => { |
Matteo Scandolo | 50eeec6 | 2016-02-23 10:04:36 -0800 | [diff] [blame] | 54 | let [nodeWidth, nodeHeight] = this.getComputeNodeSize(node.instances); |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 55 | |
Matteo Scandolo | 50eeec6 | 2016-02-23 10:04:36 -0800 | [diff] [blame] | 56 | width = nodeWidth + (serviceTopologyConfig.computeNode.margin * 2); |
| 57 | height += (nodeHeight + serviceTopologyConfig.computeNode.margin); |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 58 | }); |
| 59 | |
| 60 | return [width, height]; |
| 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * Given an instance index, return the coordinates |
| 65 | */ |
| 66 | |
| 67 | this.getInstancePosition = (position) => { |
| 68 | const row = Math.floor(position / 2); |
| 69 | const column = (position % 2) ? 1 : 0; |
| 70 | |
| 71 | // add ComputeNode label size |
| 72 | const labelSpace = this.getComputeNodeLabelSize(); |
| 73 | |
| 74 | // x = margin + (width * column) + ( maring * column) |
| 75 | const x = serviceTopologyConfig.instance.margin + (serviceTopologyConfig.instance.width * column) + (serviceTopologyConfig.instance.margin * column); |
| 76 | |
| 77 | // y = label + margin + (height * row) + ( maring * row) |
| 78 | const y = labelSpace + serviceTopologyConfig.instance.margin + (serviceTopologyConfig.instance.height * row) + (serviceTopologyConfig.instance.margin * row); |
| 79 | return [x, y]; |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * Given an Compute Node index, return the coordinates |
| 84 | */ |
| 85 | |
| 86 | this.getComputeNodePosition = (nodes, position) => { |
Matteo Scandolo | ad5b228 | 2016-02-16 11:50:51 -0800 | [diff] [blame] | 87 | |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 88 | const x = serviceTopologyConfig.computeNode.margin; |
| 89 | |
Matteo Scandolo | fe307b1 | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 90 | let previousElEight = _.reduce(nodes.slice(0, position), (val, node) => { |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 91 | return val + this.getComputeNodeSize(node.instances)[1] |
| 92 | }, 0); |
| 93 | |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 94 | const y = |
| 95 | serviceTopologyConfig.computeNode.margin |
| 96 | + (serviceTopologyConfig.computeNode.margin * position) |
| 97 | + previousElEight; |
| 98 | |
Matteo Scandolo | cc0db94 | 2016-02-11 17:37:08 -0800 | [diff] [blame] | 99 | return [x, y]; |
| 100 | }; |
| 101 | |
| 102 | }); |
| 103 | })(); |