Module: Helpers

Overview

Library filers file

Constant Summary collapse

REDIS_KEY =

Base parent key for all our Redis doings

Settings.metrics_key || "Machiavelli.Metrics"

Instance Method Summary collapse

Instance Method Details

#data_sanitize(data, start, stop, step) ⇒ Object


177
178
179
180
181
182
183
184
185
# File 'lib/helpers.rb', line 177

def data_sanitize data, start, stop, step
  data = top_tail_pad(data, start, stop, step)

               if @settings.store_settings.interpolate
                       interpolate(data, start, stop, step).to_json
               else
                       data.to_json
               end
end

#delete_metrics_cacheObject

Remove all keys from the redis


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

def delete_metrics_cache
        r = redis_conn
        keys = r.keys REDIS_KEY+'*'
        keys.each { |k| r.del k }
end

#get_json(url, args = {}, error_msg = "") ⇒ Object

Precond: valid URI, optional error parsing lambda Postcond: key-symbolized parsed JSON hash


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/helpers.rb', line 64

def get_json url, args={}, error_msg=""
  uri = URI.parse(url)

  puts "Get JSON: #{uri}" if Rails.env.development?

  http = Net::HTTP.new(uri.host, uri.port)

  if uri.is_a? URI::HTTPS then
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  request = Net::HTTP::Get.new(uri.request_uri)
  if @username
    request.basic_auth(@username,@password);
  end

  begin
    response = http.request(request)
  rescue Errno::EHOSTUNREACH, Errno::ECONNREFUSED, SocketError => e
    raise Store::Error, "#{error_msg}: #{e}"
  end

  if Rails.env.development?
    puts "Response: #{response.code}, body length: #{response.body.length} characters"
    puts "Body: #{response.body[0..50]}..."
  end

  if response.code.match(/2\d\d/)
    return JSON.parse(response.body, symbolize_names: true)
  else
    error = response.body
    error = args[:error_parse].call(error) if args[:error_parse]
    raise Store::Error, "#{error_msg}: #{response.code} - #{error}"
  end
end

#init_lib(type, name, origin, settings) ⇒ Object


131
132
133
134
135
136
137
138
139
# File 'lib/helpers.rb', line 131

def init_lib type, name, origin, settings
  name = name.titleize
  file = File.join(Rails.root, "lib", type.downcase, name.downcase+".rb")
  unless File.exists? file
    raise Store::Error, "Library file #{file} does not exist. Check settings."
  end

  return "#{type}::#{name}".constantize.new origin, settings
end

#init_source(name, origin = nil, settings = {}) ⇒ Object


127
128
129
# File 'lib/helpers.rb', line 127

def init_source name, origin=nil, settings={}
  init_lib "Source", name, origin, settings
end

#init_store(name, origin = nil, settings = {}) ⇒ Object

Library initaliziation helper


123
124
125
# File 'lib/helpers.rb', line 123

def init_store name, origin=nil, settings={}
  init_lib "Store", name, origin, settings
end

#interpolate(data, start, stop, step) ⇒ Object


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/helpers.rb', line 158

def interpolate data, start, stop, step
  lerp = []

  d = {}
  # Format to prefered format and nils to NaN for Interpolate::
  data.each{|i| y = i[:y] || (0.0/0.0); d[i[:x]] = y}

  # Dirty cheater
  path = Interpolate::Points.new(d)
  (start..stop).step(step).each do |x|
    y = path.at(x)
    lerp << {x: x, y: y}
  end

               # Ensure a hard limit on the size of the array before returning
               point_c = (stop - start) / step
               lerp.take(point_c)
end

#is_up?(uri = @base_url) ⇒ Boolean

Test if a URI is responding


19
20
21
22
23
24
25
26
# File 'lib/helpers.rb', line 19

def is_up? uri=@base_url
    begin
      #TODO test head only?
                return true if Net::HTTP.get(URI.parse(uri))
        rescue
                return false
        end
end

#json_metrics(uri, args = {}) ⇒ Object

Wrapper for getting json, with relevant error message


14
15
16
# File 'lib/helpers.rb', line 14

def json_metrics uri, args={}
  get_json uri, args, "Error retrieving #{@settings.title} #{@settings.store} metric"
end

#json_metrics_list(uri, args = {}) ⇒ Object

Wrapper for getting json, with relevant error message


9
10
11
# File 'lib/helpers.rb', line 9

def json_metrics_list uri, args={}
  get_json uri, args, "Error retrieving #{@settings.title} #{@settings.store} metrics list"
end

#keysplit(m) ⇒ Object

Take a string of SEP, KVP, and DELIM ops and split it into a nice hash


39
40
41
42
43
# File 'lib/helpers.rb', line 39

def keysplit m
        keys = Hash[*m.split(DELIM).map{|y| x = y.split(KVP); x.push("") if x.length !=2; x}.flatten]
        keys = Hash[keys.map{|k,v| [URI.decode(k), URI.decode(v)] }]
        keys
end

#mandatory_param(p, sub = nil) ⇒ Object

For a given key, ensure it exists in the Settings hash. Fail if this is not the case

Raises:


102
103
104
105
106
107
108
109
110
# File 'lib/helpers.rb', line 102

def mandatory_param p, sub=nil
  raise Store::Error, "Mandatory configuration #{p} not found in section #{sub} for #{@settings.title} origin" unless @settings[sub]
  param = sub ? @settings[sub][p.to_sym] : @settings[p.to_sym]
  if param.nil?
    raise Store::Error, "Must provide mandatory configuration value for #{p} in #{@settings.title} #{@settings.store}"
  else
    param
  end
end

#optional_param(p, default, sub = nil) ⇒ Object

For a given key, check if it exists in the Settings hash. Otherwise, shrug and use the default given.


113
114
115
116
117
118
119
120
# File 'lib/helpers.rb', line 113

def optional_param p, default, sub=nil
  param = sub ? @settings[sub][p.to_sym] : @settings[p.to_sym]
  if param.nil?
    return default
  else
    param
  end
end

#origin_settings(ostr) ⇒ Object

Given an origin ID, find the key/value in the settings file and return it, plus the search key


29
30
31
32
33
34
35
36
# File 'lib/helpers.rb', line 29

def origin_settings ostr
  settings = Settings.origins.find{|o,k| o.to_s == ostr.to_s}
  unless settings
    # Use a Error store, default Source if no settings found
    return [ostr,OpenStruct.new({store: "Errorstore", source: "Source"})]
  end
  settings
end

#redis_connObject

Create a redis connection object


49
50
51
52
53
# File 'lib/helpers.rb', line 49

def redis_conn
        host = Settings.redis_host || "127.0.0.1"
        port = Settings.redis_port || 6379
        Redis.new(host: host, port: port)
end

#top_tail_pad(data, start, stop, step, pad = nil) ⇒ Object

Data Sanitization


143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/helpers.rb', line 143

def top_tail_pad data, start, stop, step, pad=nil
  padded = []

  data = data.sort{|a,b| a[:x] <=> b[:x]}

  (data[0][:x] - step).step(start, -step).each{|x| padded.push({x:x, y: pad}) }

  padded.reverse!
  padded.concat data

               ((data[-1][:x] + step)..stop).step(step).each{|x| padded.push({x:x, y: pad}) }

  padded.take((stop - start)/step)
end