lib/rails_analyzer/summarizer.rb in wvanbergen-request-log-analyzer-0.1.2 vs lib/rails_analyzer/summarizer.rb in wvanbergen-request-log-analyzer-0.2.2

- old
+ new

@@ -1,52 +1,39 @@ +require 'date' module RailsAnalyzer # Functions to summarize an array of requets. # Can calculate request counts, duratations, mean times etc. of all the requests given. - class Summarizer + class Summarizer < Base::Summarizer - attr_reader :actions - attr_reader :errors - attr_reader :request_count - attr_reader :request_time_graph - attr_reader :first_request_at - attr_reader :last_request_at - - attr_accessor :blocker_duration - DEFAULT_BLOCKER_DURATION = 1.0 - # Initializer. Sets global variables # Options # * <tt>:calculate_database</tt> Calculate the database times if they are not explicitly logged. - def initialize(options = {}) - @actions = {} - @blockers = {} - @errors = {} - @request_count = 0 - @blocker_duration = DEFAULT_BLOCKER_DURATION + def initialize_hook(options = {}) @calculate_database = options[:calculate_database] - @request_time_graph = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] end - - # Check if any of the request parsed had a timestamp. - def has_timestamps? - @first_request_at - end - + # Parse a request string into a hash containing all keys found in the string. # Yields the hash found to the block operator. # <tt>request</tt> The request string to parse. # <tt>&block</tt> Block operator def group(request, &block) request[:duration] ||= 0 case request[:type] - when :started - @first_request_at ||= request[:timestamp] # assume time-based order of file - @last_request_at = request[:timestamp] # assume time-based order of file - @request_time_graph[request[:timestamp][11..12].to_i] +=1 - + when :started + if request[:timestamp] + @first_request_at ||= request[:timestamp] # assume time-based order of file + @last_request_at = request[:timestamp] # assume time-based order of file + @request_time_graph[request[:timestamp][11..12].to_i] +=1 + end + if request[:method] + @methods[request[:method].to_sym] ||= 0 + @methods[request[:method].to_sym] += 1 + else + @methods[:unknown] += 1 + end when :completed @request_count += 1 hash = block_given? ? yield(request) : request.hash @actions[hash] ||= {:count => 0, :total_time => 0.0, :total_db_time => 0.0, :total_rendering_time => 0.0, @@ -80,42 +67,7 @@ @errors[hash][:exception_strings][request[:exception_string]] ||= 0 @errors[hash][:exception_strings][request[:exception_string]] += 1 end end - # Return a list of requests sorted on a specific action field - # <tt>field</tt> The action field to sort by. - # <tt>min_count</tt> Values which fall below this amount are not returned (default nil). - def sort_actions_by(field, min_count = nil) - actions = min_count.nil? ? @actions.to_a : @actions.delete_if { |k, v| v[:count] < min_count}.to_a - actions.sort { |a, b| (a[1][field.to_sym] <=> b[1][field.to_sym]) } - end - - # Returns a list of request blockers sorted by a specific field - # <tt>field</tt> The action field to sort by. - # <tt>min_count</tt> Values which fall below this amount are not returned (default @blocker_duration). - def sort_blockers_by(field, min_count = @blocker_duration) - blockers = min_count.nil? ? @blockers.to_a : @blockers.delete_if { |k, v| v[:count] < min_count}.to_a - blockers.sort { |a, b| a[1][field.to_sym] <=> b[1][field.to_sym] } - end - - # Returns a list of request blockers sorted by a specific field - # <tt>field</tt> The action field to sort by. - # <tt>min_count</tt> Values which fall below this amount are not returned (default @blocker_duration). - def sort_errors_by(field, min_count = nil) - errors = min_count.nil? ? @errors.to_a : @errors.delete_if { |k, v| v[:count] < min_count}.to_a - errors.sort { |a, b| a[1][field.to_sym] <=> b[1][field.to_sym] } - end - - # Calculate the duration of a request - # Returns a DateTime object if possible, 0 otherwise. - def duration - (@last_request_at && @first_request_at) ? (DateTime.parse(@last_request_at) - DateTime.parse(@first_request_at)).round : 0 - end - - # Check if the request time graph usable data. - def request_time_graph? - @request_time_graph.uniq != [0] && duration > 0 - end - end end