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.

77 lines
2.2 KiB

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