Class: ActiveRecordQueryCounter::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_query_counter/counter.rb

Overview

Data structure for storing query information encountered within a block.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCounter



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_countObject

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_timeObject

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_countObject

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

#thresholdsObject (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_timeFloat?

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_timeFloat?

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_countInteger

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_timeFloat

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

#transactionsArray<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