Class: Profiler::MCP::Tools::QueryConsoleProfiles

Inherits:
Object
  • Object
show all
Defined in:
lib/profiler/mcp/tools/query_console_profiles.rb

Constant Summary collapse

ALL_FIELDS =
%w[time expression return_value status duration queries token].freeze

Class Method Summary collapse

Class Method Details

.call(params) ⇒ Object



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
44
45
46
47
48
# File 'lib/profiler/mcp/tools/query_console_profiles.rb', line 9

def self.call(params)
  limit = params["limit"]&.to_i || 20
  fetch_size = [limit * 5, 500].min
  profiles = Profiler.storage.list(limit: fetch_size)

  consoles = profiles.select { |p| p.profile_type == "console" }

  if params["expression"]
    term = params["expression"].downcase
    consoles = consoles.select do |p|
      console_data = p.collector_data("console")
      console_data && console_data["expression"].to_s.downcase.include?(term)
    end
  end

  if params["status"]
    consoles = consoles.select do |p|
      case params["status"]
      when "completed" then p.status == 200
      when "failed"    then p.status != 200
      else false
      end
    end
  end

  if params["min_duration"]
    min_ms = params["min_duration"].to_f
    consoles = consoles.select { |p| p.duration >= min_ms }
  end

  if params["cursor"]
    cutoff = Time.parse(params["cursor"]) rescue nil
    consoles = consoles.select { |p| p.started_at < cutoff } if cutoff
  end

  consoles = consoles.first(limit)
  fields = params["fields"]&.map(&:to_s)

  [{ type: "text", text: format_console_table(consoles, fields, limit) }]
end

.format_console_table(consoles, 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
# File 'lib/profiler/mcp/tools/query_console_profiles.rb', line 52

def self.format_console_table(consoles, fields, limit)
  return "No console profiles found matching the criteria." if consoles.empty?

  fields ||= ALL_FIELDS
  fields = fields & ALL_FIELDS

  lines = []
  lines << "# Console Profiles\n"
  lines << "Found #{consoles.size} console execution#{consoles.size > 1 ? "s" : ""}:\n"

  header = fields.map { |f| f.split("_").map(&:capitalize).join(" ") }.join(" | ")
  separator = fields.map { "------" }.join("|")
  lines << "| #{header} |"
  lines << "|#{separator}|"

  consoles.each do |profile|
    console_data = profile.collector_data("console") || {}
    db_data = profile.collector_data("database") || {}

    row = fields.map do |f|
      case f
      when "time"         then profile.started_at.strftime("%H:%M:%S")
      when "expression"   then console_data["expression"].to_s.then { |e| e.length > 60 ? "#{e[0, 57]}..." : e }
      when "return_value" then console_data["return_value"].to_s.then { |v| v.length > 80 ? "#{v[0, 77]}..." : v }
      when "status"       then profile.status == 200 ? "completed" : "failed"
      when "duration"     then "#{profile.duration.round(2)}ms"
      when "queries"      then db_data["total_queries"].to_i.to_s
      when "token"        then profile.token.to_s
      end
    end
    lines << "| #{row.join(' | ')} |"
  end

  if consoles.size == limit
    lines << ""
    lines << "*Next cursor: #{consoles.last.started_at.iso8601}*"
  end

  lines.join("\n")
end