From 0153440c1cdd02d65cfad395ee7fcc4a053ecde5 Mon Sep 17 00:00:00 2001 From: pvincent Date: Sun, 22 Sep 2024 12:27:14 +0400 Subject: [PATCH] pretty_binded_statement --- lib/semantic/subscribers/active_record.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/semantic/subscribers/active_record.rb b/lib/semantic/subscribers/active_record.rb index dfdaac0..e8278bf 100644 --- a/lib/semantic/subscribers/active_record.rb +++ b/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]