lib/request_log_analyzer/controller.rb in request-log-analyzer-1.4.2 vs lib/request_log_analyzer/controller.rb in request-log-analyzer-1.5.0
- old
+ new
@@ -25,34 +25,40 @@
# Copy fields
options[:database] = arguments[:database]
options[:reset_database] = arguments[:reset_database]
options[:debug] = arguments[:debug]
- options[:yaml] = arguments[:dump]
+ options[:yaml] = arguments[:yaml] || arguments[:dump]
options[:mail] = arguments[:mail]
options[:no_progress] = arguments[:no_progress]
options[:format] = arguments[:format]
- options[:output] = arguments[:output]
+ options[:output] = arguments[:output].downcase
options[:file] = arguments[:file]
- options[:format] = arguments[:format]
options[:after] = arguments[:after]
options[:before] = arguments[:before]
options[:reject] = arguments[:reject]
options[:select] = arguments[:select]
options[:boring] = arguments[:boring]
options[:aggregator] = arguments[:aggregator]
options[:report_width] = arguments[:report_width]
options[:report_sort] = arguments[:report_sort]
options[:report_amount] = arguments[:report_amount]
+ options[:mailhost] = arguments[:mailhost]
# Apache format workaround
if arguments[:rails_format]
options[:format] = {:rails => arguments[:rails_format]}
elsif arguments[:apache_format]
options[:format] = {:apache => arguments[:apache_format]}
end
+ # Handle output format casing
+ if options[:output].class == String
+ options[:output] = 'HTML' if options[:output] == 'html'
+ options[:output] = 'FixedWidth' if options[:output] == 'fixedwidth' || options[:output] == 'fixed_width'
+ end
+
# Register sources
if arguments.parameters.length == 1
file = arguments.parameters[0]
if file == '-' || file == 'STDIN'
options.store(:source_files, $stdin)
@@ -64,10 +70,24 @@
end
else
options.store(:source_files, arguments.parameters)
end
+ # Guess file format
+ if !options[:format] && options[:source_files]
+ options[:format] = :rails # Default
+
+ if options[:source_files] != $stdin
+ if options[:source_files].class == String
+ options[:format] = RequestLogAnalyzer::FileFormat.autodetect(options[:source_files])
+
+ elsif options[:source_files].class == Array && options[:source_files].first != $stdin
+ options[:format] = RequestLogAnalyzer::FileFormat.autodetect(options[:source_files].first)
+ end
+ end
+ end
+
build(options)
end
# Build a new controller.
# Returns a new RequestLogAnalyzer::Controller object.
@@ -78,16 +98,17 @@
# * <tt>:boring</tt> Do not show color on STDOUT (Defaults to false).
# * <tt>:before</tt> Drop all requests before this date (Date, DateTime, Time or a String in "YYYY-MM-DD hh:mm:ss" format)
# * <tt>:database</tt> Database file to insert encountered requests to.
# * <tt>:debug</tt> Enables echo aggregator which will echo each request analyzed.
# * <tt>:file</tt> Filestring, File or StringIO.
- # * <tt>:format</tt> :rails, {:apache => 'FORMATSTRING'}, :merb, etcetera or Format Class. (Defaults to :rails).
+ # * <tt>:format</tt> :rails, {:apache => 'FORMATSTRING'}, :merb, :amazon_s3, :mysql or RequestLogAnalyzer::FileFormat class. (Defaults to :rails).
# * <tt>:mail</tt> Email the results to this email address.
- # * <tt>:no_progress</tt> Do not display the progress bar (increases speed).
- # * <tt>:output</tt> :fixed_width, :html or Output class. Defaults to fixed width.
+ # * <tt>:mailhost</tt> Email the results to this mail server.
+ # * <tt>:no_progress</tt> Do not display the progress bar (increases parsing speed).
+ # * <tt>:output</tt> 'FixedWidth', 'HTML' or RequestLogAnalyzer::Output class. Defaults to 'FixedWidth'.
# * <tt>:reject</tt> Reject specific {:field => :value} combination (expects a single hash).
- # * <tt>:report_width</tt> Width or reports in characters. (Defaults to 80)
+ # * <tt>:report_width</tt> Width of reports in characters for FixedWidth reports. (Defaults to 80)
# * <tt>:reset_database</tt> Reset the database before starting.
# * <tt>:select</tt> Select specific {:field => :value} combination (expects a single hash).
# * <tt>:source_files</tt> Source files to analyze. Provide either File, array of files or STDIN.
# * <tt>:yaml</tt> Output to YAML file.
#
@@ -102,20 +123,20 @@
# === Todo
# * Check if defaults work (Aggregator defaults seem wrong).
# * Refactor :database => options[:database], :dump => options[:dump] away from contoller intialization.
def self.build(options)
# Defaults
- options[:output] ||= 'fixed_width'
+ options[:output] ||= 'FixedWidth'
options[:format] ||= :rails
options[:aggregator] ||= [:summarizer]
options[:report_width] ||= 80
options[:report_amount] ||= 20
options[:report_sort] ||= 'sum,mean'
options[:boring] ||= false
# Deprecation warnings
- if options[:dump] && options[:yaml].blank?
+ if options[:dump]
warn "[DEPRECATION] `:dump` is deprecated. Please use `:yaml` instead."
options[:yaml] = options[:dump]
end
# Set the output class
@@ -132,10 +153,10 @@
if options[:file]
output_object = %w[File StringIO].include?(options[:file].class.name) ? options[:file] : File.new(options[:file], "w+")
output_args = {:width => 80, :color => false, :characters => :ascii, :sort => output_sort, :amount => output_amount }
elsif options[:mail]
- output_object = RequestLogAnalyzer::Mailer.new(options[:mail])
+ output_object = RequestLogAnalyzer::Mailer.new(options[:mail], options[:mailhost])
output_args = {:width => 80, :color => false, :characters => :ascii, :sort => output_sort, :amount => output_amount }
else
output_object = STDOUT
output_args = {:width => options[:report_width].to_i, :color => !options[:boring],
:characters => (options[:boring] ? :ascii : :utf), :sort => output_sort, :amount => output_amount }