Sha256: 5a18bd2ccbcc43745e2f04deab666ed7e83f200213dcf3fd87a62a42e82a8d68
Contents?: true
Size: 1.82 KB
Versions: 18
Compression:
Stored size: 1.82 KB
Contents
# Copyright (c) 2018 Sqreen. All Rights Reserved. # Please refer to our terms for more information: https://www.sqreen.io/terms.html require 'sqreen/metrics/base' module Sqreen module Metric # Takes numbers as samples and bins them (effectively, rounds them). class Binning < Base # upper bound of i-th bin is factor * base^(i-1) def initialize(base, factor) super() @base = base @factor = factor log_base = Math.log(base) log_factor = Math.log(factor) @inv_log_base = 1 / log_base @add_parcel = - log_factor / log_base new_sample(Time.now.utc) end def update(_at, _key, x) h = @sample[OBSERVATION_KEY] bin = bin_no(x) h[bin] += 1 end def next_sample(time) return nil if @sample[OBSERVATION_KEY].empty? super(time) end protected # these two are called by next_sample def finalize_sample(time) @sample[FINISH_KEY] = time h = @sample[OBSERVATION_KEY] @sample[OBSERVATION_KEY] = { 'u' => @factor, 'b' => @base, 'v' => h } end def new_sample(time) @sample = { OBSERVATION_KEY => Hash.new { |hash, key| hash[key] = 0 }, START_KEY => time, } end private def bin_no(x) res = 2 + (@inv_log_base * Math.log(x) + @add_parcel).floor res < 1 ? 1 : res end end # overrides the constructor class ParameterizedBinning < Binning def initialize raise 'Not configured' unless self.class.base && self.class.factor super(self.class.base, self.class.factor) end class << self attr_reader :base, :factor def parameters(opts) @base = opts[:base] @factor = opts[:factor] end end end end end
Version data entries
18 entries across 18 versions & 2 rubygems