Class: ActiveRecordQueryCounter::Counter
- Inherits:
-
Object
- Object
- ActiveRecordQueryCounter::Counter
- Defined in:
- lib/active_record_query_counter/counter.rb
Overview
Data structure for storing query information encountered within a block.
Instance Attribute Summary collapse
-
#query_count ⇒ Object
Returns the value of attribute query_count.
-
#query_time ⇒ Object
Returns the value of attribute query_time.
-
#row_count ⇒ Object
Returns the value of attribute row_count.
-
#thresholds ⇒ Object
readonly
Returns the value of attribute thresholds.
Instance Method Summary collapse
-
#add_transaction(trace:, start_time:, end_time:) ⇒ void
private
Add a tracked transaction.
-
#first_transaction_start_time ⇒ Float?
Get the time when the first transaction began.
-
#initialize ⇒ Counter
constructor
A new instance of Counter.
-
#last_transaction_end_time ⇒ Float?
Get the time when the last transaction completed.
-
#transaction_count ⇒ Integer
Return the number of transactions that have been tracked by the counter.
-
#transaction_time ⇒ Float
Return the total time spent in transactions that have been tracked by the counter.
-
#transactions ⇒ Array<ActiveRecordQueryCounter::TransactionInfo>
Return an array of transaction information for any transactions that have been tracked by the counter.
Constructor Details
#initialize ⇒ Counter
9 10 11 12 13 14 15 |
# File 'lib/active_record_query_counter/counter.rb', line 9 def initialize @query_count = 0 @row_count = 0 @query_time = 0.0 @transactions_hash = {} @thresholds = ActiveRecordQueryCounter.default_thresholds.dup end |
Instance Attribute Details
#query_count ⇒ Object
Returns the value of attribute query_count.
6 7 8 |
# File 'lib/active_record_query_counter/counter.rb', line 6 def query_count @query_count end |
#query_time ⇒ Object
Returns the value of attribute query_time.
6 7 8 |
# File 'lib/active_record_query_counter/counter.rb', line 6 def query_time @query_time end |
#row_count ⇒ Object
Returns the value of attribute row_count.
6 7 8 |
# File 'lib/active_record_query_counter/counter.rb', line 6 def row_count @row_count end |
#thresholds ⇒ Object (readonly)
Returns the value of attribute thresholds.
7 8 9 |
# File 'lib/active_record_query_counter/counter.rb', line 7 def thresholds @thresholds end |
Instance Method Details
#add_transaction(trace:, start_time:, end_time:) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Add a tracked transaction.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/active_record_query_counter/counter.rb', line 32 def add_transaction(trace:, start_time:, end_time:) trace_transactions = @transactions_hash[trace] if trace_transactions # Memory optimization so that we don't store duplicate traces for every transaction in a loop. trace = trace_transactions.first.trace else trace_transactions = [] @transactions_hash[trace] = trace_transactions end trace_transactions << TransactionInfo.new(start_time: start_time, end_time: end_time, trace: trace) end |
#first_transaction_start_time ⇒ Float?
Get the time when the first transaction began.
63 64 65 |
# File 'lib/active_record_query_counter/counter.rb', line 63 def first_transaction_start_time transactions.first&.start_time end |
#last_transaction_end_time ⇒ Float?
Get the time when the last transaction completed.
71 72 73 |
# File 'lib/active_record_query_counter/counter.rb', line 71 def last_transaction_end_time transactions.max_by(&:end_time)&.end_time end |
#transaction_count ⇒ Integer
Return the number of transactions that have been tracked by the counter.
48 49 50 |
# File 'lib/active_record_query_counter/counter.rb', line 48 def transaction_count @transactions_hash.values.flatten.size end |
#transaction_time ⇒ Float
Return the total time spent in transactions that have been tracked by the counter.
55 56 57 |
# File 'lib/active_record_query_counter/counter.rb', line 55 def transaction_time @transactions_hash.values.flatten.sum(&:elapsed_time) end |
#transactions ⇒ Array<ActiveRecordQueryCounter::TransactionInfo>
Return an array of transaction information for any transactions that have been tracked by the counter.
21 22 23 |
# File 'lib/active_record_query_counter/counter.rb', line 21 def transactions @transactions_hash.values.flatten.sort_by(&:start_time) end |