Created
September 8, 2016 21:16
-
-
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
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 "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