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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
|
var nms = {
updater:undefined, // Active updater
update_time:0, // Client side timestamp for last update
switches_now:undefined, // Most recent data
switches_then:undefined, // 2 minutes old
speed:0, // Current aggregated speed
ping_data:undefined, // JSON data for ping history.
drawn:false, // Set to 'true' when switches are drawn
switch_showing:"", // Which switch we are displaying (if any).
switchInfo:{},
switchInfoDrawn:{},
repop_switch:false, // True if we need to repopulate the switch info when port state updates (e.g.: added comments);
repop_time:false, // Timestamp in case we get a cached result
nightMode:false,
/*
* Switch-specific variables. These are currently separate from
* "switches_now" because switches_now is reset every time we get
* new data.
*/
nightBlur:{}, // Have we blurred this switch or not?
shadowBlur:10,
shadowColor:"#EEEEEE",
switch_color:{}, // Color for switch
linknet_color:{}, // color for linknet
textDrawn:{}, // Have we drawn text for this switch?
now:false, // Date we are looking at (false for current date).
fontSize:16, // This is scaled too, but 16 seems to make sense.
fontFace:"Verdana",
fontLineFactor:3,
/*
* This is used to track outbound AJAX requests and skip updates if
* we have too many outstanding requests. The ajaxOverflow is a
* counter that tracks how many times this has happened.
*
* It's a cheap way to be nice to the server.
*/
outstandingAjaxRequests:0,
ajaxOverflow:0,
/*
* Set to 'true' after we've done some basic updating. Used to
* bootstrap the map quickly as soon as we have enough data, then
* ignored.
*/
did_update:false,
/*
* Various setInterval() handlers. See nmsTimer() for how they are
* used.
*
* Cool fact: Adding one here adds it to the 'debug timers'
* drop-down.
*/
timers: {
replay:false,
ports:false,
ping:false
},
menuShowing:true,
/*
* This is a list of nms[x] variables that we store in our
* settings-cookie when altered and restore on load.
*/
settingsList:[
'shadowBlur',
'shadowColor',
'nightMode',
'menuShowing',
'layerVisibility'
],
layerVisibility:{},
keyBindings:{
'?':toggleMenu,
'n':toggleNightMode,
'1':setMapModeFromN,
'2':setMapModeFromN,
'3':setMapModeFromN,
'4':setMapModeFromN,
'5':setMapModeFromN,
'6':setMapModeFromN,
'7':setMapModeFromN,
'h':moveTimeFromKey,
'j':moveTimeFromKey,
'k':moveTimeFromKey,
'l':moveTimeFromKey,
'p':moveTimeFromKey,
'r':moveTimeFromKey,
'not-default':keyDebug
}
};
/*
* Returns a handler object.
*
* This might seem a bit much for 'setInterval()' etc, but it's really more
* about self-documentation and predictable ways of configuring timers.
*/
function nmsTimer(handler, interval, name, description) {
this.handler = handler;
this.handle = false;
this.interval = parseInt(interval);
this.name = name;
this.description = description;
this.start = function() {
if (this.handle) {
this.stop();
}
this.handle = setInterval(this.handler,this.interval);
};
this.stop = function() {
if (this.handle)
clearInterval(this.handle);
this.handle = false;
};
this.setInterval = function(interval) {
var started = this.handle == false ? false : true;
this.stop();
this.interval = parseInt(interval);
if (started)
this.start();
};
}
/*
* Drawing primitives.
*
* This contains both canvas and context for drawing layers. It's on a
* top-level namespace to reduce SLIGHTLY the ridiculously long names
* (e.g.: dr.bg.ctx.drawImage() is long enough....).
*
* Only initialized once (for now).
*/
var dr = {};
/*
* Original scale. This is just used to define the coordinate system.
* 1920x1032 was chosen for tg15 by coincidence: We scaled the underlying
* map down to "full hd" and these are the bounds we got. There's no
* particular reason this couldn't change, except it means re-aligning all
* switches.
*/
var orig = {
width:1920,
height:1032
};
/*
* Canvas dimensions, and scale factor.
*
* We could derive scale factor from canvas.width / orig.width, but it's
* used so frequently that this makes much more sense.
*
* Width and height are rarely used.
*/
var canvas = {
width:0,
height:0,
scale:1
};
/*
* Various margins at the sides.
*
* Not really used much, except for "text", which is really more of a
* padding than margin...
*/
var margin = {
x:10,
y:20,
text:3
};
/*
* All of these should be moved into nms.*
*
* tgStart/tgEnd are "constants".
* replayTime is the current time as far as the replay-function is. This
* should be merged with nms.now.
*
* replayIncrement is how many seconds to add for each replay timer tick
* (e.g.: 30 minutes added for every 1 second display-time).
*/
var tgStart = stringToEpoch('2015-04-01T09:00:00');
var tgEnd = stringToEpoch('2015-04-05T12:00:00');
var replayTime = 0;
var replayIncrement = 60 * 60;
/*
* Convenience-function to populate the 'dr' structure.
*
* Only run once.
*/
function initDrawing() {
dr['bg'] = {};
dr['bg']['c'] = document.getElementById("bgCanvas");
dr['bg']['ctx'] = dr['bg']['c'].getContext('2d');
dr['link'] = {};
dr['link']['c'] = document.getElementById("linkCanvas");
dr['link']['ctx'] = dr['link']['c'].getContext('2d');
dr['blur'] = {};
dr['blur']['c'] = document.getElementById("blurCanvas");
dr['blur']['ctx'] = dr['blur']['c'].getContext('2d');
dr['switch'] = {};
dr['switch']['c'] = document.getElementById("switchCanvas");
dr['switch']['ctx'] = dr['switch']['c'].getContext('2d');
dr['text'] = {};
dr['text']['c'] = document.getElementById("textCanvas");
dr['text']['ctx'] = dr['text']['c'].getContext('2d');
dr['textInfo'] = {};
dr['textInfo']['c'] = document.getElementById("textInfoCanvas");
dr['textInfo']['ctx'] = dr['textInfo']['c'].getContext('2d');
dr['top'] = {};
dr['top']['c'] = document.getElementById("topCanvas");
dr['top']['ctx'] = dr['top']['c'].getContext('2d');
dr['input'] = {};
dr['input']['c'] = document.getElementById("inputCanvas");
dr['input']['ctx'] = dr['top']['c'].getContext('2d');
dr['hidden'] = {};
dr['hidden']['c'] = document.getElementById("hiddenCanvas");
dr['hidden']['ctx'] = dr['hidden']['c'].getContext('2d');
}
/*
* Convenience function that doesn't support huge numbers, and it's easier
* to comment than to fix. But not really, but I'm not fixing it anyway.
*/
function byteCount(bytes) {
var units = ['', 'K', 'M', 'G', 'T', 'P'];
i = 0;
while (bytes > 1024) {
bytes = bytes / 1024;
i++;
}
return bytes.toFixed(1) + units[i];
}
/*
* Definitely not a way to toggle night mode. Does something COMPLETELY
* DIFFERENT.
*/
function toggleNightMode()
{
setNightMode(!nms.nightMode);
saveSettings();
}
/*
* Parse 'now' from user-input.
*
* Should probably just use stringToEpoch() instead, but alas, not yet.
*/
function checkNow(now)
{
if (Date.parse(now)) {
var d = new Date(Date.parse(now));
var str = d.getFullYear() + "-" + (parseInt(d.getMonth())+1) + "-" + d.getDate() + "T";
str += d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
return str;
}
if (now == "")
return "";
return false;
}
/*
* Convert back and forth between epoch.
*
* There's no particular reason why I use seconds instead of javascript
* microseconds, except to leave the mark of a C coder on this javascript
* project.
*/
function stringToEpoch(t)
{
var foo = t.toString();
// foo = foo.replace('T',' ');
var ret = new Date(Date.parse(foo));
return parseInt(parseInt(ret.valueOf()) / 1000);
}
/*
* Have to pad with zeroes to avoid "17:5:0" instead of the conventional
* and more readable "17:05:00". I'm sure there's a better way, but this
* works just fine.
*/
function epochToString(t)
{
var d = new Date(parseInt(t) * parseInt(1000));
var str = d.getFullYear() + "-";
if (parseInt(d.getMonth()) < 9)
str += "0";
str += (parseInt(d.getMonth())+1) + "-";
if (d.getDate() < 10)
str += "0";
str += d.getDate() + "T";
if (d.getHours() < 10)
str += "0";
str += d.getHours() + ":";
if (d.getMinutes() < 10)
str += "0";
str += d.getMinutes() + ":";
if (d.getSeconds() < 10)
str += "0";
str += d.getSeconds();
return str;
}
/*
* Move 'nms.now' forward in time, unless we're at the end of the event.
*
* This is run on a timer (nms.timers.replay) every second when we are
* replaying.
*/
function timeReplay()
{
replayTime = stringToEpoch(nms.now);
if (replayTime >= tgEnd) {
nms.timers.replay.stop();
return;
}
replayTime = parseInt(replayTime) + parseInt(replayIncrement);
nms.now = epochToString(replayTime);
updatePorts();
updatePing();
}
/*
* Start replaying the event.
*
* I want this to be more generic:
* - Set time
* - Set end-time
* - Start/stop/pause
* - Set speed increment
*
* Once the lib supports this, I can move 'tgStart' and 'tgEnd' to the GUI
* and just provide them as default values or templates.
*/
function startReplay() {
nms.timers.replay.stop();
resetColors();
nms.now = epochToString(tgStart);
timeReplay();
nms.timers.replay.start();;
nms.timers.ping.stop();;
nms.timers.ports.stop();;
}
/*
* Used to move to a specific time, but not replay.
*/
function changeNow() {
var newnow = checkNow(document.getElementById("nowPicker").value);
if (!newnow) {
alert('Bad date-field in time travel field');
return;
}
if (newnow == "")
newnow = false;
nms.now = newnow;
if (newnow) {
nms.timers.ping.stop();;
nms.timers.ports.stop();;
}
updatePorts();
updatePing();
var boxHide = document.getElementById("nowPickerBox");
if (boxHide) {
boxHide.style.display = "none";
}
}
function stepTime(n)
{
var now;
nms.timers.replay.stop();
nms.timers.ping.stop();;
nms.timers.ports.stop();;
if (nms.now && nms.now != 0)
now = parseInt(stringToEpoch(nms.now));
else if (nms.switches_now)
now = parseInt(stringToEpoch(/^[^.]*/.exec(nms.switches_now.time)));
else
return;
newtime = parseInt(now) + parseInt(n);
nms.now = epochToString(parseInt(newtime));
updatePorts();
updatePing();
}
/*
* Hide switch info-box
*/
function hideSwitch()
{
var swtop = document.getElementById("info-switch-parent");
var switchele = document.getElementById("info-switch-table");
var comments = document.getElementById("info-switch-comments-table");
if (switchele != undefined)
switchele.parentNode.removeChild(switchele);
if (comments != undefined)
comments.parentNode.removeChild(comments);
commentbox = document.getElementById("commentbox");
if (commentbox != undefined)
commentbox.parentNode.removeChild(commentbox);
swtop.style.display = 'none';
nms.switch_showing = "";
}
/*
* Display info on switch "x" in the info-box
*/
function showSwitch(x)
{
var sw = nms.switches_now["switches"][x];
var swtop = document.getElementById("info-switch-parent");
var swpanel = document.getElementById("info-switch-panel-body");
var swtitle = document.getElementById("info-switch-title");
var tr;
var td1;
var td2;
hideSwitch();
nms.switch_showing = x;
document.getElementById("aboutBox").style.display = "none";
var switchele = document.createElement("table");
switchele.id = "info-switch-table";
switchele.className = "table";
switchele.classList.add("table");
switchele.classList.add("table-condensed");
swtitle.innerHTML = x + '<button type="button" class="close" aria-labe="Close" onclick="hideSwitch();" style="float: right;"><span aria-hidden="true">×</span></button>';
var speed = 0;
var speed2 = 0;
for (port in nms.switches_now["switches"][x]["ports"]) {
if (nms.switches_now["switches"][x]["ports"] == undefined ||
nms.switches_then["switches"][x]["ports"] == undefined) {
continue;
}
if (/ge-0\/0\/44$/.exec(port) ||
/ge-0\/0\/45$/.exec(port) ||
/ge-0\/0\/46$/.exec(port) ||
/ge-0\/0\/47$/.exec(port))
{
var t = nms.switches_then["switches"][x]["ports"][port];
var n = nms.switches_now["switches"][x]["ports"][port];
speed += (parseInt(t["ifhcoutoctets"]) - parseInt(n["ifhcoutoctets"])) / (parseInt(t["time"] - n["time"]));
speed2 += (parseInt(t["ifhcinoctets"]) - parseInt(n["ifhcinoctets"])) / (parseInt(t["time"] - n["time"]));
}
}
tr = switchele.insertRow(-1);
td1 = tr.insertCell(0);
td2 = tr.insertCell(1);
td1.innerHTML = "Uplink speed (out , port 44-47)";
td2.innerHTML = byteCount(8 * speed) + "b/s";
tr = switchele.insertRow(-1);
td1 = tr.insertCell(0);
td2 = tr.insertCell(1);
td1.title = "Port 44, 45, 46 and 47 are used as uplinks.";
td1.innerHTML = "Uplink speed (in , port 44-47)";
td2.innerHTML = byteCount(8 * speed2) + "b/s";
speed = 0;
for (port in nms.switches_now["switches"][x]["ports"]) {
if (nms.switches_now["switches"][x]["ports"] == undefined ||
nms.switches_then["switches"][x]["ports"] == undefined) {
continue;
}
var t = nms.switches_then["switches"][x]["ports"][port];
var n = nms.switches_now["switches"][x]["ports"][port];
speed += (parseInt(t["ifhcinoctets"]) -parseInt(n["ifhcinoctets"])) / (parseInt(t["time"] - n["time"]));
}
tr = switchele.insertRow(-1);
td1 = tr.insertCell(0);
td2 = tr.insertCell(1);
td1.innerHTML = "Total speed (in)";
td2.innerHTML = byteCount(8 * speed) + "b/s";
speed = 0;
for (port in nms.switches_now["switches"][x]["ports"]) {
if (nms.switches_now["switches"][x]["ports"] == undefined ||
nms.switches_then["switches"][x]["ports"] == undefined) {
continue;
}
var t = nms.switches_then["switches"][x]["ports"][port];
var n = nms.switches_now["switches"][x]["ports"][port];
speed += (parseInt(t["ifhcoutoctets"]) -parseInt(n["ifhcoutoctets"])) / (parseInt(t["time"] - n["time"]));
}
tr = switchele.insertRow(-1);
td1 = tr.insertCell(0);
td2 = tr.insertCell(1);
td1.innerHTML = "Total speed (out)";
td2.innerHTML = byteCount(8 * speed) + "b/s";
tr = switchele.insertRow(-1);
td1 = tr.insertCell(0);
td2 = tr.insertCell(1);
td1.innerHTML = "Management IP";
td2.innerHTML = sw["management"]["ip"];
tr = switchele.insertRow(-1);
td1 = tr.insertCell(0);
td2 = tr.insertCell(1);
td1.innerHTML = "Latency";
if (nms.ping_data && nms.ping_data["switches"] && nms.ping_data["switches"][x]) {
td2.innerHTML = nms.ping_data["switches"][x]["latency"];
} else {
td2.innerHTML = "N/A";
}
tr = switchele.insertRow(-1);
td1 = tr.insertCell(0);
td2 = tr.insertCell(1);
td1.innerHTML = "Temperature";
td2.innerHTML = sw["temp"];
tr = switchele.insertRow(-1);
td1 = tr.insertCell(0);
td2 = tr.insertCell(1);
td1.innerHTML = "Temperature age";
td2.innerHTML = sw["temp_time"];
tr = switchele.insertRow(-1);
td1 = tr.insertCell(0);
td2 = tr.insertCell(1);
td1.innerHTML = "Type";
td2.innerHTML = sw["switchtype"];
tr = switchele.insertRow(-1);
td1 = tr.insertCell(0);
td2 = tr.insertCell(1);
td1.innerHTML = "Last Updated";
td2.innerHTML = sw["management"]["last_updated"];
tr = switchele.insertRow(-1);
td1 = tr.insertCell(0);
td2 = tr.insertCell(1);
td1.innerHTML = "Poll frequency";
td2.innerHTML = sw["management"]["poll_frequency"];
comments = document.createElement("table");
comments.id = "info-switch-comments-table";
comments.className = "table table-condensed";
var cap = document.createElement("caption");
cap.textContent = "Comments";
comments.appendChild(cap);
var has_comment = false;
for (var c in sw["comments"]) {
var comment = sw["comments"][c];
has_comment = true;
if (comment["state"] == "active" || comment["state"] == "persist" || comment["state"] == "inactive") {
tr = comments.insertRow(-1);
var col;
if (comment["state"] == "active")
col = "danger";
else if (comment["state"] == "inactive")
col = false;
else
col = "info";
tr.className = col;
tr.id = "commentRow" + comment["id"];
td1 = tr.insertCell(0);
td2 = tr.insertCell(1);
td1.style.whiteSpace = "nowrap";
td1.style.width = "8em";
var txt = '<div class="btn-group" role="group" aria-label="..."><button type="button" class="btn btn-xs btn-default" data-trigger="focus" data-toggle="popover" title="Info" data-content="Comment added ' + epochToString(comment["time"]) + " by user " + comment["username"] + ' and listed as ' + comment["state"] + '"><span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span></button>';
txt += '<button type="button" class="btn btn-xs btn-danger" data-trigger="focus" data-toggle="tooltip" title="Mark as deleted" onclick="commentDelete(' + comment["id"] + ');"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>';
txt += '<button type="button" class="btn btn-xs btn-success" data-trigger="focus" data-toggle="tooltip" title="Mark as inactive/fixed" onclick="commentInactive(' + comment["id"] + ');"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></button>';
txt += '<button type="button" class="btn btn-xs btn-info" data-trigger="focus" data-toggle="tooltip" title="Mark as persistent" onclick="commentPersist(' + comment["id"] + ');"><span class="glyphicon glyphicon-star" aria-hidden="true"></span></button></div>';
td1.innerHTML = txt;
td2.textContent = comment['comment'];
}
}
swtop.appendChild(switchele);
if (has_comment) {
swtop.appendChild(comments);
$(function () { $('[data-toggle="popover"]').popover({placement:"top",continer:'body'}) })
}
var commentbox = document.createElement("div");
commentbox.id = "commentbox";
commentbox.className = "panel-body";
commentbox.style.width = "100%";
commentbox.innerHTML = '<div class="input-group"><input type="text" class="form-control" placeholder="Comment" id="' + x + '-comment"><span class=\"input-group-btn\"><button class="btn btn-default" onclick="addComment(\'' + x + '\',document.getElementById(\'' + x + '-comment\').value); document.getElementById(\'' + x + '-comment\').value = \'\'; document.getElementById(\'' + x + '-comment\').placeholder = \'Comment added. Wait for next refresh.\';">Add comment</button></span></div>';
swtop.appendChild(commentbox);
swtop.style.display = 'block';
}
/*
* There are 4 legend-bars. This is a helper-function to set the color and
* description/name for each one. Used from handler init-functions.
*
* FIXME: Should be smarter, possibly use a canvas-writer so we can get
* proper text (e.g.: not black text on dark blue).
*/
function setLegend(x,color,name)
{
var el = document.getElementById("legend-" + x);
el.style.background = color;
el.title = name;
el.textContent = name;
}
function updateAjaxInfo()
{
var out = document.getElementById('outstandingAJAX');
var of = document.getElementById('overflowAJAX');
out.textContent = nms.outstandingAjaxRequests;
of.textContent = nms.ajaxOverflow;
}
/*
* Run periodically to trigger map updates when a handler is active
*/
function updateMap()
{
/*
* XXX: This a bit hacky: There are a bunch of links that use
* href="#foo" but probably shouldn't. This breaks refresh since we
* base this on the location hash. To circumvent that issue
* somewhat, we just update the location hash if it's not
* "correct".
*/
if (nms.updater) {
if (document.location.hash != ('#' + nms.updater.tag)) {
document.location.hash = nms.updater.tag;
}
}
if (!newerSwitches())
return;
if (!nms.drawn)
setScale();
if (!nms.drawn)
return;
if (!nms.ping_data)
return;
nms.update_time = Date.now();
if (nms.updater != undefined && nms.switches_now && nms.switches_then) {
nms.updater.updater();
}
drawNow();
}
/*
* Change map handler (e.g., change from uplink map to ping map)
*/
function setUpdater(fo)
{
nms.updater = undefined;
resetColors();
resetTextInfo();
fo.init();
nms.updater = fo;
var foo = document.getElementById("updater_name");
foo.innerHTML = fo.name + " ";
document.location.hash = fo.tag;
initialUpdate();
if (nms.ping_data && nms.switches_then && nms.switches_now) {
nms.updater.updater();
}
}
/*
* Convenience function to avoid waiting for pollers when data is available
* for the first time.
*/
function initialUpdate()
{
return;
if (nms.ping_data && nms.switches_then && nms.switches_now && nms.updater != undefined && nms.did_update == false ) {
resizeEvent();
if (!nms.drawn) {
drawSwitches();
drawLinknets();
}
nms.updater.updater();
nms.did_update = true;
}
}
function resetBlur()
{
nms.nightBlur = {};
dr.blur.ctx.clearRect(0,0,canvas.width,canvas.height);
drawSwitches();
}
function applyBlur()
{
var blur = document.getElementById("shadowBlur");
var col = document.getElementById("shadowColor");
nms.shadowBlur = blur.value;
nms.shadowColor = col.value;
resetBlur();
saveSettings();
}
function showBlurBox()
{
var blur = document.getElementById("shadowBlur");
var col = document.getElementById("shadowColor");
blur.value = nms.shadowBlur;
col.value = nms.shadowColor;
document.getElementById("blurManic").style.display = '';
}
/*
* Update nms.ping_data
*/
function updatePing()
{
var now = nms.now ? ("?now=" + nms.now) : "";
if (nms.outstandingAjaxRequests > 5) {
nms.ajaxOverflow++;
updateAjaxInfo();
return;
}
nms.outstandingAjaxRequests++;
$.ajax({
type: "GET",
url: "/ping.pl" + now,
dataType: "text",
success: function (data, textStatus, jqXHR) {
nms.ping_data = JSON.parse(data);
initialUpdate();
updateMap();
},
complete: function(jqXHR, textStatus) {
nms.outstandingAjaxRequests--;
updateAjaxInfo();
}
});
}
function commentInactive(id)
{
commentChange(id,"inactive");
}
function commentPersist(id)
{
commentChange(id,"persist");
}
function commentDelete(id)
{
var r = confirm("Really delete comment? (Delted comments are still stored in the database, but never displayed)");
if (r == true) {
commentChange(id,"delete");
}
}
/*
* FIXME: Neither of these two handle failures in any way, shape or form.
* Nor do they really give user-feedback. They work, but only by magic.
*/
function commentChange(id,state)
{
var myData = {
comment:id,
state:state
};
var foo = document.getElementById("commentRow" + id);
if (foo) {
foo.className = '';
foo.style.backgroundColor = "silver";
}
$.ajax({
type: "POST",
url: "/comment-change.pl",
dataType: "text",
data:myData,
success: function (data, textStatus, jqXHR) {
nms.repop_switch = true;
}
});
}
function addComment(sw,comment)
{
var myData = {
switch:sw,
comment:comment
};
$.ajax({
type: "POST",
url: "/comment-add.pl",
dataType: "text",
data:myData,
success: function (data, textStatus, jqXHR) {
nms.repop_switch = true;
}
});
}
/*
* Update nms.switches_now and nms.switches_then
*/
function updatePorts()
{
var now = "";
if (nms.outstandingAjaxRequests > 5) {
nms.ajaxOverflow++;
updateAjaxInfo();
return;
}
nms.outstandingAjaxRequests++;
if (nms.now != false)
now = "?now=" + nms.now;
$.ajax({
type: "GET",
url: "/port-state.pl"+ now ,
dataType: "text",
success: function (data, textStatus, jqXHR) {
var switchdata = JSON.parse(data);
nms.switches_now = switchdata;
parseIntPlacements();
initialUpdate();
updateSpeed();
updateMap();
if (nms.repop_time == false && nms.repop_switch)
nms.repop_time = nms.switches_now.time;
else if (nms.repop_switch && nms.switch_showing && nms.repop_time != nms.switches_now.time) {
showSwitch(nms.switch_showing,true);
nms.repop_switch = false;
nms.repop_time = false;
}
},
complete: function(jqXHR, textStatus) {
nms.outstandingAjaxRequests--;
updateAjaxInfo();
}
});
now="";
if (nms.now != false)
now = "?now=" + nms.now;
nms.outstandingAjaxRequests++;
updateAjaxInfo();
$.ajax({
type: "GET",
url: "/port-state.pl" + now,
dataType: "text",
success: function (data, textStatus, jqXHR) {
var switchdata = JSON.parse(data);
nms.switches_then = switchdata;
initialUpdate();
updateSpeed();
updateMap();
},
complete: function(jqXHR, textStatus) {
nms.outstandingAjaxRequests--;
updateAjaxInfo();
}
})
}
/*
* Returns true if we have now and then-data for switches and that the
* "now" is actually newer. Useful for basic sanity and avoiding negative
* values when rewinding time.
*/
function newerSwitches()
{
if (!nms.switches_now || !nms.switches_then)
return false;
var now_timestamp = stringToEpoch(nms.switches_now.time);
var then_timestamp = stringToEpoch(nms.switches_then.time);
if (now_timestamp == 0 || then_timestamp == 0 || then_timestamp >= now_timestamp)
return false;
return true;
}
/*
* Use nms.switches_now and nms.switches_then to update 'nms.speed'.
*
* nms.speed is a total of ifHCInOctets across all client-interfaces
* nms.speed_full is a total of for /all/ interfaces.
*
* This is run separate of updatePorts mainly for historic reasons, but
* if it was added to the tail end of updatePorts, there'd have to be some
* logic to ensure it was run after both requests. Right now, it's just
* equally wrong for both scenarios, not consistently wrong (or something).
*
* FIXME: Err, yeah, add this to the tail-end of updatePorts instead :D
*
*/
function updateSpeed()
{
var speed_in = parseInt(0);
var speed_full = parseInt(0);
var counter=0;
var sw;
var speedele = document.getElementById("speed");
if (!newerSwitches())
return;
for (sw in nms.switches_now["switches"]) {
for (port in nms.switches_now["switches"][sw]["ports"]) {
if (!nms.switches_now["switches"][sw]["ports"][port]) {
console.log("ops");
continue;
}
if (!nms.switches_then || !nms.switches_then["switches"] || !nms.switches_then["switches"][sw] || !nms.switches_then["switches"][sw]["ports"]) {
continue;
}
if (!nms.switches_now || !nms.switches_now["switches"] || !nms.switches_now["switches"][sw] || !nms.switches_now["switches"][sw]["ports"]) {
continue;
}
if (!nms.switches_then["switches"][sw]["ports"][port]) {
console.log("ops");
continue;
}
var diff = parseInt(parseInt(nms.switches_now["switches"][sw]["ports"][port]["time"]) - parseInt(nms.switches_then["switches"][sw]["ports"][port]["time"]));
var then = parseInt(nms.switches_then["switches"][sw]["ports"][port]["ifhcinoctets"]) ;
var now = parseInt(nms.switches_now["switches"][sw]["ports"][port]["ifhcinoctets"]) ;
var diffval = (now - then);
if (then == 0 || now == 0 || diffval == 0 || diffval == NaN) {
continue;
}
speed_full += parseInt(diffval/diff);
if (( /e\d-\d/.exec(sw) || /e\d\d-\d/.exec(sw)) && ( /ge-\d\/\d\/\d$/.exec(port) || /ge-\d\/\d\/\d\d$/.exec(port))) {
if (!(
/ge-0\/0\/44$/.exec(port) ||
/ge-0\/0\/45$/.exec(port) ||
/ge-0\/0\/46$/.exec(port) ||
/ge-0\/0\/47$/.exec(port))) {
speed_in += parseInt(diffval/diff) ;
counter++;
}
}
}
}
nms.speed = speed_in;
nms.speed_full = speed_full;
if (speedele) {
speedele.innerHTML = byteCount(8 * parseInt(nms.speed)) + "b/s";
speedele.innerHTML += " / " + byteCount(8 * parseInt(nms.speed_full)) + "b/s";
}
}
/*
* Draw a linknet with index i.
*
* XXX: Might have to change the index here to match backend
*/
function drawLinknet(i)
{
var c1 = nms.linknet_color[i] && nms.linknet_color[i].c1 ? nms.linknet_color[i].c1 : blue;
var c2 = nms.linknet_color[i] && nms.linknet_color[i].c2 ? nms.linknet_color[i].c2 : blue;
if (nms.switches_now.switches[nms.switches_now.linknets[i].sysname1] && nms.switches_now.switches[nms.switches_now.linknets[i].sysname2]) {
connectSwitches(nms.switches_now.linknets[i].sysname1,nms.switches_now.linknets[i].sysname2, c1, c2);
}
}
/*
* Draw all linknets
*/
function drawLinknets()
{
if (nms.switches_now && nms.switches_now.linknets) {
for (var i in nms.switches_now.linknets) {
drawLinknet(i);
}
}
}
/*
* Change both colors of a linknet.
*
* XXX: Probably have to change this to better match the backend data
*/
function setLinknetColors(i,c1,c2)
{
if (!nms.linknet_color[i] ||
nms.linknet_color[i].c1 != c1 ||
nms.linknet_color[i].c2 != c2) {
if (!nms.linknet_color[i])
nms.linknet_color[i] = {};
nms.linknet_color[i]['c1'] = c1;
nms.linknet_color[i]['c2'] = c2;
drawLinknet(i);
}
}
/*
* (Re)draw a switch 'sw'.
*
* Color defaults to 'blue' if it's not set in the data structure.
*/
function drawSwitch(sw)
{
var box = nms.switches_now['switches'][sw]['placement'];
var color = nms.switch_color[sw];
if (color == undefined) {
color = blue;
}
dr.switch.ctx.fillStyle = color;
/*
* XXX: This is a left-over from before NMS did separate
* canvases, and might be done better elsewhere.
*/
if (nms.nightMode && nms.nightBlur[sw] != true) {
dr.blur.ctx.shadowBlur = nms.shadowBlur;
dr.blur.ctx.shadowColor = nms.shadowColor;
drawBoxBlur(box['x'],box['y'],box['width'],box['height']);
nms.nightBlur[sw] = true;
}
drawBox(box['x'],box['y'],box['width'],box['height']);
dr.switch.ctx.shadowBlur = 0;
if (!nms.textDrawn[sw]) {
if ((box['width'] + 10 )< box['height']) {
drawSideways(dr.text.ctx, sw,box['x'],box['y'],box['width'],box['height']);
} else {
drawRegular(dr.text.ctx,sw,box['x'],box['y'],box['width'],box['height']);
}
nms.textDrawn[sw] = true;
}
}
/*
* Make sure all placements of switches are parsed as integers so we don't
* have to pollute the code with pasreInt() every time we use it.
*/
function parseIntPlacements() {
for (var sw in nms.switches_now.switches) {
nms.switches_now.switches[sw]['placement']['x'] =
parseInt(nms.switches_now.switches[sw]['placement']['x']);
nms.switches_now.switches[sw]['placement']['y'] =
parseInt(nms.switches_now.switches[sw]['placement']['y']);
nms.switches_now.switches[sw]['placement']['width'] =
parseInt(nms.switches_now.switches[sw]['placement']['width']);
nms.switches_now.switches[sw]['placement']['height'] =
parseInt(nms.switches_now.switches[sw]['placement']['height']);
}
}
/*
* Draw all switches
*/
function drawSwitches()
{
if (!nms.switches_now || !nms.switches_now.switches)
return;
for (var sw in nms.switches_now.switches) {
drawSwitch(sw);
}
nms.drawn = true;
}
function drawSwitchInfo()
{
if (!nms.switches_now || !nms.switches_now.switches)
return;
for (var sw in nms.switchInfo) {
switchInfoText(sw, nms.switchInfo[sw]);
}
nms.drawn = true;
}
/*
* Draw current time-window
*
* FIXME: The math here is just wild approximation and guesswork because
* I'm lazy.
*/
function drawNow()
{
if (!nms.switches_now)
return;
// XXX: Get rid of microseconds that we get from the backend.
var now = /^[^.]*/.exec(nms.switches_now.time);
dr.top.ctx.font = Math.round(2 * nms.fontSize * canvas.scale) + "px " + nms.fontFace;
dr.top.ctx.clearRect(0,0,Math.floor(800 * canvas.scale),Math.floor(100 * canvas.scale));
dr.top.ctx.fillStyle = "white";
dr.top.ctx.strokeStyle = "black";
dr.top.ctx.lineWidth = Math.floor(nms.fontLineFactor * canvas.scale);
if (dr.top.ctx.lineWidth == 0) {
dr.top.ctx.lineWidth = Math.round(nms.fontLineFactor * canvas.scale);
}
dr.top.ctx.strokeText(now, 0 + margin.text, 25 * canvas.scale);
dr.top.ctx.fillText(now, 0 + margin.text, 25 * canvas.scale);
}
/*
* Draw foreground/scene.
*
* FIXME: Review this! This was made before linknets and switches were
* split apart.
*
* This is used so linknets are drawn before switches. If a switch is all
* that has changed, we just need to re-draw that, but linknets require
* scene-redrawing.
*/
function drawScene()
{
dr.text.ctx.font = Math.floor(nms.fontSize * canvas.scale) + "px " + nms.fontFace;
dr.textInfo.ctx.font = Math.floor(nms.fontSize * canvas.scale) + "px " + nms.fontFace;
drawLinknets();
drawSwitches();
drawSwitchInfo();
}
/*
* Set the scale factor and (re)draw the scene and background.
* Uses canvas.scale and updates canvas.height and canvas.width.
*/
function setScale()
{
canvas.height = orig.height * canvas.scale ;
canvas.width = orig.width * canvas.scale ;
for (var a in dr) {
/*
* Resizing this to a too small size breaks gradients on smaller screens.
*/
if (a == 'hidden')
continue;
dr[a].c.height = canvas.height;
dr[a].c.width = canvas.width;
}
nms.nightBlur = {};
nms.textDrawn = {};
nms.switchInfoDrawn = {};
if (nms.gradients)
drawGradient(nms.gradients);
drawBG();
drawScene();
drawNow();
document.getElementById("scaler").value = canvas.scale;
document.getElementById("scaler-text").innerHTML = (parseFloat(canvas.scale)).toPrecision(3);
}
/*
* Returns true if the coordinates (x,y) is inside the box defined by
* box.{x,y,w.h} (e.g.: placement of a switch).
*/
function isIn(box, x, y)
{
if ((x >= box.x) && (x <= (box.x + box.width)) && (y >= box.y) && (y <= (box.y + box.height))) {
return true;
}
return false;
}
/*
* Return the name of the switch found at coordinates (x,y), or 'undefined'
* if none is found.
*/
function findSwitch(x,y) {
x = parseInt(parseInt(x) / canvas.scale);
y = parseInt(parseInt(y) / canvas.scale);
for (var v in nms.switches_now.switches) {
if(isIn(nms.switches_now.switches[v]['placement'],x,y)) {
return v;
}
}
return undefined;
}
/*
* Set switch color of 'sw' to 'c', then re-draw the switch.
*/
function setSwitchColor(sw, c)
{
if(!nms.switch_color || !nms.switch_color[sw] || nms.switch_color[sw] != c) {
nms.switch_color[sw] = c;
drawSwitch(sw);
}
}
/*
* Event handler for the front-end drag bar to change scale
*/
function scaleChange()
{
var scaler = document.getElementById("scaler").value;
canvas.scale = scaler;
setScale();
}
/*
* Called when a switch is clicked
*/
function switchClick(sw)
{
if (nms.switch_showing == sw)
hideSwitch();
else
showSwitch(sw);
}
/*
* Resets the colors of linknets and switches.
*
* Useful when mode changes so we don't re-use colors from previous modes
* due to lack of data or bugs.
*/
function resetColors()
{
if (!nms.switches_now)
return;
if (nms.switches_now.linknets) {
for (var i in nms.switches_now.linknets) {
setLinknetColors(i, blue,blue);
}
}
for (var sw in nms.switches_now.switches) {
setSwitchColor(sw, blue);
}
}
function resetTextInfo()
{
if (!nms.switches_now)
return;
for (var sw in nms.switches_now.switches) {
switchInfoText(sw, undefined);
}
}
/*
* onclick handler for the canvas.
*
* Currently just shows info for a switch.
*/
function canvasClick(e)
{
var sw = findSwitch(e.pageX - e.target.offsetLeft, e.pageY - e.target.offsetTop);
if (sw != undefined) {
switchClick(sw);
}
}
/*
* Resize event-handler.
*
* Recomputes the scale and applies it.
*
* Has to use c.offset* since we are just scaling the canvas, not
* everything else.
*
*/
function resizeEvent()
{
var width = window.innerWidth - dr.bg.c.offsetLeft;
var height = window.innerHeight - dr.bg.c.offsetTop;
if (width / (orig.width + margin.x) > height / (orig.height + margin.y)) {
canvas.scale = height / (orig.height + margin.y);
} else {
canvas.scale = width / (orig.width + margin.x);
}
setScale();
}
/*
* Draws the background image (scaled).
*/
function drawBG()
{
if (nms.nightMode) {
invertCanvas();
} else {
var image = document.getElementById('source');
dr.bg.ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
}
}
/*
* Set night mode to whatever 'toggle' is.
*
* XXX: setScale() is a bit of a hack, but it really is the same stuff we
* need to do: Redraw "everything" (not really).
*/
function setNightMode(toggle) {
nms.nightMode = toggle;
var body = document.getElementById("body");
body.style.background = toggle ? "black" : "white";
var nav = document.getElementsByTagName("nav")[0];
if (toggle) {
dr.blur.c.style.display = '';
nav.classList.add('navbar-inverse');
} else {
dr.blur.c.style.display = 'none';
nav.classList.remove('navbar-inverse');
}
setScale();
}
function switchInfoText(sw, text)
{
var box = nms.switches_now['switches'][sw]['placement'];
var c = canvas.scale;
if (nms.switchInfo[sw] == text && nms.switchInfoDrawn[sw]) {
return;
}
nms.switchInfo[sw] = text;
nms.switchInfoDrawn[sw] = true;
dr.textInfo.ctx.clearRect(c* box['x'], c*box['y'], c*box['width'], c*box['height']);
if (text != undefined && text != "") {
if ((box['width'] + 10 )< box['height']) {
drawSideways(dr.textInfo.ctx, text,box['x'],box['y'],box['width'],box['height'],"end");
} else {
drawRegular(dr.textInfo.ctx, text,box['x'],box['y'],box['width'],box['height'],"end");
}
}
}
/*
* Draw a box (e.g.: switch).
*/
function drawBox(x,y,boxw,boxh)
{
var myX = Math.floor(x * canvas.scale);
var myY = Math.floor(y * canvas.scale);
var myX2 = Math.floor((boxw) * canvas.scale);
var myY2 = Math.floor((boxh) * canvas.scale);
dr.switch.ctx.fillRect(myX,myY, myX2, myY2);
dr.switch.ctx.lineWidth = Math.floor(1.0 * canvas.scale);
if (canvas.scale < 1.0) {
dr.switch.ctx.lineWidth = 1.0;
}
dr.switch.ctx.strokeStyle = "#000000";
dr.switch.ctx.strokeRect(myX,myY, myX2, myY2);
}
/*
* Draw the blur for a box.
*/
function drawBoxBlur(x,y,boxw,boxh)
{
var myX = Math.floor(x * canvas.scale);
var myY = Math.floor(y * canvas.scale);
var myX2 = Math.floor((boxw) * canvas.scale);
var myY2 = Math.floor((boxh) * canvas.scale);
dr.blur.ctx.fillRect(myX,myY, myX2, myY2);
}
/*
* Draw text on a box - sideways!
*
* XXX: This is pretty nasty and should also probably take a box as input.
*/
function drawSideways(ctx,text,x,y,w,h,align)
{
ctx.rotate(Math.PI * 3 / 2);
ctx.fillStyle = "white";
ctx.strokeStyle = "black";
if (align == "end") {
ctx.textAlign = align;
y = y-h + margin.text*2;
} else {
ctx.textAlign = "start";
}
ctx.lineWidth = Math.floor(nms.fontLineFactor * canvas.scale);
if (ctx.lineWidth == 0) {
ctx.lineWidth = Math.round(nms.fontLineFactor * canvas.scale);
}
ctx.strokeText(text, - canvas.scale * (y + h - margin.text),canvas.scale * (x + w - margin.text) );
ctx.fillText(text, - canvas.scale * (y + h - margin.text),canvas.scale * (x + w - margin.text) );
ctx.rotate(Math.PI / 2);
}
/*
* Draw background inverted (wooo)
*
* XXX: This is broken for chromium on local file system (e.g.: file:///)
* Seems like a chromium bug?
*/
function invertCanvas() {
var imageObj = document.getElementById('source');
dr.bg.ctx.drawImage(imageObj, 0, 0, canvas.width, canvas.height);
var imageData = dr.bg.ctx.getImageData(0, 0, canvas.width, canvas.height);
var data = imageData.data;
for(var i = 0; i < data.length; i += 4) {
data[i] = 255 - data[i];
data[i + 1] = 255 - data[i + 1];
data[i + 2] = 255 - data[i + 2];
}
dr.bg.ctx.putImageData(imageData, 0, 0);
}
/*
* Draw regular text on a box.
*
* Should take the same format as drawSideways()
*
* XXX: Both should be renamed to have 'text' or something in them
*/
function drawRegular(ctx,text,x,y,w,h,align) {
ctx.fillStyle = "white";
ctx.strokeStyle = "black";
ctx.lineWidth = Math.floor(nms.fontLineFactor * canvas.scale);
if (align == "end") {
ctx.textAlign = align;
x = x+w - margin.text*2;
} else {
ctx.textAlign = "start";
}
if (ctx.lineWidth == 0) {
ctx.lineWidth = Math.round(nms.fontLineFactor * canvas.scale);
}
ctx.strokeText(text, (x + margin.text) * canvas.scale, (y + h - margin.text) * canvas.scale);
ctx.fillText(text, (x + margin.text) * canvas.scale, (y + h - margin.text) * canvas.scale);
}
/*
* Draw a line between switch "insw1" and "insw2", using a gradiant going
* from color1 to color2.
*
* XXX: beginPath() and closePath() is needed to avoid re-using the
* gradient/color
*/
function connectSwitches(insw1, insw2,color1, color2) {
var sw1 = nms.switches_now.switches[insw1].placement;
var sw2 = nms.switches_now.switches[insw2].placement;
if (color1 == undefined)
color1 = blue;
if (color2 == undefined)
color2 = blue;
var x0 = Math.floor((sw1.x + sw1.width/2) * canvas.scale);
var y0 = Math.floor((sw1.y + sw1.height/2) * canvas.scale);
var x1 = Math.floor((sw2.x + sw2.width/2) * canvas.scale);
var y1 = Math.floor((sw2.y + sw2.height/2) * canvas.scale);
var gradient = dr.link.ctx.createLinearGradient(x1,y1,x0,y0);
gradient.addColorStop(0, color1);
gradient.addColorStop(1, color2);
dr.link.ctx.beginPath();
dr.link.ctx.strokeStyle = gradient;
dr.link.ctx.moveTo(x0,y0);
dr.link.ctx.lineTo(x1,y1);
dr.link.ctx.lineWidth = Math.floor(5 * canvas.scale);
dr.link.ctx.closePath();
dr.link.ctx.stroke();
dr.link.ctx.moveTo(0,0);
}
/*
* Boot up "fully fledged" NMS.
*
* If you only want parts of the functionality, then re-implement this
* (e.g., just add and start the handlers you want, don't worry about
* drawing, etc).
*/
function initNMS() {
initDrawing();
window.addEventListener('resize',resizeEvent,true);
document.addEventListener('load',resizeEvent,true);
nms.timers.ports = new nmsTimer(updatePorts, 1000, "Port updater", "AJAX request to update port data (traffic, etc)");
nms.timers.ports.start();
nms.timers.ping = new nmsTimer(updatePing, 1000, "Ping updater", "AJAX request to update ping data");
nms.timers.ping.start();
nms.timers.replay = new nmsTimer(timeReplay, 1000, "Time machine", "Handler used to change time");
detectHandler();
updatePorts();
updatePing();
setupKeyhandler();
restoreSettings();
}
function detectHandler() {
var url = document.URL;
for (var i in handlers) {
if (('#' + handlers[i].tag) == document.location.hash) {
setUpdater(handlers[i]);
return;
}
}
setUpdater(handler_ping);
}
/*
* Display and populate the dialog box for debugging timers.
*
* Could probably be cleaned up.
*/
function showTimerDebug() {
var tableTop = document.getElementById('debugTimers');
var table = document.getElementById('timerTable');
var tr, td1, td2;
if (table)
tableTop.removeChild(table);
table = document.createElement("table");
table.id = "timerTable";
table.style.zIndex = 100;
table.className = "table";
table.classList.add("table");
table.classList.add("table-default");
var header = table.createTHead();
tr = header.insertRow(0);
td = tr.insertCell(0);
td.innerHTML = "Handler";
td = tr.insertCell(1);
td.innerHTML = "Interval (ms)";
td = tr.insertCell(2);
td.innerHTML = "Name";
td = tr.insertCell(3);
td.innerHTML = "Description";
for (var v in nms.timers) {
tr = table.insertRow(-1);
td = tr.insertCell(0);
td.textContent = nms.timers[v].handle;
td = tr.insertCell(1);
td.style.width = "15em";
var tmp = "<div class=\"input-group\"><input type=\"text\" id='handlerValue" + v + "' value='" + nms.timers[v].interval + "' class=\"form-control\"></input>";
tmp += "<span class=\"input-group-btn\"><button type=\"button\" class=\"btn btn-default\" onclick=\"nms.timers['" + v + "'].setInterval(document.getElementById('handlerValue" + v + "').value);\">Apply</button></span></div>";
td.innerHTML = tmp;
td = tr.insertCell(2);
td.textContent = nms.timers[v].name;
td = tr.insertCell(3);
td.textContent = nms.timers[v].description;
}
tableTop.appendChild(table);
document.getElementById('debugTimers').style.display = 'block';
}
function hideLayer(layer) {
var l = document.getElementById(layer);
l.style.display = "none";
if (layer != "layerVisibility")
nms.layerVisibility[layer] = false;
saveSettings();
}
function showLayer(layer) {
var l = document.getElementById(layer);
l.style.display = "";
if (layer != "layerVisibility")
nms.layerVisibility[layer] = true;
saveSettings();
}
function toggleLayer(layer) {
var l = document.getElementById(layer);
if (l.style.display == 'none')
l.style.display = '';
else
l.style.display = 'none';
}
function applyLayerVis()
{
for (var l in nms.layerVisibility) {
var layer = document.getElementById(l);
if (layer)
layer.style.display = nms.layerVisibility[l] ? '' : 'none';
}
}
function setMenu()
{
var nav = document.getElementsByTagName("nav")[0];
nav.style.display = nms.menuShowing ? '' : 'none';
resizeEvent();
}
function toggleMenu()
{
nms.menuShowing = ! nms.menuShowing;
setMenu();
saveSettings();
}
function setMapModeFromN(e,key)
{
switch(key) {
case '1':
setUpdater(handler_ping);
break;
case '2':
setUpdater(handler_uplinks);
break;
case '3':
setUpdater(handler_temp);
break;
case '4':
setUpdater(handler_traffic);
break;
case '5':
setUpdater(handler_comment);
break;
case '6':
setUpdater(handler_traffic_tot);
break;
case '7':
setUpdater(handler_disco);
break;
}
return true;
}
function moveTimeFromKey(e,key)
{
switch(key) {
case 'h':
stepTime(-3600);
break;
case 'j':
stepTime(-300);
break;
case 'k':
stepTime(300);
break;
case 'l':
stepTime(3600);
break;
case 'p':
if(nms.timers.replay.handle)
nms.timers.replay.stop();
else {
timeReplay();
nms.timers.replay.start();
}
break;
case 'r':
nms.timers.replay.stop();
nms.now = false;
nms.timers.ping.start();;
nms.timers.ports.start();;
updatePorts();
updatePing();
break;
}
return true;
}
var debugEvent;
function keyDebug(e)
{
console.log(e);
debugEvent = e;
}
function keyPressed(e)
{
if (e.target.nodeName == "INPUT") {
return false;
}
var key = String.fromCharCode(e.keyCode);
if (nms.keyBindings[key])
return nms.keyBindings[key](e,key);
if (nms.keyBindings['default'])
return nms.keyBindings['default'](e,key);
return false;
}
function setupKeyhandler()
{
var b = document.getElementsByTagName("body")[0];
b.onkeypress = function(e){keyPressed(e);};
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ')
c = c.substring(1);
if (c.indexOf(name) == 0)
return c.substring(name.length,c.length);
}
return "";
}
function saveSettings()
{
var foo={};
for (var v in nms.settingsList) {
foo[nms.settingsList[v]] = nms[nms.settingsList[v]];
}
document.cookie = 'nms='+btoa(JSON.stringify(foo));
}
function restoreSettings()
{
try {
var retrieve = JSON.parse(atob(getCookie("nms")));
} catch(e) {
console.log("nothing saved");
}
for (var v in retrieve) {
nms[v] = retrieve[v];
}
setScale();
setMenu();
setNightMode(nms.nightMode);
applyLayerVis();
}
function forgetSettings()
{
document.cookie = 'nms=' + btoa('{}');
}
|