forked from pjodd/graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.js
More file actions
153 lines (140 loc) · 5.48 KB
/
Copy pathnodes.js
File metadata and controls
153 lines (140 loc) · 5.48 KB
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
// TODO, could keep nodesdata (values) in objs like these columns, rewrite the
// repr to eat vals directly, and so on... and let the sorting sort on repr if
// there is one, otherwise on val -- then I wouldn't need to do shortennodeid first?!
var columns = {
"node_id": { path: "nodeinfo.node_id",
cssclass: "monospace center" },
"desc": { path: "nodeinfo.node_id",
repr: (f) => getnodedesc(f) },
"uptime": { path: "statistics.uptime",
cssclass: "right",
repr: (f) => prettyduration(f * 1000.0) },
// "iface-tunnel": { path: "nodeinfo.network.mesh.bat0.interfaces.tunnel" },
"online": { path: "flags.online",
cssclass: "center" },
"uplink": { path: "flags.uplink",
cssclass: "center" },
// "gateway": { path: "statistics.gateway" },
"iface-wireless": { path: "nodeinfo.network.mesh.bat0.interfaces.wireless",
cssclass: "monospace center",
repr: (f) => f.replace(/:/g, "") },
"gateway_nexthop": { path: "statistics.gateway_nexthop",
cssclass: "monospace center",
repr: (f) => f.replace(/:/g, "") },
"lastseen": { path: "lastseen",
cssclass: "right",
repr: (f) => prettyduration(Date.now() - Date.parse(f)) },
"firstseen": { path: "firstseen",
cssclass: "right",
repr: (f) => prettyduration(Date.now() - Date.parse(f)) },
"clients": { path: "statistics.clients",
cssclass: "right" },
"loadavg": { path: "statistics.loadavg",
cssclass: "center",
repr: (f) => f.toFixed(2) },
"memuse": { path: "statistics.memory_usage",
cssclass: "center",
repr: (f) => f.toFixed(2) },
"model": { path: "nodeinfo.hardware.model",
repr: (f) => f.replace(/^TP-Link TL-/, "").toLowerCase() },
"release": { path: "nodeinfo.software.firmware.release" },
}
var allnodes = {};
var req = new XMLHttpRequest();
req.open("GET", "http://gateway-01.mesh.pjodd.se/hopglass/nodes.json", false);
req.send();
var nodesjson = JSON.parse(req.responseText);
nodesjson.nodes.forEach(function (e, i, arr) {
var thisnode = e.nodeinfo.node_id;
allnodes[thisnode] = e;
if (e.statistics.hasOwnProperty("gateway_nexthop")) {
// we have nexthop mac, resolve to node_id and store in statistics
var nextmac = e.statistics.gateway_nexthop;
arr.forEach(function (e) {
// this interface's mac is the relevant one (`id` in graph.json)
if (e.nodeinfo.network.mesh.bat0.interfaces.wireless == nextmac) {
allnodes[thisnode].statistics._nexthop_nodeid = e.nodeinfo.node_id;
}
});
}
});
function getbypath(obj, path) {
return path.split(".").reduce(function (prev, curr) {
// not doing prev.hasOwnProperty(curr) ? prev[curr] ... -- it's currently
// OK that what's false (false, 0 etc) becomes "".
return prev ? (prev[curr] ? prev[curr] : "") : ""
}, obj || self)
}
var nodes = nodesjson.nodes.map(function (node) {
var nodeobj = {};
Object.keys(columns).forEach(function (column) {
val = getbypath(node, columns[column].path);
// do this here since we wanna sort on the shortened node_id
if (val && column == "node_id") {
val = shortennodeid(val);
}
// it's actually an array
if (val && column == "iface-wireless") {
val = val[0];
}
nodeobj[column] = val;
});
return nodeobj;
});
var sortable = function (nodes) {
var table = document.createElement("table");
var thead = document.createElement("thead");
var tbody = document.createElement("tbody");
var sortorders = {};
thead.addEventListener("click", function (ev) {
var ordercol = ev.target.dataset.key;
sortorders[ordercol] = !sortorders[ordercol];
draw(nodes, ordercol);
});
function draw(nodes, ordercol) {
var d = sortorders[ordercol] ? -1 : 1;
nodes = nodes.sort(function (a, b) {
return a[ordercol] < b[ordercol] ? d : d * -1;
});
thead.innerHTML = "";
var tr = thead.insertRow();
Object.keys(nodes[0]).forEach(function (column) {
var th = document.createElement("th");
th.setAttribute("title", columns[column].path);
tr.appendChild(th);
var sortprefix = "";
if (ordercol == column) {
sortprefix = "^ "
if (sortorders[ordercol]) {
sortprefix = "v ";
}
}
th.appendChild(document.createTextNode(sortprefix + column));
th.setAttribute("data-key", column);
});
tbody.innerHTML = "";
var now = Date.now();
nodes.forEach(function (nodeobj) {
var tr = tbody.insertRow();
Object.keys(nodeobj).forEach(function (column) {
var td = tr.insertCell();
var field = nodeobj[column];
td.setAttribute("title", field);
var repr = columns[column].repr
if (field && repr) {
field = repr(field);
}
td.appendChild(document.createTextNode(field));
var cssclass = columns[column].cssclass
if (cssclass) {
td.setAttribute("class", cssclass);
}
});
});
}
draw(nodes, "lastseen");
table.appendChild(thead);
table.appendChild(tbody);
return table;
}
document.getElementById("table").appendChild(sortable(nodes));