bin/request-log-analyzer in wvanbergen-request-log-analyzer-0.2.2 vs bin/request-log-analyzer in wvanbergen-request-log-analyzer-0.3.0

- old
+ new

@@ -1,115 +1,48 @@ #!/usr/bin/ruby +require File.dirname(__FILE__) + '/../lib/request_log_analyzer' require File.dirname(__FILE__) + '/../lib/command_line/arguments' -require File.dirname(__FILE__) + '/../lib/base/log_parser' -require File.dirname(__FILE__) + '/../lib/base/summarizer' -require File.dirname(__FILE__) + '/../lib/rails_analyzer/log_parser' -require File.dirname(__FILE__) + '/../lib/rails_analyzer/summarizer' -require File.dirname(__FILE__) + '/../lib/merb_analyzer/log_parser' -require File.dirname(__FILE__) + '/../lib/merb_analyzer/summarizer' -require File.dirname(__FILE__) + '/../lib/bashcolorizer' -require File.dirname(__FILE__) + '/../lib/ruby-progressbar/progressbar.rb' puts "Request log analyzer, by Willem van Bergen and Bart ten Brinke\n\n" -# Substitutes variable elements in a url (like the id field) with a fixed string (like ":id") -# This is used to aggregate simular requests. -# <tt>request</tt> The request to evaluate. -# Returns uniformed url string. -# Raises on mailformed request. -def request_hasher(request) - if request[:url] - url = request[:url].downcase.split(/^http[s]?:\/\/[A-z0-9\.-]+/).last.split('?').first # only the relevant URL part - url << '/' if url[-1] != '/'[0] && url.length > 1 # pad a trailing slash for consistency - - url.gsub!(/\/\d+-\d+-\d+(\/|$)/, '/:date') # Combine all (year-month-day) queries - url.gsub!(/\/\d+-\d+(\/|$)/, '/:month') # Combine all date (year-month) queries - url.gsub!(/\/\d+[\w-]*/, '/:id') # replace identifiers in URLs - - return url - elsif request[:controller] && request[:action] - return "#{request[:controller]}##{request[:action]}" - else - raise 'Cannot hash this request! ' + request.inspect - end -end - -# Print results using a ASCII table. -# <tt>summarizer</tt> The summarizer containg information to draw the table. -# <tt>field</tt> The field containing the data to be printed -# <tt>amount</tt> The length of the table (defaults to 20) -def print_table(summarizer, field, amount = 20) - summarizer.sort_actions_by(field).reverse[0, amount.to_i].each do |a| - # As we show count by default, show totaltime if we sort by count - field = :total_time if field == :count - - puts "%-50s: %10.03fs [#{green("%d requests")}]" % [a[0], a[1][field], a[1][:count]] - end -end - # Parse the arguments given via commandline begin - $arguments = CommandLine::Arguments.parse do |command_line| - command_line.switch(:guess_database_time, :g) - command_line.switch(:fast, :f) - command_line.switch(:colorize, :z) - command_line.switch(:merb, :m) - command_line.flag(:output, :alias => :o) - command_line.flag(:amount, :alias => :c) - command_line.required_files = 1 + arguments = CommandLine::Arguments.parse do |command_line| + + #command_line.flag(:install, :alias => :i) # command_line.command(:install) + + command_line.flag(:format, :default => 'rails') + command_line.flag(:aggregator, :alias => :a, :multiple => true) + command_line.flag(:database, :alias => :d) + + command_line.switch(:combined_requests, :c) + command_line.switch(:colorize, :z) + #command_line.switch(:estimate_database_time, :e) + #command_line.switch(:fast, :f) # end rescue CommandLine::Error => e puts "ARGUMENT ERROR: " + e.message puts load File.dirname(__FILE__) + "/../output/usage.rb" exit(0) end +# if arguments[:install] +# if arguments[:install] == 'rails' +# require 'ftools' +# if File.directory?('./lib/tasks/') +# File.copy(File.dirname(__FILE__) + '/../tasks/request_log_analyzer.rake', './lib/tasks/request_log_analyze.rake') +# puts "Installed rake tasks." +# puts "To use, run: rake log:analyze" +# else +# puts "Cannot find /lib/tasks folder. Are you in your Rails directory?" +# puts "Installation aborted." +# end +# else +# raise "Cannot perform this install type!" +# end +# exit(0) +# end -if $arguments[:merb] - $summarizer = MerbAnalyzer::Summarizer.new(:calculate_database => $arguments[:guess_database_time]) -else - $summarizer = RailsAnalyzer::Summarizer.new(:calculate_database => $arguments[:guess_database_time]) -end - -if $arguments[:fast] - line_types = [:completed] -elsif $arguments[:merb] - line_types = MerbAnalyzer::LogParser::LOG_LINES.keys -else - line_types = RailsAnalyzer::LogParser::LOG_LINES.keys -end - -# Walk through al the files given via the arguments. -$arguments.files.each do |log_file| - puts "Processing #{line_types.join(', ')} log lines from #{log_file}..." - - if $arguments[:merb] - parser = MerbAnalyzer::LogParser.new(log_file) - else - parser = RailsAnalyzer::LogParser.new(log_file) - end - - # add progress bar - unless $arguments[:fast] - pbar = ProgressBar.new(green(log_file), File.size(log_file)) - parser.progress { |pos, total| (pos == :finished) ? pbar.finish : pbar.set(pos) } - end - - parser.each(*line_types) do |request| - $summarizer.group(request) { |r| request_hasher(r) } - end -end - -# Select the reports to output and generate them. -output_reports = $arguments[:output].split(',') rescue [:timespan, :most_requested, :total_time, :mean_time, :total_db_time, :mean_db_time, :mean_rendering_time, :blockers, :hourly_spread, :errors] - -output_reports.each do |report| - report_location = "#{File.dirname(__FILE__)}/../output/#{report}.rb" - - if File.exist?(report_location) - load report_location - else - puts "\nERROR: Output report #{report} not found!" - end -end +# Run the request_log_analyzer! +request_log_analyzer = RequestLogAnalyzer::Controller.build(arguments).run! \ No newline at end of file