2006-03-19 00:23:23 +00:00
|
|
|
package net.i2p.router.web;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2008-07-16 13:42:54 +00:00
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.TreeSet;
|
2006-03-19 00:23:23 +00:00
|
|
|
|
|
|
|
import net.i2p.data.DataHelper;
|
2008-07-16 13:42:54 +00:00
|
|
|
import net.i2p.stat.Rate;
|
2006-03-19 00:23:23 +00:00
|
|
|
|
2009-01-29 02:16:18 +00:00
|
|
|
public class GraphHelper extends HelperBase {
|
2006-03-19 00:23:23 +00:00
|
|
|
private int _periodCount;
|
|
|
|
private boolean _showEvents;
|
|
|
|
private int _width;
|
|
|
|
private int _height;
|
|
|
|
private int _refreshDelaySeconds;
|
2010-01-18 17:39:12 +00:00
|
|
|
|
|
|
|
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;
|
2010-01-24 02:11:55 +00:00
|
|
|
static final int MAX_X = 2048;
|
|
|
|
static final int MAX_Y = 1024;
|
|
|
|
private static final int MIN_REFRESH = 15;
|
2006-03-19 00:23:23 +00:00
|
|
|
|
|
|
|
public GraphHelper() {
|
2010-01-18 17:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** 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();
|
2006-03-19 00:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2010-01-24 02:11:55 +00:00
|
|
|
try { _height = Math.min(Integer.parseInt(str), MAX_Y); } catch (NumberFormatException nfe) {}
|
2006-03-19 00:23:23 +00:00
|
|
|
}
|
|
|
|
public void setWidth(String str) {
|
2010-01-24 02:11:55 +00:00
|
|
|
try { _width = Math.min(Integer.parseInt(str), MAX_X); } catch (NumberFormatException nfe) {}
|
2006-03-19 00:23:23 +00:00
|
|
|
}
|
|
|
|
public void setRefreshDelay(String str) {
|
2010-01-24 02:11:55 +00:00
|
|
|
try { _refreshDelaySeconds = Math.max(Integer.parseInt(str), MIN_REFRESH); } catch (NumberFormatException nfe) {}
|
2006-03-19 00:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getImages() {
|
|
|
|
try {
|
2007-07-16 20:47:57 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-06-10 13:17:28 +00:00
|
|
|
if (hasTx && hasRx && !_showEvents) {
|
2009-07-25 23:46:42 +00:00
|
|
|
_out.write("<a href=\"viewstat.jsp?stat=bw.combined"
|
2008-02-13 11:49:24 +00:00
|
|
|
+ "&periodCount=" + (3 * _periodCount )
|
|
|
|
+ "&width=" + (3 * _width)
|
|
|
|
+ "&height=" + (3 * _height)
|
2009-07-25 23:46:42 +00:00
|
|
|
+ "\" / target=\"_blank\">");
|
2010-06-02 18:16:43 +00:00
|
|
|
String title = _("Combined bandwidth graph");
|
2009-07-25 23:46:42 +00:00
|
|
|
_out.write("<img class=\"statimage\" width=\""
|
2007-07-14 18:44:11 +00:00
|
|
|
+ (_width + 83) + "\" height=\"" + (_height + 92)
|
|
|
|
+ "\" src=\"viewstat.jsp?stat=bw.combined"
|
2006-04-10 05:37:28 +00:00
|
|
|
+ "&periodCount=" + _periodCount
|
|
|
|
+ "&width=" + _width
|
2007-07-14 18:44:11 +00:00
|
|
|
+ "&height=" + (_height - 14)
|
2010-06-02 18:16:43 +00:00
|
|
|
+ "\" alt=\"" + title + "\" title=\"" + title + "\"></a>\n");
|
2008-06-10 13:17:28 +00:00
|
|
|
}
|
2006-04-10 05:37:28 +00:00
|
|
|
|
2006-04-19 17:46:51 +00:00
|
|
|
for (Iterator iter = ordered.iterator(); iter.hasNext(); ) {
|
|
|
|
SummaryListener lsnr = (SummaryListener)iter.next();
|
2006-03-19 00:23:23 +00:00
|
|
|
Rate r = lsnr.getRate();
|
2010-06-02 18:16:43 +00:00
|
|
|
// e.g. "statname for 60m"
|
2010-11-06 12:28:38 +00:00
|
|
|
String title = _("{0} for {1}", r.getRateStat().getName(), DataHelper.formatDuration2(_periodCount * r.getPeriod()));
|
2008-02-13 11:49:24 +00:00
|
|
|
_out.write("<a href=\"viewstat.jsp?stat="
|
|
|
|
+ r.getRateStat().getName()
|
|
|
|
+ "&showEvents=" + _showEvents
|
|
|
|
+ "&period=" + r.getPeriod()
|
|
|
|
+ "&periodCount=" + (3 * _periodCount)
|
|
|
|
+ "&width=" + (3 * _width)
|
|
|
|
+ "&height=" + (3 * _height)
|
2009-08-17 20:17:30 +00:00
|
|
|
+ "\" target=\"_blank\">");
|
2009-07-25 19:47:16 +00:00
|
|
|
_out.write("<img class=\"statimage\" border=\"0\" width=\""
|
2007-07-14 18:44:11 +00:00
|
|
|
+ (_width + 83) + "\" height=\"" + (_height + 92)
|
|
|
|
+ "\" src=\"viewstat.jsp?stat="
|
|
|
|
+ r.getRateStat().getName()
|
2006-03-19 00:23:23 +00:00
|
|
|
+ "&showEvents=" + _showEvents
|
|
|
|
+ "&period=" + r.getPeriod()
|
|
|
|
+ "&periodCount=" + _periodCount
|
|
|
|
+ "&width=" + _width
|
|
|
|
+ "&height=" + _height
|
2009-08-17 20:17:30 +00:00
|
|
|
+ "\" alt=\"" + title
|
|
|
|
+ "\" title=\"" + title + "\"></a>\n");
|
2006-03-19 00:23:23 +00:00
|
|
|
}
|
|
|
|
if (_refreshDelaySeconds > 0)
|
2009-07-19 22:32:01 +00:00
|
|
|
// shorten the refresh by 3 seconds so we beat the iframe
|
2009-08-17 20:17:30 +00:00
|
|
|
_out.write("<meta http-equiv=\"refresh\" content=\"" + (_refreshDelaySeconds - 3) + "\">\n");
|
2006-03-19 00:23:23 +00:00
|
|
|
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
ioe.printStackTrace();
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2010-01-18 17:39:12 +00:00
|
|
|
|
2006-03-19 00:23:23 +00:00
|
|
|
public String getForm() {
|
2010-01-18 17:39:12 +00:00
|
|
|
saveSettings();
|
2006-03-19 00:23:23 +00:00
|
|
|
try {
|
2010-11-17 22:26:31 +00:00
|
|
|
_out.write("<br><h3>" + _("Configure Graph Display") + " [<a href=\"configstats\">" + _("Select Stats") + "</a>]</h3>");
|
|
|
|
_out.write("<form action=\"graphs\" method=\"GET\">");
|
2009-10-27 11:11:51 +00:00
|
|
|
_out.write(_("Periods") + ": <input size=\"3\" type=\"text\" name=\"periodCount\" value=\"" + _periodCount + "\"><br>\n");
|
|
|
|
_out.write(_("Plot averages") + ": <input type=\"radio\" class=\"optbox\" name=\"showEvents\" value=\"false\" " + (_showEvents ? "" : "checked=\"true\" ") + "> ");
|
|
|
|
_out.write(_("or")+ " " +_("plot events") + ": <input type=\"radio\" class=\"optbox\" name=\"showEvents\" value=\"true\" "+ (_showEvents ? "checked=\"true\" " : "") + "><br>\n");
|
|
|
|
_out.write(_("Image sizes") + ": " + _("width") + ": <input size=\"4\" type=\"text\" name=\"width\" value=\"" + _width
|
|
|
|
+ "\"> " + _("pixels") + ", " + _("height") + ": <input size=\"4\" type=\"text\" name=\"height\" value=\"" + _height
|
|
|
|
+ "\"> " + _("pixels") + "<br>\n");
|
|
|
|
_out.write(_("Refresh delay") + ": <select name=\"refreshDelay\"><option value=\"60\">1 " + _("minute") + "</option><option value=\"120\">2 " + _("minutes") + "</option><option value=\"300\">5 " + _("minutes") + "</option><option value=\"600\">10 " + _("minutes") + "</option><option value=\"1800\">30 " + _("minutes") + "</option><option value=\"3600\">1 " + _("hour") + "</option><option value=\"-1\">" + _("Never") + "</option></select><br>\n");
|
2009-11-03 15:17:14 +00:00
|
|
|
_out.write("<hr><div class=\"formaction\"><input type=\"submit\" value=\"" + _("Redraw") + "\"></div></form>");
|
2006-03-19 00:23:23 +00:00
|
|
|
} catch (IOException ioe) {
|
|
|
|
ioe.printStackTrace();
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2006-04-19 17:46:51 +00:00
|
|
|
|
2010-01-18 17:39:12 +00:00
|
|
|
/**
|
|
|
|
* Silently save settings if changed, no indication of success or failure
|
|
|
|
* @since 0.7.10
|
|
|
|
*/
|
|
|
|
private void saveSettings() {
|
|
|
|
if (_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()) {
|
|
|
|
_context.router().setConfigSetting(PROP_X, "" + _width);
|
|
|
|
_context.router().setConfigSetting(PROP_Y, "" + _height);
|
|
|
|
_context.router().setConfigSetting(PROP_PERIODS, "" + _periodCount);
|
|
|
|
_context.router().setConfigSetting(PROP_REFRESH, "" + _refreshDelaySeconds);
|
|
|
|
_context.router().setConfigSetting(PROP_EVENTS, "" + _showEvents);
|
|
|
|
_context.router().saveConfig();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-31 18:18:36 +00:00
|
|
|
/** inner class, don't bother reindenting */
|
|
|
|
private static class AlphaComparator implements Comparator {
|
2006-04-19 17:46:51 +00:00
|
|
|
public int compare(Object lhs, Object rhs) {
|
|
|
|
SummaryListener l = (SummaryListener)lhs;
|
|
|
|
SummaryListener r = (SummaryListener)rhs;
|
|
|
|
String lName = l.getRate().getRateStat().getName() + "." + l.getRate().getPeriod();
|
|
|
|
String rName = r.getRate().getRateStat().getName() + "." + r.getRate().getPeriod();
|
|
|
|
return lName.compareTo(rName);
|
|
|
|
}
|
2007-07-14 18:44:11 +00:00
|
|
|
}
|
2009-10-31 18:18:36 +00:00
|
|
|
|
|
|
|
}
|