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.
|
|
module Semantic module Subscribers # LogSubscriber for event_group :active_record class ActiveRecord < LogSubscriber include AnsiColors
IGNORE_PAYLOAD_NAMES = %w[SCHEMA EXPLAIN].freeze TRANSACTION_TAINT = AnsiColors::DARK_TEXT_CYAN
def sql(event) name = event.payload[:name] return if IGNORE_PAYLOAD_NAMES.include?(name)
category, model, *remaining = name.split.reverse return if category == 'TRANSACTION'
case category when 'Count' statement_taint = AnsiColors::TEXT_GRAY_300 name = "#{category} #{model}" when 'Load' name = if event.payload[:cached] statement_taint = AnsiColors::TEXT_GRAY_300 name = "Cache #{model}" else statement_taint = TEXT_BLUE row_count = event.payload[:row_count] name = "#{category} #{row_count} #{model.pluralize(row_count)}" end when 'Update', 'Create', 'Destroy' name = "#{category} #{model}" category_taint = TRANSACTION_TAINT statement_taint = TEXT__PINK increment_transaction_local(event.payload[:transaction].uuid, category.downcase.to_sym) else raise "unknown sql category: <#{category}>" end name = "#{name} #{remaining.join(' ')}" if remaining.any? name = colorize(name, category_taint) if category_taint statement = colorize(event.payload[:sql], statement_taint) logger.debug("#{name} #{statement}") end
def start_transaction(event) logger.info(colorize('Begin', TRANSACTION_TAINT)) end
def transaction(event) outcome = colorize(event.payload[:outcome].capitalize, TRANSACTION_TAINT) summary = transaction_summary(event.payload[:transaction]) logger.info("#{outcome} (#{summary})") end
def instantiation(event); end
def strict_loading_violation(event) = any_hook event
private
def transaction_summary(transaction) transaction_local(transaction.uuid) .select { |_, value| value.positive? } .map { |k, v| "#{v} #{k.to_s.pluralize(v)}" } .join(',') end
def increment_transaction_local(transaction_id, sql_command) = transaction_local(transaction_id)[sql_command] += 1
def thread_local = Thread.current[self.class.to_s] ||= {}
def transaction_local(transaction_id) thread_local[transaction_id] ||= { update: 0, create: 0, destroy: 0 } end end end end
|