class RunningStat

Constants

BASE_KEY
VERSION

Public Class Methods

instance(data_bucket, opts = {}) click to toggle source

Returns an instance of RunningStat for the given dataset

# File lib/running_stat.rb, line 15
def self.instance(data_bucket, opts = {})
  new(data_bucket, opts)
end
new(data_bucket, opts = {}) click to toggle source
# File lib/running_stat.rb, line 19
def initialize(data_bucket, opts = {})
  @data_bucket = data_bucket
  @redis = opts[:redis]
end

Public Instance Methods

cardinality() click to toggle source

Returns the number of data points seen, or 0 if the stat does not exist

# File lib/running_stat.rb, line 32
def cardinality
  redis.hget(bucket_key, RedisBackend::COUNT_FIELD).to_i
end
flush() click to toggle source

Resets the stat to reflect an empty dataset

# File lib/running_stat.rb, line 58
def flush
  redis.del(bucket_key)
end
mean() click to toggle source

Returns the arithmetic mean of data points seen, or 0.0 if non-existent

# File lib/running_stat.rb, line 37
def mean
  redis.hget(bucket_key, RedisBackend::MEAN_FIELD).to_f
end
push(datum) click to toggle source

Adds a piece of numerical data to the dataset’s stats

# File lib/running_stat.rb, line 25
def push(datum)
  redis.eval(Lua::PUSH_DATUM, [bucket_key], [Float(datum)])
rescue ArgumentError => e
  raise InvalidDataError.new(e)  # datum was non-numerical
end
std_dev() click to toggle source

Returns the standard deviation of the dataset so far, or raises an InsufficientDataError if insufficient data (< 2 datapoints) has been pushed

# File lib/running_stat.rb, line 53
def std_dev
  Math.sqrt(variance)
end
variance() click to toggle source

Returns the sample variance of the dataset so far, or raises an InsufficientDataError if insufficient data (< 2 datapoints) has been pushed

# File lib/running_stat.rb, line 44
def variance
  redis.eval(Lua::VARIANCE, [bucket_key], []).to_f
rescue Redis::CommandError => e
  raise InsufficientDataError.new(e)  # only CommandError possible
end

Private Instance Methods

bucket_key() click to toggle source
# File lib/running_stat.rb, line 68
def bucket_key
  "#{BASE_KEY}:#{@data_bucket}"
end
redis() click to toggle source
# File lib/running_stat.rb, line 64
def redis
  @redis || Redis.current
end