You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.4 KiB

  1. module Semantic
  2. module Subscribers
  3. # LogSubscriber for event_group :action_view
  4. class ActionView < LogSubscriber
  5. include AnsiColors
  6. REGEX_BASEDIR = %r{^#{Rails.root}/(.*)}
  7. def initialize(*)
  8. super(:view)
  9. @partials = {}
  10. end
  11. def render_partial(event)
  12. identifier = pathname(event.payload[:identifier])
  13. partial_savings = @partials[event.transaction_id] ||= {}
  14. partial_saving = partial_savings[identifier] ||= {}
  15. origin = Rails.backtrace_cleaner.clean(caller)[1] # [0] is related to Instrumentalizer, get rid of it!
  16. partial_saving[origin] = partial_saving.key?(origin) ? partial_saving[origin] + 1 : 1
  17. end
  18. def render_template(event)
  19. layout = event.payload[:layout]
  20. return unless layout
  21. identifier = pathname(event.payload[:identifier])
  22. return unless identifier
  23. logger.debug do
  24. pop_partial_savings(event.transaction_id)
  25. "Rendered template #{identifier}"
  26. end
  27. end
  28. def render_collection(event)
  29. identifier = pathname(event.payload[:identifier])
  30. return unless identifier
  31. count = event.payload[:count]
  32. cache_hits = event.payload[:cache_hits] || 0
  33. logger.debug do
  34. pop_partial_savings(event.transaction_id)
  35. "Rendered collection #{identifier} (#{count} times, #{cache_hits} cached)"
  36. end
  37. end
  38. def render_layout(event)
  39. identifier = pathname(event.payload[:identifier])
  40. return unless identifier
  41. logger.debug do
  42. pop_partial_savings(event.transaction_id)
  43. "Rendered layout #{identifier}"
  44. end
  45. end
  46. private
  47. def pop_partial_savings(transaction_id)
  48. partial_savings = @partials.delete(transaction_id)
  49. partial_savings.each do |partial, origins|
  50. sum_count = origins.values.reduce(:+)
  51. sum_count_info = " (#{sum_count} times in total)" if sum_count > 1
  52. logger.debug("Rendered partial #{partial}#{sum_count_info}")
  53. origins.each do |origin, count|
  54. count_info = colorize("(#{count} times)", TEXT_GRAY_400) if count > 1
  55. logger.debug("#{Semantic::Helper.stackisize(origin, symbol: '⇤')}#{count_info}")
  56. end
  57. end
  58. end
  59. def pathname(location)
  60. m = REGEX_BASEDIR.match(location)
  61. m[1] if m
  62. end
  63. end
  64. end
  65. end