Sha256: f86f9da5a95cfa76b64017117ec3fdd1d748d4f5a117b0dbdb6460a1df96e9ce

Contents?: true

Size: 1.62 KB

Versions: 23

Compression:

Stored size: 1.62 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)
        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

      # 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

23 entries across 23 versions & 2 rubygems

Version Path
honeybadger-2.6.1 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.6.0 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.5.3 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.5.2 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.5.1 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.5.0 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.4.1 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.4.0 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.3.3 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.3.2 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.3.2.beta.1 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.3.1 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.3.0 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.2.0 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.2.0.beta.1 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.1.6.beta.1 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.1.5 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.1.4 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.1.3 lib/honeybadger/agent/metrics_collection.rb
honeybadger-2.1.1 lib/honeybadger/agent/metrics_collection.rb