Module: DulyNoted::Helpers
- Included in:
- DulyNoted
- Defined in:
- lib/duly_noted/helpers.rb
Instance Method Summary collapse
- #assemble_for(options) ⇒ Object
- #build_key(str, validity_test = true) ⇒ Object
- #fields_for(metric_name, options = {}) ⇒ Object
- #find_keys(key, redis = nil) ⇒ Object
- #metrics ⇒ Object
- #normalize(str) ⇒ Object
- #parse_time_range(options) ⇒ Object
- #valid_metric?(metric_name) ⇒ Boolean
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() case when [:for].is_a?(String) ":#{normalize([:for])}" when [:for].is_a?(Array) ":" << [: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, ={}) key = build_key(metric_name) key << assemble_for() 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 |
#metrics ⇒ Object
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() if [:time_range] [:time_start] = [:time_range].first [:time_end] = [:time_range].last end end |