Module: DulyNoted::Helpers

Included in:
DulyNoted
Defined in:
lib/duly_noted/helpers.rb

Instance Method Summary collapse

Instance Method Details

#assemble_for(options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/duly_noted/helpers.rb', line 15

def assemble_for(options)
  case
  when options[:for].is_a?(String)
    ":#{normalize(options[:for])}"
  when options[:for].is_a?(Array)
    ":" << options[:for].collect{ |x| normalize(x) }.join(":")
  else
    ""
  end
end

#build_key(str, validity_test = true) ⇒ Object



4
5
6
7
8
9
# File 'lib/duly_noted/helpers.rb', line 4

def build_key(str, validity_test=true)
  if validity_test
    raise NotValidMetric if !valid_metric?(str) && !(caller[0] =~ /track/)
  end
  return "dn:" + normalize(str)
end

#fields_for(metric_name, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/duly_noted/helpers.rb', line 50

def fields_for(metric_name, options={})
  key = build_key(metric_name)
  key << assemble_for(options)
  keys = find_keys(key)
  fields = []
  keys.each do |key|
    fields += DulyNoted.redis.smembers("#{key}:fields")
  end
  fields
end

#find_keys(key, redis = nil) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/duly_noted/helpers.rb', line 26

def find_keys(key, redis=nil)
  redis ||= DulyNoted.redis
  keys = []
  keys += redis.keys("#{key}*")
  keys -= redis.keys("#{key}:*:meta")
  keys -= redis.keys("#{key}:ref:*")
  keys -= redis.keys("#{key}*fields")
end

#metricsObject



42
43
44
# File 'lib/duly_noted/helpers.rb', line 42

def metrics
  DulyNoted.redis.smembers build_key("metrics", false)
end

#normalize(str) ⇒ Object



11
12
13
# File 'lib/duly_noted/helpers.rb', line 11

def normalize(str)
  str.downcase.gsub(/[^a-z0-9 ]/i, '').strip
end

#parse_time_range(options) ⇒ Object



35
36
37
38
39
40
# File 'lib/duly_noted/helpers.rb', line 35

def parse_time_range(options)
  if options[:time_range]
    options[:time_start] = options[:time_range].first
    options[:time_end] = options[:time_range].last
  end
end

#valid_metric?(metric_name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/duly_noted/helpers.rb', line 46

def valid_metric?(metric_name)
  DulyNoted.redis.sismember build_key("metrics", false), build_key(metric_name, false)
end