detailed_view.rb 1 KB
Newer Older
Stan Hu's avatar
Stan Hu committed
1 2 3 4 5 6 7 8 9 10 11 12 13
# frozen_string_literal: true

module Peek
  module Views
    class DetailedView < View
      def results
        {
          duration: formatted_duration,
          calls: calls,
          details: details
        }
      end

14 15 16 17
      def detail_store
        ::Gitlab::SafeRequestStore["#{key}_call_details"] ||= []
      end

Stan Hu's avatar
Stan Hu committed
18 19 20
      private

      def duration
21
        detail_store.map { |entry| entry[:duration] }.sum # rubocop:disable CodeReuse/ActiveRecord
Stan Hu's avatar
Stan Hu committed
22 23 24
      end

      def calls
25
        detail_store.count
Stan Hu's avatar
Stan Hu committed
26 27 28
      end

      def call_details
29
        detail_store
Stan Hu's avatar
Stan Hu committed
30 31 32
      end

      def format_call_details(call)
33
        call.merge(duration: (call[:duration] * 1000).round(3))
Stan Hu's avatar
Stan Hu committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
      end

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

      def formatted_duration
        ms = duration * 1000

        if ms >= 1000
          "%.2fms" % ms
        else
          "%.0fms" % ms
        end
      end
    end
  end
end