Class: Sqreen::Metric::Binning

Inherits:
Base
  • Object
show all
Defined in:
lib/sqreen/metrics/binning.rb

Overview

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

Constant Summary collapse

BASE_KEY =
'base'.freeze
FACTOR_KEY =
'factor'.freeze

Instance Attribute Summary

Attributes inherited from Base

#name, #period, #rule

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Binning

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



17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#next_sample(time) ⇒ Object



36
37
38
39
# 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) ⇒ Object



29
30
31
32
33
34
# 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