Class: PgInsights::QueryAnalysisService

Inherits:
Object
  • Object
show all
Defined in:
app/services/pg_insights/query_analysis_service.rb

Class Method Summary collapse

Class Method Details

.analyze_query_async(sql, execution_type: "analyze", options: {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'app/services/pg_insights/query_analysis_service.rb', line 35

def analyze_query_async(sql, execution_type: "analyze", options: {})
  if background_jobs_available?
    execution = create_execution_record(sql, execution_type)
    QueryAnalysisJob.perform_later(execution.id, options)
    execution
  else
    # Fallback to synchronous execution
    execute_query(sql, execution_type: execution_type, options: options)
  end
end

.execute_query(sql, execution_type: "execute", options: {}) ⇒ Object



6
7
8
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
# File 'app/services/pg_insights/query_analysis_service.rb', line 6

def execute_query(sql, execution_type: "execute", options: {})
  execution = create_execution_record(sql, execution_type)

  begin
    execution.mark_as_running!

    results = case execution_type.to_s
    when "execute"
               execute_only(sql, options)
    when "analyze"
               analyze_only(sql, options)
    when "both"
               execute_and_analyze(sql, options)
    else
               raise ArgumentError, "Invalid execution_type: #{execution_type}"
    end

    execution.mark_as_completed!(results)
    execution

  rescue => e
    Rails.logger.error "Query analysis failed: #{e.message}"
    Rails.logger.error e.backtrace.join("\n") if Rails.env.development?

    execution.mark_as_failed!(e.message, e.backtrace&.first&.truncate(500))
    execution
  end
end