luci-mod-admin-full: status: survive broken DSL status output
[project/luci.git] / modules / luci-mod-admin-full / luasrc / view / admin_status / wireless.htm
1 <%#
2  Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
3  Licensed to the public under the Apache License 2.0.
4 -%>
5
6 <%-
7         local ntm = require "luci.model.network".init()
8
9         local dev
10         local devices = { }
11         for _, dev in luci.util.vspairs(luci.sys.net.devices()) do
12                 if dev:match("^wlan%d") or dev:match("^ath%d") or dev:match("^wl%d") then
13                         devices[#devices+1] = dev
14                 end
15         end
16
17         local curdev = luci.dispatcher.context.requestpath
18               curdev = curdev[#curdev] ~= "wireless" and curdev[#curdev] or devices[1]
19 -%>
20
21 <%+header%>
22
23 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
24 <script type="text/javascript">//<![CDATA[
25         var bwxhr = new XHR();
26
27         var G, G2;
28         var TIME  = 0;
29         var RATE  = 1;
30         var RSSI  = 2;
31         var NOISE = 3;
32
33         var width  = 760;
34         var height = 300;
35         var step   = 5;
36
37         var data_wanted = Math.floor(width / step);
38         var data_fill   = 0;
39         var data_stamp  = 0;
40
41         var data_rssi = [ ];
42         var data_noise = [ ];
43         var data_rate = [ ];
44
45         var line_rssi;
46         var line_noise;
47         var line_rate;
48
49         var label_25, label_25_2;
50         var label_50, label_50_2;
51         var label_75, label_75_2;
52
53         var label_rssi_cur;
54         var label_rssi_avg;
55         var label_rssi_peak;
56
57         var label_noise_cur;
58         var label_noise_avg;
59         var label_noise_peak;
60
61         var label_rate_cur;
62         var label_rate_avg;
63         var label_rate_peak;
64
65         var label_scale;
66
67
68         /* wait for SVG */
69         window.setTimeout(
70                 function() {
71                         var svg = document.getElementById('iwsvg');
72                         var svg2 = document.getElementById('iwsvg2');
73
74                         try {
75                                 G = svg.getSVGDocument
76                                         ? svg.getSVGDocument() : svg.contentDocument;
77                                 G2 = svg2.getSVGDocument
78                                         ? svg2.getSVGDocument() : svg2.contentDocument;
79                         }
80                         catch(e) {
81                                 G = document.embeds['iwsvg'].getSVGDocument();
82                                 G2 = document.embeds['iwsvg2'].getSVGDocument();
83                         }
84
85                         if (!G || !G2)
86                         {
87                                 window.setTimeout(arguments.callee, 1000);
88                         }
89                         else
90                         {
91                                 /* find sizes */
92                                 width       = svg.offsetWidth  - 2;
93                                 height      = svg.offsetHeight - 2;
94                                 data_wanted = Math.ceil(width / step);
95
96                                 /* prefill datasets */
97                                 for (var i = 0; i < data_wanted; i++)
98                                 {
99                                         data_rssi[i] = 0;
100                                         data_noise[i] = 0;
101                                         data_rate[i] = 0;
102                                 }
103
104                                 /* find svg elements */
105                                 line_rssi = G.getElementById('rssi');
106                                 line_noise = G.getElementById('noise');
107                                 line_rate = G2.getElementById('rate');
108
109                                 label_25 = G.getElementById('label_25');
110                                 label_50 = G.getElementById('label_50');
111                                 label_75 = G.getElementById('label_75');
112                                 label_25_2 = G2.getElementById('label_25');
113                                 label_50_2 = G2.getElementById('label_50');
114                                 label_75_2 = G2.getElementById('label_75');
115
116                                 label_rssi_cur  = document.getElementById('rssi_bw_cur');
117                                 label_rssi_avg  = document.getElementById('rssi_bw_avg');
118                                 label_rssi_peak = document.getElementById('rssi_bw_peak');
119
120                                 label_noise_cur  = document.getElementById('noise_bw_cur');
121                                 label_noise_avg  = document.getElementById('noise_bw_avg');
122                                 label_noise_peak = document.getElementById('noise_bw_peak');
123
124                                 label_rate_cur  = document.getElementById('rate_bw_cur');
125                                 label_rate_avg  = document.getElementById('rate_bw_avg');
126                                 label_rate_peak = document.getElementById('rate_bw_peak');
127
128                                 label_scale   = document.getElementById('scale');
129                                 label_scale_2 = document.getElementById('scale2');
130
131
132                                 /* plot horizontal time interval lines */
133                                 for (var i = width % (step * 60); i < width; i += step * 60)
134                                 {
135                                         var line = G.createElementNS('http://www.w3.org/2000/svg', 'line');
136                                                 line.setAttribute('x1', i);
137                                                 line.setAttribute('y1', 0);
138                                                 line.setAttribute('x2', i);
139                                                 line.setAttribute('y2', '100%');
140                                                 line.setAttribute('style', 'stroke:black;stroke-width:0.1');
141
142                                         var text = G.createElementNS('http://www.w3.org/2000/svg', 'text');
143                                                 text.setAttribute('x', i + 5);
144                                                 text.setAttribute('y', 15);
145                                                 text.setAttribute('style', 'fill:#999999; font-size:9pt');
146                                                 text.appendChild(G.createTextNode(Math.round((width - i) / step / 60) + 'm'));
147
148                                         label_25.parentNode.appendChild(line);
149                                         label_25.parentNode.appendChild(text);
150
151
152                                         var line2 = G2.createElementNS('http://www.w3.org/2000/svg', 'line');
153                                                 line2.setAttribute('x1', i);
154                                                 line2.setAttribute('y1', 0);
155                                                 line2.setAttribute('x2', i);
156                                                 line2.setAttribute('y2', '100%');
157                                                 line2.setAttribute('style', 'stroke:black;stroke-width:0.1');
158
159                                         var text2 = G2.createElementNS('http://www.w3.org/2000/svg', 'text');
160                                                 text2.setAttribute('x', i + 5);
161                                                 text2.setAttribute('y', 15);
162                                                 text2.setAttribute('style', 'fill:#999999; font-size:9pt');
163                                                 text2.appendChild(G.createTextNode(Math.round((width - i) / step / 60) + 'm'));
164
165                                         label_25_2.parentNode.appendChild(line2);
166                                         label_25_2.parentNode.appendChild(text2);
167                                 }
168
169                                 label_scale.innerHTML = String.format('<%:(%d minute window, %d second interval)%>', data_wanted / 60, 3);
170                                 label_scale_2.innerHTML = String.format('<%:(%d minute window, %d second interval)%>', data_wanted / 60, 3);
171
172                                 /* render datasets, start update interval */
173                                 XHR.poll(3, '<%=build_url("admin/status/realtime/wireless_status", curdev)%>', null,
174                                         function(x, data)
175                                         {
176                                                 var noise_floor = 255;
177                                                 var rate_floor = 60000;
178
179                                                 for (var i = 0; i < data.length; i++) {
180                                                         noise_floor = Math.min(noise_floor, data[i][NOISE]);
181                                                         rate_floor = Math.min(rate_floor, data[i][RATE]);
182                                                 }
183
184                                                 noise_floor -= 5;
185
186                                                 var data_max   = 0;
187                                                 var data_scale = 0;
188                                                 var data_max_2   = 0;
189                                                 var data_scale_2 = 0;
190
191                                                 var data_rssi_avg = 0;
192                                                 var data_noise_avg = 0;
193                                                 var data_rate_avg = 0;
194
195                                                 var data_rssi_peak = 0;
196                                                 var data_noise_peak = 0;
197                                                 var data_rate_peak = 0;
198
199                                                 for (var i = data_stamp ? 0 : 1; i < data.length; i++)
200                                                 {
201                                                         /* skip overlapping entries */
202                                                         if (data[i][TIME] <= data_stamp)
203                                                                 continue;
204
205                                                         data_rssi.push(data[i][RSSI] - noise_floor);
206                                                         data_noise.push(data[i][NOISE] - noise_floor);
207                                                         data_rate.push(Math.floor(data[i][RATE] / 1000));
208                                                 }
209
210                                                 /* cut off outdated entries */
211                                                 data_rssi = data_rssi.slice(data_rssi.length - data_wanted, data_rssi.length);
212                                                 data_noise = data_noise.slice(data_noise.length - data_wanted, data_noise.length);
213                                                 data_rate = data_rate.slice(data_rate.length - data_wanted, data_rate.length);
214
215                                                 /* find peak */
216                                                 for (var i = 0; i < data_rssi.length; i++)
217                                                 {
218                                                         data_max = Math.max(data_max, data_rssi[i]);
219                                                         data_max_2 = Math.max(data_max_2, data_rate[i]);
220
221                                                         data_rssi_peak = Math.max(data_rssi_peak, data_rssi[i]);
222                                                         data_noise_peak = Math.max(data_noise_peak, data_noise[i]);
223                                                         data_rate_peak = Math.max(data_rate_peak, data_rate[i]);
224
225                                                         if (i > 0)
226                                                         {
227                                                                 data_rssi_avg = (data_rssi_avg + data_rssi[i]) / 2;
228                                                                 data_noise_avg = (data_noise_avg + data_noise[i]) / 2;
229                                                                 data_rate_avg = (data_rate_avg + data_rate[i]) / 2;
230                                                         }
231                                                         else
232                                                         {
233                                                                 data_rssi_avg = data_rssi[i];
234                                                                 data_noise_avg = data_noise[i];
235                                                                 data_rate_avg = data_rate[i];
236                                                         }
237                                                 }
238
239                                                 /* remember current timestamp, calculate horizontal scale */
240                                                 data_stamp = data[data.length-1][TIME];
241                                                 data_scale = (height / (data_max * 1.1)).toFixed(1);
242                                                 data_scale_2 = (height / (data_max_2 * 1.1)).toFixed(1);
243
244                                                 /* plot data */
245                                                 var pt_rssi = '0,' + height;
246                                                 var pt_noise = '0,' + height;
247                                                 var pt_rate = '0,' + height;
248
249                                                 var y_rssi = 0;
250                                                 var y_noise = 0;
251                                                 var y_rate = 0;
252
253                                                 for (var i = 0; i < data_rssi.length; i++)
254                                                 {
255                                                         var x = i * step;
256
257                                                         y_rssi = height - Math.floor(data_rssi[i] * data_scale);
258                                                         y_noise = height - Math.floor(data_noise[i] * data_scale);
259                                                         y_rate = height - Math.floor(data_rate[i] * data_scale_2);
260
261                                                         y_rssi -= Math.floor(y_rssi % (1/data_scale));
262                                                         y_noise -= Math.floor(y_noise % (1/data_scale));
263
264                                                         pt_rssi += ' ' + x + ',' + y_rssi;
265                                                         pt_noise += ' ' + x + ',' + y_noise;
266                                                         pt_rate += ' ' + x + ',' + y_rate;
267                                                 }
268
269                                                 pt_rssi += ' ' + width + ',' + y_rssi + ' ' + width + ',' + height;
270                                                 pt_noise += ' ' + width + ',' + y_noise + ' ' + width + ',' + height;
271                                                 pt_rate += ' ' + width + ',' + y_rate + ' ' + width + ',' + height;
272
273                                                 line_rssi.setAttribute('points', pt_rssi);
274                                                 line_noise.setAttribute('points', pt_noise);
275                                                 line_rate.setAttribute('points', pt_rate);
276
277                                                 function wireless_label(dbm, noise)
278                                                 {
279                                                         if (noise)
280                                                                 return String.format("%d <%:dBm%> (SNR %d <%:dBm%>)", noise_floor + dbm - 255, dbm - noise);
281                                                         else
282                                                                 return String.format("%d <%:dBm%>", noise_floor + dbm - 255);
283                                                 }
284
285                                                 function rate_label(mbit)
286                                                 {
287                                                         return String.format("%d <%:Mbit/s%>", mbit);
288                                                 }
289
290                                                 label_25.firstChild.data = wireless_label(1.1 * 0.25 * data_max);
291                                                 label_50.firstChild.data = wireless_label(1.1 * 0.50 * data_max);
292                                                 label_75.firstChild.data = wireless_label(1.1 * 0.75 * data_max);
293
294                                                 label_25_2.firstChild.data = rate_label(1.1 * 0.25 * data_max_2);
295                                                 label_50_2.firstChild.data = rate_label(1.1 * 0.50 * data_max_2);
296                                                 label_75_2.firstChild.data = rate_label(1.1 * 0.75 * data_max_2);
297
298                                                 label_rssi_cur.innerHTML = wireless_label(data_rssi[data_rssi.length-1], data_noise[data_noise.length-1]).nobr();
299                                                 label_noise_cur.innerHTML = wireless_label(data_noise[data_noise.length-1]).nobr();
300
301                                                 label_rssi_avg.innerHTML = wireless_label(data_rssi_avg, data_noise_avg).nobr();
302                                                 label_noise_avg.innerHTML = wireless_label(data_noise_avg).nobr();
303
304                                                 label_rssi_peak.innerHTML = wireless_label(data_rssi_peak, data_noise_peak).nobr();
305                                                 label_noise_peak.innerHTML = wireless_label(data_noise_peak).nobr();
306
307                                                 label_rate_cur.innerHTML = rate_label(data_rate[data_rate.length-1]);
308                                                 label_rate_avg.innerHTML = rate_label(data_rate_avg);
309                                                 label_rate_peak.innerHTML = rate_label(data_rate_peak);
310                                         }
311                                 );
312                         }
313                 }, 1000
314         );
315 //]]></script>
316
317 <h2><a id="content" name="content"><%:Realtime Wireless%></a></h2>
318
319 <ul class="cbi-tabmenu">
320         <% for _, dev in ipairs(devices) do %>
321                 <li class="cbi-tab<%= dev == curdev and "" or "-disabled" %>"><a href="<%=pcdata(dev)%>"><%=pcdata(dev)%></a></li>
322         <% end %>
323 </ul>
324
325 <embed id="iwsvg" style="width:100%; height:300px; border:1px solid #000000; background-color:#FFFFFF" src="<%=resource%>/wireless.svg" />
326 <div style="text-align:right"><small id="scale">-</small></div>
327 <br />
328
329 <table style="width:100%; table-layout:fixed" cellspacing="5">
330         <tr>
331                 <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid blue"><%:Signal:%></strong></td>
332                 <td id="rssi_bw_cur">0 <%:dBm%></td>
333
334                 <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
335                 <td id="rssi_bw_avg">0 <%:dBm%></td>
336
337                 <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
338                 <td id="rssi_bw_peak">0 <%:dBm%></td>
339         </tr>
340         <tr>
341                 <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid red"><%:Noise:%></strong></td>
342                 <td id="noise_bw_cur">0 <%:dBm%></td>
343
344                 <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
345                 <td id="noise_bw_avg">0 <%:dBm%></td>
346
347                 <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
348                 <td id="noise_bw_peak">0 <%:dBm%></td>
349         </tr>
350 </table>
351
352 <br />
353
354 <embed id="iwsvg2" style="width:100%; height:300px; border:1px solid #000000; background-color:#FFFFFF" src="<%=resource%>/wifirate.svg" />
355 <div style="text-align:right"><small id="scale2">-</small></div>
356 <br />
357
358 <table style="width:100%; table-layout:fixed" cellspacing="5">
359         <tr>
360                 <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid green"><%:Phy Rate:%></strong></td>
361                 <td id="rate_bw_cur">0 MBit/s</td>
362
363                 <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
364                 <td id="rate_bw_avg">0 MBit/s</td>
365
366                 <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
367                 <td id="rate_bw_peak">0 MBit/s</td>
368         </tr>
369 </table>
370
371 <%+footer%>