class PxxBuckets
Attributes
ary[R]
buckets[R]
total[R]
Public Class Methods
new(options)
click to toggle source
# File lib/vpsb_client/datafiles/pxx_aggregator.rb, line 35 def initialize(options) i = 0 @ary = [ 0 ] ranges = options.fetch(:ranges) ranges.keys.sort.each do |ceiling| while(i < ceiling) i += ranges[ceiling] i = i.round(2) if i.is_a?(Float) # float decimal errors @ary << i end end @buckets = {} @ary.each do |min| @buckets[min] = 0 end @pxx_keys = options.fetch(:pxx_keys) @total = 0 end
Public Instance Methods
[](key)
click to toggle source
# File lib/vpsb_client/datafiles/pxx_aggregator.rb, line 64 def [](key) value(key.to_f/100.0) end
each_key(&block)
click to toggle source
# File lib/vpsb_client/datafiles/pxx_aggregator.rb, line 60 def each_key(&block) keys.each(&block) end
empty?()
click to toggle source
# File lib/vpsb_client/datafiles/pxx_aggregator.rb, line 74 def empty? return total==0 end
find_bucket_key(n)
click to toggle source
# File lib/vpsb_client/datafiles/pxx_aggregator.rb, line 88 def find_bucket_key(n) idx = (0...@ary.size).bsearch do |i| @ary[i] > n end idx ? idx - 1 : @ary.size - 1 end
increment(n)
click to toggle source
# File lib/vpsb_client/datafiles/pxx_aggregator.rb, line 68 def increment(n) k = find_bucket_key(n) @buckets[@ary[k]] += 1 @total += 1 end
keys()
click to toggle source
# File lib/vpsb_client/datafiles/pxx_aggregator.rb, line 56 def keys @pxx_keys end
value(percent)
click to toggle source
# File lib/vpsb_client/datafiles/pxx_aggregator.rb, line 78 def value(percent) target = @total.to_f * (1.0 - percent) sum = 0 @buckets.keys.sort.reverse.each do |floor| sum += @buckets[floor] # puts "target=#{target} floor=#{floor} sum=#{sum}" return floor if sum >= target end end