Class: RailsVitals::MCP::Tools::GetN1Queries

Inherits:
Base
  • Object
show all
Defined in:
lib/rails_vitals/mcp/tools/get_n1_queries.rb

Constant Summary collapse

TOOL_NAME =
"railsvitals_get_n1_queries"
DESCRIPTION =
<<~DESC.strip
  Returns all N+1 query patterns detected across recent requests, grouped and
  ranked by number of occurrences. Each pattern includes the normalized SQL
  fingerprint, affected endpoints with hit counts, and a deterministic fix
  suggestion (the correct includes() call) derived from ActiveRecord reflection.
  Use this after railsvitals_get_score to identify which N+1 patterns to fix.
DESC
INPUT_SCHEMA =
{
  type: "object",
  properties: {
    limit: {
      type: "integer",
      description: "Maximum number of patterns to return, ordered by occurrences desc. Defaults to 10."
    }
  }
}.freeze
DEFAULT_LIMIT =
10

Instance Method Summary collapse

Methods inherited from Base

definition, tool_name

Instance Method Details

#call(params) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rails_vitals/mcp/tools/get_n1_queries.rb', line 27

def call(params)
  records = RailsVitals.store.all
  return no_data_response if records.empty?

  limit = (params[:limit] || params["limit"] || DEFAULT_LIMIT).to_i
  patterns = Analyzers::NPlusOneAggregator.aggregate(records)

  return no_n1_response if patterns.empty?

  {
    total_patterns: patterns.size,
    shown: [ limit, patterns.size ].min,
    patterns: patterns.first(limit).map { |p| serialize(p) }
  }
end