-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
405 lines (342 loc) · 14 KB
/
index.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Network Graph Visualization with Legend</title>
<style>
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
}
text {
pointer-events: none;
font-family: sans-serif;
}
.legend {
font-family: sans-serif;
font-size: 14px;
}
.loading {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-family: sans-serif;
font-size: 20px;
color: #333;
}
.instructions {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-family: sans-serif;
font-size: 20px;
color: #333;
text-align: center;
}
</style>
</head>
<body>
<div id="loading" class="loading">Loading... <span id="timer">0</span>s</div>
<div id="instructions" class="instructions" style="display: none;">
<p>Please provide a <strong>streamId</strong> as a query parameter in the URL.</p>
<p>Example: <code>?streamId=yourStreamIdHere</code></p>
</div>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
// Extract streamId from the URL query parameters
const urlParams = new URLSearchParams(window.location.search);
const streamId = urlParams.get('streamId');
if (!streamId) {
// Show instructions if no streamId is provided
document.getElementById('loading').style.display = 'none';
document.getElementById('instructions').style.display = 'block';
} else {
// Timer for loading screen
let secondsElapsed = 0;
const timerElement = document.getElementById('timer');
const timerInterval = setInterval(() => {
secondsElapsed++;
timerElement.textContent = secondsElapsed;
}, 1000);
// Fetch topology data from localhost with streamId as a query parameter
fetch(`http://localhost:3000/topology?streamId=${streamId}`).then(function(response) {
return response.json()
}).then(function(data) {
// Remove loading indicator
document.getElementById('loading').style.display = 'none';
clearInterval(timerInterval);
// Build nodes and links
const nodes = [];
const nodeById = new Map();
data.forEach(function(d) {
const node = { id: d.id, ipAddress: d.ipAddress };
nodes.push(node);
nodeById.set(d.id, node);
});
const links = [];
const linkSet = new Set();
data.forEach(function(d) {
const sourceId = d.id;
d.neighbors.forEach(function(targetId) {
// To avoid duplicates, create a unique key for each link
const key = [sourceId, targetId].sort().join("-");
if (!linkSet.has(key)) {
linkSet.add(key);
links.push({ source: sourceId, target: targetId });
}
});
});
// Function to compute the network diameter
function computeNetworkDiameter(nodes, links) {
const adjacencyList = new Map();
// Build adjacency list
nodes.forEach(node => {
adjacencyList.set(node.id, []);
});
links.forEach(link => {
adjacencyList.get(link.source).push(link.target);
adjacencyList.get(link.target).push(link.source);
});
let diameter = 0;
// Function to perform BFS and find shortest paths from a source node
function bfs(startId) {
const visited = new Set();
const queue = [];
const distances = new Map();
visited.add(startId);
queue.push(startId);
distances.set(startId, 0);
while (queue.length > 0) {
const currentId = queue.shift();
const neighbors = adjacencyList.get(currentId);
neighbors.forEach(neighborId => {
if (!visited.has(neighborId)) {
visited.add(neighborId);
queue.push(neighborId);
distances.set(neighborId, distances.get(currentId) + 1);
}
});
}
return distances;
}
// Compute the shortest paths between all pairs of nodes
nodes.forEach(node => {
const distances = bfs(node.id);
distances.forEach(distance => {
if (distance > diameter) {
diameter = distance;
}
});
});
return diameter;
}
// Calculate statistics for the legend
const totalNodes = nodes.length;
const totalConnections = links.length;
let totalNeighborCount = 0;
data.forEach(d => {
totalNeighborCount += d.neighbors.length;
});
const averageNeighborCount = (totalNeighborCount / totalNodes).toFixed(2);
const networkDiameter = computeNetworkDiameter(nodes, links);
// Set up the SVG canvas dimensions
const width = 1400;
const height = 900;
// Define zoom behavior
const zoom = d3.zoom()
.scaleExtent([0.1, 10])
.on('zoom', zoomed);
// Create the SVG and apply zoom behavior
const svg = d3.select("body")
.append("svg")
.attr("width", width + 200) // Extra width for the legend
.attr("height", height)
.call(zoom);
// Create a container for all graph elements
const container = svg.append("g");
// Initialize the simulation
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(function(d) { return d.id; }).distance(100))
.force("charge", d3.forceManyBody().strength(-300))
.force("center", d3.forceCenter(width / 2, height / 2));
// Draw links (edges)
const link = container.append("g")
.attr("class", "links")
.selectAll("line")
.data(links)
.enter().append("line")
.attr("stroke-width", 1);
// Draw nodes
const node = container.append("g")
.attr("class", "nodes")
.selectAll("g")
.data(nodes)
.enter().append("g");
const circles = node.append("circle")
.attr("r", 10)
.attr("fill", "blue");
// Add labels to nodes
const labels = node.append("text")
.attr("dy", -15)
.text(function(d) {
if (d.ipAddress) {
return d.id.substring(0, 4) + "..." + d.id.substring(d.id.length - 4) + " (" + d.ipAddress + ")";
} else {
return d.id.substring(0, 6);
}
})
.attr("font-size", "10px")
.attr("text-anchor", "middle");
// Add tooltips to display full node ID and IP address
node.append("title")
.text(function(d) {
return "Node ID: " + d.id + "\nIP Address: " + (d.ipAddress ? d.ipAddress : "N/A");
});
// Enable dragging of nodes
node.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
// Variables to keep track of highlighted nodes and links
let highlightedNodes = new Set();
let highlightedLinks = new Set();
// Add click event to circles for highlighting
circles.on("click", function(event, d) {
// Prevent the click from propagating to the SVG background
event.stopPropagation();
// Reset all nodes and links to default style
resetHighlighting();
// Start highlighting from the clicked node
highlightConnections(d.id);
});
// Function to highlight connections progressively
function highlightConnections(startNodeId) {
const visited = new Set();
const queue = [startNodeId];
let currentLevel = 0;
function highlightNextLevel() {
if (queue.length === 0) return;
const nextQueue = [];
queue.forEach(nodeId => {
if (!visited.has(nodeId)) {
visited.add(nodeId);
// Highlight the node
circles.filter(n => n.id === nodeId)
.attr("fill", "red")
.transition()
.duration(2000)
.attr("fill", "blue");
// Highlight links and gather next level nodes
links.forEach(linkData => {
if (linkData.source.id === nodeId && !visited.has(linkData.target.id)) {
d3.selectAll("line")
.filter(l => l.source.id === linkData.source.id && l.target.id === linkData.target.id)
.attr("stroke", "red")
.attr("stroke-width", 2)
.transition()
.duration(2000)
.attr("stroke", "#999")
.attr("stroke-width", 1);
nextQueue.push(linkData.target.id);
} else if (linkData.target.id === nodeId && !visited.has(linkData.source.id)) {
d3.selectAll("line")
.filter(l => l.source.id === linkData.source.id && l.target.id === linkData.target.id)
.attr("stroke", "red")
.attr("stroke-width", 2)
.transition()
.duration(2000)
.attr("stroke", "#999")
.attr("stroke-width", 1);
nextQueue.push(linkData.source.id);
}
});
}
});
// Move to the next level
queue.length = 0;
queue.push(...nextQueue);
// Schedule the next level highlighting
setTimeout(highlightNextLevel, 600);
}
// Start the highlighting process
highlightNextLevel();
}
// Add click event to the SVG background to reset highlighting
svg.on("click", function(event) {
if (event.target === svg.node()) {
resetHighlighting();
}
});
// Function to reset highlighting
function resetHighlighting() {
// Reset node colors
circles.attr("fill", "blue");
highlightedNodes.clear();
// Reset link styles
link.attr("stroke", "#999").attr("stroke-width", 1);
highlightedLinks.clear();
}
// Update positions on each tick of the simulation
simulation.on("tick", () => {
link
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
});
// Drag event handlers
function dragstarted(event, d) {
if (!event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
// Stop propagation to prevent zooming while dragging
event.sourceEvent.stopPropagation();
}
function dragged(event, d) {
d.fx = event.x;
d.fy = event.y;
}
function dragended(event, d) {
if (!event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
// Function to handle zooming and panning
function zoomed(event) {
container.attr("transform", event.transform);
}
// Add legend to the right side
const legend = svg.append("g")
.attr("class", "legend")
.attr("transform", "translate(" + (width + 20) + ", 50)");
legend.append("text")
.text("Network Statistics")
.attr("font-size", "16px")
.attr("font-weight", "bold");
legend.append("text")
.attr("dy", "1.5em")
.text("Total Nodes: " + totalNodes);
legend.append("text")
.attr("dy", "3em")
.text("Total Connections: " + totalConnections);
legend.append("text")
.attr("dy", "4.5em")
.text("Mean node degree: " + averageNeighborCount);
legend.append("text")
.attr("dy", "6em")
.text("Network Diameter: " + networkDiameter);
});
}
</script>
</body>
</html>