summaryrefslogtreecommitdiff
path: root/lib/gitlab/request_profiler/profile.rb
blob: 74f2ec1d0835e1ced404804bb0fec2314ff1763b (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 Gitlab
  module RequestProfiler
    class Profile
      attr_reader :name, :time, :file_path, :request_path, :profile_mode, :type

      alias_method :to_param, :name

      def self.all
        Dir["#{PROFILES_DIR}/*.{html,txt}"].map do |path|
          new(File.basename(path))
        end
      end

      def self.find(name)
        file_path = File.join(PROFILES_DIR, name)
        return unless File.exist?(file_path)

        new(name)
      end

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

        set_attributes
      end

      def content
        File.read("#{PROFILES_DIR}/#{name}")
      end

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

      private

      def set_attributes
        _, path, timestamp, profile_mode, type = name.split(/(.*)_(\d+)_(.*)\.(html|txt)$/)
        @request_path      = path.tr('|', '/')
        @time              = Time.at(timestamp.to_i).utc
        @profile_mode      = profile_mode
        @type              = type
      end
    end
  end
end