Class: Profiler::Models::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/profiler/models/profile.rb

Constant Summary collapse

TEXT_BODY_LIMIT =
512 * 1024
BINARY_BODY_LIMIT =
256 * 1024

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request = nil) ⇒ Profile

Returns a new instance of Profile.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/profiler/models/profile.rb', line 17

def initialize(request = nil)
  @token = SecureRandom.hex(16)
  @started_at = Time.now
  @collectors_data = {}
  @collectors_metadata = []
  @parent_token = nil
  @is_ajax = false
  @profile_type = "http"

  if request
    @path = request.path
    @method = request.request_method
    @params = sanitize_params(request.params)
    @headers = extract_headers(request.env)
  end
end

Instance Attribute Details

#collectors_dataObject

Returns the value of attribute collectors_data.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def collectors_data
  @collectors_data
end

#collectors_metadataObject

Returns the value of attribute collectors_metadata.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def 
  @collectors_metadata
end

#durationObject

Returns the value of attribute duration.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def duration
  @duration
end

#finished_atObject

Returns the value of attribute finished_at.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def finished_at
  @finished_at
end

#headersObject

Returns the value of attribute headers.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def headers
  @headers
end

#is_ajaxObject

Returns the value of attribute is_ajax.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def is_ajax
  @is_ajax
end

#memoryObject

Returns the value of attribute memory.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def memory
  @memory
end

#methodObject

Returns the value of attribute method.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def method
  @method
end

#paramsObject

Returns the value of attribute params.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def params
  @params
end

#parent_tokenObject

Returns the value of attribute parent_token.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def parent_token
  @parent_token
end

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def path
  @path
end

#profile_typeObject

Returns the value of attribute profile_type.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def profile_type
  @profile_type
end

#request_bodyObject

Returns the value of attribute request_body.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def request_body
  @request_body
end

#request_body_encodingObject

Returns the value of attribute request_body_encoding.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def request_body_encoding
  @request_body_encoding
end

#response_bodyObject

Returns the value of attribute response_body.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def response_body
  @response_body
end

#response_body_encodingObject

Returns the value of attribute response_body_encoding.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def response_body_encoding
  @response_body_encoding
end

#response_headersObject

Returns the value of attribute response_headers.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def response_headers
  @response_headers
end

#started_atObject

Returns the value of attribute started_at.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def started_at
  @started_at
end

#statusObject

Returns the value of attribute status.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def status
  @status
end

#tokenObject

Returns the value of attribute token.



10
11
12
# File 'lib/profiler/models/profile.rb', line 10

def token
  @token
end

Class Method Details

.deep_stringify_keys(obj) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/profiler/models/profile.rb', line 141

def self.deep_stringify_keys(obj)
  case obj
  when Hash
    obj.transform_keys(&:to_s).transform_values { |v| deep_stringify_keys(v) }
  when Array
    obj.map { |item| deep_stringify_keys(item) }
  else
    obj
  end
end

.from_hash(data) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/profiler/models/profile.rb', line 109

def self.from_hash(data)
  profile = new
  profile.token = data[:token]
  profile.path = data[:path]
  profile.method = data[:method]
  profile.status = data[:status]
  profile.duration = data[:duration]
  profile.memory = data[:memory]
  profile.started_at = data[:started_at] ? Time.parse(data[:started_at]) : nil
  profile.finished_at = data[:finished_at] ? Time.parse(data[:finished_at]) : nil
  profile.params = data[:params]
  profile.headers = data[:headers]
  profile.response_headers = data[:response_headers]
  profile.request_body = data[:request_body]
  profile.request_body_encoding = data[:request_body_encoding] || "text"
  profile.response_body = data[:response_body]
  profile.response_body_encoding = data[:response_body_encoding] || "text"
  profile.parent_token = data[:parent_token]
  profile.is_ajax = data[:is_ajax] || false
  profile.profile_type = data[:profile_type] || "http"

  # Convert collectors_data keys to strings recursively for consistency
  profile.collectors_data = (data[:collectors_data] || {}).transform_keys(&:to_s).transform_values do |value|
    deep_stringify_keys(value)
  end

  # Restore tabs metadata
  profile. = data[:tabs] || []

  profile
end

.from_json(json_string) ⇒ Object



104
105
106
107
# File 'lib/profiler/models/profile.rb', line 104

def self.from_json(json_string)
  data = JSON.parse(json_string, symbolize_names: true)
  from_hash(data)
end

Instance Method Details

#add_collector_data(name, data) ⇒ Object



53
54
55
# File 'lib/profiler/models/profile.rb', line 53

def add_collector_data(name, data)
  @collectors_data[name.to_s] = data
end

#add_collector_metadata(collector) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/profiler/models/profile.rb', line 61

def (collector)
  config = collector.tab_config
  @collectors_metadata << {
    key: config[:key],
    label: config[:label],
    icon: config[:icon],
    priority: config[:priority],
    enabled: config[:enabled],
    default_active: config[:default_active],
    render_mode: collector.render_mode.to_s,
    has_data: collector.has_data?
  }
end

#collector_data(name) ⇒ Object



57
58
59
# File 'lib/profiler/models/profile.rb', line 57

def collector_data(name)
  @collectors_data[name.to_s]
end

#finish(status, response_headers = {}) ⇒ Object



46
47
48
49
50
51
# File 'lib/profiler/models/profile.rb', line 46

def finish(status, response_headers = {})
  @finished_at = Time.now
  @duration = ((@finished_at - @started_at) * 1000).round(2) # milliseconds
  @status = status
  @response_headers = response_headers
end

#set_bodies(request_body:, response_body:, req_content_type:, resp_content_type:) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/profiler/models/profile.rb', line 37

def set_bodies(request_body:, response_body:, req_content_type:, resp_content_type:)
  req  = process_body(request_body, req_content_type)
  resp = process_body(response_body, resp_content_type)
  @request_body          = req[:body]
  @request_body_encoding = req[:encoding]
  @response_body         = resp[:body]
  @response_body_encoding = resp[:encoding]
end

#to_hObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/profiler/models/profile.rb', line 75

def to_h
  {
    profile_type: @profile_type,
    token: @token,
    path: @path,
    method: @method,
    status: @status,
    duration: @duration,
    memory: @memory,
    started_at: @started_at&.iso8601,
    finished_at: @finished_at&.iso8601,
    params: @params,
    headers: @headers,
    response_headers: @response_headers,
    request_body: @request_body,
    request_body_encoding: @request_body_encoding,
    response_body: @response_body,
    response_body_encoding: @response_body_encoding,
    collectors_data: @collectors_data,
    tabs: @collectors_metadata,
    parent_token: @parent_token,
    is_ajax: @is_ajax
  }
end

#to_json(*args) ⇒ Object



100
101
102
# File 'lib/profiler/models/profile.rb', line 100

def to_json(*args)
  to_h.to_json(*args)
end