speedmeter: Show speeds in Bit/s, kBit/s, MBit/s, ...
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Jan 2014 13:40:06 +0000 (14:40 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Jan 2014 13:40:06 +0000 (14:40 +0100)
Also clean up the dirty HTML code.

html/html/themes/ipfire-new/include/functions.pl
html/html/themes/ipfire-new/include/js/refreshInetInfo.js

index b644c073d5b5246aaf01ad206df6cf8942489275..a060ead7da7adf1adc281117f40ce025bc8af0a3 100644 (file)
@@ -73,13 +73,19 @@ sub showmenu() {
                &showsubmenu($submenus) if ($submenus);
                print "</li>";
        }
+       print "</ul>";
+
        if ($settings{'SPEED'} ne 'off') {
-               print"<div id='traffic'>";
-               print"<table><tr><td style='font-weight: bold;'>Traffic: &nbsp;</td>";
-               print"<td id='bandwidthCalculationContainer'>In <span id='rx_kbs'></span> &nbsp; Out <span id='tx_kbs'></span></td>";
-               print"</tr></table>";
-               print '</ul></div></div>';
+               print <<EOF;
+                       <div id='traffic'>
+                               <strong>Traffic:</strong>
+                               In  <span id='rx_kbs'>--.-- Bit/s</span> &nbsp;
+                               Out <span id='tx_kbs'>--.-- Bit/s</span>
+                       </div>
+EOF
        }
+
+       print "</div>";
 }
 
 ###############################################################################
index ed4a966b402d74903958b8280258a3e07a396270..f17b50a9a72ad4e357389495271f698e1ed1b35b 100644 (file)
@@ -20,30 +20,27 @@ $(document).ready(function(){
 function refreshInetInfo() {
        $.ajax({
                url: '/cgi-bin/speed.cgi',
-               success: function(xml){
-
+               success: function(xml) {
                        t_current = new Date();
                        var t_diff = t_current - t_last;
 
-                       rxb_current = $("rxb",xml).text();
+                       rxb_current = $("rxb", xml).text();
                        var rxb_diff = rxb_current - rxb_last;
                        rxb_last = rxb_current;
 
-                       var rx_kbs = rxb_diff/t_diff;
-                       rx_kbs = Math.round(rx_kbs*10)/10;
+                       var rx_bits = rxb_diff * 1024 / t_diff;
+                       var rx_fmt = format_bytes(rx_bits);
 
-                       txb_current = $("txb",xml).text();
+                       txb_current = $("txb", xml).text();
                        var txb_diff = txb_current - txb_last;
                        txb_last = txb_current;
 
-                       var tx_kbs = txb_diff/t_diff;
-                       tx_kbs = Math.round(tx_kbs*10)/10;
+                       var tx_bits = txb_diff * 1024 / t_diff;
+                       var tx_fmt = format_bytes(tx_bits);
 
                        if (t_last != 0) {
-                               $("#rx_kbs").text(rx_kbs + ' kb/s');
-                               $("#tx_kbs").text(tx_kbs + ' kb/s');
-                               if ($("#bandwidthCalculationContainer").css('display') == 'none')
-                                       $("#bandwidthCalculationContainer").css('display','block');
+                               $("#rx_kbs").text(rx_fmt);
+                               $("#tx_kbs").text(tx_fmt);
                        }
 
                        t_last = t_current;
@@ -52,3 +49,21 @@ function refreshInetInfo() {
 
        window.setTimeout("refreshInetInfo()", 2000);
 }
+
+function format_bytes(bytes) {
+       var units = ["Bit/s", "kBit/s", "MBit/s", "GBit/s", "TBit/s"];
+
+       var unit = units[0];
+       for (var i = 1; i < units.length; i++) {
+               if (bytes < 1024)
+                       break;
+
+               unit = units[i];
+               bytes /= 1024;
+       }
+
+       // Round the output.
+       bytes = bytes.toFixed(2);
+
+       return bytes + " " + unit;
+}