Class: RReDis

Inherits:
Object
  • Object
show all
Defined in:
lib/rredis.rb

Constant Summary collapse

VERSION =
'0.1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RReDis

Returns a new instance of RReDis.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rredis.rb', line 8

def initialize(options = {})
  @r = Redis.new
  @default_config = {:steps=>10, :rows=>8640, 
                     :aggregations=>["average", "min", "max"], 
                     :rra => [ {:steps=>60, :rows=>10080, :xff=>0.5},
                               {:steps=>900, :rows=>2976, :xff=>0.5},
                               {:steps=>3600, :rows=>8760, :xff=>0.5}]}
  self.default_config = @default_config


  @script_cache = {}
  @sha_cache = {}
  Dir.glob(File.join(File.dirname(__FILE__), '/lua/*.lua')).each do |file|
    name = File.basename(file, File.extname(file))
    @script_cache[name.to_sym] = File.open(file).read
    @sha_cache[name.to_sym] = @r.script(:load, @script_cache[name.to_sym])
  end
  
end

Instance Attribute Details

#default_configObject

Returns the value of attribute default_config.



6
7
8
# File 'lib/rredis.rb', line 6

def default_config
  @default_config
end

Instance Method Details

#config(metric, config, set_metric = true) ⇒ Object



32
33
34
35
36
# File 'lib/rredis.rb', line 32

def config(metric, config, set_metric=true)
  config[:rra] = config[:rra].sort {|a,b| a[:steps] <=> b[:steps] } if config[:rra]
  @r.set("rrd_#{metric}_config", JSON.dump(config))
  @r.sadd "rrd_metrics_set", metric if set_metric
end

#get(metric, start, stop, method = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/rredis.rb', line 46

def get(metric, start, stop, method = nil)
  resp = @r.evalsha(@sha_cache[:get], :keys => [metric], :argv => [start.to_i, stop.to_i, method])
  if resp
    resp[1].collect! { |x| x.to_f} 
    resp
  else
    [[],[]]
  end
end

#pipelined(&block) ⇒ Object



56
57
58
59
60
# File 'lib/rredis.rb', line 56

def pipelined(&block)
  @r.pipelined do
    yield self
  end
end

#store(metric, timestamp, value = nil) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/rredis.rb', line 38

def store(metric, timestamp, value=nil)
  if value.nil?
    value = timestamp
    timestamp = Time.now
  end
  @r.evalsha(@sha_cache[:store], :keys => [metric], :argv => [value, timestamp.to_f])
end