Class: Profiler::MCP::Tools::QueryTestProfiles
- Inherits:
-
Object
- Object
- Profiler::MCP::Tools::QueryTestProfiles
- Defined in:
- lib/profiler/mcp/tools/query_test_profiles.rb
Constant Summary collapse
- ALL_FIELDS =
%w[time test_name status duration queries n1 token].freeze
Class Method Summary collapse
- .call(params) ⇒ Object
- .count_n1_patterns(queries) ⇒ Object
- .format_tests_table(tests, fields, limit) ⇒ Object
- .normalize_sql(sql) ⇒ Object
Class Method Details
.call(params) ⇒ Object
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 44 45 46 47 48 |
# File 'lib/profiler/mcp/tools/query_test_profiles.rb', line 11 def self.call(params) limit = params["limit"]&.to_i || 20 fetch_size = [limit * 5, 500].min storage = MCP::SlaveSupport.resolve_storage(params) profiles = storage.list(limit: fetch_size) tests = profiles.select { |p| p.profile_type == "test" } if params["test_name"] term = params["test_name"].downcase tests = tests.select do |p| test_data = p.collector_data("test") (test_data&.dig("test_name") || p.path).to_s.downcase.include?(term) end end if params["status"] tests = tests.select do |p| test_data = p.collector_data("test") test_data && test_data["status"] == params["status"] end end if params["min_duration"] min_ms = params["min_duration"].to_f tests = tests.select { |p| p.duration >= min_ms } end if params["cursor"] cutoff = Time.parse(params["cursor"]) rescue nil tests = tests.select { |p| p.started_at < cutoff } if cutoff end tests = tests.first(limit) fields = params["fields"]&.map(&:to_s) [{ type: "text", text: format_tests_table(tests, fields, limit) }] end |
.count_n1_patterns(queries) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/profiler/mcp/tools/query_test_profiles.rb', line 95 def self.count_n1_patterns(queries) return 0 if queries.size < 3 queries.group_by { |q| normalize_sql(q["sql"].to_s) } .count { |_, qs| qs.size >= 3 } end |
.format_tests_table(tests, fields, limit) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/profiler/mcp/tools/query_test_profiles.rb', line 52 def self.format_tests_table(tests, fields, limit) return "No test profiles found matching the criteria." if tests.empty? fields ||= ALL_FIELDS fields = fields & ALL_FIELDS lines = [] lines << "# Test Profiles\n" lines << "Found #{tests.size} tests:\n" header = fields.map { |f| f.split("_").map(&:capitalize).join(" ") }.join(" | ") separator = fields.map { "------" }.join("|") lines << "| #{header} |" lines << "|#{separator}|" tests.each do |profile| test_data = profile.collector_data("test") || {} db_data = profile.collector_data("database") || {} queries = db_data["queries"] || [] n1_count = count_n1_patterns(queries) row = fields.map do |f| case f when "time" then profile.started_at&.strftime("%H:%M:%S") || "-" when "test_name" then (test_data["test_name"] || profile.path).to_s.then { |n| n.length > 60 ? n[0, 57] + "..." : n } when "status" then test_data["status"] || "-" when "duration" then profile.duration ? "#{profile.duration.round(2)}ms" : "-" when "queries" then db_data["total_queries"].to_i.to_s when "n1" then n1_count > 0 ? "⚠ #{n1_count}" : "✓" when "token" then profile.token.to_s end end lines << "| #{row.join(' | ')} |" end if tests.size == limit lines << "" lines << "*Next cursor: #{tests.last.started_at.iso8601}*" end lines.join("\n") end |
.normalize_sql(sql) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/profiler/mcp/tools/query_test_profiles.rb', line 102 def self.normalize_sql(sql) sql.gsub(/\$\d+/, "?") .gsub(/\b\d+\b/, "?") .gsub(/'[^']*'/, "?") .gsub(/"[^"]*"/, "?") .strip end |