Class: Profiler::MCP::Resources::SlowTests

Inherits:
Object
  • Object
show all
Defined in:
lib/profiler/mcp/resources/slow_tests.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
# File 'lib/profiler/mcp/resources/slow_tests.rb', line 7

def self.call
  profiles = Profiler.storage.list(limit: 500)
  tests = profiles.select { |p| p.profile_type == "test" }
                  .sort_by { |p| -p.duration }
                  .first(10)

  data = tests.map do |profile|
    test_data = profile.collector_data("test") || {}
    db_data   = profile.collector_data("database") || {}
    queries   = db_data["queries"] || []
    n1 = queries.group_by { |q| normalize_sql(q["sql"].to_s) }.any? { |_, qs| qs.size >= 3 }

    {
      token: profile.token,
      test_name: test_data["test_name"] || profile.path,
      status: test_data["status"],
      framework: test_data["framework"],
      file: test_data["test_file"],
      duration_ms: profile.duration&.round(2),
      query_count: db_data["total_queries"].to_i,
      n1_detected: n1,
      timestamp: profile.started_at&.iso8601
    }
  end

  {
    uri: "profiler://slow-tests",
    mimeType: "application/json",
    text: JSON.pretty_generate({ total: data.size, slow_tests: data })
  }
end

.normalize_sql(sql) ⇒ Object



39
40
41
# File 'lib/profiler/mcp/resources/slow_tests.rb', line 39

def self.normalize_sql(sql)
  sql.gsub(/\$\d+/, "?").gsub(/\b\d+\b/, "?").gsub(/'[^']*'/, "?").gsub(/"[^"]*"/, "?").strip
end