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
|
'use strict';
'require view';
'require dom';
'require poll';
'require request';
'require ui';
'require rpc';
'require network';
var callLuciRealtimeStats = rpc.declare({
object: 'luci',
method: 'getRealtimeStats',
params: [ 'mode', 'device' ],
expect: { result: [] }
});
var graphPolls = [],
pollInterval = 3;
Math.log2 = Math.log2 || function(x) { return Math.log(x) * Math.LOG2E; };
function rate(n, br) {
n = (n || 0).toFixed(2);
return [ '%1024.2mbit/s'.format(n * 8), br ? E('br') : ' ', '(%1024.2mB/s)'.format(n) ]
}
return view.extend({
load: function() {
return Promise.all([
this.loadSVG(L.resource('svg/bandwidth.svg')),
network.getDevices()
]);
},
updateGraph: function(ifname, svg, lines, cb) {
var G = svg.firstElementChild;
var view = document.querySelector('#view');
var width = view.offsetWidth - 2;
var height = 300 - 2;
var step = 5;
var data_wanted = Math.floor(width / step);
var data_values = [],
line_elements = [];
for (var i = 0; i < lines.length; i++)
if (lines[i] != null)
data_values.push([]);
var info = {
line_current: [],
line_average: [],
line_peak: []
};
/* prefill datasets */
for (var i = 0; i < data_values.length; i++)
for (var j = 0; j < data_wanted; j++)
data_values[i][j] = 0;
/* plot horizontal time interval lines */
for (var i = width % (step * 60); i < width; i += step * 60) {
var line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
line.setAttribute('x1', i);
line.setAttribute('y1', 0);
line.setAttribute('x2', i);
line.setAttribute('y2', '100%');
line.setAttribute('style', 'stroke:black;stroke-width:0.1');
var text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
text.setAttribute('x', i + 5);
text.setAttribute('y', 15);
text.setAttribute('style', 'fill:#eee; font-size:9pt; font-family:sans-serif; text-shadow:1px 1px 1px #000');
text.appendChild(document.createTextNode(Math.round((width - i) / step / 60) + 'm'));
G.appendChild(line);
G.appendChild(text);
}
info.interval = pollInterval;
info.timeframe = data_wanted / 60;
graphPolls.push({
ifname: ifname,
svg: svg,
lines: lines,
cb: cb,
info: info,
width: width,
height: height,
step: step,
values: data_values,
timestamp: 0,
fill: 1
});
},
pollData: function() {
poll.add(L.bind(function() {
var tasks = [];
for (var i = 0; i < graphPolls.length; i++) {
var ctx = graphPolls[i];
tasks.push(L.resolveDefault(callLuciRealtimeStats('interface', ctx.ifname), []));
}
return Promise.all(tasks).then(L.bind(function(datasets) {
for (var gi = 0; gi < graphPolls.length; gi++) {
var ctx = graphPolls[gi],
data = datasets[gi],
values = ctx.values,
lines = ctx.lines,
info = ctx.info;
var data_scale = 0;
var data_wanted = Math.floor(ctx.width / ctx.step);
var last_timestamp = NaN;
for (var i = 0, di = 0; di < lines.length; di++) {
if (lines[di] == null)
continue;
var multiply = (lines[di].multiply != null) ? lines[di].multiply : 1,
offset = (lines[di].offset != null) ? lines[di].offset : 0;
for (var j = ctx.timestamp ? 0 : 1; j < data.length; j++) {
/* skip overlapping entries */
if (data[j][0] <= ctx.timestamp)
continue;
if (i == 0) {
ctx.fill++;
last_timestamp = data[j][0];
}
if (lines[di].counter) {
/* normalize difference against time interval */
if (j > 0) {
var time_delta = data[j][0] - data[j - 1][0];
if (time_delta) {
info.line_current[i] = (data[j][di + 1] * multiply - data[j - 1][di + 1] * multiply) / time_delta;
info.line_current[i] -= Math.min(info.line_current[i], offset);
values[i].push(info.line_current[i]);
}
}
}
else {
info.line_current[i] = data[j][di + 1] * multiply;
info.line_current[i] -= Math.min(info.line_current[i], offset);
values[i].push(info.line_current[i]);
}
}
i++;
}
/* cut off outdated entries */
ctx.fill = Math.min(ctx.fill, data_wanted);
for (var i = 0; i < values.length; i++) {
var len = values[i].length;
values[i] = values[i].slice(len - data_wanted, len);
/* find peaks, averages */
info.line_peak[i] = NaN;
info.line_average[i] = 0;
for (var j = 0; j < values[i].length; j++) {
info.line_peak[i] = isNaN(info.line_peak[i]) ? values[i][j] : Math.max(info.line_peak[i], values[i][j]);
info.line_average[i] += values[i][j];
}
info.line_average[i] = info.line_average[i] / ctx.fill;
}
info.peak = Math.max.apply(Math, info.line_peak);
/* remember current timestamp, calculate horizontal scale */
if (!isNaN(last_timestamp))
ctx.timestamp = last_timestamp;
var size = Math.floor(Math.log2(info.peak)),
div = Math.pow(2, size - (size % 10)),
mult = info.peak / div,
mult = (mult < 5) ? 2 : ((mult < 50) ? 10 : ((mult < 500) ? 100 : 1000));
info.peak = info.peak + (mult * div) - (info.peak % (mult * div));
data_scale = ctx.height / info.peak;
/* plot data */
for (var i = 0, di = 0; di < lines.length; di++) {
if (lines[di] == null)
continue;
var el = ctx.svg.firstElementChild.getElementById(lines[di].line),
pt = '0,' + ctx.height,
y = 0;
if (!el)
continue;
for (var j = 0; j < values[i].length; j++) {
var x = j * ctx.step;
y = ctx.height - Math.floor(values[i][j] * data_scale);
//y -= Math.floor(y % (1 / data_scale));
y = isNaN(y) ? ctx.height : y;
pt += ' ' + x + ',' + y;
}
pt += ' ' + ctx.width + ',' + y + ' ' + ctx.width + ',' + ctx.height;
el.setAttribute('points', pt);
i++;
}
info.label_25 = 0.25 * info.peak;
info.label_50 = 0.50 * info.peak;
info.label_75 = 0.75 * info.peak;
if (typeof(ctx.cb) == 'function')
ctx.cb(ctx.svg, info);
}
}, this));
}, this), pollInterval);
},
loadSVG: function(src) {
return request.get(src).then(function(response) {
if (!response.ok)
throw new Error(response.statusText);
return E('div', {
'style': 'width:100%;height:300px;border:1px solid #000;background:#fff'
}, E(response.text()));
});
},
render: function(data) {
var svg = data[0],
devs = data[1];
var v = E('div', { 'class': 'cbi-map', 'id': 'map' }, E('div'));
for (var i = 0; i < devs.length; i++) {
var ifname = devs[i].getName();
if (!ifname)
continue;
var csvg = svg.cloneNode(true);
v.firstElementChild.appendChild(E('div', { 'class': 'cbi-section', 'data-tab': ifname, 'data-tab-title': ifname }, [
csvg,
E('div', { 'class': 'right' }, E('small', { 'id': 'scale' }, '-')),
E('br'),
E('table', { 'class': 'table', 'style': 'width:100%;table-layout:fixed' }, [
E('tr', { 'class': 'tr' }, [
E('td', { 'class': 'td right top' }, E('strong', { 'style': 'border-bottom:2px solid blue' }, [ _('Inbound:') ])),
E('td', { 'class': 'td', 'id': 'rx_bw_cur' }, rate(0, true)),
E('td', { 'class': 'td right top' }, E('strong', {}, [ _('Average:') ])),
E('td', { 'class': 'td', 'id': 'rx_bw_avg' }, rate(0, true)),
E('td', { 'class': 'td right top' }, E('strong', {}, [ _('Peak:') ])),
E('td', { 'class': 'td', 'id': 'rx_bw_peak' }, rate(0, true))
]),
E('tr', { 'class': 'tr' }, [
E('td', { 'class': 'td right top' }, E('strong', { 'style': 'border-bottom:2px solid green' }, [ _('Outbound:') ])),
E('td', { 'class': 'td', 'id': 'tx_bw_cur' }, rate(0, true)),
E('td', { 'class': 'td right top' }, E('strong', {}, [ _('Average:') ])),
E('td', { 'class': 'td', 'id': 'tx_bw_avg' }, rate(0, true)),
E('td', { 'class': 'td right top' }, E('strong', {}, [ _('Peak:') ])),
E('td', { 'class': 'td', 'id': 'tx_bw_peak' }, rate(0, true))
])
]),
E('div', {'class': 'cbi-section-create'})
]));
this.updateGraph(ifname, csvg, [ { line: 'rx', counter: true }, null, { line: 'tx', counter: true } ], function(svg, info) {
var G = svg.firstElementChild, tab = svg.parentNode;
G.getElementById('label_25').firstChild.data = rate(info.label_25).join('');
G.getElementById('label_50').firstChild.data = rate(info.label_50).join('');
G.getElementById('label_75').firstChild.data = rate(info.label_75).join('');
tab.querySelector('#scale').firstChild.data = _('(%d minute window, %d second interval)').format(info.timeframe, info.interval);
dom.content(tab.querySelector('#rx_bw_cur'), rate(info.line_current[0], true));
dom.content(tab.querySelector('#rx_bw_avg'), rate(info.line_average[0], true));
dom.content(tab.querySelector('#rx_bw_peak'), rate(info.line_peak[0], true));
dom.content(tab.querySelector('#tx_bw_cur'), rate(info.line_current[1], true));
dom.content(tab.querySelector('#tx_bw_avg'), rate(info.line_average[1], true));
dom.content(tab.querySelector('#tx_bw_peak'), rate(info.line_peak[1], true));
});
}
ui.tabs.initTabGroup(v.firstElementChild.childNodes);
this.pollData();
return E([], [
E('h2', _('Bandwidth')),
E('div', {'class': 'cbi-map-descr'}, _('This page displays the bandwidth used for all available physical interfaces.')),
v
]);
},
handleSaveApply: null,
handleSave: null,
handleReset: null
});
|