blob: 17eb0dbb55b828f24abd00dbb1402d5dd14961d2 [file] [log] [blame]
Matteo Scandolocc0db942016-02-11 17:37:08 -08001(function () {
Matteo Scandolo04564952016-02-24 11:22:48 -08002 angular.module('xos.diagnostic')
Matteo Scandolocc0db942016-02-11 17:37:08 -08003 .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 Scandoload5b2282016-02-16 11:50:51 -080022
Matteo Scandolocc0db942016-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) => {
Matteo Scandolo50eeec62016-02-23 10:04:36 -080036 let [nodeWidth, nodeHeight] = this.getComputeNodeSize(node.instances);
Matteo Scandolocc0db942016-02-11 17:37:08 -080037
Matteo Scandolo50eeec62016-02-23 10:04:36 -080038 width = nodeWidth + (serviceTopologyConfig.computeNode.margin * 2);
39 height += (nodeHeight + serviceTopologyConfig.computeNode.margin);
Matteo Scandolocc0db942016-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 Scandoload5b2282016-02-16 11:50:51 -080069
Matteo Scandolocc0db942016-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 Scandolocc0db942016-02-11 17:37:08 -080076 const y =
77 serviceTopologyConfig.computeNode.margin
78 + (serviceTopologyConfig.computeNode.margin * position)
79 + previousElEight;
80
Matteo Scandolocc0db942016-02-11 17:37:08 -080081 return [x, y];
82 };
83
84 });
85})();