summaryrefslogtreecommitdiff
path: root/lib/gitlab/request_profiler/profile.rb
blob: 76c675658b10f349b64c044e6b941425aa30ff55 (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
# frozen_string_literal: true

module Gitlab
  module RequestProfiler
    class Profile
      attr_reader :name, :time, :file_path, :request_path, :profile_mode, :type

      alias_method :to_param, :name

      def initialize(name)
        @name = name
        @file_path = File.join(PROFILES_DIR, name)

        set_attributes
      end

      def valid?
        @request_path.present?
      end

      def content_type
        case type
        when 'html'
          'text/html'
        when 'txt'
          'text/plain'
        end
      end

      private

      def set_attributes
        matches = name.match(/^(?<path>.*)_(?<timestamp>\d+)(_(?<profile_mode>\w+))?\.(?<type>html|txt)$/)
        return unless matches

        @request_path      = matches[:path].tr('|', '/')
        @time              = Time.at(matches[:timestamp].to_i).utc
        @profile_mode      = matches[:profile_mode] || 'unknown'
        @type              = matches[:type]
      end
    end
  end
end