Class: AppProfiler::Profile
- Inherits:
-
Object
- Object
- AppProfiler::Profile
- Defined in:
- lib/app_profiler/profile.rb
Defined Under Namespace
Classes: UnsafeFilename
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.from_stackprof(data) ⇒ Object
This function should not be called if ‘StackProf.results` returns nil.
Instance Method Summary collapse
- #file ⇒ Object
-
#initialize(data, id: nil, context: nil) ⇒ Profile
constructor
‘data` is assumed to be a Hash.
- #mode ⇒ Object
- #to_h ⇒ Object
- #upload ⇒ Object
- #valid? ⇒ Boolean
- #view ⇒ Object
Constructor Details
#initialize(data, id: nil, context: nil) ⇒ Profile
‘data` is assumed to be a Hash.
22 23 24 25 26 |
# File 'lib/app_profiler/profile.rb', line 22 def initialize(data, id: nil, context: nil) @id = id.presence || SecureRandom.hex @context = context @data = data end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
10 11 12 |
# File 'lib/app_profiler/profile.rb', line 10 def context @context end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
10 11 12 |
# File 'lib/app_profiler/profile.rb', line 10 def id @id end |
Class Method Details
.from_stackprof(data) ⇒ Object
This function should not be called if ‘StackProf.results` returns nil.
13 14 15 16 17 18 19 |
# File 'lib/app_profiler/profile.rb', line 13 def self.from_stackprof(data) = INTERNAL_METADATA_KEYS.map { |key| [key, data[:metadata]&.delete(key)] }.to_h new(data, **).tap do |profile| raise ArgumentError, "invalid profile data" unless profile.valid? end end |
Instance Method Details
#file ⇒ Object
59 60 61 62 63 64 |
# File 'lib/app_profiler/profile.rb', line 59 def file @file ||= path.tap do |p| p.dirname.mkpath p.write(JSON.dump(@data)) end end |
#mode ⇒ Object
32 33 34 |
# File 'lib/app_profiler/profile.rb', line 32 def mode @data[:mode] end |
#to_h ⇒ Object
66 67 68 |
# File 'lib/app_profiler/profile.rb', line 66 def to_h @data end |
#upload ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/app_profiler/profile.rb', line 40 def upload AppProfiler.storage.upload(self).tap do |upload| if upload && defined?(upload.url) AppProfiler.logger.info( <<~INFO.squish [Profiler] data uploaded: profile_url=#{upload.url} profile_viewer_url=#{AppProfiler.profile_url(upload)} INFO ) end end rescue => error AppProfiler.logger.info( "[Profiler] failed to upload profile error_class=#{error.class} error_message=#{error.}" ) nil end |
#valid? ⇒ Boolean
28 29 30 |
# File 'lib/app_profiler/profile.rb', line 28 def valid? mode.present? end |
#view ⇒ Object
36 37 38 |
# File 'lib/app_profiler/profile.rb', line 36 def view AppProfiler.viewer.view(self) end |