const INTERVAL = 5 ; const SECONDS_PER_MINUTE = 60 ; const ONE_MINUTE = 1 ; const FIVE_MINUTES = 5 ; const FIFTEEN_MINUTES = 15 ; const M1_ALPHA = 1 - Math . exp ( - INTERVAL / SECONDS_PER_MINUTE / ONE_MINUTE ) ; const M5_ALPHA = 1 - Math . exp ( - INTERVAL / SECONDS_PER_MINUTE / FIVE_MINUTES ) ; const M15_ALPHA = 1 - Math . exp ( - INTERVAL / SECONDS_PER_MINUTE / FIFTEEN_MINUTES ) ; see see see export class EWMA { alpha ; interval ; initialized = false ; currentRate = 0.0 ; uncounted = 0 ; tickInterval ; constructor ( alpha , interval ) { this . alpha = alpha ; this . interval = ( interval || 5 ) * 1000 ; } return static oneMinuteEWMA ( ) { return new EWMA ( M1_ALPHA , INTERVAL ) ; } return static fiveMinuteEWMA ( ) { return new EWMA ( M5_ALPHA , INTERVAL ) ; } return static fifteenMinuteEWMA ( ) { return new EWMA ( M15_ALPHA , INTERVAL ) ; } update ( n ) { this . uncounted += ( n || 1 ) ; } tick ( count = this . uncounted ) { let instantRate = count / this . interval ; this . uncounted = 0 ; if ( this . initialized ) { this . currentRate += this . alpha * ( instantRate - this . currentRate ) ; } else { this . currentRate = instantRate ; this . initialized = true ; } } getRate ( ) { return this . currentRate * 1000 ; } }