Browse Source

pretty_binded_statement

main
pvincent 3 months 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
return if category == 'TRANSACTION'
statement_taint = AnsiColors::TEXT_GRAY_300
statement_taint = TEXT_BLUE
case category
when 'Count'
name = "#{category} #{model}"
when 'Load'
if event.payload[:cached]
statement_taint = TEXT_GRAY_300
name = "Cache Read #{model}"
no_stats = true
else
statement_taint = TEXT_BLUE
row_count = event.payload[:row_count]
name = "Read #{row_count} #{model.pluralize(row_count)}"
end
@ -38,8 +38,7 @@ module Semantic
name = "#{name} #{stats_event(event.duration)}" unless no_stats
name = "#{name} #{remaining.join(' ')}" if remaining.any?
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}")
end
@ -61,6 +60,17 @@ module Semantic
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 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]

Loading…
Cancel
Save