package net.i2p.router.web;
import java.io.IOException;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import net.i2p.data.DataHelper;
import net.i2p.stat.Rate;
public class GraphHelper extends HelperBase {
private int _periodCount;
private boolean _showEvents;
private int _width;
private int _height;
private int _refreshDelaySeconds;
private static final String PROP_X = "routerconsole.graphX";
private static final String PROP_Y = "routerconsole.graphY";
private static final String PROP_REFRESH = "routerconsole.graphRefresh";
private static final String PROP_PERIODS = "routerconsole.graphPeriods";
private static final String PROP_EVENTS = "routerconsole.graphEvents";
private static final int DEFAULT_X = 250;
private static final int DEFAULT_Y = 100;
private static final int DEFAULT_REFRESH = 60;
private static final int DEFAULT_PERIODS = 60;
static final int MAX_X = 2048;
static final int MAX_Y = 1024;
private static final int MIN_REFRESH = 15;
public GraphHelper() {
}
/** set the defaults after we have a context */
@Override
public void setContextId(String contextId) {
super.setContextId(contextId);
_width = _context.getProperty(PROP_X, DEFAULT_X);
_height = _context.getProperty(PROP_Y, DEFAULT_Y);
_periodCount = _context.getProperty(PROP_PERIODS, DEFAULT_PERIODS);
_refreshDelaySeconds = _context.getProperty(PROP_REFRESH, DEFAULT_REFRESH);
_showEvents = Boolean.valueOf(_context.getProperty(PROP_EVENTS)).booleanValue();
}
public void setPeriodCount(String str) {
try { _periodCount = Integer.parseInt(str); } catch (NumberFormatException nfe) {}
}
public void setShowEvents(boolean b) { _showEvents = b; }
public void setHeight(String str) {
try { _height = Math.min(Integer.parseInt(str), MAX_Y); } catch (NumberFormatException nfe) {}
}
public void setWidth(String str) {
try { _width = Math.min(Integer.parseInt(str), MAX_X); } catch (NumberFormatException nfe) {}
}
public void setRefreshDelay(String str) {
try { _refreshDelaySeconds = Math.max(Integer.parseInt(str), MIN_REFRESH); } catch (NumberFormatException nfe) {}
}
public String getImages() {
try {
List listeners = StatSummarizer.instance().getListeners();
TreeSet ordered = new TreeSet(new AlphaComparator());
ordered.addAll(listeners);
// go to some trouble to see if we have the data for the combined bw graph
boolean hasTx = false;
boolean hasRx = false;
for (Iterator iter = ordered.iterator(); iter.hasNext(); ) {
SummaryListener lsnr = (SummaryListener)iter.next();
String title = lsnr.getRate().getRateStat().getName();
if (title.equals("bw.sendRate")) hasTx = true;
else if (title.equals("bw.recvRate")) hasRx = true;
}
if (hasTx && hasRx && !_showEvents) {
_out.write("");
String title = _("Combined bandwidth graph");
_out.write("\n");
}
for (Iterator iter = ordered.iterator(); iter.hasNext(); ) {
SummaryListener lsnr = (SummaryListener)iter.next();
Rate r = lsnr.getRate();
// e.g. "statname for 60m"
String title = _("{0} for {1}", r.getRateStat().getName(), DataHelper.formatDuration2(_periodCount * r.getPeriod()));
_out.write("");
_out.write("
\n");
}
if (_refreshDelaySeconds > 0)
// shorten the refresh by 3 seconds so we beat the iframe
_out.write("\n");
} catch (IOException ioe) {
ioe.printStackTrace();
}
return "";
}
public String getForm() {
saveSettings();
try {
_out.write("