Class: Profiler::MCP::Resources::SlowQueries

Inherits:
Object
  • Object
show all
Defined in:
lib/profiler/mcp/resources/slow_queries.rb

Class Method Summary collapse

Class Method Details

.callObject



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
34
35
36
37
38
39
40
41
42
43
# File 'lib/profiler/mcp/resources/slow_queries.rb', line 7

def self.call
  profiles = Profiler.storage.list(limit: 100)
  slow_threshold = Profiler.configuration.slow_query_threshold

  slow_queries = []

  profiles.each do |profile|
    db_data = profile.collector_data("database")
    next unless db_data && db_data["queries"]

    db_data["queries"].each do |query|
      if query["duration"] > slow_threshold
        slow_queries << {
          profile_token: profile.token,
          profile_path: profile.path,
          sql: query["sql"],
          duration: query["duration"].round(2),
          timestamp: profile.started_at&.iso8601
        }
      end
    end
  end

  # Sort by duration descending
  slow_queries.sort_by! { |q| -q[:duration] }
  slow_queries = slow_queries.first(50)

  {
    uri: "profiler://slow-queries",
    mimeType: "application/json",
    text: JSON.pretty_generate({
      threshold: slow_threshold,
      total: slow_queries.size,
      queries: slow_queries
    })
  }
end