Class: AppProfiler::Profile

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

Defined Under Namespace

Classes: UnsafeFilename

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#idObject (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)
  options = INTERNAL_METADATA_KEYS.map { |key| [key, data[:metadata]&.delete(key)] }.to_h

  new(data, **options).tap do |profile|
    raise ArgumentError, "invalid profile data" unless profile.valid?
  end
end

Instance Method Details

#fileObject



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

#modeObject



32
33
34
# File 'lib/app_profiler/profile.rb', line 32

def mode
  @data[:mode]
end

#to_hObject



66
67
68
# File 'lib/app_profiler/profile.rb', line 66

def to_h
  @data
end

#uploadObject



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.message}"
  )
  nil
end

#valid?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/app_profiler/profile.rb', line 28

def valid?
  mode.present?
end

#viewObject



36
37
38
# File 'lib/app_profiler/profile.rb', line 36

def view
  AppProfiler.viewer.view(self)
end