Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java counter with value for last one hour

Tags:

java

counter

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?

like image 945
rajesh Avatar asked Sep 01 '26 01:09

rajesh


1 Answers

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)
like image 139
Christian d'Heureuse Avatar answered Sep 03 '26 14:09

Christian d'Heureuse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!