Skip to content

Instantly share code, notes, and snippets.

@trekr5
Created June 9, 2016 11:38
Show Gist options
  • Save trekr5/46d6411097f37957ec23a900d20a696f to your computer and use it in GitHub Desktop.
Save trekr5/46d6411097f37957ec23a900d20a696f to your computer and use it in GitHub Desktop.
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