Class: Twingly::Metrics::EWMA
- Inherits:
-
Object
- Object
- Twingly::Metrics::EWMA
- Defined in:
- lib/twingly/metrics/ewma.rb
Constant Summary collapse
- INTERVAL =
5.0- SECONDS_PER_MINUTE =
60.0- ONE_MINUTE =
1- FIVE_MINUTES =
5- FIFTEEN_MINUTES =
15- M1_ALPHA =
1 - Math.exp(-INTERVAL / SECONDS_PER_MINUTE / ONE_MINUTE)
- M5_ALPHA =
1 - Math.exp(-INTERVAL / SECONDS_PER_MINUTE / FIVE_MINUTES)
- M15_ALPHA =
1 - Math.exp(-INTERVAL / SECONDS_PER_MINUTE / FIFTEEN_MINUTES)
Class Method Summary collapse
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize(alpha, interval) ⇒ EWMA
constructor
A new instance of EWMA.
- #rate ⇒ Object
- #tick ⇒ Object
- #update(value) ⇒ Object
Constructor Details
#initialize(alpha, interval) ⇒ EWMA
Returns a new instance of EWMA.
31 32 33 34 35 36 37 38 |
# File 'lib/twingly/metrics/ewma.rb', line 31 def initialize(alpha, interval) @alpha = alpha @interval = interval @initialized = false @rate = Atomic.new(0.0) @uncounted = Atomic.new(0) end |
Class Method Details
.new_m1 ⇒ Object
19 20 21 |
# File 'lib/twingly/metrics/ewma.rb', line 19 def self.new_m1 new(M1_ALPHA, INTERVAL) end |
Instance Method Details
#clear ⇒ Object
40 41 42 43 44 |
# File 'lib/twingly/metrics/ewma.rb', line 40 def clear @initialized = false @rate.value = 0.0 @uncounted.value = 0 end |
#rate ⇒ Object
62 63 64 |
# File 'lib/twingly/metrics/ewma.rb', line 62 def rate @rate.value end |
#tick ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/twingly/metrics/ewma.rb', line 50 def tick count = @uncounted.swap(0) instant_rate = count / @interval.to_f if @initialized @rate.update { |v| v + (@alpha * (instant_rate - v)) } else @rate.value = instant_rate @initialized = true end end |
#update(value) ⇒ Object
46 47 48 |
# File 'lib/twingly/metrics/ewma.rb', line 46 def update(value) @uncounted.update { |v| v + value } end |