Class: SqlGenius::Core::Ai::ConnectionAdvisor

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_genius/core/ai/connection_advisor.rb

Overview

Diagnoses connection pool health by gathering connection-related metrics from SHOW GLOBAL STATUS and SHOW GLOBAL VARIABLES, then asking the LLM to distinguish between pool misconfiguration, connection leaks, missing pooling, and traffic saturation.

Constant Summary collapse

STATUS_KEYS =
[
  "Threads_connected",
  "Threads_running",
  "Max_used_connections",
  "Aborted_connects",
  "Aborted_clients",
  "Connections",
  "Threads_created",
].freeze
VARIABLE_KEYS =
[
  "max_connections",
  "wait_timeout",
  "interactive_timeout",
  "thread_cache_size",
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(client, config, connection) ⇒ ConnectionAdvisor

Returns a new instance of ConnectionAdvisor.



28
29
30
31
32
# File 'lib/sql_genius/core/ai/connection_advisor.rb', line 28

def initialize(client, config, connection)
  @client = client
  @config = config
  @connection = connection
end

Instance Method Details

#callObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sql_genius/core/ai/connection_advisor.rb', line 34

def call
  if @connection.server_version.postgresql?
    raise Core::UnsupportedDialect.for_postgresql("Connection Pressure Advisor")
  end

  variables = fetch_variables
  status = fetch_status

  messages = [
    { role: "system", content: system_prompt },
    { role: "user",   content: user_prompt(variables, status) },
  ]
  @client.chat(messages: messages)
end