forked from loginoff/infiniband_topology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topo.html
375 lines (331 loc) · 10.4 KB
/
topo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.selected {
stroke: red;
stroke-width: 3px;
}
.faded {
opacity: 0.2;
stroke-width: 0.2px;
}
.highlight {
font-weight: bold;
stroke-width: 2px;
}
.link {
stroke: #999;
stroke-opacity: 1;
fill: none;
/*stroke-width: 1;*/
}
.hidden {
display: none;
}
.nodename {
font-size: 10px;
}
div {
position: absolute;
top: 10px;
left: 10px;
}
svg {
width: 100%;
height: 100%;
}
body, html {
height: 100%;
width: 100%;
}
</style>
<div id="infoview" class="hidden">
<table>
<tr>
<td id="name" colspan=2 style="font-weight: bold;">stage12</td>
</tr>
<tr>
<td>Guid:</td>
<td id="guid">48294820480</td>
</tr>
<tr>
<td>Type:</td>
<td id="type">switch</td>
</tr>
<tr>
<td>Ports (connected):</td>
<td id="portcount">36 (27)</td>
</tr>
<tr>
<td>Connected nodes:</td>
<td id="connections">36</td>
</tr>
</table>
</div>
<svg id="chart">
</svg>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
//A utility function for parsing URL parameters
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
//Define some global variables
var svg = d3.select("#chart")
var width = svg.style('width').replace('px','');
var height = svg.style('height').replace('px','');
//Set up the colour scale
var color = d3.scale.category20();
//The scales for implementing zoom, every coordinate used for
//drawing will have to go through these.
var xzoom = d3.scale.linear()
.domain([0,width])
.range([0,width]);
var yzoom = d3.scale.linear()
.domain([0,height])
.range([0,height]);
//Assign the zoom behaviour to our SVG object.
//The zoom behavior makes changes to our xzoom and yzoom scales based on
//user input and calls the updateCoordinates function to update the coordinates
//of our graph based on these updated scales.
svg.call(d3.behavior.zoom()
.x(xzoom)
.y(yzoom)
.scaleExtent([0.1,8])
.on("zoom", updateCoordinates))
//Do not "zoom" on double-click
.on('dblclick.zoom', null)
//On window resize, we want the graph to also resize, so we have to update
//the zoom scales, the force layout size and update all the coordinates of our elements.
window.onresize = function(){
width = window.innerWidth;
height = window.innerHeight;
force.size([width,height]);
xzoom.range([0,width]);
yzoom.range([0,height]);
updateCoordinates();
}
//Set up the force layout
var force = d3.layout.force()
.charge(-1000)
.linkDistance(50)
.size([width, height])
.on("tick", updateCoordinates);
//Make the nodes draggable
var drag = force.drag()
//Once a node's been dragged it will stay fixed
.on('dragstart', function(d){
d.fixed = true;
//Do not propagate the drag event to svg element, which would
//activate the zoom behavior's drag event and pan the entire view
d3.event.sourceEvent.stopPropagation();
mode='drag';
})
.on('dragend', function(d){
mode='idle';
})
.on('drag', function(d){
})
//Variables to hold the different components of our graph
//we declare them here so they can be accessed from different functions
var link, circle, text, node, mode
//This will contain the currently selected node, used to display
//connectivity
var selectedNode = null;
//This will contain the node that has been hovered on
var activeNode=null;
svg.on("click", function(){
mode='idle';
unselectNodes();
})
//This function will update the coordinates of our nodes and links
//based on the data. This basically carries out the layout animation
//as it is used for the tick function of the force layout
function updateCoordinates() {
//We draw the links as curved paths, with the radius of the curve
//being dependant on the distance between the nodes
link.attr('d', function(d){
var src_x = xzoom(d.source.x);
var src_y = yzoom(d.source.y);
var dst_x = xzoom(d.target.x);
var dst_y = yzoom(d.target.y);
var dx = dst_x - src_x;
var dy = dst_y - src_y;
//The radius is also dependant of the "rank" of the link
dr = Math.sqrt(dx*dx + dy*dy)*d.rank;
//And we alternate the "direction" of the curve for even and odd ranks
//The direction can be altered by swapping the endpoints of the curve.
if(d.rank%2==0){
var swap=src_x;
src_x=dst_x;
dst_x=swap;
swap=src_y;
src_y=dst_y;
dst_y=swap;
}
return "M" +
src_x + "," +
src_y + "A" +
dr + "," + dr + " 0 0,1 " +
dst_x + "," +
dst_y;
})
//Causes some weird yanking when dragging a node
// node.attr('transform', function(d) {
// return 'translate(' + d.x + ',' + d.y +')'
// })
//Changing the coordinates of the circle and text
//separately makes dragging smooth.
circle.attr("cx", function (d) {
return xzoom(d.x);
})
.attr("cy", function (d) {
return yzoom(d.y);
});
text.attr("x", function (d) {
return xzoom(d.x);
})
.attr("y", function (d) {
return yzoom(d.y);
});
}
topofile = getParameterByName('topofile');
//We load the topology data from file as JSON
d3.json(topofile, function(error, topoData){
console.log(topofile);
if(error){
alert('Unable to load '+topofile);
return;
}
//Each node has a unique GUID, we use it to index the nodes for
//accessing them later on
nodeindex={}
for(var i=0; i < topoData.nodes.length; i++){
node = topoData.nodes[i];
nodeindex[node.guid] = node;
node.connections={}
node.connected_nodes=0;
}
//For each link we create references to nodes at both sides,
//this is needed by the force layout.
//Also add to each node the GUID's of nodes they are connected to.
//This is used later for visualizing directly connected nodes
for(var i=0; i < topoData.links.length; i++){
link = topoData.links[i];
link.source = nodeindex[link.host1_guid];
link.target = nodeindex[link.host2_guid];
//We also add how many connections we have from each node to each node
if(link.source.connections[link.target.guid])
link.source.connections[link.target.guid]++;
else{
link.source.connections[link.target.guid]=1;
link.source.connected_nodes++;
}
if(link.target.connections[link.source.guid])
link.target.connections[link.source.guid]++;
else{
link.target.connections[link.source.guid]=1;
link.target.connected_nodes++;
}
//The rank on the link will symbolize what "nth" connection
//between the same nodes this link is, this is used to visualize
//many connections between the same two nodes in a visually pleasing way
link.rank = link.source.connections[link.target.guid];
}
updateGraph({nodes:topoData.nodes,links:topoData.links});
});
function updateNodeinfo(node) {
var info = d3.select('#infoview');
info.select('#name').html(node.name);
info.select('#guid').html('0x'+node.guid);
info.select('#type').html(node.type);
info.select('#portcount').html(node.available_ports+' ('+node.connected_ports+')');
info.select('#connections').html(node.connected_nodes);
}
//This function highlights all the nodes and links
//directly connected to the node with the guid given as argument
//and fades everything else
function selectNodes(curnode){
updateNodeinfo(curnode);
d3.select('#infoview').classed('hidden', false);
node.classed({
'faded': function(d) {
return !(d.guid==curnode.guid || d.connections[curnode.guid]);
},
'highlight': function(d) {
return d.guid==curnode.guid || d.connections[curnode.guid];
}
});
link.classed({
'faded': function(d){
return !(d.source.guid == curnode.guid ||
d.target.guid == curnode.guid);
},
'highlight': function(d){
return d.source.guid == curnode.guid ||
d.target.guid == curnode.guid;
}
});
}
//This function displays all the nodes and links as unselected
function unselectNodes(){
node.classed('selected', false);
node.classed('faded',false);
node.classed('highlight',false);
link.classed('faded',false);
link.classed('highlight',false);
d3.select('#infoview').classed('hidden',true);
}
//This function takes care of drawing the graph once the data has been
//loaded and formatted correctly
function updateGraph(graph){
//We add our nodes and links and start the force layout generation.
force.nodes(graph.nodes)
.links(graph.links)
.start();
//Create all the line svgs but without locations yet
link = svg.selectAll(".link")
.data(graph.links)
.enter().append("path")
.attr("class", "link")
//A node is a g element containing a circle and some text
node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.on('mouseover', function(){
if(mode=='drag'||mode=='selected')
return;
selectNodes(this.__data__);
})
.on('click', function(){
d3.event.stopPropagation();
// d3.select(this).classed('selected',true);
// mode='selected';
})
.on('dblclick', function(d){
d.fixed=false;
})
.on('mouseout', function(){
unselectNodes();
})
.call(force.drag);
circle = node.append('circle')
.attr("class", "node")
.attr("r", 8)
.style("fill", function (d) {
return color(d.type);
})
text = node.append('text')
.text(function(d) { return d.name; })
.attr('class', 'nodename')
.attr('dx', 8)
.attr('dy', ".35em")
}
</script>