class Sqreen::Metric::Binning

Takes numbers as samples and bins them (effectively, rounds them). Also collect max values

Constants

BASE_KEY
FACTOR_KEY

Public Class Methods

new(opts={}) click to toggle source

upper bound of i-th bin is factor * base^(i-1)

Calls superclass method Sqreen::Metric::Base::new
# File lib/sqreen/metrics/binning.rb, line 17
def initialize(opts={})
  options = opts.dup
  @base = options.delete(BASE_KEY)
  @factor = options.delete(FACTOR_KEY)
  super(options)
  log_base = Math.log(@base)
  log_factor = Math.log(@factor)
  @inv_log_base = 1 / log_base
  @add_parcel = - log_factor / log_base
  new_sample(Sqreen.time)
end

Public Instance Methods

next_sample(time) click to toggle source
Calls superclass method Sqreen::Metric::Base#next_sample
# File lib/sqreen/metrics/binning.rb, line 36
def next_sample(time)
  return nil if @sample[OBSERVATION_KEY].empty?
  super(time)
end
update(_key, x) click to toggle source
# File lib/sqreen/metrics/binning.rb, line 29
def update(_key, x)
  h = @sample[OBSERVATION_KEY]
  bin = bin_no(x)
  h[bin] += 1
  h['max'] = x if x > h['max']
end

Protected Instance Methods

finalize_sample(time) click to toggle source

these two are called by next_sample

# File lib/sqreen/metrics/binning.rb, line 45
def finalize_sample(time)
  @sample[FINISH_KEY] = time
  h = @sample[OBSERVATION_KEY]
  @sample[OBSERVATION_KEY] = { 'u' => @factor, 'b' => @base, 'v' => h }
end
new_sample(time) click to toggle source
# File lib/sqreen/metrics/binning.rb, line 51
def new_sample(time)
  @sample = {
    OBSERVATION_KEY => Hash.new { |hash, key| hash[key] = 0 },
    START_KEY => time,
  }
end

Private Instance Methods

bin_no(x) click to toggle source
# File lib/sqreen/metrics/binning.rb, line 60
def bin_no(x)
  return 1 if x <= 0
  res = 2 + (@inv_log_base * Math.log(x) + @add_parcel).floor
  res < 1 ? 1 : res
end