-
Notifications
You must be signed in to change notification settings - Fork 2
/
nodes.js
156 lines (143 loc) · 5.68 KB
/
nodes.js
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
// 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...
var q = d3.queue()
q.defer(d3.json, 'https://lublin.se/pjodd/gateway-01.mesh.pjodd.se/hopglass/nodes.json')
q.await(function (err, nodesjson) {
if (err) throw (err)
main(nodesjson)
})
function main (nodesjson) {
var columns = {
'node_id': { path: 'nodeinfo.node_id',
cssclass: 'monospace center' },
'desc': { path: 'nodeinfo.node_id',
repr: (f) => getdesc(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' },
// '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',
repr: (f) => getdesc(f) || f },
'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.total',
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 /i, '').toLowerCase() },
'release': { path: 'nodeinfo.software.firmware.release' },
'bat-version': { path: 'nodeinfo.software.batman-adv.version' },
'addrs (non-fe80)':{ path: 'nodeinfo.network.addresses',
repr: (f) => [...new Set(f.filter(addr => !addr.startsWith('fe80:')))].join(' ') }
}
var nodes = nodesjson.nodes.map(function (node) {
var nodeobj = {}
Object.keys(columns).forEach(function (column) {
var val = getbypath(node, columns[column].path)
// 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)
var url = new URL(window.location.href)
url.searchParams.set("sort", ordercol)
url.searchParams.delete("desc")
if (sortorders[ordercol] == false) {
url.searchParams.set("desc", "")
}
window.history.replaceState("", "", url);
})
function draw (nodes, ordercol) {
if (!Object.keys(columns).includes(ordercol)) {
table.innerHTML = 'Bad sorting column. <a href="nodes.html">go back</a>'
return
}
var d = sortorders[ordercol] ? -1 : 1
nodes = nodes.sort(function (a, b) {
var vala = a[ordercol]
var valb = b[ordercol]
// should we sort by repr(val) instead?
var repr = columns[ordercol].repr
if (repr && ['desc'].includes(ordercol)) {
vala = repr(vala)
valb = repr(valb)
}
// is inverted order more sensible?
if (['lastseen', 'firstseen'].includes(ordercol)) {
[vala, valb] = [valb, vala]
}
return vala < valb ? 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 = '▴ '
}
}
th.appendChild(document.createTextNode(sortprefix + column))
th.setAttribute('data-key', column)
})
tbody.innerHTML = ''
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)
}
})
})
}
var params = new URL(window.location.href).searchParams
var ordercol = params.get("sort") || 'lastseen'
sortorders[ordercol] = !params.has("desc")
draw(nodes, ordercol)
table.appendChild(thead)
table.appendChild(tbody)
return table
}
document.getElementById('table').appendChild(sortable(nodes))
}