blob: 2802943eaa69f47a35b9c7a521c10da8d18aedf5 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
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 Scandolocc0db942016-02-11 17:37:08 -080019(function () {
Matteo Scandolo04564952016-02-24 11:22:48 -080020 angular.module('xos.diagnostic')
Matteo Scandolofe307b12016-05-17 14:29:01 -070021 .service('RackHelper', function(serviceTopologyConfig, _){
Matteo Scandolocc0db942016-02-11 17:37:08 -080022
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 Scandolofe307b12016-05-17 14:29:01 -070032 this.getComputeNodeSize = _.memoize((instances) => {
Matteo Scandolocc0db942016-02-11 17:37:08 -080033 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 Scandoload5b2282016-02-16 11:50:51 -080040
Matteo Scandolocc0db942016-02-11 17:37:08 -080041 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 Scandolofe307b12016-05-17 14:29:01 -070053 _.forEach(nodes, (node) => {
Matteo Scandolo50eeec62016-02-23 10:04:36 -080054 let [nodeWidth, nodeHeight] = this.getComputeNodeSize(node.instances);
Matteo Scandolocc0db942016-02-11 17:37:08 -080055
Matteo Scandolo50eeec62016-02-23 10:04:36 -080056 width = nodeWidth + (serviceTopologyConfig.computeNode.margin * 2);
57 height += (nodeHeight + serviceTopologyConfig.computeNode.margin);
Matteo Scandolocc0db942016-02-11 17:37:08 -080058 });
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 Scandoload5b2282016-02-16 11:50:51 -080087
Matteo Scandolocc0db942016-02-11 17:37:08 -080088 const x = serviceTopologyConfig.computeNode.margin;
89
Matteo Scandolofe307b12016-05-17 14:29:01 -070090 let previousElEight = _.reduce(nodes.slice(0, position), (val, node) => {
Matteo Scandolocc0db942016-02-11 17:37:08 -080091 return val + this.getComputeNodeSize(node.instances)[1]
92 }, 0);
93
Matteo Scandolocc0db942016-02-11 17:37:08 -080094 const y =
95 serviceTopologyConfig.computeNode.margin
96 + (serviceTopologyConfig.computeNode.margin * position)
97 + previousElEight;
98
Matteo Scandolocc0db942016-02-11 17:37:08 -080099 return [x, y];
100 };
101
102 });
103})();