Class: QueryGuard::Analyzers::SlowQueryAnalyzer

Inherits:
Base
  • Object
show all
Defined in:
lib/query_guard/analyzers/slow_query_analyzer.rb

Overview

Detects slow queries based on duration threshold.

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#enabled?

Constructor Details

#initialize ⇒ SlowQueryAnalyzer

Returns a new instance of SlowQueryAnalyzer.



7
8
9
# File 'lib/query_guard/analyzers/slow_query_analyzer.rb', line 7

def initialize
  super(:slow_query)
end

Instance Method Details

#analyze(context, config) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/query_guard/analyzers/slow_query_analyzer.rb', line 11

def analyze(context, config)
  findings = []
  threshold = config.max_duration_ms_per_query

  return findings if threshold.nil?

  context.queries.each do |query|
    next if exceeds_ignored_sql?(query.sql, config)
    next unless query.duration_ms > threshold

    findings << Core::FindingBuilders.slow_query(
      query,
      duration_ms: query.duration_ms,
      threshold_ms: threshold,
      severity: config.slow_query_severity || :warn
    )
  end

  findings
end