modules/admin-full: add DSL status to index page (#620)
[project/luci.git] / modules / admin-full / luasrc / view / admin_status / bandwidth.htm
1 <%#
2 LuCI - Lua Configuration Interface
3 Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9         http://www.apache.org/licenses/LICENSE-2.0
10
11 $Id$
12
13 -%>
14
15 <%-
16         local ntm = require "luci.model.network".init()
17
18         local dev
19         local devices = { }
20         for _, dev in luci.util.vspairs(luci.sys.net.devices()) do
21                 if dev ~= "lo" and not ntm:ignore_interface(dev) then
22                         devices[#devices+1] = dev
23                 end
24         end
25
26         local curdev = luci.dispatcher.context.requestpath
27               curdev = curdev[#curdev] ~= "bandwidth" and curdev[#curdev] or devices[1]
28 -%>
29
30 <%+header%>
31
32 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
33 <script type="text/javascript">//<![CDATA[
34         var bwxhr = new XHR();
35
36         var G;
37         var TIME = 0;
38         var RXB  = 1;
39         var RXP  = 2;
40         var TXB  = 3;
41         var TXP  = 4;
42
43         var width  = 760;
44         var height = 300;
45         var step   = 5;
46
47         var data_wanted = Math.floor(width / step);
48         var data_fill   = 0;
49         var data_stamp  = 0;
50
51         var data_rx = [ ];
52         var data_tx = [ ];
53
54         var line_rx;
55         var line_tx;
56
57         var label_25;
58         var label_50;
59         var label_75;
60
61         var label_rx_cur;
62         var label_rx_avg;
63         var label_rx_peak;
64
65         var label_tx_cur;
66         var label_tx_avg;
67         var label_tx_peak;
68
69         var label_scale;
70
71
72         function bandwidth_label(bytes, br)
73         {
74                 var uby = '<%:kB/s%>';
75                 var kby = (bytes / 1024);
76
77                 if (kby >= 1024)
78                 {
79                         uby = '<%:MB/s%>';
80                         kby = kby / 1024;
81                 }
82
83                 var ubi = '<%:kbit/s%>';
84                 var kbi = (bytes * 8 / 1024);
85
86                 if (kbi >= 1024)
87                 {
88                         ubi = '<%:Mbit/s%>';
89                         kbi = kbi / 1024;
90                 }
91
92                 return String.format("%f %s%s(%f %s)",
93                         kbi.toFixed(2), ubi,
94                         br ? '<br />' : ' ',
95                         kby.toFixed(2), uby
96                 );
97         }
98
99         /* wait for SVG */
100         window.setTimeout(
101                 function() {
102                         var svg = document.getElementById('bwsvg');
103
104                         try {
105                                 G = svg.getSVGDocument
106                                         ? svg.getSVGDocument() : svg.contentDocument;
107                         }
108                         catch(e) {
109                                 G = document.embeds['bwsvg'].getSVGDocument();
110                         }
111
112                         if (!G)
113                         {
114                                 window.setTimeout(arguments.callee, 1000);
115                         }
116                         else
117                         {
118                                 /* find sizes */
119                                 width       = svg.offsetWidth  - 2;
120                                 height      = svg.offsetHeight - 2;
121                                 data_wanted = Math.ceil(width / step);
122
123                                 /* prefill datasets */
124                                 for (var i = 0; i < data_wanted; i++)
125                                 {
126                                         data_rx[i] = 0;
127                                         data_tx[i] = 0;
128                                 }
129
130                                 /* find svg elements */
131                                 line_rx = G.getElementById('rx');
132                                 line_tx = G.getElementById('tx');
133
134                                 label_25 = G.getElementById('label_25');
135                                 label_50 = G.getElementById('label_50');
136                                 label_75 = G.getElementById('label_75');
137
138                                 label_rx_cur  = document.getElementById('rx_bw_cur');
139                                 label_rx_avg  = document.getElementById('rx_bw_avg');
140                                 label_rx_peak = document.getElementById('rx_bw_peak');
141
142                                 label_tx_cur  = document.getElementById('tx_bw_cur');
143                                 label_tx_avg  = document.getElementById('tx_bw_avg');
144                                 label_tx_peak = document.getElementById('tx_bw_peak');
145
146                                 label_scale   = document.getElementById('scale');
147
148
149                                 /* plot horizontal time interval lines */
150                                 for (var i = width % (step * 60); i < width; i += step * 60)
151                                 {
152                                         var line = G.createElementNS('http://www.w3.org/2000/svg', 'line');
153                                                 line.setAttribute('x1', i);
154                                                 line.setAttribute('y1', 0);
155                                                 line.setAttribute('x2', i);
156                                                 line.setAttribute('y2', '100%');
157                                                 line.setAttribute('style', 'stroke:black;stroke-width:0.1');
158
159                                         var text = G.createElementNS('http://www.w3.org/2000/svg', 'text');
160                                                 text.setAttribute('x', i + 5);
161                                                 text.setAttribute('y', 15);
162                                                 text.setAttribute('style', 'fill:#999999; font-size:9pt');
163                                                 text.appendChild(G.createTextNode(Math.round((width - i) / step / 60) + 'm'));
164
165                                         label_25.parentNode.appendChild(line);
166                                         label_25.parentNode.appendChild(text);
167                                 }
168
169                                 label_scale.innerHTML = String.format('<%:(%d minute window, %d second interval)%>', data_wanted / 60, 3);
170
171                                 /* render datasets, start update interval */
172                                 XHR.poll(3, '<%=build_url("admin/status/realtime/bandwidth_status", curdev)%>', null,
173                                         function(x, data)
174                                         {
175                                                 var data_max   = 0;
176                                                 var data_scale = 0;
177
178                                                 var data_rx_avg = 0;
179                                                 var data_tx_avg = 0;
180
181                                                 var data_rx_peak = 0;
182                                                 var data_tx_peak = 0;
183
184                                                 for (var i = data_stamp ? 0 : 1; i < data.length; i++)
185                                                 {
186                                                         /* skip overlapping entries */
187                                                         if (data[i][TIME] <= data_stamp)
188                                                                 continue;
189
190                                                         /* normalize difference against time interval */
191                                                         if (i > 0)
192                                                         {
193                                                                 var time_delta = data[i][TIME] - data[i-1][TIME];
194                                                                 if (time_delta)
195                                                                 {
196                                                                         data_rx.push((data[i][RXB] - data[i-1][RXB]) / time_delta);
197                                                                         data_tx.push((data[i][TXB] - data[i-1][TXB]) / time_delta);
198                                                                 }
199                                                         }
200                                                 }
201
202                                                 /* cut off outdated entries */
203                                                 data_rx = data_rx.slice(data_rx.length - data_wanted, data_rx.length);
204                                                 data_tx = data_tx.slice(data_tx.length - data_wanted, data_tx.length);
205
206                                                 /* find peak */
207                                                 for (var i = 0; i < data_rx.length; i++)
208                                                 {
209                                                         data_max = Math.max(data_max, data_rx[i]);
210                                                         data_max = Math.max(data_max, data_tx[i]);
211
212                                                         data_rx_peak = Math.max(data_rx_peak, data_rx[i]);
213                                                         data_tx_peak = Math.max(data_tx_peak, data_tx[i]);
214
215                                                         if (i > 0)
216                                                         {
217                                                                 data_rx_avg = (data_rx_avg + data_rx[i]) / 2;
218                                                                 data_tx_avg = (data_tx_avg + data_tx[i]) / 2;
219                                                         }
220                                                         else
221                                                         {
222                                                                 data_rx_avg = data_rx[i];
223                                                                 data_tx_avg = data_tx[i];
224                                                         }
225                                                 }
226
227                                                 /* remember current timestamp, calculate horizontal scale */
228                                                 data_stamp = data[data.length-1][TIME];
229                                                 data_scale = height / (data_max * 1.1);
230
231
232                                                 /* plot data */
233                                                 var pt_rx = '0,' + height;
234                                                 var pt_tx = '0,' + height;
235
236                                                 var y_rx = 0;
237                                                 var y_tx = 0;
238
239                                                 for (var i = 0; i < data_rx.length; i++)
240                                                 {
241                                                         var x = i * step;
242
243                                                         y_rx = height - Math.floor(data_rx[i] * data_scale);
244                                                         y_tx = height - Math.floor(data_tx[i] * data_scale);
245
246                                                         pt_rx += ' ' + x + ',' + y_rx;
247                                                         pt_tx += ' ' + x + ',' + y_tx;
248                                                 }
249
250                                                 pt_rx += ' ' + width + ',' + y_rx + ' ' + width + ',' + height;
251                                                 pt_tx += ' ' + width + ',' + y_tx + ' ' + width + ',' + height;
252
253
254                                                 line_rx.setAttribute('points', pt_rx);
255                                                 line_tx.setAttribute('points', pt_tx);
256
257                                                 label_25.firstChild.data = bandwidth_label(1.1 * 0.25 * data_max);
258                                                 label_50.firstChild.data = bandwidth_label(1.1 * 0.50 * data_max);
259                                                 label_75.firstChild.data = bandwidth_label(1.1 * 0.75 * data_max);
260
261                                                 label_rx_cur.innerHTML = bandwidth_label(data_rx[data_rx.length-1], true);
262                                                 label_tx_cur.innerHTML = bandwidth_label(data_tx[data_tx.length-1], true);
263
264                                                 label_rx_avg.innerHTML = bandwidth_label(data_rx_avg, true);
265                                                 label_tx_avg.innerHTML = bandwidth_label(data_tx_avg, true);
266
267                                                 label_rx_peak.innerHTML = bandwidth_label(data_rx_peak, true);
268                                                 label_tx_peak.innerHTML = bandwidth_label(data_tx_peak, true);
269                                         }
270                                 );
271                         }
272                 }, 1000
273         );
274 //]]></script>
275
276 <h2><a id="content" name="content"><%:Realtime Traffic%></a></h2>
277
278 <ul class="cbi-tabmenu">
279         <% for _, dev in ipairs(devices) do %>
280                 <li class="cbi-tab<%= dev == curdev and "" or "-disabled" %>"><a href="<%=pcdata(dev)%>"><%=pcdata(dev)%></a></li>
281         <% end %>
282 </ul>
283
284 <embed id="bwsvg" style="width:100%; height:300px; border:1px solid #000000; background-color:#FFFFFF" src="<%=resource%>/bandwidth.svg" />
285 <div style="text-align:right"><small id="scale">-</small></div>
286 <br />
287
288 <table style="width:100%; table-layout:fixed" cellspacing="5">
289         <tr>
290                 <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid blue"><%:Inbound:%></strong></td>
291                 <td id="rx_bw_cur">0 <%:kbit/s%><br />(0 <%:kB/s%>)</td>
292
293                 <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
294                 <td id="rx_bw_avg">0 <%:kbit/s%><br />(0 <%:kB/s%>)</td>
295
296                 <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
297                 <td id="rx_bw_peak">0 <%:kbit/s%><br />(0 <%:kB/s%>)</td>
298         </tr>
299         <tr>
300                 <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid green"><%:Outbound:%></strong></td>
301                 <td id="tx_bw_cur">0 <%:kbit/s%><br />(0 <%:kB/s%>)</td>
302
303                 <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
304                 <td id="tx_bw_avg">0 <%:kbit/s%><br />(0 <%:kB/s%>)</td>
305
306                 <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
307                 <td id="tx_bw_peak">0 <%:kbit/s%><br />(0 <%:kB/s%>)</td>
308         </tr>
309 </table>
310
311 <%+footer%>