Class: RailsPulse::Analysis::NPlusOneDetector

Inherits:
BaseAnalyzer
  • Object
show all
Defined in:
app/services/rails_pulse/analysis/n_plus_one_detector.rb

Constant Summary collapse

REQUEST_GROUPING_WINDOW =
0.1.seconds
REPETITION_THRESHOLD =
3

Instance Attribute Summary

Attributes inherited from BaseAnalyzer

#operations, #query

Instance Method Summary collapse

Methods inherited from BaseAnalyzer

#initialize

Constructor Details

This class inherits a constructor from RailsPulse::Analysis::BaseAnalyzer

Instance Method Details

#analyzeObject



9
10
11
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
44
45
46
47
# File 'app/services/rails_pulse/analysis/n_plus_one_detector.rb', line 9

def analyze
  return default_result if operations.empty?

  analysis = {
    is_likely_n_plus_one: false,
    confidence_score: 0,
    evidence: [],
    suggested_fixes: [],
    execution_patterns: {}
  }

  # Group operations by request cycles
  request_groups = group_operations_by_request

  # Analyze patterns within each request
  request_groups.each do |group|
    pattern_analysis = analyze_request_pattern(group)

    if pattern_analysis[:repetitive_queries]
      analysis[:is_likely_n_plus_one] = true
      analysis[:confidence_score] += pattern_analysis[:confidence]
      analysis[:evidence].concat(pattern_analysis[:evidence])
      analysis[:suggested_fixes].concat(pattern_analysis[:fixes])
    end
  end

  # Normalize confidence score
  analysis[:confidence_score] = [ analysis[:confidence_score], 100 ].min

  # Add execution pattern analysis
  analysis[:execution_patterns] = analyze_execution_patterns

  # Generate ActiveRecord-specific suggestions
  if analysis[:is_likely_n_plus_one]
    analysis[:suggested_fixes].concat(generate_activerecord_fixes)
  end

  analysis
end