rate.htm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <%#
  2. Copyright 2018 Rosy Song <rosysong@rosinson.com>
  3. Licensed to the public under the Apache License 2.0.
  4. -%>
  5. <%+header%>
  6. <script type="text/javascript">//<![CDATA[
  7. var bwxhr = new XHR();
  8. var RC = { };
  9. var em = 0;
  10. var ec = 1;
  11. var rate_table_dl;
  12. var rate_table_ul;
  13. function init_bytes(rl, ra) {
  14. var bytes_pre;
  15. var obj = { };
  16. obj.chain = rl.chain;
  17. obj.ipaddr = rl.expr[em].match.right;
  18. obj.bytes = rl.expr[ec].counter.bytes;
  19. obj.packets = rl.expr[ec].counter.packets;
  20. obj.rate = 0;
  21. if (RC[obj.chain] && RC[obj.chain][obj.ipaddr])
  22. bytes_pre = RC[obj.chain][obj.ipaddr];
  23. else
  24. bytes_pre = 0;
  25. obj.rate = (bytes_pre > 0) ? (obj.bytes - bytes_pre) / 3: 0;
  26. if (!RC[obj.chain])
  27. RC[obj.chain] = { };
  28. RC[obj.chain][obj.ipaddr] = obj.bytes;
  29. if (!ra[obj.chain])
  30. ra[obj.chain] = [ ];
  31. ra[obj.chain].push(obj);
  32. } /* function init_bytes(rl, ra) */
  33. function bytes_label(bytes) {
  34. var uby = '<%:kB%>';
  35. var kby = (bytes / 1024);
  36. if (kby > 1024) {
  37. uby = '<%:MB%>';
  38. kby = (kby / 1024);
  39. }
  40. return String.format("%f %s", kby.toFixed(2), uby);
  41. }
  42. function print_table(tbl, rs, ra) {
  43. ra.sort(function(a, b) { return b.rate - a.rate });
  44. for (var i = 0; i < ra.length; i++) {
  45. rs.push([
  46. ra[i].ipaddr,
  47. bytes_label(ra[i].rate) + '/s',
  48. bytes_label(ra[i].bytes),
  49. '%s Pkts.'.format(ra[i].packets),
  50. ]);
  51. }
  52. cbi_update_table(tbl, rs, '<em><%:No information available%></em>');
  53. } /* function print_table(tbl, ra) */
  54. /* wait for SVG */
  55. window.setTimeout(
  56. function() {
  57. if (!RC)
  58. {
  59. window.setTimeout(arguments.callee, 1000);
  60. }
  61. else
  62. {
  63. rate_table_dl = document.getElementById('rate_table_dl');
  64. rate_table_ul = document.getElementById('rate_table_ul');
  65. /* render datasets, start update interval */
  66. XHR.poll(3, '<%=build_url("admin/status/realtime/rate_status")%>', null,
  67. function(x, json)
  68. {
  69. var RA = {};
  70. var rows_dl = [];
  71. var rows_ul = [];
  72. var rules = json.nftables;
  73. for (var i = 0; i < rules.length; i++)
  74. {
  75. if (!rules[i].rule)
  76. continue;
  77. if (rules[i].rule.table != 'nft-qos-monitor')
  78. continue;
  79. var rl = rules[i].rule;
  80. switch (rl.chain)
  81. {
  82. case 'download':
  83. case 'upload': init_bytes(rl, RA); break;
  84. }
  85. } /* for (var i = 0; i < rules.length; i++) */
  86. /* display the result */
  87. if (RA.download) {
  88. while (rate_table_dl.firstElementChild !== rate_table_dl.lastElementChild)
  89. rate_table_dl.removeChild(rate_table_dl.lastElementChild);
  90. print_table(rate_table_dl, rows_dl, RA.download);
  91. }
  92. if (RA.upload) {
  93. while (rate_table_ul.firstElementChild !== rate_table_ul.lastElementChild)
  94. rate_table_ul.removeChild(rate_table_ul.lastElementChild);
  95. print_table(rate_table_ul, rows_ul, RA.upload);
  96. }
  97. } /* function(x, json) */
  98. ); /* XHR.poll() */
  99. XHR.run();
  100. }
  101. }, 1000
  102. );
  103. //]]></script>
  104. <h2 name="content"><%:Realtime Rate%></h2>
  105. <div class="cbi-map-descr"><%:This page gives an overview over currently download/upload rate.%></div>
  106. <fieldset class="cbi-section" id="cbi-table-table">
  107. <legend><%:Realtime Download Rate%></legend>
  108. <div class="cbi-section-node">
  109. <div class="table" id="rate_table_dl">
  110. <div class="tr table-titles">
  111. <div class="th col-2 hide-xs"><%:IP Address%></div>
  112. <div class="th col-2"><%:Download Rate%></div>
  113. <div class="th col-7"><%:Bytes Total%></div>
  114. <div class="th col-7"><%:Packets Total%></div>
  115. </div>
  116. <div class="tr placeholder">
  117. <div class="td">
  118. <em><%:Collecting data...%></em>
  119. </div>
  120. </div>
  121. </div>
  122. </div>
  123. </fieldset>
  124. <fieldset class="cbi-section" id="cbi-table-table">
  125. <legend><%:Realtime Upload Rate%></legend>
  126. <div class="cbi-section-node">
  127. <div class="table" id="rate_table_ul">
  128. <div class="tr table-titles">
  129. <div class="th col-2 hide-xs"><%:IP Address%></div>
  130. <div class="th col-2"><%:Upload Rate%></div>
  131. <div class="th col-7"><%:Bytes Total%></div>
  132. <div class="th col-7"><%:Packets Total%></div>
  133. </div>
  134. <div class="tr placeholder">
  135. <div class="td">
  136. <em><%:Collecting data...%></em>
  137. </div>
  138. </div>
  139. </div>
  140. </div>
  141. </fieldset>
  142. <%+footer%>