package net.i2p.router.web; import java.io.IOException; import java.io.Serializable; import java.io.Writer; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; import net.i2p.data.DataHelper; import net.i2p.stat.Rate; /** * /graphs.jsp, including form, and /graph.jsp */ public class GraphHelper extends FormHandler { protected Writer _out; private int _periodCount; private boolean _showEvents; private int _width; private int _height; private int _refreshDelaySeconds; private boolean _persistent; private String _stat; private int _end; 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"; public static final int DEFAULT_X = 250; public static final int DEFAULT_Y = 100; private static final int DEFAULT_REFRESH = 5*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_X = 200; private static final int MIN_Y = 60; private static final int MIN_C = 20; private static final int MAX_C = SummaryListener.MAX_ROWS; private static final int MIN_REFRESH = 15; /** 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 = _context.getBooleanProperty(PROP_EVENTS); } /** * This must be output in the jsp since must be in the
* @since 0.8.7 */ public String getRefreshMeta() { if (_refreshDelaySeconds <= 8 || ConfigRestartBean.getRestartTimeRemaining() < (1000 * (_refreshDelaySeconds + 30))) return ""; // shorten the refresh by 3 seconds so we beat the iframe return ""; } /** * This was a HelperBase but now it's a FormHandler * @since 0.8.2 */ public void storeWriter(Writer out) { _out = out; } public void setPeriodCount(String str) { setC(str); } /** @since 0.9 */ public void setE(String str) { try { _end = Math.max(0, Integer.parseInt(str)); } catch (NumberFormatException nfe) {} } /** @since 0.9 shorter parameter */ public void setC(String str) { try { _periodCount = Math.max(MIN_C, Math.min(Integer.parseInt(str), MAX_C)); } catch (NumberFormatException nfe) {} } public void setShowEvents(String b) { _showEvents = !"false".equals(b); } public void setHeight(String str) { setH(str); } /** @since 0.9 shorter parameter */ public void setH(String str) { try { _height = Math.max(MIN_Y, Math.min(Integer.parseInt(str), MAX_Y)); } catch (NumberFormatException nfe) {} } public void setWidth(String str) { setW(str); } /** @since 0.9 shorter parameter */ public void setW(String str) { try { _width = Math.max(MIN_X, Math.min(Integer.parseInt(str), MAX_X)); } catch (NumberFormatException nfe) {} } public void setRefreshDelay(String str) { try { int rds = Integer.parseInt(str); if (rds > 0) _refreshDelaySeconds = Math.max(rds, MIN_REFRESH); else _refreshDelaySeconds = -1; } catch (NumberFormatException nfe) {} } /** @since 0.8.7 */ public void setPersistent(String foo) { _persistent = true; } /** * For single stat page * @since 0.9 */ public void setStat(String stat) { _stat = stat; } public String getImages() { if (StatSummarizer.isDisabled()) return ""; try { List" + _("All times are UTC.") + "
\n"); } catch (IOException ioe) { ioe.printStackTrace(); } return ""; } /** * For single stat page; * stat = "bw.combined" treated specially * * @since 0.9 */ public String getSingleStat() { try { if (StatSummarizer.isDisabled()) return ""; if (_stat == null) { _out.write("No stat specified"); return ""; } long period; String name, displayName; if (_stat.equals("bw.combined")) { period = 60000; name = _stat; displayName = _("Bandwidth usage"); } else { Set\n");
if (_width < MAX_X && _height < MAX_Y) {
_out.write(link(_stat, _showEvents, _periodCount, _end, _width * 3 / 2, _height * 3 / 2));
_out.write(_("Larger"));
_out.write(" - ");
}
if (_width > MIN_X && _height > MIN_Y) {
_out.write(link(_stat, _showEvents, _periodCount, _end, _width * 2 / 3, _height * 2 / 3));
_out.write(_("Smaller"));
_out.write(" - ");
}
if (_height < MAX_Y) {
_out.write(link(_stat, _showEvents, _periodCount, _end, _width, _height * 3 / 2));
_out.write(_("Taller"));
_out.write(" - ");
}
if (_height > MIN_Y) {
_out.write(link(_stat, _showEvents, _periodCount, _end, _width, _height * 2 / 3));
_out.write(_("Shorter"));
_out.write(" - ");
}
if (_width < MAX_X) {
_out.write(link(_stat, _showEvents, _periodCount, _end, _width * 3 / 2, _height));
_out.write(_("Wider"));
_out.write(" - ");
}
if (_width > MIN_X) {
_out.write(link(_stat, _showEvents, _periodCount, _end, _width * 2 / 3, _height));
_out.write(_("Narrower"));
_out.write("");
}
_out.write("
");
if (_periodCount < MAX_C) {
_out.write(link(_stat, _showEvents, _periodCount * 2, _end, _width, _height));
_out.write(_("Larger interval"));
_out.write(" - ");
}
if (_periodCount > MIN_C) {
_out.write(link(_stat, _showEvents, _periodCount / 2, _end, _width, _height));
_out.write(_("Smaller interval"));
_out.write("");
}
_out.write("
");
if (_periodCount < MAX_C) {
_out.write(link(_stat, _showEvents, _periodCount, _end + _periodCount, _width, _height));
_out.write(_("Previous interval"));
_out.write("");
}
if (_end > 0) {
int end = _end - _periodCount;
if (end <= 0)
end = 0;
if (_periodCount < MAX_C)
_out.write(" - ");
_out.write(link(_stat, _showEvents, _periodCount, end, _width, _height));
_out.write(_("Next interval"));
_out.write(" ");
}
_out.write("
");
_out.write(link(_stat, !_showEvents, _periodCount, _end, _width, _height));
if (!_stat.equals("bw.combined"))
_out.write(_showEvents ? _("Plot averages") : _("plot events"));
_out.write("");
_out.write("
" + _("All times are UTC.") + "
\n"); } catch (IOException ioe) { ioe.printStackTrace(); } return ""; } /** @since 0.9 */ private static String link(String stat, boolean showEvents, int periodCount, int end, int width, int height) { return " 0 ? "&e=" + end : "") + (showEvents ? "&showEvents=1" : "") + "\">"; } private static final int[] times = { 60, 2*60, 5*60, 10*60, 30*60, 60*60, -1 }; public String getForm() { if (StatSummarizer.isDisabled()) return ""; // too hard to use the standard formhandler.jsi / FormHandler.java session nonces // since graphs.jsp needs the refresh value in its . // So just use the "shared/console nonce". String nonce = CSSHelper.getNonce(); try { _out.write("