Class: Rhoconnect::Stats::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/rhoconnect/stats/record.rb

Class Method Summary collapse

Class Method Details

.add(metric, value = 1) ⇒ Object



29
30
31
# File 'lib/rhoconnect/stats/record.rb', line 29

def add(metric, value = 1)
  set(metric,value) { |current,value| current.to_i + value } 
end

.get_value(metric) ⇒ Object

Returns simple string metric



71
72
73
# File 'lib/rhoconnect/stats/record.rb', line 71

def get_value(metric)
  Store.get_value(key(metric))
end

.key(metric) ⇒ Object



102
103
104
# File 'lib/rhoconnect/stats/record.rb', line 102

def key(metric)
  "stat:#{metric}"
end

.keys(glob = '*') ⇒ Object



58
59
60
# File 'lib/rhoconnect/stats/record.rb', line 58

def keys(glob='*')
  Store.keys(key(glob)).collect {|c| c[5..-1]}
end

.range(metric, start, finish = -1)) ⇒ Object

Returns the metric data, uses array indexing



81
82
83
# File 'lib/rhoconnect/stats/record.rb', line 81

def range(metric, start, finish = -1)
  Store.zrange(key(metric), start, finish)
end

.record_size(metric) ⇒ Object

Returns the # of records to save for a given metric



92
93
94
95
# File 'lib/rhoconnect/stats/record.rb', line 92

def record_size(metric)
  size = STATS_RECORD_SIZE rescue nil
  size || 60 * 24 * 31 #=> 44640 minutes
end

.reset(metric) ⇒ Object



62
63
64
# File 'lib/rhoconnect/stats/record.rb', line 62

def reset(metric)
  Store.flush_data(key(metric))
end

.reset_allObject



66
67
68
# File 'lib/rhoconnect/stats/record.rb', line 66

def reset_all
  Store.flush_data('stat:*')
end

.resolution(metric) ⇒ Object

Returns the resolution for a given metric, default 60 seconds



86
87
88
89
# File 'lib/rhoconnect/stats/record.rb', line 86

def resolution(metric)
  resolution = STATS_RECORD_RESOLUTION rescue nil
  resolution || 60 #=> 1 minute aggregate
end

.rtype(metric) ⇒ Object

Returns redis object type for a record



98
99
100
# File 'lib/rhoconnect/stats/record.rb', line 98

def rtype(metric)
  Store.doc_type(key(metric))
end

.save_average(metric, value) ⇒ Object

Saves the accumulated average for a resolution in a metric



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rhoconnect/stats/record.rb', line 34

def save_average(metric, value)
  set(metric,value) do |current,value| 
    sum = value
    if current.is_a?(String)
      current,sum = current.split(',')
      current = current.to_f
      sum = sum.to_f+value
    end
    "#{current + 1},#{sum}"
  end  
end

.set(metric, value = 0) ⇒ Object

Add a value to a metric. If zset already has a member, update the existing member with an incremented value by default. Also supports updating the value with a block (useful for averages)



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

def set(metric, value = 0)
  start = (Time.now.to_i / resolution(metric)) * resolution(metric)
  current, current_score = 0, start
  Store.lock(key(metric)) do
    range = Store.zrevrange(key(metric), 0, 0)
    if !range.empty?
      member = range[0]
      m_current = member.split(':')[0]
      m_current_score = Store.zscore(key(metric), member).to_i
      if m_current_score > (start - resolution(metric))
        Store.zrem(key(metric), member)
        current, current_score = m_current, m_current_score
      end
    end
    value = block_given? ? yield(current, value) : value 
    Store.zadd(key(metric), current_score, "#{value}:#{start}")
    Store.zremrangebyscore(key(metric), 0, start - record_size(metric))
  end
end

.set_value(metric, value) ⇒ Object

Sets a string metric



76
77
78
# File 'lib/rhoconnect/stats/record.rb', line 76

def set_value(metric, value)
  Store.set_value(key(metric), value)
end

.update(metric) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rhoconnect/stats/record.rb', line 46

def update(metric)
  if Rhoconnect.stats
    start = Time.now.to_f
    # perform the operations
    yield
    finish = Time.now.to_f
    save_average(metric, finish - start)
  else
    yield
  end
end