Class: Profiler::Models::Profile
- Inherits:
-
Object
- Object
- Profiler::Models::Profile
- Defined in:
- lib/profiler/models/profile.rb
Instance Attribute Summary collapse
-
#collectors_data ⇒ Object
Returns the value of attribute collectors_data.
-
#collectors_metadata ⇒ Object
Returns the value of attribute collectors_metadata.
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#finished_at ⇒ Object
Returns the value of attribute finished_at.
-
#gem_version ⇒ Object
Returns the value of attribute gem_version.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#is_ajax ⇒ Object
Returns the value of attribute is_ajax.
-
#memory ⇒ Object
Returns the value of attribute memory.
-
#method ⇒ Object
Returns the value of attribute method.
-
#params ⇒ Object
Returns the value of attribute params.
-
#parent_token ⇒ Object
Returns the value of attribute parent_token.
-
#path ⇒ Object
Returns the value of attribute path.
-
#profile_type ⇒ Object
Returns the value of attribute profile_type.
-
#request_body ⇒ Object
Returns the value of attribute request_body.
-
#request_body_encoding ⇒ Object
Returns the value of attribute request_body_encoding.
-
#response_body ⇒ Object
Returns the value of attribute response_body.
-
#response_body_encoding ⇒ Object
Returns the value of attribute response_body_encoding.
-
#response_headers ⇒ Object
Returns the value of attribute response_headers.
-
#started_at ⇒ Object
Returns the value of attribute started_at.
-
#status ⇒ Object
Returns the value of attribute status.
-
#token ⇒ Object
Returns the value of attribute token.
Class Method Summary collapse
Instance Method Summary collapse
- #add_collector_data(name, data) ⇒ Object
- #add_collector_metadata(collector) ⇒ Object
- #collector_data(name) ⇒ Object
- #finish(status, response_headers = {}) ⇒ Object
-
#initialize(request = nil) ⇒ Profile
constructor
A new instance of Profile.
- #set_bodies(request_body:, response_body:, req_content_type:, resp_content_type:) ⇒ Object
- #to_h ⇒ Object
- #to_json(*args) ⇒ Object
Constructor Details
#initialize(request = nil) ⇒ Profile
Returns a new instance of Profile.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/profiler/models/profile.rb', line 19 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_data ⇒ Object
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_metadata ⇒ Object
Returns the value of attribute collectors_metadata.
11 12 13 |
# File 'lib/profiler/models/profile.rb', line 11 def @collectors_metadata end |
#duration ⇒ Object
Returns the value of attribute duration.
11 12 13 |
# File 'lib/profiler/models/profile.rb', line 11 def duration @duration end |
#finished_at ⇒ Object
Returns the value of attribute finished_at.
11 12 13 |
# File 'lib/profiler/models/profile.rb', line 11 def finished_at @finished_at end |
#gem_version ⇒ Object
Returns the value of attribute gem_version.
11 12 13 |
# File 'lib/profiler/models/profile.rb', line 11 def gem_version @gem_version end |
#headers ⇒ Object
Returns the value of attribute headers.
11 12 13 |
# File 'lib/profiler/models/profile.rb', line 11 def headers @headers end |
#is_ajax ⇒ Object
Returns the value of attribute is_ajax.
11 12 13 |
# File 'lib/profiler/models/profile.rb', line 11 def is_ajax @is_ajax end |
#memory ⇒ Object
Returns the value of attribute memory.
11 12 13 |
# File 'lib/profiler/models/profile.rb', line 11 def memory @memory end |
#method ⇒ Object
Returns the value of attribute method.
11 12 13 |
# File 'lib/profiler/models/profile.rb', line 11 def method @method end |
#params ⇒ Object
Returns the value of attribute params.
11 12 13 |
# File 'lib/profiler/models/profile.rb', line 11 def params @params end |
#parent_token ⇒ Object
Returns the value of attribute parent_token.
11 12 13 |
# File 'lib/profiler/models/profile.rb', line 11 def parent_token @parent_token end |
#path ⇒ Object
Returns the value of attribute path.
11 12 13 |
# File 'lib/profiler/models/profile.rb', line 11 def path @path end |
#profile_type ⇒ Object
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_body ⇒ Object
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_encoding ⇒ Object
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_body ⇒ Object
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_encoding ⇒ Object
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_headers ⇒ Object
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_at ⇒ Object
Returns the value of attribute started_at.
11 12 13 |
# File 'lib/profiler/models/profile.rb', line 11 def started_at @started_at end |
#status ⇒ Object
Returns the value of attribute status.
11 12 13 |
# File 'lib/profiler/models/profile.rb', line 11 def status @status end |
#token ⇒ Object
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
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 140 141 142 143 |
# File 'lib/profiler/models/profile.rb', line 112 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" profile.gem_version = data[:gem_version] # 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
107 108 109 110 |
# File 'lib/profiler/models/profile.rb', line 107 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
52 53 54 |
# File 'lib/profiler/models/profile.rb', line 52 def add_collector_data(name, data) @collectors_data[name.to_s] = data end |
#add_collector_metadata(collector) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/profiler/models/profile.rb', line 60 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
56 57 58 |
# File 'lib/profiler/models/profile.rb', line 56 def collector_data(name) @collectors_data[name.to_s] end |
#finish(status, response_headers = {}) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/profiler/models/profile.rb', line 45 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
36 37 38 39 40 41 42 43 |
# File 'lib/profiler/models/profile.rb', line 36 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_h ⇒ Object
74 75 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 |
# File 'lib/profiler/models/profile.rb', line 74 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, gem_version: @gem_version, 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
103 104 105 |
# File 'lib/profiler/models/profile.rb', line 103 def to_json(*args) to_h.to_json(*args) end |