Class: FnordMetric::Histogram

Inherits:
Hash
  • Object
show all
Defined in:
lib/fnordmetric/histogram.rb

Instance Method Summary collapse

Constructor Details

#initializeHistogram

Returns a new instance of Histogram.



3
4
5
# File 'lib/fnordmetric/histogram.rb', line 3

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

Instance Method Details

#[](key) ⇒ Object



11
12
13
# File 'lib/fnordmetric/histogram.rb', line 11

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

#[]=(key, val) ⇒ Object



15
16
17
# File 'lib/fnordmetric/histogram.rb', line 15

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

#histogram(windows) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/fnordmetric/histogram.rb', line 27

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

#json_histogram(windows) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/fnordmetric/histogram.rb', line 38

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 : json_value(r), v.to_i]
  end.to_json
end

#json_value(r) ⇒ Object



46
47
48
# File 'lib/fnordmetric/histogram.rb', line 46

def json_value(r)
  "#{r.first.round(@opts[:precision]).to_s}-#{r.last.round(@opts[:precision]).to_s}"
end

#maxObject



23
24
25
# File 'lib/fnordmetric/histogram.rb', line 23

def max
  keys.sort.last.to_i
end

#minObject



19
20
21
# File 'lib/fnordmetric/histogram.rb', line 19

def min
	keys.sort.first.to_i
end

#set_opts(opts = {}) ⇒ Object



7
8
9
# File 'lib/fnordmetric/histogram.rb', line 7

def set_opts(opts = {})
  @opts = opts
end