From 90a32fa60ce3e58a30ec46e624da0597571a0347 Mon Sep 17 00:00:00 2001 From: Stephan Schwaer Date: Thu, 28 May 2015 18:31:27 +0200 Subject: Added upload rate to the server statistics. --- static/status-dnbd3.html | 70 +++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/static/status-dnbd3.html b/static/status-dnbd3.html index 1ac6155..645447f 100644 --- a/static/status-dnbd3.html +++ b/static/status-dnbd3.html @@ -29,7 +29,7 @@ var colorList = d3.scale.category20(); // IDs need to begin with a letter. function makeId(prefix, text) { - return prefix + text.replace(/\./g, "-"); + return prefix + text.replace(/\./g, "-"); } function myGraph(el) { @@ -305,6 +305,7 @@ setInterval( function() { var g = data.graph; var stats = data.servers; updateGraph(g, data); + // updateTrafficGraph has to be called before updateTextStatistics to populate servers updateTrafficGraph(stats, data); updateTextStatistics(stats); lastTime = data.timestamp; @@ -326,37 +327,53 @@ function updateGraph(g, data){ } } + +// Convert bytes to GiB or TiB and return a string in form "10,23 GiB" +function bytesToString( bytes ) { + var convertedValue; + var unit; + if (bytes > 1099511627776 ) { + convertedValue = Math.round( (bytes / 1099511627776) * 100 ) / 100 ; + unit = " TiB"; + } else if (bytes > 1073741824 ) { + convertedValue = Math.round( (bytes / 1073741824) * 100 ) / 100 ; + unit = " GiB"; + } else if (bytes > 1048576 ) { + convertedValue = Math.round( (bytes / 1048576) * 100 ) / 100 ; + unit = " MiB"; + } else if ( bytes > 1024 ) { + convertedValue = Math.round( (bytes / 1024) * 100 ) / 100 ; +// convertedValue = Math.round( ( convertedValue / 1024 ) * 100 ) / 100; + unit = " KiB"; + } else { + convertedValue = Math.round(bytes); + unit = " B"; + } + return convertedValue + unit; +} + // Update data of the statistics divs function updateTextStatistics(stats){ if (stats) { for (var i = 0; i < stats.length; ++i) { - var server = document.getElementById(makeId("b", stats[i].address)); - if (!server){ + var divId = makeId("b", stats[i].address); + var server = $('#' + divId); + if (server.length == 0){ $("#statistics").append("
"); - server = document.getElementById(makeId("b", stats[i].address)); - - } - var sent = Math.floor(stats[i].bytesSent / Math.pow(1024, 3)); - var received = Math.floor(stats[i].bytesReceived / Math.pow(1024, 3)); - var unitSent = " GiB"; - var unitReceived = " GiB"; - if (sent > 1024) { - sent = Math.round((sent/1024)*100)/100; - unitSent = " TiB"; - } - if (received > 1024) { - received = Math.round((received/1024)*100)/100; - unitReceived = " TiB"; + server = $("#" + divId); } + var upload = servers[stats[i].address].uploadRate; + upload = upload ? upload : 0; // Generate the HTML string - server.innerHTML = "

Server: " + stats[i].address + "
" + server.html( "

Server: " + stats[i].address + "
" + "Number of clients: " + stats[i].clientCount + "
" + "uptime: " + Math.floor(stats[i].uptime / (3600 * 24)) + "d " + Math.floor(stats[i].uptime / 3600) % 24 + "h " + Math.floor((stats[i].uptime) / 60) % 60 + "min" + "
" - + "Sent: " + sent + unitSent + "
" - + "Received: " + received + unitReceived + "

"; + + "Sent: " + bytesToString(stats[i].bytesSent) + "
" + + "Received: " + bytesToString(stats[i].bytesReceived) + "
" + + "Upload: " + bytesToString(servers[stats[i].address].uploadRate) + "/s" + "

"); } } } @@ -368,19 +385,24 @@ function updateTrafficGraph(stats, data){ for (var i = 0; i < stats.length; ++i) { var server = servers[stats[i].address]; if (!server) { - servers[stats[i].address] = server = { 'lastUptime': 0, 'lastSent': 0, 'line': new TimeSeries(), 'index': serverCount++ }; + servers[stats[i].address] = server = { 'lastUptime': 0, 'lastSent': 0, 'line': new TimeSeries(), 'index': serverCount++ , 'uploadRate': 0, 'downloadRate': 0} server.color = colorList(stats[i].address); smoothie.addTimeSeries(server['line'], {lineWidth:2, strokeStyle: server.color}); -// console.log("Added"); +// console.log("Added"); } // Server seems to have rebootet, redo values but add no point to chart. if (server['lastUptime'] > stats[i].uptime) { server['lastUptime'] = 0; } - // Add points to graph + // Add points to graph and set the upload rate for the statistics if (server['lastUptime'] != 0) { - server['line'].append(new Date().getTime(), (stats[i].bytesSent - server['lastSent'])/1000/(data.timestamp - lastTime)); + // Upload rate in bytes/s + server['uploadRate'] = (stats[i].bytesSent - server['lastSent'])/(data.timestamp - lastTime) * 1000; + // Rate in MiB/s + server['line'].append(new Date().getTime(), server['uploadRate']/( 1024 * 1024)); + } else { + server['uploadRate'] = 0; } server['lastUptime'] = stats[i].uptime; -- cgit v1.2.3-55-g7522