Class: PxxBuckets

Inherits:
Object
  • Object
show all
Defined in:
lib/vpsb_client/datafiles/pxx_aggregator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(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

Instance Attribute Details

#aryObject (readonly)

Returns the value of attribute ary.



33
34
35
# File 'lib/vpsb_client/datafiles/pxx_aggregator.rb', line 33

def ary
  @ary
end

#bucketsObject (readonly)

Returns the value of attribute buckets.



33
34
35
# File 'lib/vpsb_client/datafiles/pxx_aggregator.rb', line 33

def buckets
  @buckets
end

#totalObject (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

Returns:

  • (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

#keysObject



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