Class: Profiler::MCP::Tools::QueryMailers

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

Constant Summary collapse

ALL_FIELDS =
%w[time mailer action subject to mode duration status token].freeze

Class Method Summary collapse

Class Method Details

.call(params) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/profiler/mcp/tools/query_mailers.rb', line 10

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

  emails = []

  profiles.each do |profile|
    mailer_data = profile.collector_data("mailer")
    next unless mailer_data

    all_emails = Array(mailer_data["emails"]) + Array(mailer_data["errors"])
    all_emails.each do |email|
      entry = email.merge("profile_token" => profile.token, "profile_started_at" => profile.started_at)

      if params["mailer_class"]
        next unless entry["mailer_class"].to_s.downcase.include?(params["mailer_class"].downcase)
      end

      if params["action"]
        next unless entry["action"].to_s.downcase.include?(params["action"].downcase)
      end

      if params["delivery_mode"]
        next unless entry["delivery_mode"] == params["delivery_mode"]
      end

      if params["has_error"]
        has_err = !entry["error"].nil?
        next unless has_err == (params["has_error"] == true || params["has_error"] == "true")
      end

      emails << entry
    end

    break if emails.size >= limit * 2
  end

  if params["cursor"]
    cutoff = Time.parse(params["cursor"]) rescue nil
    if cutoff
      emails = emails.select do |e|
        started = e["profile_started_at"]
        started && started < cutoff
      end
    end
  end

  emails = emails.first(limit)

  [{ type: "text", text: format_mailers_table(emails, params["fields"]&.map(&:to_s), limit) }]
end