summaryrefslogtreecommitdiff
path: root/lib/peek/views/rugged.rb
blob: 7b0ab162b15ad0baa6e012a146a3737e7d43ac5f (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
55
56
57
58
# frozen_string_literal: true

module Peek
  module Views
    class Rugged < View
      def duration
        ::Gitlab::RuggedInstrumentation.query_time
      end

      def calls
        ::Gitlab::RuggedInstrumentation.query_count
      end

      def results
        return {} unless calls > 0

        {
          duration: formatted_duration,
          calls: calls,
          details: details
        }
      end

      private

      def details
        ::Gitlab::RuggedInstrumentation.list_call_details
          .sort { |a, b| b[:duration] <=> a[:duration] }
          .map(&method(:format_call_details))
      end

      def format_call_details(call)
        call.merge(duration: (call[:duration] * 1000).round(3),
                   args: format_args(call[:args]))
      end

      def format_args(args)
        args.map do |arg|
          # Needed to avoid infinite as_json calls
          if arg.is_a?(Gitlab::Git::Repository)
            arg.to_s
          else
            arg
          end
        end
      end

      def formatted_duration
        ms = duration * 1000
        if ms >= 1000
          "%.2fms" % ms
        else
          "%.0fms" % ms
        end
      end
    end
  end
end