Class: Metriks::EWMA

Inherits:
Object
  • Object
show all
Defined in:
lib/metriks/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

Constructor Details

#initialize(alpha, interval) ⇒ EWMA

Returns a new instance of EWMA.



28
29
30
31
32
33
34
35
# File 'lib/metriks/ewma.rb', line 28

def initialize(alpha, interval)
  @alpha    = alpha
  @interval = interval

  @initialized = false
  @rate        = Atomic.new(0.0)
  @uncounted   = Atomic.new(0)
end

Class Method Details

.new_m1Object



16
17
18
# File 'lib/metriks/ewma.rb', line 16

def self.new_m1
  new(M1_ALPHA, INTERVAL)
end

.new_m15Object



24
25
26
# File 'lib/metriks/ewma.rb', line 24

def self.new_m15
  new(M15_ALPHA, INTERVAL)
end

.new_m5Object



20
21
22
# File 'lib/metriks/ewma.rb', line 20

def self.new_m5
  new(M5_ALPHA, INTERVAL)
end

Instance Method Details

#clearObject



37
38
39
40
41
# File 'lib/metriks/ewma.rb', line 37

def clear
  @initialized = false
  @rate.value = 0.0
  @uncounted.value = 0
end

#rateObject



59
60
61
# File 'lib/metriks/ewma.rb', line 59

def rate
  @rate.value
end

#tickObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/metriks/ewma.rb', line 47

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



43
44
45
# File 'lib/metriks/ewma.rb', line 43

def update(value)
  @uncounted.update { |v| v + value }
end