blob: 67156d5050109a0459e71ac7879ff24d750a3dde [file] [log] [blame]
Matteo Scandolo38ba3312016-02-09 16:01:49 -08001(function () {
2 'use strict';
3
4 const shapes = {
5 cloud: ' M 79.72 49.60 C 86.00 37.29 98.57 29.01 111.96 26.42 C 124.27 24.11 137.53 26.15 148.18 32.90 C 158.08 38.78 165.39 48.87 167.65 60.20 C 176.20 57.90 185.14 56.01 194.00 57.73 C 206.08 59.59 217.92 66.01 224.37 76.66 C 227.51 81.54 228.85 87.33 229.23 93.06 C 237.59 93.33 246.22 95.10 253.04 100.19 C 256.69 103.13 259.87 107.67 258.91 112.59 C 257.95 118.43 252.78 122.38 247.78 124.82 C 235.27 130.43 220.23 130.09 207.98 123.93 C 199.33 127.88 189.76 129.43 180.30 128.57 C 173.70 139.92 161.70 147.65 148.86 149.93 C 133.10 153.26 116.06 148.15 104.42 137.08 C 92.98 143.04 78.96 143.87 66.97 139.04 C 57.75 135.41 49.70 128.00 46.60 118.43 C 43.87 109.95 45.81 100.29 51.30 93.32 C 57.38 85.18 67.10 80.44 76.99 78.89 C 74.38 69.20 74.87 58.52 79.72 49.60 Z'
6 }
7
Matteo Scandolo14183932016-02-11 08:58:04 -08008 var computeNodeId = 0;
9 var instanceId = 0;
10
Matteo Scandolo38ba3312016-02-09 16:01:49 -080011 angular.module('xos.serviceTopology')
Matteo Scandolo14183932016-02-11 08:58:04 -080012 .service('NodeDrawer', function(d3, serviceTopologyConfig, Node){
Matteo Scandolo38ba3312016-02-09 16:01:49 -080013 this.addNetworks = (nodes) => {
14 nodes.append('path')
15 .attr({
16 d: shapes.cloud,
17 transform: 'translate(-63, -52), scale(0.5)',
18 class: 'cloud'
19 });
20
21 nodes.append('text')
22 .attr({
23 'text-anchor': 'middle'
24 })
25 .text(d => d.name)
26 }
27
28 this.addRack = (nodes) => {
Matteo Scandolo14183932016-02-11 08:58:04 -080029
30 // NOTE is good to request data here? Probably not.
31
32 Node.queryWithInstances().$promise
33 .then((computeNodes) => {
34 this.drawComputeNodes(nodes, computeNodes);
35 });
36
37 // NOTE should we draw the rack?
Matteo Scandolo38ba3312016-02-09 16:01:49 -080038 nodes.append('rect')
39 .attr(serviceTopologyConfig.rack);
40
41 nodes.append('text')
42 .attr({
43 'text-anchor': 'middle',
44 y: serviceTopologyConfig.rack.y - 10
45 })
Matteo Scandolo14183932016-02-11 08:58:04 -080046 .text(d => d.name);
47 };
48
49 this.drawComputeNodes = (container, nodes) => {
50 console.log('drawComputeNodes');
51 let elements = container.selectAll('.compute-nodes')
52 .data(nodes, d => {
53 if(!angular.isString(d.d3Id)){
54 d.d3Id = `compute-node-${++computeNodeId}`;
55 }
56 return d.d3Id;
57 });
58
59 var nodeContainer = elements.enter().append('g')
60 .attr({
61 class: 'compute-node'
62 });
63
64 nodeContainer.append('rect')
65 .attr(serviceTopologyConfig.computeNode);
66
67 // if there are Compute Nodes
68 if(nodeContainer.length > 0){
69 angular.forEach(nodeContainer.data(), (computeNode) => {
70 this.drawInstances(nodeContainer, computeNode.instances);
71 });
72 }
73
74 };
75
76 this.drawInstances = (container, instances) => {
77
78 // TODO position instances... How??
79
80 let elements = container.selectAll('.instances')
81 .data(instances, d => angular.isString(d.d3Id) ? d.d3Id : d.d3Id = `instance-${++instanceId}`)
82
83 var instanceContainer = elements.enter().append('g')
84 .attr({
85 class: 'instance'
86 });
87
88 instanceContainer.append('rect')
89 .attr(serviceTopologyConfig.instance);
90 };
Matteo Scandolo38ba3312016-02-09 16:01:49 -080091
92 this.addPhisical = (nodes) => {
93 nodes.append('rect')
94 .attr(serviceTopologyConfig.square);
95
96 nodes.append('text')
97 .attr({
98 'text-anchor': 'middle',
99 y: serviceTopologyConfig.square.y - 10
100 })
101 .text(d => d.name);
102 }
103
104 this.addDevice = (nodes) => {
105 nodes.append('circle')
106 .attr(serviceTopologyConfig.circle);
107
108 nodes.append('text')
109 .attr({
110 'text-anchor': 'end',
111 x: - serviceTopologyConfig.circle.r - 10,
112 y: serviceTopologyConfig.circle.r / 2
113 })
114 .text(d => d.name);
115 }
116 });
117})();