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.



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

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.



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

def collectors_data
  @collectors_data
end

#collectors_metadataObject

Returns the value of attribute collectors_metadata.



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

def 
  @collectors_metadata
end

#durationObject

Returns the value of attribute duration.



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

def duration
  @duration
end

#finished_atObject

Returns the value of attribute finished_at.



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

def finished_at
  @finished_at
end

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#is_ajaxObject

Returns the value of attribute is_ajax.



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

def is_ajax
  @is_ajax
end

#memoryObject

Returns the value of attribute memory.



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

def memory
  @memory
end

#methodObject

Returns the value of attribute method.



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

def method
  @method
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

#parent_tokenObject

Returns the value of attribute parent_token.



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

def parent_token
  @parent_token
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#profile_typeObject

Returns the value of attribute profile_type.



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

def profile_type
  @profile_type
end

#request_bodyObject

Returns the value of attribute request_body.



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

def request_body
  @request_body
end

#request_body_encodingObject

Returns the value of attribute request_body_encoding.



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

def request_body_encoding
  @request_body_encoding
end

#response_bodyObject

Returns the value of attribute response_body.



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

def response_body
  @response_body
end

#response_body_encodingObject

Returns the value of attribute response_body_encoding.



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

def response_body_encoding
  @response_body_encoding
end

#response_headersObject

Returns the value of attribute response_headers.



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

def response_headers
  @response_headers
end

#started_atObject

Returns the value of attribute started_at.



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

def started_at
  @started_at
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

Class Method Details

.deep_stringify_keys(obj) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/profiler/models/profile.rb', line 145

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



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
140
141
142
143
# File 'lib/profiler/models/profile.rb', line 113

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



108
109
110
111
# File 'lib/profiler/models/profile.rb', line 108

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



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

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

#add_collector_metadata(collector) ⇒ Object



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

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



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

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

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



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

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



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

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



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

def to_h
  req_body,  req_enc  = decode_body(@request_body,  @request_body_encoding)
  resp_body, resp_enc = decode_body(@response_body, @response_body_encoding)

  {
    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: req_body,
    request_body_encoding: req_enc,
    response_body: resp_body,
    response_body_encoding: resp_enc,
    collectors_data: @collectors_data,
    tabs: @collectors_metadata,
    parent_token: @parent_token,
    is_ajax: @is_ajax
  }
end

#to_json(*args) ⇒ Object



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

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