Class: Scryer::QueryWatcher::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/scryer/query_watcher.rb

Overview

Everything this module needs to know about one request/job: which SQL shapes ran from which call sites and how many times, which associations got eager-loaded and from where, and which associations were actually read. Scoped per-thread (see .watch) so concurrent requests/jobs on different threads never share state.

Instance Method Summary collapse

Constructor Details

#initializeScope

Returns a new instance of Scope.



36
37
38
39
40
# File 'lib/scryer/query_watcher.rb', line 36

def initialize
  @query_shapes = Hash.new { |h, k| h[k] = Hash.new(0) } # shape => {call_site => count}
  @eager_loads = []      # [{owner:, association:, call_site:}]
  @accessed = Set.new    # "OwnerClass#association" strings
end

Instance Method Details

#findings(n_plus_one_threshold:) ⇒ Object



54
55
56
# File 'lib/scryer/query_watcher.rb', line 54

def findings(n_plus_one_threshold:)
  n_plus_one_findings(n_plus_one_threshold) + unused_eager_load_findings
end

#record_access(owner, association) ⇒ Object



50
51
52
# File 'lib/scryer/query_watcher.rb', line 50

def record_access(owner, association)
  @accessed << "#{owner}##{association}"
end

#record_eager_load(owner, association, call_site) ⇒ Object



46
47
48
# File 'lib/scryer/query_watcher.rb', line 46

def record_eager_load(owner, association, call_site)
  @eager_loads << { owner: owner, association: association, call_site: call_site }
end

#record_query(shape, call_site) ⇒ Object



42
43
44
# File 'lib/scryer/query_watcher.rb', line 42

def record_query(shape, call_site)
  @query_shapes[shape][call_site] += 1
end