Class: AppProfiler::BaseProfile

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

Direct Known Subclasses

StackprofProfile, VernierProfile

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) ⇒ BaseProfile

‘data` is assumed to be a Hash for Stackprof, a vernier “result” object for vernier



39
40
41
42
43
# File 'lib/app_profiler/profile.rb', line 39

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.



14
15
16
# File 'lib/app_profiler/profile.rb', line 14

def context
  @context
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/app_profiler/profile.rb', line 14

def id
  @id
end

Class Method Details

.from_stackprof(data) ⇒ Object

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



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

def from_stackprof(data)
  options = INTERNAL_METADATA_KEYS.map { |key| [key, data[:metadata]&.delete(key)] }.to_h

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

.from_vernier(data) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/app_profiler/profile.rb', line 28

def from_vernier(data)
  options = INTERNAL_METADATA_KEYS.map { |key| [key, data[:meta]&.delete(key)] }.to_h

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

Instance Method Details

#enqueue_uploadObject



64
65
66
# File 'lib/app_profiler/profile.rb', line 64

def enqueue_upload
  AppProfiler.storage.enqueue_upload(self)
end

#fileObject



72
73
74
75
76
77
# File 'lib/app_profiler/profile.rb', line 72

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

#formatObject

Raises:

  • (NotImplementedError)


91
92
93
# File 'lib/app_profiler/profile.rb', line 91

def format
  raise NotImplementedError
end

#metadataObject



83
84
85
# File 'lib/app_profiler/profile.rb', line 83

def 
  @data[:metadata]
end

#modeObject

Raises:

  • (NotImplementedError)


87
88
89
# File 'lib/app_profiler/profile.rb', line 87

def mode
  raise NotImplementedError
end

#to_hObject



79
80
81
# File 'lib/app_profiler/profile.rb', line 79

def to_h
  @data
end

#uploadObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/app_profiler/profile.rb', line 45

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)


68
69
70
# File 'lib/app_profiler/profile.rb', line 68

def valid?
  mode.present?
end

#view(params = {}) ⇒ Object

Raises:

  • (NotImplementedError)


95
96
97
# File 'lib/app_profiler/profile.rb', line 95

def view(params = {})
  raise NotImplementedError
end