Class: Honeybadger::Agent::MetricsCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/honeybadger/agent/metrics_collection.rb

Overview

This code comes from batsd

Constant Summary collapse

PERCENTILE_METHOD_SIGNATURE =
/\Apercentile_(.+)\z/.freeze

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Allow [1,2,3].percentile_90, [1,2,3].percentile(75), etc.



46
47
48
49
50
51
52
# File 'lib/honeybadger/agent/metrics_collection.rb', line 46

def method_missing(method, *args, &block)
  if method.to_s =~ PERCENTILE_METHOD_SIGNATURE
    percentile($1.to_i)
  else
    super
  end
end

Instance Method Details

#meanObject

Calculates the arithmetic mean of values in the array



13
14
15
# File 'lib/honeybadger/agent/metrics_collection.rb', line 13

def mean
  self.sum.to_f / self.length
end

#mean_squaredObject

Calculates the mean squared error of values in the array



35
36
37
38
# File 'lib/honeybadger/agent/metrics_collection.rb', line 35

def mean_squared
  m = mean
  self.class.new(map{|v| (v-m)**2}).sum
end

#medianObject

Calculates the median of values in the array



18
19
20
# File 'lib/honeybadger/agent/metrics_collection.rb', line 18

def median
  self.sort[self.length/2]
end

#percentile(threshold) ⇒ Object

Calculates the value of the upper percentile of values in the array. If only a single value is provided in the array, that is returned



25
26
27
28
29
30
31
32
# File 'lib/honeybadger/agent/metrics_collection.rb', line 25

def percentile(threshold)
  return self.first unless count > 1

  self.sort!
  # strip off the top 100-threshold
  threshold_index = (((100 - threshold).to_f / 100) * count).round
  self[0..-threshold_index].last
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/honeybadger/agent/metrics_collection.rb', line 54

def respond_to_missing?(method, include_private = false)
  method.to_s =~ PERCENTILE_METHOD_SIGNATURE or super
end

#standard_devObject

Calculates the standard deviatiation of values in the array



41
42
43
# File 'lib/honeybadger/agent/metrics_collection.rb', line 41

def standard_dev
  (mean_squared/(count-1))**0.5
end

#sumObject

Calculates the sum of values in the array



8
9
10
# File 'lib/honeybadger/agent/metrics_collection.rb', line 8

def sum
  inject( nil ) { |sum,x| sum ? sum+x : x };
end