Class: AppProfiler::Profile

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

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.



21
22
23
24
25
# File 'lib/app_profiler/profile.rb', line 21

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.



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

def context
  @context
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

Class Method Details

.from_stackprof(data) ⇒ Object

This function should not be called if ‘StackProf.results` returns nil.



12
13
14
15
16
17
18
# File 'lib/app_profiler/profile.rb', line 12

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



52
53
54
55
56
57
# File 'lib/app_profiler/profile.rb', line 52

def file
  @file ||= path.tap do |p|
    p.dirname.mkpath
    p.write(JSON.dump(@data))
  end
end

#modeObject



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

def mode
  @data[:mode]
end

#to_hObject



59
60
61
# File 'lib/app_profiler/profile.rb', line 59

def to_h
  @data
end

#uploadObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/app_profiler/profile.rb', line 39

def upload
  AppProfiler.storage.upload(self).tap do |upload|
    if upload && defined?(upload.url)
      AppProfiler.logger.info("[Profiler] uploaded profile profile_url=#{upload.url}")
    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)


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

def valid?
  mode.present?
end

#viewObject



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

def view
  AppProfiler.viewer.view(self)
end