lib/request_log_analyzer/controller.rb in request-log-analyzer-1.2.9 vs lib/request_log_analyzer/controller.rb in request-log-analyzer-1.3.0

- old
+ new

@@ -30,18 +30,24 @@ # <tt>arguments<tt> A CommandLine::Arguments hash containing parsed commandline parameters. # <rr>report_with</tt> Width of the report. Defaults to 80. def self.build(arguments) options = { } - options[:database] = arguments[:database] if arguments[:database] + # Database command line options + options[:database] = arguments[:database] if arguments[:database] + options[:reset_database] = arguments[:reset_database] + options[:debug] = arguments[:debug] options[:dump] = arguments[:dump] output_class = RequestLogAnalyzer::Output::const_get(arguments[:output]) if arguments[:file] output_file = File.new(arguments[:file], "w+") options[:output] = output_class.new(output_file, :width => 80, :color => false, :characters => :ascii) + elsif arguments[:mail] + output_mail = RequestLogAnalyzer::Mailer.new(arguments[:mail]) + options[:output] = output_class.new(output_mail, :width => 80, :color => false, :characters => :ascii) else options[:output] = output_class.new(STDOUT, :width => arguments[:report_width].to_i, :color => !arguments[:boring], :characters => (arguments[:boring] ? :ascii : :utf)) end @@ -66,11 +72,11 @@ else options.store(:source_files, arguments.parameters) end controller = Controller.new(RequestLogAnalyzer::Source::LogParser.new(file_format, options), options) - #controller = Controller.new(RequestLogAnalyzer::Source::Database.new(file_format, options), options) + #controller = Controller.new(RequestLogAnalyzer::Source::DatabaseLoader.new(file_format, options), options) options[:parse_strategy] = arguments[:parse_strategy] # register filters if arguments[:after] || arguments[:before] @@ -90,13 +96,13 @@ # register aggregators arguments[:aggregator].each { |agg| controller.add_aggregator(agg.to_sym) } # register the database - controller.add_aggregator(:database) if arguments[:database] && !arguments[:aggregator].include?('database') - controller.add_aggregator(:summarizer) if arguments[:aggregator].empty? - + controller.add_aggregator(:summarizer) if arguments[:aggregator].empty? + controller.add_aggregator(:database_inserter) if arguments[:database] && !arguments[:aggregator].include?('database') + # register the echo aggregator in debug mode controller.add_aggregator(:echo) if arguments[:debug] file_format.setup_environment(controller) @@ -126,10 +132,12 @@ # Pass all warnings to every aggregator so they can do something useful with them. @source.warning = lambda { |type, message, lineno| @aggregators.each { |agg| agg.warning(type, message, lineno) } } if @source # Handle progress messagess @source.progress = lambda { |message, value| handle_progress(message, value) } if @source + + @source.source_changes = lambda { |change, filename| handle_source_change(change, filename) } if @source end # Progress function. # Expects :started with file, :progress with current line and :finished or :interrupted when done. # <tt>message</tt> Current state (:started, :finished, :interupted or :progress). @@ -149,10 +157,15 @@ when :progress @progress_bar.set(value) end end + # Source change handler + def handle_source_change(change, filename) + @aggregators.each { |agg| agg.source_change(change, File.expand_path(filename, Dir.pwd)) } + end + # Adds an aggregator to the controller. The aggregator will be called for every request # that is parsed from the provided sources (see add_source) def add_aggregator(agg) agg = RequestLogAnalyzer::Aggregator.const_get(RequestLogAnalyzer::to_camelcase(agg)) if agg.kind_of?(Symbol) @aggregators << agg.new(@source, @options) @@ -178,12 +191,13 @@ end # Push a request to all the aggregators (@aggregators). # <tt>request</tt> The request to push to the aggregators. def aggregate_request(request) - return unless request + return false unless request @aggregators.each { |agg| agg.aggregate(request) } + return true end # Runs RequestLogAnalyzer # 1. Call prepare on every aggregator # 2. Generate requests from source object @@ -196,12 +210,12 @@ @aggregators.each { |agg| agg.prepare } install_signal_handlers @source.each_request do |request| - aggregate_request(filter_request(request)) break if @interrupted + aggregate_request(filter_request(request)) end @aggregators.each { |agg| agg.finalize } @output.header @@ -215,9 +229,11 @@ puts "Report written to: " + File.expand_path(@output.io.path) puts "Need an expert to analyze your application?" puts "Mail to contact@railsdoctors.com or visit us at http://railsdoctors.com" puts "Thanks for using request-log-analyzer!" @output.io.close + elsif @output.io.kind_of?(RequestLogAnalyzer::Mailer) + @output.io.mail end end def install_signal_handlers Signal.trap("INT") do