blob: 15dcc0cf223c707f63001128cc8cd771e01c1f63 [file] [log] [blame]
Matteo Scandolo594dfbc2016-02-11 17:37:08 -08001(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 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
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 Scandolob785b572016-02-16 11:50:51 -080069
Matteo Scandolo594dfbc2016-02-11 17:37:08 -080070 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 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})();