Statistics Examples

Creating a statistic

When you create a statistic you will need to extend the Statistic base class.  You will want to initialize all the variables you will want to store in your statistic.  You may also want to override the renderToHTML() method, giving the display of your statistic a custom look.

Example of Statistic from Apollo:

Using a statistic

    In the following examples from the Demo site, you can see how you can manipulate values inside of a statistic:

This example shows incrementing a counter:

This example shows adding a new element to a vector:
    MT.getStatsManager().getStat("UserExceptions").addStat("Exceptions",new ExceptionData(e));

This example shows incrementing a variable by a particular step:
MT.getStatsManager().getStat("Servlet").incStat("TotalServletCPUTime",(((float) (System.currentTimeMillis() - startTime))/1000));

Binding an action  to a statistic is not something which was used in the demo site.  The BindMethodToStat allows you to have an ExecutableMethod object triggered each time a particular value on a statistic is updated.  An ExcutableMethod is an inteface which defines a single method which is called when it is triggered by the Statistics system.

Rendering a statistic

    You can always fetch the values in your statistic using your own getter and setter methods, but each statistic also has a mechinism to display itself as an HTML page.  If you do not override the renderToHTML method, the Statistic super class will iterate through the registered values in that statistic and generate it's own page.  This ability allows you to easily display a statistic.  The StatisticsManager also has the ability of generating an HTML page with all of the statistics registered in the system.  This makes dumping the entire contents of the statistics system to a user very easy.  renderToHTML() is the method on both on an individual Statistic and the StatisticsManager to render to HTML.