Sha256: 758b924fb2903c4bda9998c6b5bde5a812f6c7ac84c43c7b3ce7708acae2553d

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

class FnordMetric::Histogram < Hash

  def initialize
    super{ |h,k| h[k]=0 }
  end

  def [](key)
  	super(key.to_f)
  end

  def []=(key, val)
  	super(key.to_f, val)
  end

  def min
  	keys.sort.first.to_i
  end

  def max
    keys.sort.last.to_i
  end

  def histogram(windows)
    windows = histogram_windows(windows) unless windows.is_a?(Array)
    Hash[windows.map{ |w| [w,0] }].tap do |histo|
      self.each do |k,v|
        histo.detect do |win, wval|          
          histo[win] += v if win.include?(k)
        end
      end
    end
  end

  def json_histogram(windows)
    histogram(windows).to_a.sort do |a, b|
      a[0].first <=> b[0].first
    end.map do |r, v|
      [r.size == 1.0 ? r.last.to_s :
        "#{r.first.round(1).to_s}-#{r.last.round(1).to_s}", v.to_i]
    end.to_json
  end

private

  def histogram_windows(windows)
    _min = min 
    _max = max

    return [(0..1)] if (_max-_min == 0)

    windows.times
      .inject((_min.._max)
      .step(((_max-_min)/windows.to_f)).to_a << _max){ |a,n| 
        a[n]=(a[n]..a[n+1]); a }.take(windows)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fnordmetric-0.9.7 lib/fnordmetric/histogram.rb