I need a counter which will get incremented as certain tasks are complete. We need the value for only last one hour, ie the window will be moving and not for static hours.
What is the best way to go about this? One way I could think of was having an array of size 60, one for each minute and update the entry for respective minute and do a total of this on a get(). On turning to a new minute, the array will be reset.
Are there better solutions? Any time based implementations of Counter available?
I have written the Java class DecayingCounter for this purpose.
It uses the following formulas to implement an exponentially time-decaying counter:
tau = halfLifeInSeconds / Math.log(2.0)
value *= Math.exp(deltaTimeInNanos * -1E-9 / tau)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With