Class: RRDSimple
- Inherits:
-
Object
- Object
- RRDSimple
- Defined in:
- lib/rrdsimple.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Method Summary collapse
- #bucket(k, i) ⇒ Object
- #bucket_key(k, i) ⇒ Object
- #buckets(k) ⇒ Object
- #clear(k) ⇒ Object
- #current_bucket ⇒ Object
- #current_epoch ⇒ Object
- #epoch(k) ⇒ Object
- #epochs_ago(k, num) ⇒ Object
- #incr(k, val = 1) ⇒ Object
-
#initialize(opts) ⇒ RRDSimple
constructor
A new instance of RRDSimple.
- #last_bucket(set) ⇒ Object
- #last_epoch(k) ⇒ Object
- #last_epoch_key(k) ⇒ Object
- #relative_bucket(value, i) ⇒ Object
- #set(k, val) ⇒ Object
- #set_last_epoch(k, v = Time.now.utc.to_i) ⇒ Object
- #values(k) ⇒ Object
Constructor Details
#initialize(opts) ⇒ RRDSimple
Returns a new instance of RRDSimple.
8 9 10 11 12 13 |
# File 'lib/rrdsimple.rb', line 8 def initialize(opts) @buckets = opts[:buckets] @step = opts[:step] @debug = opts[:debug] || false @db = opts[:db] || Redis.new end |
Instance Method Details
#bucket(k, i) ⇒ Object
43 44 45 |
# File 'lib/rrdsimple.rb', line 43 def bucket(k,i) @db.get(bucket_key(k,i)).to_i end |
#bucket_key(k, i) ⇒ Object
39 40 41 |
# File 'lib/rrdsimple.rb', line 39 def bucket_key(k,i) "#{k}:#{i}" end |
#buckets(k) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rrdsimple.rb', line 56 def buckets(k) a = [] i = 0 last_b = last_bucket(k) while (i < @buckets) do a.push bucket_key(k,relative_bucket(last_b,i)) i += 1 end a end |
#clear(k) ⇒ Object
102 103 104 105 |
# File 'lib/rrdsimple.rb', line 102 def clear(k) @db.del(last_epoch_key(k)) buckets(k){|b| clear_bucket(b)} end |
#current_bucket ⇒ Object
19 20 21 |
# File 'lib/rrdsimple.rb', line 19 def current_bucket current_epoch % @buckets end |
#current_epoch ⇒ Object
15 16 17 |
# File 'lib/rrdsimple.rb', line 15 def current_epoch Time.now.utc.to_i / @step end |
#epoch(k) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rrdsimple.rb', line 80 def epoch(k) current_e = current_epoch last_e = last_epoch(k) if current_e != last_e [(current_e - last_e).abs, @buckets].min.times do |n| clear_bucket(epochs_ago(k, n)) end set_last_epoch(k, current_e) end bucket_key(k, current_bucket) end |
#epochs_ago(k, num) ⇒ Object
52 53 54 |
# File 'lib/rrdsimple.rb', line 52 def epochs_ago(k, num) bucket_key(k, relative_bucket(current_bucket,num)) end |
#incr(k, val = 1) ⇒ Object
92 93 94 95 |
# File 'lib/rrdsimple.rb', line 92 def incr(k, val=1) debug [:incr, epoch(k), val] @db.incrby(epoch(k), val).to_i end |
#last_bucket(set) ⇒ Object
35 36 37 |
# File 'lib/rrdsimple.rb', line 35 def last_bucket(set) last_epoch(set) % @buckets end |
#last_epoch(k) ⇒ Object
27 28 29 |
# File 'lib/rrdsimple.rb', line 27 def last_epoch(k) @db.get(last_epoch_key(k)).to_i end |
#last_epoch_key(k) ⇒ Object
23 24 25 |
# File 'lib/rrdsimple.rb', line 23 def last_epoch_key(k) "#{k}:epoch" end |
#relative_bucket(value, i) ⇒ Object
47 48 49 50 |
# File 'lib/rrdsimple.rb', line 47 def relative_bucket(value, i) b = value - i b = (b < 0) ? @buckets + b : b end |
#set(k, val) ⇒ Object
97 98 99 100 |
# File 'lib/rrdsimple.rb', line 97 def set(k, val) debug [:set, epoch(k), val] @db.set(epoch(k), val) end |
#set_last_epoch(k, v = Time.now.utc.to_i) ⇒ Object
31 32 33 |
# File 'lib/rrdsimple.rb', line 31 def set_last_epoch(k,v = Time.now.utc.to_i) @db.set(last_epoch_key(k), v) end |
#values(k) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rrdsimple.rb', line 67 def values(k) a = [] i = 0 last_e = last_epoch(k) last_b = last_bucket(k) while (i < @buckets) do v = bucket(k,relative_bucket(last_b,i)) a.push({:value => v, :epoch => last_e - i}) if v != 0 i += 1 end a end |