Class: RailsVitals::NPlusOnesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/rails_vitals/n_plus_ones_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



3
4
5
6
7
# File 'app/controllers/rails_vitals/n_plus_ones_controller.rb', line 3

def index
  records    = RailsVitals.store.all
  @patterns  = Analyzers::NPlusOneAggregator.aggregate(records)
  @total_requests = records.size
end

#showObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/rails_vitals/n_plus_ones_controller.rb', line 9

def show
  records   = RailsVitals.store.all
  @patterns = Analyzers::NPlusOneAggregator.aggregate(records)
  @pattern  = @patterns.find { |p| pattern_id(p) == params[:id] }

  return render plain: "Pattern not found", status: :not_found unless @pattern

  @affected_requests = records.select do |r|
    r.n_plus_one_patterns.any? do |sql, count|
      normalize_sql(sql) == @pattern[:pattern]
    end
  end

  @estimated_saving_ms     = (@pattern[:occurrences] * 0.5).round(1)
  @avg_saving_per_request  = @affected_requests.size > 0 ?
    (@estimated_saving_ms / @affected_requests.size).round(1) : 0
end