Module: MetricsDownloader
- Defined in:
- lib/metrics_downloader.rb
Class Method Summary collapse
- .create_urls_from_host_params(host_list, port, route = "metricz/") ⇒ Object
- .download_and_parse_json_from_urls(urls, timeout = 5) ⇒ Object
- .expand_host_expr(host_expr) ⇒ Object
-
.parse_json_from(hosts, port, route = "metricz/", timeout = 5) ⇒ Object
Parses JSON from a list of host expressions, as passed to expand_host_expr.
-
.split_host_exprs(host_exprs_str) ⇒ Object
Splits a comma-delimited string of host exprs.
Class Method Details
.create_urls_from_host_params(host_list, port, route = "metricz/") ⇒ Object
29 30 31 32 33 34 |
# File 'lib/metrics_downloader.rb', line 29 def self.create_urls_from_host_params(host_list, port, route = "metricz/") host_list.map do |host| host = host[7..-1] if host.start_with?("http:") "http://#{host}:#{port}/#{route}" end end |
.download_and_parse_json_from_urls(urls, timeout = 5) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/metrics_downloader.rb', line 7 def self.download_and_parse_json_from_urls(urls, timeout = 5) threads = urls.map do |url| Thread.new do Thread.current[:output] = begin body = RestClient::Request.execute(:method => :get, :url => url, :timeout => timeout, :open_timeout => timeout) JSON.parse(body) rescue # TODO: log the exception? STDERR.puts "WARNING: Unable to parse JSON from #{url}" {} end end end threads.map { |t| t.join; t[:output] } end |
.expand_host_expr(host_expr) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/metrics_downloader.rb', line 39 def self.(host_expr) if host_expr =~ /(\[([0-9.,]+)\])/ = $1 numbers = $2.split(",").map { |item| item.include?("..") ? eval(item).to_a : [item.to_i] }.flatten numbers.map { |n| host_expr.gsub(, n.to_s) } else [host_expr] end end |
.parse_json_from(hosts, port, route = "metricz/", timeout = 5) ⇒ Object
Parses JSON from a list of host expressions, as passed to expand_host_expr. Basically a combination of expand_host_expr, create_urls_from_host_params, and download_and_parse_json_from_urls.
79 80 81 82 83 84 |
# File 'lib/metrics_downloader.rb', line 79 def self.parse_json_from(hosts, port, route = "metricz/", timeout = 5) = hosts.map { |expr| MetricsDownloader.(expr) }.flatten urls = MetricsDownloader.create_urls_from_host_params(, port, route) json_trees = MetricsDownloader.download_and_parse_json_from_urls(urls, timeout) Hash[.zip(json_trees)] end |
.split_host_exprs(host_exprs_str) ⇒ Object
Splits a comma-delimited string of host exprs. This is tricky since there may be commas within brackets
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/metrics_downloader.rb', line 51 def self.split_host_exprs(host_exprs_str) host_exprs = [] expr = "" in_brackets = false host_exprs_str.each_char do |c| if c == "," && !in_brackets host_exprs << expr expr = "" next elsif c == "[" in_brackets = true elsif c == "]" in_brackets = false end expr << c end host_exprs << expr if expr != "" host_exprs end |