Move SummaryHelper.getTransferred() to DataHelper, rename to formatSize(), use on tunnels.jsp

This commit is contained in:
zzz
2009-01-22 04:02:41 +00:00
parent c620420a6f
commit 10e2c3832d
3 changed files with 42 additions and 47 deletions

View File

@ -244,7 +244,7 @@ public class SummaryHelper {
*/
public String getInboundSecondKBps() {
if (_context == null)
return "0.0";
return "0";
double kbps = _context.bandwidthLimiter().getReceiveBps()/1024d;
DecimalFormat fmt = new DecimalFormat("##0.00");
return fmt.format(kbps);
@ -256,7 +256,7 @@ public class SummaryHelper {
*/
public String getOutboundSecondKBps() {
if (_context == null)
return "0.0";
return "0";
double kbps = _context.bandwidthLimiter().getSendBps()/1024d;
DecimalFormat fmt = new DecimalFormat("##0.00");
return fmt.format(kbps);
@ -269,10 +269,10 @@ public class SummaryHelper {
*/
public String getInboundFiveMinuteKBps() {
if (_context == null)
return "0.0";
return "0";
RateStat receiveRate = _context.statManager().getRate("bw.recvRate");
if (receiveRate == null) return "0.0";
if (receiveRate == null) return "0";
Rate rate = receiveRate.getRate(5*60*1000);
double kbps = rate.getAverageValue()/1024;
DecimalFormat fmt = new DecimalFormat("##0.00");
@ -286,10 +286,10 @@ public class SummaryHelper {
*/
public String getOutboundFiveMinuteKBps() {
if (_context == null)
return "0.0";
return "0";
RateStat receiveRate = _context.statManager().getRate("bw.sendRate");
if (receiveRate == null) return "0.0";
if (receiveRate == null) return "0";
Rate rate = receiveRate.getRate(5*60*1000);
double kbps = rate.getAverageValue()/1024;
DecimalFormat fmt = new DecimalFormat("##0.00");
@ -303,10 +303,10 @@ public class SummaryHelper {
*/
public String getInboundLifetimeKBps() {
if (_context == null)
return "0.0";
return "0";
RateStat receiveRate = _context.statManager().getRate("bw.recvRate");
if (receiveRate == null) return "0.0";
if (receiveRate == null) return "0";
double kbps = receiveRate.getLifetimeAverageValue()/1024;
DecimalFormat fmt = new DecimalFormat("##0.00");
return fmt.format(kbps);
@ -319,10 +319,10 @@ public class SummaryHelper {
*/
public String getOutboundLifetimeKBps() {
if (_context == null)
return "0.0";
return "0";
RateStat sendRate = _context.statManager().getRate("bw.sendRate");
if (sendRate == null) return "0.0";
if (sendRate == null) return "0";
double kbps = sendRate.getLifetimeAverageValue()/1024;
DecimalFormat fmt = new DecimalFormat("##0.00");
return fmt.format(kbps);
@ -335,11 +335,11 @@ public class SummaryHelper {
*/
public String getInboundTransferred() {
if (_context == null)
return "0.0";
return "0";
long received = _context.bandwidthLimiter().getTotalAllocatedInboundBytes();
return getTransferred(received);
return DataHelper.formatSize(received) + 'B';
}
/**
@ -349,40 +349,10 @@ public class SummaryHelper {
*/
public String getOutboundTransferred() {
if (_context == null)
return "0.0";
return "0";
long sent = _context.bandwidthLimiter().getTotalAllocatedOutboundBytes();
return getTransferred(sent);
}
private static String getTransferred(long bytes) {
double val = bytes;
int scale = 0;
if (bytes > 1024*1024*1024) {
// gigs transferred
scale = 3;
val /= (double)(1024*1024*1024);
} else if (bytes > 1024*1024) {
// megs transferred
scale = 2;
val /= (double)(1024*1024);
} else if (bytes > 1024) {
// kbytes transferred
scale = 1;
val /= (double)1024;
} else {
scale = 0;
}
DecimalFormat fmt = new DecimalFormat("##0.00");
String str = fmt.format(val);
switch (scale) {
case 1: return str + "KB";
case 2: return str + "MB";
case 3: return str + "GB";
default: return bytes + "bytes";
}
return DataHelper.formatSize(sent) + 'B';
}
/**

View File

@ -25,6 +25,7 @@ import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -235,7 +236,7 @@ public class DataHelper {
int split = line.indexOf('=');
if (split <= 0) continue;
String key = line.substring(0, split);
String val = line.substring(split+1);
String val = line.substring(split+1); //.trim() ??????????????
// Unescape line breaks after loading.
// Remember: "\" needs escaping both for regex and string.
val = val.replaceAll("\\\\r","\r");
@ -842,6 +843,29 @@ public class DataHelper {
}
}
/**
* Caller should append 'B' or 'b' as appropriate
*/
public static String formatSize(long bytes) {
double val = bytes;
int scale = 0;
while (val >= 1024) {
scale++;
val /= 1024;
}
DecimalFormat fmt = new DecimalFormat("##0.00");
String str = fmt.format(val);
switch (scale) {
case 1: return str + "K";
case 2: return str + "M";
case 3: return str + "G";
case 4: return str + "T";
default: return bytes + "";
}
}
/**
* Strip out any HTML (simply removing any less than / greater than symbols)
*/

View File

@ -507,7 +507,7 @@ public class TunnelPoolManager implements TunnelManagerFacade {
}
out.write("</table>\n");
out.write("Inactive participating tunnels: " + inactive + "<br />\n");
out.write("Lifetime bandwidth usage: " + processed + "KB<br />\n");
out.write("Lifetime bandwidth usage: " + DataHelper.formatSize(processed*1024) + "B<br />\n");
}
class TunnelComparator implements Comparator {
@ -577,7 +577,8 @@ public class TunnelPoolManager implements TunnelManagerFacade {
}
if (live <= 0)
out.write("<b>No tunnels, waiting for the grace period to end</b><br />\n");
out.write("Lifetime bandwidth usage: " + processedIn + "KB in, " + processedOut + "KB out<br />");
out.write("Lifetime bandwidth usage: " + DataHelper.formatSize(processedIn*1024) + "B in, " +
DataHelper.formatSize(processedOut*1024) + "B out<br />");
}
private String getCapacity(Hash peer) {