Files
dyndnsd/lib/metriks/ewma.rb
T
cn 7fa9f499e1 gems: include vendored metrisk without hitime dependency
hitime is only needed for Metriks::Timer and requires native extensions.
Those do not compile with default settings under Ubuntu 26.04 or Alpine 3.24.
2026-11-13 17:35:46 +01:00

66 lines
1.4 KiB
Ruby

# Vendored from metriks 0.9.9.8 — https://github.com/eric/metriks
# Copyright (c) 2012 Eric Lindvall, MIT licensed.
# Trimmed to the subset dyndnsd uses (meters + registry + reporters).
module Metriks
class EWMA
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)
def self.new_m1
new(M1_ALPHA, INTERVAL)
end
def self.new_m5
new(M5_ALPHA, INTERVAL)
end
def self.new_m15
new(M15_ALPHA, INTERVAL)
end
def initialize(alpha, interval)
@alpha = alpha
@interval = interval
@initialized = false
@rate = Atomic.new(0.0)
@uncounted = Atomic.new(0)
end
def clear
@initialized = false
@rate.value = 0.0
@uncounted.value = 0
end
def update(value)
@uncounted.update { |v| v + value }
end
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
def rate
@rate.value
end
end
end