Sha256: 4385da554009cd060c9539246d31f7eadf46c75168258056d07e75956bae3808

Contents?: true

Size: 1.29 KB

Versions: 12

Compression:

Stored size: 1.29 KB

Contents

# Copyright (c) 2015 Sqreen. All Rights Reserved.
# Please refer to our terms for more information: https://www.sqreen.com/terms.html

require 'sqreen/exception'

module Sqreen
  module Metric
    OBSERVATION_KEY = 'observation'.freeze
    START_KEY = 'start'.freeze
    FINISH_KEY = 'finish'.freeze
    # Base interface for a metric
    class Base
      def initialize(_opts={})
        @sample = nil
      end

      # Update the current metric with a new observation
      # @param _at [Time] when was the observation made
      # @param _key [String] which aggregation key was it made for
      # @param _value [Object] The observation
      def update(_key, _value)
        raise Sqreen::Exception, 'No current sample' unless @sample
      end

      # create a new empty sample and publish the last one
      # @param time [Float] Time of start of new sample/end of the last one
      def next_sample(time)
        finalize_sample(time) unless @sample.nil?
        current_sample = @sample
        new_sample(time)
        current_sample
      end

      protected

      # @param time [Float]
      def new_sample(time)
        @sample = { OBSERVATION_KEY => {}, START_KEY => time }
      end

      # @param time [Float]
      def finalize_sample(time)
        @sample[FINISH_KEY] = time
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
sqreen-1.18.6-java lib/sqreen/metrics/base.rb
sqreen-1.18.6 lib/sqreen/metrics/base.rb
sqreen-1.18.5-java lib/sqreen/metrics/base.rb
sqreen-1.18.5 lib/sqreen/metrics/base.rb
sqreen-1.18.4-java lib/sqreen/metrics/base.rb
sqreen-1.18.4 lib/sqreen/metrics/base.rb
sqreen-1.18.3-java lib/sqreen/metrics/base.rb
sqreen-1.18.3 lib/sqreen/metrics/base.rb
sqreen-1.18.3.beta2 lib/sqreen/metrics/base.rb
sqreen-1.18.3.beta1 lib/sqreen/metrics/base.rb
sqreen-1.18.2-java lib/sqreen/metrics/base.rb
sqreen-1.18.2 lib/sqreen/metrics/base.rb