Class: Chef::Handler::SlowReport

Inherits:
Chef::Handler show all
Defined in:
lib/chef/handler/slow_report.rb

Instance Attribute Summary collapse

Attributes inherited from Chef::Handler

#run_status

Instance Method Summary collapse

Methods inherited from Chef::Handler

#action_collection, #all_resources, #data, exception_handlers, #failed_resources, handler_for, report_handlers, resolve_handler_instance, run_exception_handlers, run_report_handlers, #run_report_safely, #run_report_unsafe, run_start_handlers, #skipped_resources, start_handlers, #unprocessed_resources, #up_to_date_resources, #updated_resources

Constructor Details

#initialize(amount) ⇒ SlowReport

Returns a new instance of SlowReport.



26
27
28
29
# File 'lib/chef/handler/slow_report.rb', line 26

def initialize(amount)
  @amount = Integer(amount) rescue nil
  @amount ||= 10
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



24
25
26
# File 'lib/chef/handler/slow_report.rb', line 24

def amount
  @amount
end

Instance Method Details

#all_recordsObject



51
52
53
# File 'lib/chef/handler/slow_report.rb', line 51

def all_records
  @all_records ||= action_collection&.filtered_collection(unprocessed: false) || []
end

#countObject



55
56
57
58
# File 'lib/chef/handler/slow_report.rb', line 55

def count
  num = all_resources.count
  num > amount ? amount : num
end

#reportObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chef/handler/slow_report.rb', line 31

def report
  if count == 0
    puts "\nNo resources to profile\n\n"
    return
  end

  top = all_records.sort_by(&:elapsed_time).last(amount).reverse
  data = top.map { |r| [ r.new_resource.to_s, r.elapsed_time, r.action, r.new_resource.cookbook_name, r.new_resource.recipe_name, stripped_source_line(r.new_resource) ] }
  puts "\nTop #{count} slowest #{count == 1 ? "resource" : "resources"}:\n\n"
  table = TTY::Table.new(%w{resource elapsed_time action cookbook recipe source}, data)
  rendered = table.render do |renderer|
    renderer.border do
      mid          "-"
      mid_mid      " "
    end
  end
  puts rendered
  puts "\n"
end

#stripped_source_line(resource) ⇒ Object



60
61
62
63
# File 'lib/chef/handler/slow_report.rb', line 60

def stripped_source_line(resource)
  # strip the leading path off of the source line
  resource.source_line&.gsub(%r{.*/cookbooks/}, "")&.gsub(%r{.*/chef-[0-9\.]+/}, "")
end