Class: PxxBuckets
- Inherits:
-
Object
- Object
- PxxBuckets
- Defined in:
- lib/vpsb_client/datafiles/pxx_aggregator.rb
Instance Attribute Summary collapse
-
#ary ⇒ Object
readonly
Returns the value of attribute ary.
-
#buckets ⇒ Object
readonly
Returns the value of attribute buckets.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #each_key(&block) ⇒ Object
- #empty? ⇒ Boolean
- #find_bucket_key(n) ⇒ Object
- #increment(n) ⇒ Object
-
#initialize(options) ⇒ PxxBuckets
constructor
A new instance of PxxBuckets.
- #keys ⇒ Object
- #value(percent) ⇒ Object
Constructor Details
#initialize(options) ⇒ PxxBuckets
Returns a new instance of PxxBuckets.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/vpsb_client/datafiles/pxx_aggregator.rb', line 35 def initialize() i = 0 @ary = [ 0 ] ranges = .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 = .fetch(:pxx_keys) @total = 0 end |
Instance Attribute Details
#ary ⇒ Object (readonly)
Returns the value of attribute ary.
33 34 35 |
# File 'lib/vpsb_client/datafiles/pxx_aggregator.rb', line 33 def ary @ary end |
#buckets ⇒ Object (readonly)
Returns the value of attribute buckets.
33 34 35 |
# File 'lib/vpsb_client/datafiles/pxx_aggregator.rb', line 33 def buckets @buckets end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
33 34 35 |
# File 'lib/vpsb_client/datafiles/pxx_aggregator.rb', line 33 def total @total end |
Instance Method Details
#[](key) ⇒ Object
64 65 66 |
# File 'lib/vpsb_client/datafiles/pxx_aggregator.rb', line 64 def [](key) value(key.to_f/100.0) end |
#each_key(&block) ⇒ Object
60 61 62 |
# File 'lib/vpsb_client/datafiles/pxx_aggregator.rb', line 60 def each_key(&block) keys.each(&block) end |
#empty? ⇒ Boolean
74 75 76 |
# File 'lib/vpsb_client/datafiles/pxx_aggregator.rb', line 74 def empty? return total==0 end |
#find_bucket_key(n) ⇒ Object
88 89 90 91 92 93 |
# 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) ⇒ Object
68 69 70 71 72 |
# 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 ⇒ Object
56 57 58 |
# File 'lib/vpsb_client/datafiles/pxx_aggregator.rb', line 56 def keys @pxx_keys end |
#value(percent) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# 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 |