com.boylesoftware.cb2.util
Class EventHistory

java.lang.Object
  extended bycom.boylesoftware.cb2.util.EventHistory
All Implemented Interfaces:
java.io.Serializable

public class EventHistory
extends java.lang.Object
implements java.io.Serializable

Counts events and maintains a statistics for a certain period of time. Canbe used together with StatsGraph for creating nice event statistics images.

Note, that all get methods are not synchronized. Usually you call a few get methods from a single synchronized on the EventHistory instance block. The first call should be getSamples, because it updates timestamps:

 long totalSpan = eventHistory.getTotalSpan(); // don't need to be synchronized, since it never changes
 long [] samples;
 int lastSampleIndex;
 int nSamples;
 long lastSampleTs;
 long maxSample;
 synchronized(eventHistory) {
     samples = eventHistory.getSamples();
     lastSampleIndex = eventHistory.getLastSampleIndex();
     nSamples = eventHistory.getNSamples();
     lastSampleTs = eventHistory.getLastSampleTs();
     maxSample = eventHistory.getMaxSample();
 }
 

Version:
$Id: EventHistory.java,v 1.2 2003/07/20 16:04:19 levahim Exp $
Author:
Lev Himmelfarb
See Also:
StatsGraph, Serialized Form

Field Summary
protected  int curSampleInd
          Index of the currently being built sample.
protected  long maxSample
          Maximum sample value.
protected  int nSamples
          Number of samples in the timeline including the current one (so it never can be zero).
protected  long[] samples
          Containes samples.
protected  long sampleSpan
          Number of milliseconds for a single sample data.
protected  long totalSpan
          Number of milliseconds representing the whole timeline.
protected  long tsLastSampleStart
          Timestamp when collecting data for the current sample was started.
 
Constructor Summary
EventHistory(long totalSpan, int timelineSamples)
          Constructs and initializes a new event history object.
 
Method Summary
 long eventStarted()
          Registers an event happened now.
 void eventStarted(long ts)
          Registers an event happened at the specified time.
 int getLastSampleIndex()
          Gets index of the last sample in the array of samples.
 long getLastSampleTs()
          Gets timestamp for the last sample.
 long getMaxSample()
          Gets the maximum sample value.
 int getNSamples()
          Gets the number of samples in the array of samples.
 long[] getSamples()
          Gets the samples.
 long getSampleSpan()
          Gets number of milliseconds of one sample.
 long getTotalSpan()
          Gets number of milliseconds in the whole timeline.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

samples

protected final long[] samples
Containes samples.


totalSpan

protected final long totalSpan
Number of milliseconds representing the whole timeline.


sampleSpan

protected final long sampleSpan
Number of milliseconds for a single sample data.


curSampleInd

protected int curSampleInd
Index of the currently being built sample.


nSamples

protected int nSamples
Number of samples in the timeline including the current one (so it never can be zero).


tsLastSampleStart

protected long tsLastSampleStart
Timestamp when collecting data for the current sample was started.


maxSample

protected long maxSample
Maximum sample value.

Constructor Detail

EventHistory

public EventHistory(long totalSpan,
                    int timelineSamples)
Constructs and initializes a new event history object. The instance starts collecting statistics for the event as soon as it is created.

Parameters:
totalSpan - number of milliseconds in the timeline, meaing that the event history object will keep statistics for events for the last specified number of milliseconds.
timelineSamples - number of samples to keep, so this parameters defines the resolution of the timeline. For example if the timeline is 1 hour (totalSpan = 3600000) and the timelineSamples is set to 60 then we will have a sample for every minute. When you use EventHistory with StatsGraph class for generating event timeline images, for optimal mode it's better to set timelineSamples to be the same as the width of the picture. This way you will have a sample per each pixel on the horizontal axis of the generated image.
Method Detail

eventStarted

public long eventStarted()
Registers an event happened now.

Returns:
timestamp of the event.

eventStarted

public void eventStarted(long ts)
Registers an event happened at the specified time.

Parameters:
ts - timestamp when the event happened.

getSamples

public long[] getSamples()
Gets the samples.

Returns:
array of samples.

getNSamples

public int getNSamples()
Gets the number of samples in the array of samples.

Returns:
the number of samples.

getLastSampleIndex

public int getLastSampleIndex()
Gets index of the last sample in the array of samples.

Returns:
the index of the last sample.

getTotalSpan

public long getTotalSpan()
Gets number of milliseconds in the whole timeline. This method does not have to be in the synchronized block.

Returns:
number of milliseconds in the timeline.

getSampleSpan

public long getSampleSpan()
Gets number of milliseconds of one sample. Thie method does not have to be in the synchronized block.

Returns:
number of milliseconds per sample.

getLastSampleTs

public long getLastSampleTs()
Gets timestamp for the last sample.

Returns:
timestamp for the last sample.

getMaxSample

public long getMaxSample()
Gets the maximum sample value.

Returns:
maximum sample.


Copyright © 2002,2003,2004 - Boyle Software, Inc.