Welcome to mirror list, hosted at ThFree Co, Russian Federation.

active_record.rb « views « peek « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 77108bb81caa162389f26abe5c9e0132675ddf7d (plain)
1
2
3
4
5
6
7
8
9
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
# frozen_string_literal: true

module Peek
  module Views
    class ActiveRecord < DetailedView
      DEFAULT_THRESHOLDS = {
        calls: 100,
        duration: 3000,
        individual_call: 1000
      }.freeze

      THRESHOLDS = {
        production: {
          calls: 100,
          duration: 15000,
          individual_call: 5000
        }
      }.freeze

      def results
        super.merge(calls: detailed_calls)
      end

      def self.thresholds
        @thresholds ||= THRESHOLDS.fetch(Rails.env.to_sym, DEFAULT_THRESHOLDS)
      end

      private

      def detailed_calls
        "#{calls} (#{cached_calls} cached)"
      end

      def cached_calls
        detail_store.count { |item| item[:cached] == 'cached' }
      end

      def setup_subscribers
        super

        subscribe('sql.active_record') do |_, start, finish, _, data|
          if Gitlab::PerformanceBar.enabled_for_request?
            detail_store << {
              duration: finish - start,
              sql: data[:sql].strip,
              backtrace: Gitlab::BacktraceCleaner.clean_backtrace(caller),
              cached: data[:cached] ? 'cached' : ''
            }
          end
        end
      end
    end
  end
end