Sha256: d74a72912d93d08f879c491a01a2876d76349e9117cdde1bb9e50cb85b505a2a

Contents?: true

Size: 838 Bytes

Versions: 9

Compression:

Stored size: 838 Bytes

Contents

module PulseMeter
  module Sensor
    module Timelined
      # Calculates n'th percentile in interval
      class Percentile < Timeline
        attr_reader :p_value

        def initialize(name, options)
          @p_value = assert_ranged_float!(options, :p, 0, 1)
          super(name, options)
        end

        def aggregate_event(key, value)
          redis.zadd(key, value, "#{value}::#{uniqid}")
        end

        def summarize(key)
          count = redis.zcard(key)
          if count > 0
            position = @p_value > 0 ? (@p_value * count).round - 1 : 0
            el = redis.zrange(key, position, position)[0]
            redis.zscore(key, el)
          else
            nil
          end
        end

        private
        
        def deflate(value)
          value.to_f
        end

      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pulse-meter-0.3.1 lib/pulse-meter/sensor/timelined/percentile.rb
pulse-meter-0.3.0 lib/pulse-meter/sensor/timelined/percentile.rb
pulse-meter-0.2.11 lib/pulse-meter/sensor/timelined/percentile.rb
pulse-meter-0.2.10 lib/pulse-meter/sensor/timelined/percentile.rb
pulse-meter-0.2.9 lib/pulse-meter/sensor/timelined/percentile.rb
pulse-meter-0.2.8 lib/pulse-meter/sensor/timelined/percentile.rb
pulse-meter-0.2.7 lib/pulse-meter/sensor/timelined/percentile.rb
pulse-meter-0.2.6 lib/pulse-meter/sensor/timelined/percentile.rb
pulse-meter-0.2.5 lib/pulse-meter/sensor/timelined/percentile.rb