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::DEFAULT_SESSION, BaseTool::MAX_SESSIONS, BaseTool::MAX_SESSION_ID_LENGTH, BaseTool::SESSION_CONTEXT, BaseTool::SHARED_CACHE

Class Method Summary collapse

Methods inherited from BaseTool

abstract!, abstract?, api_only_app?, api_only_note, cache_key, cached_context, config, current_session, #dedupe_put_patch_routes, detail_param?, error_response, evict_oldest_sessions, extract_method_source_from_file, extract_method_source_from_string, find_closest_match, fuzzy_find_key, guide_row, inherited, introspection_warnings_note, invalid_detail_note, normalize_detail, not_found_response, paginate, rails_app, rails_env_name, registered_tools, reset_all_caches!, reset_cache!, session_from, session_params, session_queries, session_record, session_reset!, static_tier_banner, static_tier_refusal, text_response, touch_session, unavailable_note, with_session, with_session_for

Methods included from SectionFetch

#fetch_section, usable?

Class Method Details

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



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
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rails_ai_context/tools/performance_check.rb', line 41

def self.call(model: nil, category: "all", detail: "standard", server_context: nil)
  fetch_section(:performance, unusable_message: "No performance data available. Ensure :performance introspector is enabled.") do |data|
    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 RailsAiContext::DetailLevel.summary?(detail)
      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
      scoped = []
      scoped << "for #{model}" if model && !model.empty?
      scoped << "in category '#{category}'" if category != "all"

      if scoped.empty?
        lines << "No performance issues detected. Your app looks good!"
      else
        lines << "No issues found #{scoped.join(' ')}. Other categories or models may still have issues - " \
          "call rails_performance_check() without filters for the full report."
      end
    end

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