Class: RailsAiContext::Tools::PerformanceCheck

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/rails_ai_context/tools/performance_check.rb

Constant Summary

Constants inherited from BaseTool

BaseTool::SESSION_CONTEXT, BaseTool::SHARED_CACHE

Class Method Summary collapse

Methods inherited from BaseTool

abstract!, abstract?, cache_key, cached_context, config, extract_method_source_from_file, extract_method_source_from_string, find_closest_match, fuzzy_find_key, inherited, not_found_response, paginate, rails_app, registered_tools, reset_all_caches!, reset_cache!, session_queries, session_record, session_reset!, set_call_params, text_response

Class Method Details

.call(model: nil, category: "all", detail: "standard", server_context: nil) ⇒ Object



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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rails_ai_context/tools/performance_check.rb', line 34

def self.call(model: nil, category: "all", detail: "standard", server_context: nil)
  data = cached_context[:performance]

  unless data.is_a?(Hash) && !data[:error]
    return text_response("No performance data available. Ensure :performance introspector is enabled.")
  end

  model = model.to_s.strip if model

  # Validate model exists if specified
  if model && !model.empty?
    models_data = cached_context[:models]
    if models_data.is_a?(Hash) && !models_data[:error]
      model_names = models_data.keys.map(&:to_s)
      unless model_names.any? { |m| m.downcase == model.downcase }
        return not_found_response("Model", model, model_names,
          recovery_tool: "Call rails_performance_check() without model filter to see all issues")
      end
    end
  end

  lines = [ "# Performance Analysis", "" ]

  # Collect all items then filter, so the count reflects actual displayed results
  all_sections = {}
  all_sections[:n_plus_one] = data[:n_plus_one_risks] || []
  all_sections[:counter_cache] = data[:missing_counter_cache] || []
  all_sections[:indexes] = data[:missing_fk_indexes] || []
  all_sections[:model_all] = data[:model_all_in_controllers] || []
  all_sections[:eager_load] = data[:eager_load_candidates] || []

  # Apply model filter to count
  filtered_count = if model && !model.empty?
    all_sections.values.sum { |items| filter_items(items, model).size }
  elsif category != "all"
    (all_sections[category.to_sym] || []).size
  else
    all_sections.values.sum(&:size)
  end

  lines << "**Total issues found:** #{filtered_count}"
  lines << ""

  if detail == "summary"
    n1_items = filter_items(all_sections[:n_plus_one], model)
    lines << "- N+1 risks: #{n1_items.size}#{risk_summary_counts(n1_items)}"
    lines << "- Missing counter_cache: #{filter_items(all_sections[:counter_cache], model).size}"
    lines << "- Missing FK indexes: #{filter_items(all_sections[:indexes], model).size}"
    lines << "- Model.all in controllers: #{filter_items(all_sections[:model_all], model).size}"
    lines << "- Eager load candidates: #{filter_items(all_sections[:eager_load], model).size}"
  else
    if category == "all" || category == "n_plus_one"
      lines.concat(render_n_plus_one_section(data[:n_plus_one_risks], model, detail))
    end
    if category == "all" || category == "counter_cache"
      lines.concat(render_section("Missing counter_cache", data[:missing_counter_cache], model, detail))
    end
    if category == "all" || category == "indexes"
      lines.concat(render_section("Missing FK Indexes", data[:missing_fk_indexes], model, detail))
    end
    if category == "all" || category == "model_all"
      lines.concat(render_section("Model.all in Controllers", data[:model_all_in_controllers], model, detail))
    end
    if category == "all" || category == "eager_load"
      lines.concat(render_section("Eager Load Candidates", data[:eager_load_candidates], model, detail))
    end
  end

  if filtered_count == 0
    lines << "No performance issues detected#{model && !model.empty? ? " for #{model}" : ""}. Your app looks good!"
  end

  text_response(lines.join("\n"))
end