forked from matrix-org/meshsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
703 lines (591 loc) · 22.4 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
<html>
<!--
Copyright 2019 New Vector Ltd
This file is part of meshsim.
meshsim is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
meshsim is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with coap-proxy. If not, see <https://www.gnu.org/licenses/>.
-->
<head>
<script src="static/js/d3.v5.min.js"></script>
<style type="text/css">
body {
margin: 0px;
font-family: "Open Sans", Arial, Helvetica, sans-serif;
font-size: 12px;
height: 100%;
}
.node text {
font-size: 12px;
font-weight: bold;
}
.link text {
font-size: 11px;
}
.message {
pointer-events: none;
}
#content {
display: flex;
width: 100%;
height: 100%;
}
#viz {
flex: 1;
}
#sidebar {
background-color: #eee;
flex: 0 200px;
padding: 1em;
border-left: 1px solid #ddd;
}
#sidebar input[type='text'] {
display: block;
padding: .5em;
}
.control {
margin-top: 1em;
margin-bottom: 1em;
}
#sidebar button {
width: 100%;
padding: .5em;
}
#sidebar button.pin {
width: 5em;
}
#local label {
display: block;
}
#local input[type='text'] {
display: inline ! important;
}
</style>
</head>
<body>
<section id="content">
<svg id="viz"></svg>
<aside id="sidebar">
<h3>Global</h3>
<div class="control">
<label for="bandwidth">Default bandwidth (bps)</label>
<input id="bandwidth" type="text">
</div>
<div class="control">
<label for="decay_bandwidth">Decay bandwidth</label>
<input id="decay_bandwidth" type="checkbox">
</div>
<div class="control">
<label for="max_latency">Max latency (ms)</label>
<input id="max_latency" type="text">
</div>
<div class="control">
<label for="min_bandwidth">Min bandwidth (bps)</label>
<input id="min_bandwidth" type="text">
</div>
<div class="control">
<label for="jitter">Default jitter (%)</label>
<input id="jitter" type="text">
</div>
<div class="control">
<label for="cost_min_latency">Minimse latency</label>
<input id="cost_min_latency" type="radio" name="cost_function" value="cost_min_latency">
</div>
<div class="control">
<label for="cost_max_bandwidth">Maximise bandwidth</label>
<input id="cost_max_bandwidth" type="radio" name="cost_function" value="cost_max_bandwidth">
</div>
<div class="control">
<label for="latency_scale">Scale latency (%)</label>
<input id="latency_scale" type="text">
</div>
<hr/>
<div class="control">
<label for="client_latency">Client Latency (ms)</label>
<input id="client_latency" type="text">
</div>
<div class="control">
<label for="client_bandwidth">Client Bandwidth (bps)</label>
<input id="client_bandwidth" type="text">
</div>
<div class="control">
<label for="client_jitter">Client Jitter (%)</label>
<input id="client_jitter" type="text">
</div>
<button onClick="applyDefaults()">Apply</button>
<div id="local" style="display: none">
<h3>Link</h3>
<div class="control">
<label for="link_latency">Latency (ms)</label>
<input id="link_latency" type="text">
<button class="pin" id="pin_latency" onClick="pinOverride('latency')">Pin</button>
<button style="display: none" class="pin" id="unpin_latency" onClick="unpinOverride('latency')">Unpin</button>
</div>
<div class="control">
<label for="link_bandwidth">Bandwidth (bps)</label>
<input id="link_bandwidth" type="text">
<button class="pin" id="pin_bandwidth" onClick="pinOverride('bandwidth')">Pin</button>
<button style="display: none" class="pin" id="unpin_bandwidth" onClick="unpinOverride('bandwidth')">Unpin</button>
</div>
<div class="control">
<label for="link_jitter">Jitter (%)</label>
<input id="link_jitter" type="text">
<button class="pin" id="pin_jitter" onClick="pinOverride('jitter')">Pin</button>
<button style="display: none" class="pin" id="unpin_jitter" onClick="unpinOverride('jitter')">Unpin</button>
</div>
</div>
</aside>
</section>
<script type="text/javascript">
function hashString(str) {
var hash = 0, i, chr;
if (str.length === 0) return hash;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
const svg = d3.select("svg")
let data = {
links: [],
nodes: [],
};
let linksById = {};
let lastNodeId = 0;
let selectedLinkId = null;
var c10 = d3.scaleOrdinal(d3.schemeCategory10);
// init D3 drag support
const drag = d3.drag()
.on('start', (d) => {
})
.on('drag', function(d) {
data.nodes[d3.event.subject.name].x = d3.event.x;
data.nodes[d3.event.subject.name].y = d3.event.y;
d3.select(this)
.attr("transform", `translate(${ d3.event.x }, ${ d3.event.y })`)
updateLinks();
})
.on('end', (d) => {
fetch(`/server/${d3.event.subject.name}/position`, {
method: "PUT",
headers: { 'Content-type': 'application/json '},
body: JSON.stringify({ x: d3.event.subject.x, y: d3.event.subject.y }),
}).then(r=>{
fetchData();
}).catch(function(error) {
console.log('Request failed', error);
});
});
function update() {
updateLinks();
let nodes = svg.selectAll(".node")
.data(data.nodes, n=>n.name);
nodes.exit().remove()
let nodesEnter = nodes.enter()
.append("g")
.attr("class", "node")
.attr("transform", d=>`translate(${ d.x }, ${ d.y })`)
.call(drag)
let node = nodes.select("circle")
let nodeEnter = nodesEnter.append("circle")
node.merge(nodeEnter)
.attr("r", 6)
.attr("stroke", (d, i)=>c10(i))
.attr("stroke-width", 2)
.attr("fill", "#fff")
.attr("id", (d)=>`node-${d.name}`)
let label = nodes.select(".label")
let labelEnter = nodesEnter.append("g")
label.merge(labelEnter)
.attr("class", "label")
.attr("transform", "translate(10,2)")
let t1 = label.select("text")
let t1Enter = labelEnter.append("text")
t1.merge(t1Enter)
.text(d=>d.local_echo ? '' : d.name)
.attr("style", "stroke:white; stroke-width:0.4em")
let t2 = label.select("text:nth-child(2)")
let t2Enter = labelEnter.append("text")
t2.merge(t2Enter)
.text(d=>d.local_echo ? '' : d.name)
}
function updateLinks() {
let links = svg.selectAll(".link")
.data(data.links, l=>l.id);
links.exit().remove();
let linksEnter = links.enter()
.append("g")
.lower()
.attr("id", l=>l.id)
.attr("class", "link")
.on('mousedown', (d, i)=>{
if (d3.event.button != 0) return;
d3.event.stopPropagation();
d3.event.preventDefault();
selectLink(selectedLinkId === d.id ? null : d);
})
let line2 = links.select('line')
let line2Enter = linksEnter.append('line')
line2.merge(line2Enter)
.attr("fill", "none")
.attr("stroke", "#fff")
.attr("stroke-opacity", 0)
.attr("stroke-width", "8")
.attr("x1", function(l) {
var sourceNode = data.nodes.filter((d, i)=>(i == l.source))[0];
d3.select(this).attr("y1", sourceNode.y);
return sourceNode.x
})
.attr("x2", function(l) {
var targetNode = data.nodes.filter((d, i)=>(i == l.target))[0];
d3.select(this).attr("y2", targetNode.y);
return targetNode.x
})
let line = links.select('line:nth-child(2)')
let lineEnter = linksEnter.append('line')
line.merge(lineEnter)
.attr("fill", "none")
.attr("stroke", "grey")
.attr("x1", function(l) {
var sourceNode = data.nodes.filter((d, i)=>(i == l.source))[0];
d3.select(this).attr("y1", sourceNode.y);
return sourceNode.x
})
.attr("x2", function(l) {
var targetNode = data.nodes.filter((d, i)=>(i == l.target))[0];
d3.select(this).attr("y2", targetNode.y);
return targetNode.x
})
let label = links.select(".label")
let labelEnter = linksEnter.append("g")
label.merge(labelEnter)
.attr("class", "label")
.attr("transform", function(l) {
var sourceNode = data.nodes.filter(function(d, i) {
return i == l.source
})[0];
var targetNode = data.nodes.filter(function(d, i) {
return i == l.target
})[0];
return `translate(${ (sourceNode.x + targetNode.x)/2 }, ${ 7 + (sourceNode.y + targetNode.y)/2 })`;
})
let t1 = label.select("text")
let t1Enter = labelEnter.append("text")
t1.merge(t1Enter)
.attr("style", "stroke:white; stroke-width:0.6em")
let t1Top = label.select("tspan")
let t1TopEnter = t1Enter.append("tspan")
t1Top.merge(t1TopEnter)
.text(d => `${ Math.round(d.latency*100) / 100 }ms`)
.attr("text-anchor", "middle")
.attr("dominant-baseline", "middle")
.attr("x", 0)
.attr("dy", -13)
let t1Bottom = t1.select("tspan:nth-child(2)")
let t1BottomEnter = t1Enter.append("tspan")
t1Bottom.merge(t1BottomEnter)
.attr("text-anchor", "middle")
.attr("dominant-baseline", "middle")
.text(d => getPrettyBw(d.bandwidth))
.attr("x", 0)
.attr("dy", +13)
let t2 = label.select("text:nth-child(2)")
let t2Enter = labelEnter.append("text")
t2.merge(t2Enter)
let t2Top = t2.select("tspan")
let t2TopEnter = t2Enter.append("tspan")
t2Top.merge(t2TopEnter)
.text(d => `${ Math.round(d.latency*100) / 100 }ms`)
.attr("text-anchor", "middle")
.attr("dominant-baseline", "middle")
.attr("x", 0)
.attr("dy", -13)
let t2Bottom = t2.select("tspan:nth-child(2)")
let t2BottomEnter = t2Enter.append("tspan")
t2Bottom.merge(t2BottomEnter)
.attr("text-anchor", "middle")
.attr("dominant-baseline", "middle")
.text(d => getPrettyBw(d.bandwidth))
.attr("x", 0)
.attr("dy", +13)
selectLink(linksById[selectedLinkId]);
}
function getLinkText(d) {
if (d.overrides) {
let text = `${ Math.round(d.latency*100) / 100 }ms\n`
//if (d.overrides.bandwidth) {
text += getPrettyBw(d.bandwidth) + "\n"
//}
if (d.overrides.jitter) {
text += d.jitter + "%"
}
return text;
}
else {
return `${ Math.round(d.latency*100) / 100 }ms ${ getPrettyBw(d.bandwidth) }`
}
}
function getPrettyBw(bw) {
f = 10;
if (bw > 1024*1024*1024) {
return Math.round((f * bw) / (1024*1024*1024))/f + "Gbps";
}
if (bw > 1024*1024) {
return Math.round((f * bw) / (1024*1024))/f + "Mbps";
}
if (bw > 1024) {
return Math.round((f * bw) / 1024)/f + "Kbps";
}
return bw + "bps";
}
function mousedown() {
const point = d3.mouse(this);
if (d3.event.defaultPrevented) return;
// create only with left mouse button
if (d3.event.button != 0) return;
// local echo
const node = {
local_echo: true,
name: lastNodeId,
x: point[0],
y: point[1]
};
data.nodes.push(node);
fetch("/server", {
method: "POST",
headers: { 'Content-type': 'application/json '},
body: JSON.stringify({ x: node.x, y: node.y }),
}).then(r=>{
// XXX: we could grab then node ID at this point and sync incrementally
// rather than just refresh everything
fetchData();
}).catch(function(error) {
console.log('Request failed', error);
});
lastNodeId++;
update();
}
function selectLink(link) {
if (selectedLinkId !== null) {
d3.select(`#${selectedLinkId}`)
.attr("fill", "")
.select('line:nth-child(2)')
.attr("stroke", "grey")
selectedLinkId = null;
}
if (!link) {
document.getElementById("local").style.display = 'none';
return;
}
selectedLinkId = link.id;
document.getElementById("local").style.display = 'block';
for (type of ['bandwidth', 'latency', 'jitter']) {
document.getElementById(`link_${type}`).value = link[type];
if (link.overrides && link.overrides[type]) {
document.getElementById(`pin_${type}`).style.display = 'none';
document.getElementById(`unpin_${type}`).style.display = 'inline';
}
else {
document.getElementById(`pin_${type}`).style.display = 'inline';
document.getElementById(`unpin_${type}`).style.display = 'none';
}
}
d3.select(`#${selectedLinkId}`)
.attr("fill", "red")
.select('line:nth-child(2)')
.attr("stroke", "red")
}
function fetchData() {
fetch("/data")
.then(r=>r.json())
.then(json=>{
console.log(json);
const new_data = {
nodes: json.nodes,
links: json.links,
}
for (let i in data.nodes) {
new_data.nodes[i] = json.nodes[i] ? json.nodes[i] : data.nodes[i];
}
data = new_data;
lastNodeId = data.nodes.length;
for (let link of data.links) {
link.id = `l_${link.source}_${link.target}`;
linksById[link.id] = link;
}
update();
})
.catch(function(error) {
console.log('Request failed', error);
});
}
function fetchDefaults() {
fetch("/defaults")
.then(r=>r.json())
.then(json=>{
console.log(json);
for (i of [
'bandwidth',
'max_latency',
'min_bandwidth',
'jitter',
'latency_scale',
'client_latency',
'client_bandwidth',
'client_jitter',
]) {
document.getElementById(i).value = json[i];
}
document.getElementById('decay_bandwidth').checked = json.decay_bandwidth;
document.getElementById(json.cost_function).checked = true;
})
.catch(function(error) {
console.log('Request failed', error);
});
}
function applyDefaults() {
fetch("/defaults", {
method: "PUT",
headers: { 'Content-type': 'application/json '},
body: JSON.stringify({
// we sanitise on the server
bandwidth: document.getElementById('bandwidth').value,
decay_bandwidth: document.getElementById('decay_bandwidth').checked,
min_bandwidth: document.getElementById('min_bandwidth').value,
max_latency: document.getElementById('max_latency').value,
jitter: document.getElementById('jitter').value,
cost_function: document.querySelector('input[name="cost_function"]:checked').value,
latency_scale: document.getElementById('latency_scale').value,
client_latency: document.getElementById('client_latency').value,
client_bandwidth:document.getElementById('client_bandwidth').value,
client_jitter: document.getElementById('client_jitter').value,
})
}).then(r=>{
// grab sanitised versions from the server
fetchDefaults();
fetchData();
})
.catch(function(error) {
console.log('Request failed', error);
});
}
function pinOverride(type) {
link = linksById[selectedLinkId];
json = {}
json[type] = document.getElementById(`link_${type}`).value;
fetch(`/link/${link.source}/${link.target}/${type}`, {
method: "PUT",
headers: { 'Content-type': 'application/json '},
body: JSON.stringify(json),
}).then(r=>{
fetchData();
})
.catch(function(error) {
console.log('Request failed', error);
});
}
function unpinOverride(type) {
link = linksById[selectedLinkId];
fetch(`/link/${link.source}/${link.target}/${type}`, {
method: "DELETE",
}).then(r=>{
fetchData();
})
.catch(function(error) {
console.log('Request failed', error);
});
}
svg.on('mousedown', mousedown);
fetchData();
fetchDefaults();
function translateAlong(path, backwards) {
var node = path.node();
// return an interpolator factory
return function(d, i) {
// return the interpolator itself
return function(t) {
t = backwards ? (1.0 - t) : t;
var l = node.getTotalLength();
var p = node.getPointAtLength(t * l);
return "translate(" + p.x + "," + p.y + ")";
};
};
}
function eventIdToMessageId(target, event_id) {
event_id = event_id.replace(":", "_").replace("$", "_");
return `m_${target}_${event_id}`
}
var exampleSocket = new WebSocket("ws://" + window.location.host + "/event_notifs");
exampleSocket.onmessage = function (event) {
var event_data = JSON.parse(event.data);
console.log("Received WS event", event_data);
if (event_data.event_type == "sending") {
// animate a message through the network path with the right timings.
const dest = event_data.path[event_data.path.length - 1];
message = svg.append("g")
.attr("id", eventIdToMessageId(dest, event_data.event))
.attr("class", "message")
message.append("circle")
.attr("r", 4)
.attr("fill", c10(hashString(event_data.event)))
// .attr("stroke", c10(hashString(event_data.event)))
// .attr("stroke-opacity", 1)
// .attr("fill-opacity", 0)
// message.append("text")
// .attr("dx", "8px")
// .attr("dy", "-8px")
// .text(msg);
const path = event_data.path;
for (let i = 0; i < path.length - 1; i++) {
let from = path[i], to = path[i+1], backwards = false;
if (from > to) {
from = path[i+1];
to = path[i];
backwards = true;
}
const hop = svg.select(`#l_${from}_${to}`).select("line");
message = message.transition()
// 2.5x is a fudge factor to slow the packets down to take delays due to HTTPS into account
// so the correlation between a packet being sent & received is more obvious.
.duration(hop.datum().latency * 1)
.ease(d3.easeLinear)
.attrTween("transform", translateAlong(hop, backwards));
}
}
else if (event_data.event_type == "receive") {
const target = parseInt(event_data.target.replace("synapse", ""));
// we should have already seen this message received; now delete it
svg.select('#' + eventIdToMessageId(target, event_data.event)).remove();
const halo = svg.append("circle")
.attr("cx", data.nodes[target].x)
.attr("cy", data.nodes[target].y)
.attr("fill", () => c10(hashString(event_data.event)))
.attr("stroke", () => c10(hashString(event_data.event)))
.attr("r", 6)
.attr("fill-opacity", .75)
.attr("stroke-opacity", 1)
.transition()
.duration(1000)
.ease(d3.easeCubicOut)
.attr("r", 20)
.attr("fill-opacity", 0)
.attr("stroke-opacity", 0)
.remove()
d3.selectAll(".node")
.data(data.nodes.filter(d=>d.name==target), d=>d.name)
.select("circle")
.attr("stroke", () => c10(hashString(event_data.event)))
}
}
</script>
</body>
</html>