blob: af3eb304dbdf4a3c57e8ad0234747bc1816f3963 [file] [log] [blame]
Matteo Scandolo594dfbc2016-02-11 17:37:08 -08001(function () {
Matteo Scandolo4b3d8722016-02-24 11:22:48 -08002 angular.module('xos.diagnostic')
Matteo Scandolo78e9d0e2016-05-17 14:29:01 -07003 .service('RackHelper', function(serviceTopologyConfig, _){
Matteo Scandolo594dfbc2016-02-11 17:37:08 -08004
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 Scandolo78e9d0e2016-05-17 14:29:01 -070014 this.getComputeNodeSize = _.memoize((instances) => {
Matteo Scandolo594dfbc2016-02-11 17:37:08 -080015 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 Scandolob785b572016-02-16 11:50:51 -080022
Matteo Scandolo594dfbc2016-02-11 17:37:08 -080023 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 Scandolo78e9d0e2016-05-17 14:29:01 -070035 _.forEach(nodes, (node) => {
Matteo Scandolodb8a1852016-02-23 10:04:36 -080036 let [nodeWidth, nodeHeight] = this.getComputeNodeSize(node.instances);
Matteo Scandolo594dfbc2016-02-11 17:37:08 -080037
Matteo Scandolodb8a1852016-02-23 10:04:36 -080038 width = nodeWidth + (serviceTopologyConfig.computeNode.margin * 2);
39 height += (nodeHeight + serviceTopologyConfig.computeNode.margin);
Matteo Scandolo594dfbc2016-02-11 17:37:08 -080040 });
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 Scandolob785b572016-02-16 11:50:51 -080069
Matteo Scandolo594dfbc2016-02-11 17:37:08 -080070 const x = serviceTopologyConfig.computeNode.margin;
71
Matteo Scandolo78e9d0e2016-05-17 14:29:01 -070072 let previousElEight = _.reduce(nodes.slice(0, position), (val, node) => {
Matteo Scandolo594dfbc2016-02-11 17:37:08 -080073 return val + this.getComputeNodeSize(node.instances)[1]
74 }, 0);
75
Matteo Scandolo594dfbc2016-02-11 17:37:08 -080076 const y =
77 serviceTopologyConfig.computeNode.margin
78 + (serviceTopologyConfig.computeNode.margin * position)
79 + previousElEight;
80
Matteo Scandolo594dfbc2016-02-11 17:37:08 -080081 return [x, y];
82 };
83
84 });
85})();