@@ -4,7 +4,7 @@ import cats._
44import cats .implicits ._
55import scala .concurrent .duration .{Duration , FiniteDuration }
66
7- /** Track statistics over time. */
7+ /** Track statistics over time a fixed size timewindow . */
88case class TimeSlotStats [K , V : Monoid ] private (
99 // Time resolution.
1010 slotDuration : FiniteDuration ,
@@ -18,8 +18,8 @@ case class TimeSlotStats[K, V: Monoid] private (
1818) {
1919 import TimeSlotStats ._
2020
21- private def succ ( idx : Int ) : Int = (idx + 1 ) % slotCount
22- private def pred ( idx : Int ) : Int = (idx - 1 ) % slotCount
21+ /** Overall length of the timewindow. */
22+ def duration = slotDuration * slotCount
2323
2424 /** Merge new stats for a given key in the current timestamp. */
2525 def add (key : K , stat : V , timestamp : Timestamp = System .currentTimeMillis): TimeSlotStats [K , V ] = {
@@ -87,6 +87,19 @@ case class TimeSlotStats[K, V: Monoid] private (
8787 val startSlot = slotId(timestamp - slotDuration.toMillis * slotCount)
8888 startSlot -> endSlot
8989 }
90+
91+ private def succ (idx : Int ): Int = (idx + 1 ) % slotCount
92+ private def pred (idx : Int ): Int = (idx - 1 ) % slotCount
93+
94+ private def copy (statSlots : IndexedSeq [Map [K , V ]]): TimeSlotStats [K , V ] =
95+ copy(lastIdx, timeSlots, statSlots)
96+
97+ private def copy (
98+ lastIdx : Int ,
99+ timeSlots : IndexedSeq [Timestamp ],
100+ statSlots : IndexedSeq [Map [K , V ]]
101+ ): TimeSlotStats [K , V ] =
102+ new TimeSlotStats [K , V ](slotDuration, slotCount, lastIdx, timeSlots, statSlots)
90103}
91104
92105object TimeSlotStats {
0 commit comments