Class: VCAP::RollingMetric
- Inherits:
-
Object
- Object
- VCAP::RollingMetric
- Defined in:
- lib/vcap/rolling_metric.rb
Direct Known Subclasses
Instance Method Summary collapse
- #<<(value) ⇒ Object
-
#initialize(duration, num_buckets = 60) ⇒ RollingMetric
constructor
A new instance of RollingMetric.
- #to_json ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(duration, num_buckets = 60) ⇒ RollingMetric
Returns a new instance of RollingMetric.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/vcap/rolling_metric.rb', line 7 def initialize(duration, num_buckets = 60) @duration = duration num_buckets = [@duration, num_buckets].min @bucket_duration = (@duration / num_buckets).to_i @eviction_duration = @bucket_duration * 2 @buckets = [] num_buckets.times do @buckets << {:timestamp => 0, :value => 0, :samples => 0} end end |
Instance Method Details
#<<(value) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/vcap/rolling_metric.rb', line 18 def <<(value) = Time.now.to_i bucket = @buckets[( / @bucket_duration) % @buckets.length] if - bucket[:timestamp] > @eviction_duration bucket[:timestamp] = bucket[:value] = value bucket[:samples] = 1 else bucket[:value] += value bucket[:samples] += 1 end end |
#to_json ⇒ Object
51 52 53 |
# File 'lib/vcap/rolling_metric.rb', line 51 def to_json Yajl::Encoder.encode(value) end |
#value ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/vcap/rolling_metric.rb', line 31 def value = Time.now.to_i = - @duration value = 0 samples = 0 @buckets.each do |bucket| if bucket[:timestamp] > value += bucket[:value] samples += bucket[:samples] end end { :value => value, :samples => samples } end |