bandwidth.htm 8.2 KB

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