Skip to content

Instantly share code, notes, and snippets.

@leandrosilva
Last active December 28, 2015 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leandrosilva/7436242 to your computer and use it in GitHub Desktop.
Save leandrosilva/7436242 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "csv"
require "net/http"
require "date"
require "json"
#
# Get log data
#
def get_log(job_range)
log = {}
from_job, to_job = parse_job_range(job_range)
(from_job..to_job).each do |job_id|
log_entry = get_job_log(job_id)
if log_entry then
puts "get_log: #{log_entry}"
log[job_id] = log_entry
end
end
log
end
def parse_job_range(job_range)
job_range.split("-")
end
def get_job_log(job_id)
url = "http://ec2-111-222-333-444.sa-east-1.compute.amazonaws.com:8080/job/SmartCheckout-Monitoring/#{job_id}/api/json?tree=number,result,id,duration"
response = Net::HTTP.get(URI.parse(url))
log = JSON.parse(response) unless response.include?("404")
if log then
duration = parse_time(log["duration"])
date = parse_date(log["id"])
timeout = parse_time(get_timeout(log["duration"]))
has_timeout = has_timeout(timeout)
{ job: job_id, date: date, duration: duration, timeout: timeout, has_timeout: has_timeout }
end
end
def parse_time(duration_in_ms)
Time.at(duration_in_ms / 1000).gmtime.strftime("%R:%S")
end
def parse_date(date_as_string)
date, time = date_as_string.split("_")
time.gsub!("-", ":")
"#{date} #{time}"
end
def get_timeout(duration_in_ms)
timeout = duration_in_ms - 40000
if timeout > 0 then
timeout
else
0
end
end
def has_timeout(timeout)
if timeout == "00:00:00" then
"no"
else
"yes"
end
end
#
# Write log to a CSV
#
def write_log_to_csv(log, file_name)
CSV.open(file_name, "w") do |rows|
rows << header
log.each do |job, entry|
puts "write_log_to_csv: #{entry}"
rows << line(job, entry)
end
end
puts "Done. #{log.length} jobs exported."
end
def header
["job", "date", "duration", "timeout", "has_timeout"]
end
def line(job, entry)
[job, entry[:date], entry[:duration], entry[:timeout], entry[:has_timeout]]
end
#
# Here we go!
#
job_range = ARGV[0]
csv_file = ARGV[1]
write_log_to_csv get_log(job_range), csv_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment