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.

76 lines
2.1 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. count: 0,
  16. first_origin: Rails.backtrace_cleaner.clean(caller)[1]
  17. }
  18. partial_saving[:count] += 1
  19. end
  20. def render_template(event)
  21. layout = event.payload[:layout]
  22. return unless layout
  23. identifier = pathname(event.payload[:identifier])
  24. return unless identifier
  25. logger.debug do
  26. pop_partial_savings(event.transaction_id)
  27. "Rendered template #{identifier}"
  28. end
  29. end
  30. def render_collection(event)
  31. identifier = pathname(event.payload[:identifier])
  32. return unless identifier
  33. count = event.payload[:count]
  34. cache_hits = event.payload[:cache_hits] || 0
  35. logger.debug do
  36. pop_partial_savings(event.transaction_id)
  37. "Rendered collection #{identifier} (#{count} times, #{cache_hits} cached)"
  38. end
  39. end
  40. def render_layout(event)
  41. identifier = pathname(event.payload[:identifier])
  42. return unless identifier
  43. logger.debug do
  44. pop_partial_savings(event.transaction_id)
  45. "Rendered layout #{identifier}"
  46. end
  47. end
  48. private
  49. def pop_partial_savings(transaction_id)
  50. partial_savings = @partials.delete(transaction_id)
  51. partial_savings.each do |partial, saving|
  52. count_info = " (#{saving[:count]} times)" if saving[:count] > 1
  53. logger.debug("Rendered partial #{partial}#{count_info}")
  54. logger.debug(Semantic::Helper.stackisize(saving[:first_origin]))
  55. end
  56. end
  57. def pathname(location)
  58. m = REGEX_BASEDIR.match(location)
  59. m[1] if m
  60. end
  61. end
  62. end
  63. end