Browse Source

pretty_binded_statement

main
pvincent 4 weeks ago
parent
commit
0153440c1c
  1. 18
      lib/semantic/subscribers/active_record.rb

18
lib/semantic/subscribers/active_record.rb

@ -14,16 +14,16 @@ module Semantic
category, model, *remaining = name.split.reverse category, model, *remaining = name.split.reverse
return if category == 'TRANSACTION' return if category == 'TRANSACTION'
statement_taint = AnsiColors::TEXT_GRAY_300
statement_taint = TEXT_BLUE
case category case category
when 'Count' when 'Count'
name = "#{category} #{model}" name = "#{category} #{model}"
when 'Load' when 'Load'
if event.payload[:cached] if event.payload[:cached]
statement_taint = TEXT_GRAY_300
name = "Cache Read #{model}" name = "Cache Read #{model}"
no_stats = true no_stats = true
else else
statement_taint = TEXT_BLUE
row_count = event.payload[:row_count] row_count = event.payload[:row_count]
name = "Read #{row_count} #{model.pluralize(row_count)}" name = "Read #{row_count} #{model.pluralize(row_count)}"
end end
@ -38,8 +38,7 @@ module Semantic
name = "#{name} #{stats_event(event.duration)}" unless no_stats name = "#{name} #{stats_event(event.duration)}" unless no_stats
name = "#{name} #{remaining.join(' ')}" if remaining.any? name = "#{name} #{remaining.join(' ')}" if remaining.any?
name = colorize(name, category_taint) if category_taint name = colorize(name, category_taint) if category_taint
statement = colorize(event.payload[:sql], statement_taint)
statement = colorize(pretty_binded_statement(event), statement_taint)
logger.debug("#{name} #{statement}") logger.debug("#{name} #{statement}")
end end
@ -61,6 +60,17 @@ module Semantic
private private
def pretty_binded_statement(event)
statement = event.payload[:sql].dup
bounds = event.payload[:binds].map(&:value)
return statement if bounds.empty?
bounds.map { |b| boolean_or_numeric?(b) ? b.to_s : "'#{b}'" }
.each_with_index { |sb, index| statement.gsub!("$#{index + 1}", sb) }
statement
end
def boolean_or_numeric?(value) = value.is_a?(Numeric) || value.is_a?(TrueClass) || value.is_a?(FalseClass)
def stats_event(duration)= colorize("(#{number_to_ms(duration)})", TEXT_GRAY_400) def stats_event(duration)= colorize("(#{number_to_ms(duration)})", TEXT_GRAY_400)
def number_to_ms(number) = "#{::ActionController::Base.helpers.number_with_precision(number, precision: 1)}ms" def number_to_ms(number) = "#{::ActionController::Base.helpers.number_with_precision(number, precision: 1)}ms"
def transaction_internal_duration(transaction) = transaction_local(transaction.uuid)[:total_duration] def transaction_internal_duration(transaction) = transaction_local(transaction.uuid)[:total_duration]

Loading…
Cancel
Save