Package org.rapidcontext.core.data.stat
Class MovingSum
java.lang.Object
org.rapidcontext.core.data.stat.MovingSum
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
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
time()
Returns the timestamp (in millis) for the most recent move.double
valueFor
(MovingSum.Interval interval) Returns the approximate sum for the specified interval.values()
Returns a dictionary with the approximate sums for each of the window sizes.
-
Constructor Details
-
MovingSum
public MovingSum(long now) Initializes a new set of moving sums.- Parameters:
now
- the current time (in millis)
-
MovingSum
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
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
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
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)
-