output_lines = [ ]
%x[find /home/gravitystorm/public_html/redactions/logs-live/ -name "*.log" -type f -print0 | xargs -0 grep -F "Processing region"].each do |line|

  filename = line.split(":")[0]
  filename = filename.split("logs-live/")[1]

  status = "UNKNOWN"
  tail = `tail /home/gravitystorm/public_html/redactions/logs-live/#{filename}`
  if tail.include?(': Summary')
    tail = tail.split(': Summary')[1]
    summary = ""
    successful_changesets = true
    successful_candidates = true
    failed_changesets = true
    failed_candidates = true
    tail.lines.each do | tailline |
      if tailline.include?('-- :')
        summary += tailline.split('-- : ')[1].strip + "<br>"
        successful_changesets = false if tailline.include?(" 0 successful changesets")
        successful_candidates = false if tailline.include?(" 0 successful candidates")
        failed_changesets = false if tailline.include?(" 0 failed changesets")
        failed_candidates = false if tailline.include?(" 0 failed candidates")
      end
    end
    status = "SUCCESS"
    status = "ZEROS" if not successful_changesets and not successful_candidates
    status = "ERRORS" if failed_changesets or failed_candidates
  elsif tail.include?('Marking region failed')
    status="FAIL"
    summary="Region failed"
  end

  line = line.split("[")[1]
  time = line.split(".")[0]

  region_hash = line.split("region {")[1]
  region_hash.gsub!(":","")
  region_hash.gsub!("=>",":")
  line = "{ filename:\"" + filename + "\", status:\"" + status + "\", summary:\"" + summary.to_s + "\", time:\"" + time + "\", " + region_hash.strip
  output_lines << line 

end

json_output = "[" + output_lines.join(",\n") + "]"

puts "data=" + json_output
