Class: AndOne::Detector
- Inherits:
-
Object
- Object
- AndOne::Detector
- Defined in:
- lib/and_one/detector.rb
Overview
Subscribes to ActiveRecord SQL notifications and detects N+1 query patterns. Each instance tracks queries for a single request/block scope.
Constant Summary collapse
- DEFAULT_ALLOW_LIST =
[ %r{active_record/relation.*preload_associations}, %r{active_record/validations/uniqueness} ].freeze
Instance Attribute Summary collapse
-
#detections ⇒ Object
readonly
Returns the value of attribute detections.
Instance Method Summary collapse
- #finish ⇒ Object
-
#initialize(allow_stack_paths: [], ignore_queries: [], min_n_queries: 2) ⇒ Detector
constructor
A new instance of Detector.
Constructor Details
#initialize(allow_stack_paths: [], ignore_queries: [], min_n_queries: 2) ⇒ Detector
Returns a new instance of Detector.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/and_one/detector.rb', line 14 def initialize(allow_stack_paths: [], ignore_queries: [], min_n_queries: 2) @allow_stack_paths = allow_stack_paths @ignore_queries = ignore_queries @min_n_queries = min_n_queries # location_key => count @query_counter = Hash.new(0) # location_key => [sql, ...] @query_holder = Hash.new { |h, k| h[k] = [] } # location_key => caller_locations @query_callers = {} # location_key => additional metadata from AR notification @query_metadata = {} @detections = [] subscribe end |
Instance Attribute Details
#detections ⇒ Object (readonly)
Returns the value of attribute detections.
12 13 14 |
# File 'lib/and_one/detector.rb', line 12 def detections @detections end |
Instance Method Details
#finish ⇒ Object
33 34 35 36 37 |
# File 'lib/and_one/detector.rb', line 33 def finish unsubscribe analyze @detections end |