lib/request_log_analyzer/tracker/duration.rb in wvanbergen-request-log-analyzer-1.1.3 vs lib/request_log_analyzer/tracker/duration.rb in wvanbergen-request-log-analyzer-1.1.4

- old
+ new

@@ -47,13 +47,15 @@ else category = options[:category].respond_to?(:call) ? options[:category].call(request) : request[options[:category]] duration = options[:duration].respond_to?(:call) ? options[:duration].call(request) : request[options[:duration]] if !duration.nil? && !category.nil? - @categories[category] ||= {:hits => 0, :cumulative => 0.0} + @categories[category] ||= {:hits => 0, :cumulative => 0.0, :min => duration, :max => duration } @categories[category][:hits] += 1 @categories[category][:cumulative] += duration + @categories[category][:min] = duration if duration < @categories[category][:min] + @categories[category][:max] = duration if duration > @categories[category][:max] end end end def hits(cat) @@ -61,11 +63,19 @@ end def cumulative_duration(cat) categories[cat][:cumulative] end - + + def min_duration(cat) + categories[cat][:min] + end + + def max_duration(cat) + categories[cat][:max] + end + def average_duration(cat) categories[cat][:cumulative] / categories[cat][:hits] end def overall_average_duration @@ -104,15 +114,20 @@ def report_table(output, amount = 10, options = {}, &block) output.title(options[:title]) top_categories = @categories.sort { |a, b| yield(b[1]) <=> yield(a[1]) }.slice(0...amount) - output.table({:title => 'Category', :width => :rest}, {:title => 'Hits', :align => :right, :min_width => 4}, - {:title => 'Cumulative', :align => :right, :min_width => 10}, {:title => 'Average', :align => :right, :min_width => 8}) do |rows| + output.table({:title => 'Category', :width => :rest}, + {:title => 'Hits', :align => :right, :highlight => (options[:sort] == :hits), :min_width => 4}, + {:title => 'Cumulative', :align => :right, :highlight => (options[:sort] == :cumulative), :min_width => 10}, + {:title => 'Average', :align => :right, :highlight => (options[:sort] == :average), :min_width => 8}, + {:title => 'Min', :align => :right, :highlight => (options[:sort] == :min)}, + {:title => 'Max', :align => :right, :highlight => (options[:sort] == :max)}) do |rows| top_categories.each do |(cat, info)| - rows << [cat, info[:hits], "%0.02fs" % info[:cumulative], "%0.02fs" % (info[:cumulative] / info[:hits])] + rows << [cat, info[:hits], "%0.02fs" % info[:cumulative], "%0.02fs" % (info[:cumulative] / info[:hits]), + "%0.02fs" % info[:min], "%0.02fs" % info[:max]] end end end @@ -123,14 +138,14 @@ options[:top] ||= 20 options[:report].each do |report| case report when :average - report_table(output, options[:top], :title => "#{options[:title]} - top #{options[:top]} by average time") { |cat| cat[:cumulative] / cat[:hits] } + report_table(output, options[:top], :title => "#{options[:title]} - top #{options[:top]} by average time", :sort => :average) { |cat| cat[:cumulative] / cat[:hits] } when :cumulative - report_table(output, options[:top], :title => "#{options[:title]} - top #{options[:top]} by cumulative time") { |cat| cat[:cumulative] } + report_table(output, options[:top], :title => "#{options[:title]} - top #{options[:top]} by cumulative time", :sort => :cumulative) { |cat| cat[:cumulative] } when :hits - report_table(output, options[:top], :title => "#{options[:title]} - top #{options[:top]} by hits") { |cat| cat[:hits] } + report_table(output, options[:top], :title => "#{options[:title]} - top #{options[:top]} by hits", :sort => :hits) { |cat| cat[:hits] } else raise "Unknown duration report specified: #{report}!" end end end