Sha256: 7f723c2df4692b1cf6530a6b93278f66b67f1941e87592d6dac5195906d723db

Contents?: true

Size: 1.65 KB

Versions: 19

Compression:

Stored size: 1.65 KB

Contents

module Honeybadger
  class Agent
    # This code comes from batsd
    class MetricsCollection < Array
      PERCENTILE_METHOD_SIGNATURE = /\Apercentile_(.+)\z/.freeze

      # Calculates the sum of values in the array
      def sum
        inject( nil ) { |sum,x| sum ? sum+x : x };
      end

      # Calculates the arithmetic mean of values in the array
      def mean
        self.sum.to_f / self.length
      end

      # Calculates the median of values in the array
      def median
        self.sort[self.length/2]
      end

      # 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
      def percentile(threshold)
        if (count > 1)
          self.sort!
          # strip off the top 100-threshold
          threshold_index = (((100 - threshold).to_f / 100) * count).round
          self[0..-threshold_index].last
        else
          self.first
        end
      end

      # Calculates the mean squared error of values in the array
      def mean_squared
        m = mean
        self.class.new(map{|v| (v-m)**2}).sum
      end

      # Calculates the standard deviatiation of values in the array
      def standard_dev
        (mean_squared/(count-1))**0.5
      end

      # Allow [1,2,3].percentile_90, [1,2,3].percentile(75), etc.
      def method_missing(method, *args, &block)
        if method.to_s =~ PERCENTILE_METHOD_SIGNATURE
          percentile($1.to_i)
        else
          super
        end
      end

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

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
honeybadger-2.0.12 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.11 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.10 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.9 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.8 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.6 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.5 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.4 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.3 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.2 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.1 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.0 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.0.beta.13 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.0.beta.12 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.0.beta.11 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.0.beta.10 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.0.beta.9 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.0.beta.8 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.0.0.beta.7 lib/honeybadger/agent/metrics_collection.rb