lib/request_log_analyzer/tracker/duration.rb in wvanbergen-request-log-analyzer-1.0.4 vs lib/request_log_analyzer/tracker/duration.rb in wvanbergen-request-log-analyzer-1.1.0
- old
+ new
@@ -1,25 +1,25 @@
module RequestLogAnalyzer::Tracker
# Analyze the duration of a specific attribute
#
- # Accepts the following options:
+ # Options:
# * <tt>:line_type</tt> The line type that contains the duration field (determined by the category proc).
# * <tt>:if</tt> Proc that has to return !nil for a request to be passed to the tracker.
# * <tt>:title</tt> Title do be displayed above the report
# * <tt>:category</tt> Proc that handles request categorization for given fileformat (REQUEST_CATEGORIZER)
# * <tt>:duration</tt> The field containing the duration in the request hash.
# * <tt>:amount</tt> The amount of lines in the report
#
# The items in the update request hash are set during the creation of the Duration tracker.
#
# Example output:
- # Request duration - top 20 by cumulative time ┃ Hits ┃ Sum. | Avg.
- # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
- # EmployeeController#show.html [GET] ┃ 4742 ┃ 4922.56s ┃ 1.04s
- # EmployeeController#update.html [POST] ┃ 4647 ┃ 2731.23s ┃ 0.59s
- # EmployeeController#index.html [GET] ┃ 5802 ┃ 1477.32s ┃ 0.25s
+ # Request duration - top 20 by cumulative time | Hits | Sum. | Avg.
+ # ---------------------------------------------------------------------------------
+ # EmployeeController#show.html [GET] | 4742 | 4922.56s | 1.04s
+ # EmployeeController#update.html [POST] | 4647 | 2731.23s | 0.59s
+ # EmployeeController#index.html [GET] | 5802 | 1477.32s | 0.25s
# .............
class Duration < RequestLogAnalyzer::Tracker::Base
attr_reader :categories
def prepare
@@ -38,41 +38,36 @@
@categories[category][:count] += 1
@categories[category][:total_duration] += duration
end
end
- def report_table(output = STDOUT, amount = 10, options = {}, &block)
+ def report_table(output, amount = 10, options = {}, &block)
top_categories = @categories.sort { |a, b| yield(b[1]) <=> yield(a[1]) }.slice(0...amount)
- max_cat_length = top_categories.map { |a| a[0].length }.max || 0
- space_left = [options[:report_width] - 33, [max_cat_length + 1, options[:title].length].max].min
-
- output << "\n"
- output << "%-#{space_left+1}s┃ Hits ┃ Sum. | Avg." % [options[:title][0...space_left]] + "\n"
- output << green('━' * options[:report_width], options[:color]) + "\n"
-
- top_categories.each do |(cat, info)|
- hits = info[:count]
- total = "%0.02f" % info[:total_duration]
- avg = "%0.02f" % (info[:total_duration] / info[:count])
- output << "%-#{space_left+1}s┃%8d ┃%9ss ┃%9ss" % [cat[0...space_left], hits, total, avg] + "\n"
+ output.table({:title => options[:title]}, {:title => 'Hits', :align => :right, :min_width => 4},
+ {:title => 'Cumulative', :align => :right, :min_width => 10}, {:title => 'Average', :align => :right, :min_width => 8}) do |rows|
+
+ top_categories.each do |(cat, info)|
+ rows << [cat, info[:count], "%0.02fs" % info[:total_duration], "%0.02fs" % (info[:total_duration] / info[:count])]
+ end
end
+
end
- def report(output = STDOUT, report_width = 80, color = false)
+ def report(output)
options[:title] ||= 'Request duration'
options[:report] ||= [:total, :average]
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", :color => color, :report_width => report_width) { |request| request[:total_duration] / request[:count] }
+ report_table(output, options[:top], :title => "#{options[:title]} - top #{options[:top]} by average time") { |request| request[:total_duration] / request[:count] }
when :total
- report_table(output, options[:top], :title => "#{options[:title]} - top #{options[:top]} by cumulative time", :color => color, :report_width => report_width) { |request| request[:total_duration] }
+ report_table(output, options[:top], :title => "#{options[:title]} - top #{options[:top]} by cumulative time") { |request| request[:total_duration] }
when :hits
- report_table(output, options[:top], :title => "#{options[:title]} - top #{options[:top]} by hits", :color => color, :report_width => report_width) { |request| request[:count] }
+ report_table(output, options[:top], :title => "#{options[:title]} - top #{options[:top]} by hits") { |request| request[:count] }
else
output << "Unknown duration report specified\n"
end
end
end
\ No newline at end of file