Created
June 9, 2016 11:38
-
-
Save trekr5/46d6411097f37957ec23a900d20a696f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'logger' | |
LOGGER = Logger.new('list.log') | |
def get_data(url) | |
output = %x(curl -ss "#{url}") | |
output_hash = JSON.parse(output) | |
return output_hash | |
end | |
def structure_data(url) | |
names = [] | |
values = Hash.new | |
new_values = Hash.new | |
data = get_data(url) | |
data["aggregations"]["2"]["buckets"].each do |source| | |
names << source["key"] | |
values[source["key"]] = source["doc_count"] | |
end | |
#reduce size of array and hash to 20 | |
new_names = names[0..19] | |
count = 0 | |
values.each do |k, v| | |
if count = 19 | |
new_values[k] = v | |
count += 1 | |
end | |
end | |
return new_names, new_values | |
end | |
def start_scheduler(title, url) | |
# names, values = structure_data(url) | |
SCHEDULER.every '30s', :first_in => 0 do |job| | |
names, values = structure_data(url) | |
items = [] | |
LOGGER.info "#{title}: names: #{names} values: #{values}" | |
names.each do |name| | |
value = values[name] | |
items << {label: name, value: value} | |
LOGGER.info "#{title}: \nitems: #{items}" | |
end | |
begin | |
send_event("#{title}", { items: items}) | |
rescue Exception => err | |
LOGGER.info " ***Data sending failure for #{title} #{Time.now} err=#{err}" | |
LOGGER.info " ***Data sending failure for #{title} #{Time.now} err=#{err.backtrace.join("\n")}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment