Class: ActiveRecord::TransactionSubscriber
- Inherits:
-
LogSubscriber
- Object
- LogSubscriber
- ActiveRecord::TransactionSubscriber
- Defined in:
- lib/activerecord/transaction_subscriber.rb,
lib/activerecord/transaction_subscriber/version.rb
Constant Summary collapse
- VERSION =
'0.1.3'
Instance Method Summary collapse
- #logging(sql_statement, tx, end_at) ⇒ Object
- #my_transactions ⇒ Object
- #sql(event) ⇒ Object
- #sql_type(sql) ⇒ Object
Instance Method Details
#logging(sql_statement, tx, end_at) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/activerecord/transaction_subscriber.rb', line 49 def logging(sql_statement, tx, end_at) elapsed_time = ((end_at - tx[:start_at]) * 1000.0).round(1) text = " Transaction #{sql_statement} real time: #{elapsed_time}ms" %i[lock select insert update delete].each do |type| next unless tx[type] count = tx[type].size total = tx[type].sum.round(1) text << ", #{type}: #{count} (#{total}ms)" end info color(text, :magenta, true) end |
#my_transactions ⇒ Object
45 46 47 |
# File 'lib/activerecord/transaction_subscriber.rb', line 45 def my_transactions self.class.transactions ||= [] end |
#sql(event) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/activerecord/transaction_subscriber.rb', line 12 def sql(event) payload = event.payload return if payload[:cached] sql_statement = payload[:sql] case payload[:name] when 'TRANSACTION' case sql_statement when 'BEGIN', 'SAVEPOINT' my_transactions << { start_at: event.time } when 'COMMIT', 'ROLLBACK' tx = my_transactions.pop if tx logging(sql_statement, tx, event.end) end else # Unknown transaction end when *IGNORE_PAYLOAD_NAMES # no-op else tx = my_transactions.last if tx type = sql_type(sql_statement) if type tx[type] ||= [] tx[type] << event.duration end end end end |
#sql_type(sql) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/activerecord/transaction_subscriber.rb', line 61 def sql_type(sql) case sql when /select .*for update/mi, /\A\s*lock/mi :lock when /\A\s*select/i :select when /\A\s*insert/i :insert when /\A\s*update/i :update when /\A\s*delete/i :delete else nil end end |