Class MovingSum

java.lang.Object
org.rapidcontext.core.data.stat.MovingSum

public class MovingSum extends Object
A set of approximate moving sums for predefined time intervals. For each interval, both the current (running) and previous sums are stored, allowing for calculating an approximate sum for the whole interval. This uses a percentage of the previous sum depending on current time. Three predefined time intervals (hour, day, month) are provided.
Version:
1.0
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    The predefined time intervals.
  • Constructor Summary

    Constructors
    Constructor
    Description
    MovingSum(long now)
    Initializes a new set of moving sums.
    MovingSum(long now, Array arr)
    Initializes a new set of moving sums from a serialized array.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(double value)
    Adds a value to the moving sum (for all intervals).
    void
    moveTo(long now)
    Moves the current time forward.
    Returns an array with the sums for each of the intervals (hour to month).
    long
    Returns the timestamp (in millis) for the most recent move.
    double
    Returns the approximate sum for the specified interval.
    Returns a dictionary with the approximate sums for each of the window sizes.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MovingSum

      public MovingSum(long now)
      Initializes a new set of moving sums.
      Parameters:
      now - the current time (in millis)
    • MovingSum

      public MovingSum(long now, Array arr)
      Initializes a new set of moving sums from a serialized array.
      Parameters:
      now - the serialization time (in millis)
      arr - the serialized array of long values
  • Method Details

    • serialize

      public Array serialize()
      Returns an array with the sums for each of the intervals (hour to month). Two long values are provided for each interval.
      Returns:
      the serialized array of long values
    • time

      public long time()
      Returns the timestamp (in millis) for the most recent move.
      Returns:
      the current interval timestamp (in millis)
    • values

      public Dict values()
      Returns a dictionary with the approximate sums for each of the window sizes. The returned sums will be a rounded to long values.
      Returns:
      a dictionary with sums for each interval
    • valueFor

      public double valueFor(MovingSum.Interval interval)
      Returns the approximate sum for the specified interval. The current time is used to determine what percentage of the previous sum to add to the current (running) sum.
      Parameters:
      interval - the interval to fetch
      Returns:
      the approximate sum for the specified interval
    • add

      public void add(double value)
      Adds a value to the moving sum (for all intervals).
      Parameters:
      value - the value to add
    • moveTo

      public void moveTo(long now)
      Moves the current time forward. If needed, one or more of the sums will be rolled over.
      Parameters:
      now - the current time (in millis)