Skip to content

Instantly share code, notes, and snippets.

@fguillen
Created September 8, 2016 21:16
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 fguillen/79dd62eefc79094e8c3dcd791f611fb6 to your computer and use it in GitHub Desktop.
Save fguillen/79dd62eefc79094e8c3dcd791f611fb6 to your computer and use it in GitHub Desktop.
Script to extract, from a Rails log, all the SELECT queries and the duration of each one in a csv format
require "csv"
module Script
def self.run(input_path, output_path)
log = File.read(input_path)
lines_count = log.lines.count
index = 0
File.open(output_path, "w") do |output_file|
log.each_line do |line|
puts "Reading line [#{index + 1} of #{lines_count}]"
if line.match("SELECT")
ms = line.scan(/\((\d*\.\d+)ms\)/)[0][0]
select = line.scan(/SELECT.*/)[0]
output_file.write("#{ms},\"#{select}\"\n")
end
index += 1
end
end
puts "File written in '#{output_path}'"
end
end
if(ARGV.length != 2)
puts "use: script.rb <path to your Rails log> <file output path>"
exit 1
end
Script.run(ARGV[0], ARGV[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment