Class: PulseMeter::Sensor::Timelined::Percentile

Inherits:
PulseMeter::Sensor::Timeline show all
Defined in:
lib/pulse-meter/sensor/timelined/percentile.rb

Overview

Calculates n’th percentile in interval

Direct Known Subclasses

Median

Constant Summary

Constants inherited from PulseMeter::Sensor::Timeline

PulseMeter::Sensor::Timeline::DEFAULTS

Constants included from Mixins::Dumper

Mixins::Dumper::DUMP_REDIS_KEY

Instance Attribute Summary collapse

Attributes inherited from PulseMeter::Sensor::Timeline

#interval, #raw_data_ttl, #reduce_delay, #ttl

Attributes inherited from Base

#name, #redis

Instance Method Summary collapse

Methods inherited from PulseMeter::Sensor::Timeline

#cleanup, #current_interval_id, #current_raw_data_key, #data_key, #event, #get_interval_id, #get_timeline_value, #raw_data_key, #reduce, #reduce_all_raw, reduce_all_raw, #timeline, #timeline_within

Methods included from Mixins::Utils

#assert_positive_integer!, #assert_ranged_float!, #camelize, #camelize_keys, #constantize, #symbolize_keys, #titleize, #uniqid

Methods inherited from Base

#annotate, #annotation, #cleanup, #event

Methods included from Mixins::Dumper

included

Constructor Details

#initialize(name, options) ⇒ Percentile

Returns a new instance of Percentile.



8
9
10
11
# File 'lib/pulse-meter/sensor/timelined/percentile.rb', line 8

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

Instance Attribute Details

#p_valueObject (readonly)

Returns the value of attribute p_value.



6
7
8
# File 'lib/pulse-meter/sensor/timelined/percentile.rb', line 6

def p_value
  @p_value
end

Instance Method Details

#aggregate_event(key, value) ⇒ Object



13
14
15
# File 'lib/pulse-meter/sensor/timelined/percentile.rb', line 13

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

#summarize(key) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/pulse-meter/sensor/timelined/percentile.rb', line 17

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).to_f
  else
    nil
  end
end