Module: StatsdServer::Math

Defined in:
lib/statsdserver/math.rb

Class Method Summary collapse

Class Method Details

.summarize(values, opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/statsdserver/math.rb', line 5

def self.summarize(values, opts = {})
  opts = {
    :percentile => 90,
  }.merge(opts)
  res = {}

  values.sort!
  res[:min] = values[0]
  res[:max] = values[-1]
  res[:mean] = res[:min]
  res[:max_at_threshold] = res[:min]
  if values.length > 1
    threshold_index = ((100 - opts[:percentile]) / 100.0) * values.length
    threshold_count = values.length - threshold_index.round
    valid_values = values.slice(0, threshold_count)
    res[:max_at_threshold] = valid_values[-1]
    sum = 0
    valid_values.each { |v| sum += v }
    res[:mean] = sum / valid_values.length
  end

  return res
end