java.lang.Object
net.i2p.router.web.FormHandler
net.i2p.router.web.helpers.GraphHelper

public class GraphHelperextends FormHandler
Renders the graphs list (/graphs.jsp) and the single-stat page (/graph.jsp), plus the graph display configuration form.

Stateless across requests except for per-request form state set via the setXxx() accessors; preferences are read from context properties.

  • Field Details

    • _periodCount

      private int _periodCount
      Number of stat periods shown on the graph.
    • _showEvents

      private boolean _showEvents
      Whether event markers are drawn.
    • _width

      private int _width
      Graph width in pixels.
    • _height

      private int _height
      Graph height in pixels.
    • _refreshDelaySeconds

      private int _refreshDelaySeconds
      Seconds between graph refreshes.
    • _persistent

      private boolean _persistent
      Whether the settings are saved to the config.
    • _graphHideLegend

      private boolean _graphHideLegend
      Whether the legend is hidden.
    • _graphHideRestarts

      private boolean _graphHideRestarts
      Whether restart markers are hidden.
    • _graphGlow

      private boolean _graphGlow
      Whether the graph line glows.
    • _graphSmooth

      private boolean _graphSmooth
      Whether the graph line is smoothed.
    • _graphFill

      private boolean _graphFill
      Whether the area under the line is filled.
    • _graphCombine

      private boolean _graphCombine
      Whether in and out rates share one line.
    • _useUtc

      private boolean _useUtc
      Whether graph times are shown in UTC.
    • _stat

      private String _stat
      The stat name being graphed.
    • _end

      private int _end
      Which of the two stat values to graph, -1 for both.
    • PROP_X

      private static final String PROP_X
      Config key for the graph width.
      See Also:
    • PROP_Y

      private static final String PROP_Y
      Config key for the graph height.
      See Also:
    • PROP_REFRESH

      private static final String PROP_REFRESH
      Config key for the refresh delay.
      See Also:
    • PROP_PERIODS

      private static final String PROP_PERIODS
      Config key for the period count.
      See Also:
    • PROP_EVENTS

      private static final String PROP_EVENTS
      Config key for showing event markers.
      See Also:
    • PROP_HIDE_LEGEND

      private static final String PROP_HIDE_LEGEND
      Config key for hiding the legend.
      See Also:
    • PROP_HIDE_RESTARTS

      private static final String PROP_HIDE_RESTARTS
      Config key for hiding restart markers.
      See Also:
    • PROP_GLOW

      private static final String PROP_GLOW
      Config key for the glow effect.
      See Also:
    • PROP_SMOOTH

      private static final String PROP_SMOOTH
      Config key for line smoothing.
      See Also:
    • PROP_FILL

      private static final String PROP_FILL
      Config key for area filling.
      See Also:
    • PROP_COMBINE

      private static final String PROP_COMBINE
      Config key for combining the rates.
      See Also:
    • PROP_UTC

      private static final String PROP_UTC
      Config key for UTC graph times.
      See Also:
    • DEFAULT_REFRESH

      private static final int DEFAULT_REFRESH
      Default refresh delay, in seconds.
      See Also:
    • DEFAULT_PERIODS

      private static final int DEFAULT_PERIODS
      Default number of stat periods shown.
      See Also:
    • DEFAULT_HIDE_LEGEND

      private static final boolean DEFAULT_HIDE_LEGEND
      The legend is shown by default.
      See Also:
    • DEFAULT_HIDE_RESTARTS

      private static final boolean DEFAULT_HIDE_RESTARTS
      Restart markers are shown by default.
      See Also:
    • MIN_X

      private static final int MIN_X
      Smallest graph width accepted.
      See Also:
    • MIN_Y

      private static final int MIN_Y
      Smallest graph height accepted.
      See Also:
    • MIN_C

      private static final int MIN_C
      Smallest period count accepted.
      See Also:
    • MAX_C

      private static final int MAX_C
      Largest period count accepted.
      See Also:
    • MIN_REFRESH

      private static final int MIN_REFRESH
      Smallest refresh delay accepted, in seconds.
      See Also:
    • AMP

      private static final String AMP
      Escaped ampersand reused in the graph links.
      See Also:
    • SHOW_EVENTS_PARAM

      private static final String SHOW_EVENTS_PARAM
      Query parameter for the event markers.
      See Also:
    • HIDE_LEGEND_PARAM

      private static final String HIDE_LEGEND_PARAM
      Query parameter for the legend.
      See Also:
    • HIDE_RESTARTS_PARAM

      private static final String HIDE_RESTARTS_PARAM
      Query parameter for the restart markers.
      See Also:
    • PERIOD_COUNT_PARAM

      private static final String PERIOD_COUNT_PARAM
      Query parameter for the period count.
      See Also:
    • WIDTH_PARAM

      private static final String WIDTH_PARAM
      Query parameter for the graph width.
      See Also:
    • HEIGHT_PARAM

      private static final String HEIGHT_PARAM
      Query parameter for the graph height.
      See Also:
    • TIME_PARAM

      private static final String TIME_PARAM
      Query parameter for the graph end time.
      See Also:
    • STAT_PARAM

      private static final String STAT_PARAM
      Link prefix to the stat detail page.
      See Also:
    • GRAPH_HREF

      private static final String GRAPH_HREF
      Link prefix to the graph page.
      See Also:
    • HEX

      private static final char[] HEX
      Hex digits for the percent-escapes urlStatName() emits.
    • _hideLegend

      private boolean _hideLegend
      Whether this graph hides its legend.
    • times

      private static final int[] times
      Period lengths offered on the graph page.
  • Constructor Details

    • GraphHelper

      public GraphHelper()
      Leaves the form state unset; setContextId(String) supplies the context and preferences.
  • Method Details

    • setContextId

      public void setContextId(String contextId)
      Set the defaults after we have a context.
      Overrides:
      setContextId in class FormHandler
      Parameters:
      contextId - beginning few characters of the routerHash, or null to pick the first one we come across.
    • getRefreshMeta

      public String getRefreshMeta()
      This must be output in the jsp since <meta> must be in the <head>.
      Returns:
      the refresh meta
    • getRefreshValue

      public int getRefreshValue()
      The refresh delay the current render should actually use, in seconds.
      Returns:
      the delay in seconds, or -1 when graph generation is unavailable
    • effectiveRefresh

      static int effectiveRefresh(int saved, boolean graphingDisabled)
      The auto-refresh delay a page render should actually use.

      Refreshing is pointless while graph generation is unavailable, and hammering a console that cannot render is worse than showing nothing, so the delay is suppressed for that one render. It is deliberately not persisted: graph generation can come back, and writing the suppression into the config made it permanent, so once graphing had been unavailable for any reason the graphs page would freeze on its first paint and never refresh again, even after the router had recovered.

      Parameters:
      saved - the configured delay in seconds
      graphingDisabled - whether graph generation is currently unavailable
      Returns:
      the delay to use for this render, or -1 to suppress refreshing
    • setPeriodCount

      public void setPeriodCount(String str)
      Sets how many periods the graph spans.
      Parameters:
      str - period count in minutes; non-numeric text is ignored and numbers are clamped to MIN_C..MAX_C
    • setE

      public void setE(String str)
      Ends the view window that many periods before the most recent one.
      Parameters:
      str - how many periods back to end, so 0 ends at the latest period; non-numeric text is ignored
    • setC

      public void setC(String str)
      Shorter form of setPeriodCount(String) bound to the c= URL parameter.
      Parameters:
      str - period count in minutes; non-numeric text is ignored and numbers are clamped to MIN_C..MAX_C
    • setShowEvents

      public void setShowEvents(String b)
      Chooses between plotting individual events and plotting averages.
      Parameters:
      b - true to plot individual events, false to plot averages; an absent, empty or unrecognised value leaves the current mode in force
    • resolveShowEvents

      static boolean resolveShowEvents(String param, boolean current)
      Resolves the requested events mode against the mode in force.

      An absent or empty parameter means "not specified", and the mode already in force - which came from routerconsole.graphEvents - stands. It must not be read as a request for events: the enlarged-graph link only emits showEvents when events are on, so a time-mode link omits it, and the old !"false".equals(b) turned that omission into true. Opening the larger view of a time graph silently switched it to events and contradicted the configured default. The two URL-building sites disagreed about whether the parameter is always present; this makes the reader tolerant of its absence instead.

      The value is also parsed symmetrically with the way it is written. One link emits showEvents=1 and the image sources emit showEvents=true|false, but the reader recognised only the literal string "false" - so showEvents=0, a perfectly ordinary way to spell it, was read as a request for events.

      Parameters:
      param - the request parameter, may be null or empty
      current - the mode currently in force
      Returns:
      the mode to use
    • setHeight

      public void setHeight(String str)
      Sets the image height in pixels.
      Parameters:
      str - image height in pixels; non-numeric text is ignored and numbers are clamped to MIN_Y..MAX_Y
    • setH

      public void setH(String str)
      Shorter form of setHeight(String) bound to the h= URL parameter.
      Parameters:
      str - image height in pixels; non-numeric text is ignored and numbers are clamped to MIN_Y..MAX_Y
    • setWidth

      public void setWidth(String str)
      Sets the image width in pixels.
      Parameters:
      str - image width in pixels; non-numeric text is ignored and numbers are clamped to MIN_X..MAX_X
    • setW

      public void setW(String str)
      Shorter form of setWidth(String) bound to the w= URL parameter.
      Parameters:
      str - image width in pixels; non-numeric text is ignored and numbers are clamped to MIN_X..MAX_X
    • setRefreshDelay

      public void setRefreshDelay(String str)
      Sets how often the graph refreshes, in seconds.
      Parameters:
      str - refresh delay in seconds; a positive value is raised to MIN_REFRESH, zero or negative selects never refresh, and non-numeric text is ignored
    • setPersistent

      public void setPersistent(String foo)
      Enables storing graph data on disk; the submitted value is not examined.
      Parameters:
      foo - the persistent checkbox value, ignored
    • setGraphGlow

      public void setGraphGlow(String foo)
      Sets the glow effect drawn around graph lines.
      Parameters:
      foo - only the exact text false turns the glow off; any other value enables it
    • setGraphSmooth

      public void setGraphSmooth(String foo)
      Sets bezier curves in place of a staircase plot.
      Parameters:
      foo - only the exact text false turns smoothing off; any other value enables it
    • setGraphFill

      public void setGraphFill(String foo)
      Sets whether graphs are drawn with filled areas.
      Parameters:
      foo - only the exact text false turns filling off; any other value enables it
    • setGraphCombine

      public void setGraphCombine(String foo)
      Sets whether related stats are merged into one overlaid plot.
      Parameters:
      foo - only the exact text false turns combining off; any other value enables it
    • setUseUtc

      public void setUseUtc(String foo)
      Sets whether graph axes are labelled in UTC instead of local time.
      Parameters:
      foo - only the exact text false turns UTC off; any other value enables it
    • setHideLegend

      public void setHideLegend(String foo)
      Sets whether the legend is suppressed on graphs.
      Parameters:
      foo - true hides the legend and false shows it; any other value leaves the current setting alone
    • setHideRestarts

      public void setHideRestarts(String foo)
      Sets whether restart markers are suppressed on graphs.
      Parameters:
      foo - true suppresses them and false restores them; any other value leaves the current setting alone
    • setStat

      public void setStat(String stat)
      For single stat page.
      Parameters:
      stat - the rate name to plot, or a group id when combining is on
    • getImages

      public String getImages()
      The thumbnail list for the graphs page, one anchor-wrapped image per enabled stat plus a tile per active group.
      Returns:
      the markup for the thumbnails, or the empty string when graphs are not available
    • renderGroupTiles

      private String renderGroupTiles(Set<String> enabledStats, boolean hideLegend, boolean hideRestarts, long now)
      Emit one tile per active group, each plotting its members as overlaid lines.

      A group appears only when at least two members are enabled and carry data, so enabling a single stat is never silently turned into a combined plot.

      Parameters:
      enabledStats - the enabled stats, from which group members are picked
      hideLegend - true to hide the legend, shrinking the tile height to match
      hideRestarts - true to hide restart markers on the tile's graphs
      now - the graph end time in ms since epoch
      Returns:
      markup for every qualifying group, empty when none qualify
    • enabledStatNames

      private static Set<String> enabledStatNames(GraphGenerator ss)
      The stat names the user has switched on for graphing.
      Parameters:
      ss - the graph generator
      Returns:
      enabled stat names, without period suffixes
    • countGraphs

      public int countGraphs()
      How many graphs the configured listeners will draw.
      Returns:
      the number of configured graph listeners, or 0 if graphs are disabled
    • getSingleStat

      public String getSingleStat()
      For single stat page; stat = "bw.combined" treated specially.
      Returns:
      the single stat
    • urlStatName

      static String urlStatName(String name)
      Percent-encode a stat or group name for use as a URL query value.

      Only the space was escaped before, so a tunnel nickname carrying &, +, = or % silently split or corrupted the graph link, since per-tunnel stats are named [nickname] InBps and the nickname is user input. Everything outside the unreserved set of RFC 3986 is escaped, UTF-8, uppercase hex. The name itself is never rewritten: the space that GraphGroups pairs In/Out rates on stays intact in the model and only becomes %20 on the wire, and a plain name such as bw.sendRate comes back byte-identical.

      URLEncoder is deliberately not used: it turns the space into + and rewrites characters that are already safe, which would change URLs that work today.

      Parameters:
      name - the stat or group name as registered
      Returns:
      the name safe to append to a query value
    • link

      private static String link(String stat, boolean showEvents, int periodCount, int end, int width, int height, boolean hideLegend, boolean hideRestarts)
      Link to the graph page for a stat.
      Parameters:
      stat - the stat name to link to
      showEvents - true to ask the graph page for event markers
      periodCount - the number of periods to plot, sent as the c parameter
      end - the end-time offset, left out of the URL when 0 or less
      width - the graph width in pixels
      height - the graph height in pixels
      hideLegend - this page's legend setting, written inverted into the URL
      hideRestarts - this page's restart-marker setting, passed through
      Returns:
      the opening anchor tag for the graph page with these parameters
    • getForm

      public String getForm()
      The graph display configuration form posted to /graphs.
      Returns:
      the form markup, or the empty string when graphs are not available
    • getAllMessages

      public String getAllMessages()
      We have to do this here because processForm() isn't called unless the nonces are good.
      Overrides:
      getAllMessages in class FormHandler
      Returns:
      the all messages
    • processForm

      protected void processForm()
      This was a HelperBase but now it's a FormHandler.
      Specified by:
      processForm in class FormHandler
    • saveSettings

      private void saveSettings()
      Silently save settings if changed, no indication of success or failure.