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



37
38
39
40
41
# File 'lib/app_profiler/profile.rb', line 37

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.



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

def self.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



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

def self.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



62
63
64
# File 'lib/app_profiler/profile.rb', line 62

def enqueue_upload
  AppProfiler.storage.enqueue_upload(self)
end

#fileObject



70
71
72
73
74
75
# File 'lib/app_profiler/profile.rb', line 70

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

#formatObject

Raises:

  • (NotImplementedError)


85
86
87
# File 'lib/app_profiler/profile.rb', line 85

def format
  raise NotImplementedError
end

#modeObject

Raises:

  • (NotImplementedError)


81
82
83
# File 'lib/app_profiler/profile.rb', line 81

def mode
  raise NotImplementedError
end

#to_hObject



77
78
79
# File 'lib/app_profiler/profile.rb', line 77

def to_h
  @data
end

#uploadObject



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

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)


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

def valid?
  mode.present?
end

#view(params = {}) ⇒ Object

Raises:

  • (NotImplementedError)


89
90
91
# File 'lib/app_profiler/profile.rb', line 89

def view(params = {})
  raise NotImplementedError
end