Class: RailsAiContext::Introspectors::PerformanceIntrospector

Inherits:
Object
  • Object
show all
Extended by:
StaticTier
Defined in:
lib/rails_ai_context/introspectors/performance_introspector.rb

Overview

Static analysis for common performance anti-patterns: N+1 query risks, missing counter_cache, Model.all in controllers, missing foreign key indexes.

Model and class structure is read from the AST. The N+1 detection below deliberately stays on text: a risk is a correlation between a controller action, a query chain and an association touched in an ERB view, and ERB has no Ruby AST to walk. Matching both sides as text keeps them comparable, and every finding here is a heuristic, not a fact.

Constant Summary

Constants included from StaticTier

StaticTier::ALTERNATE_SOURCE, StaticTier::FILES_ONLY, StaticTier::KINDS, StaticTier::RUNTIME_ONLY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StaticTier

answers_statically?, static_tier, static_tier_declared?, unavailable_reason, validate_static_tier!

Constructor Details

#initialize(app) ⇒ PerformanceIntrospector

Returns a new instance of PerformanceIntrospector.



20
21
22
# File 'lib/rails_ai_context/introspectors/performance_introspector.rb', line 20

def initialize(app)
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



18
19
20
# File 'lib/rails_ai_context/introspectors/performance_introspector.rb', line 18

def app
  @app
end

Instance Method Details

#callObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails_ai_context/introspectors/performance_introspector.rb', line 24

def call
  schema_data = load_schema_data
  model_data = load_model_data

  {
    n_plus_one_risks: detect_n_plus_one(model_data),
    missing_counter_cache: detect_missing_counter_cache(model_data, schema_data),
    missing_fk_indexes: detect_missing_fk_indexes(schema_data),
    model_all_in_controllers: detect_model_all_in_controllers,
    eager_load_candidates: detect_eager_load_candidates,
    summary: nil # populated below
  }.tap { |result| result[:summary] = build_summary(result) }
rescue => e
  { error: e.message }
end