Class: AppProfiler::BaseProfile

Inherits:
Object
  • Object
show all
Defined in:
lib/app_profiler/base_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



36
37
38
39
40
# File 'lib/app_profiler/base_profile.rb', line 36

def initialize(data, id: nil, context: nil)
  @id      = id.presence || ProfileId.current
  @context = context
  @data    = data
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



11
12
13
# File 'lib/app_profiler/base_profile.rb', line 11

def context
  @context
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/app_profiler/base_profile.rb', line 11

def id
  @id
end

Class Method Details

.from_stackprof(data) ⇒ Object

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



17
18
19
20
21
22
23
# File 'lib/app_profiler/base_profile.rb', line 17

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



25
26
27
28
29
30
31
# File 'lib/app_profiler/base_profile.rb', line 25

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



61
62
63
# File 'lib/app_profiler/base_profile.rb', line 61

def enqueue_upload
  AppProfiler.storage.enqueue_upload(self)
end

#fileObject



69
70
71
72
73
74
# File 'lib/app_profiler/base_profile.rb', line 69

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

#formatObject

Raises:

  • (NotImplementedError)


88
89
90
# File 'lib/app_profiler/base_profile.rb', line 88

def format
  raise NotImplementedError
end

#metadataObject

Raises:

  • (NotImplementedError)


80
81
82
# File 'lib/app_profiler/base_profile.rb', line 80

def 
  raise NotImplementedError
end

#modeObject

Raises:

  • (NotImplementedError)


84
85
86
# File 'lib/app_profiler/base_profile.rb', line 84

def mode
  raise NotImplementedError
end

#to_hObject



76
77
78
# File 'lib/app_profiler/base_profile.rb', line 76

def to_h
  @data
end

#uploadObject



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

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)


65
66
67
# File 'lib/app_profiler/base_profile.rb', line 65

def valid?
  mode.present?
end

#view(params = {}) ⇒ Object

Raises:

  • (NotImplementedError)


92
93
94
# File 'lib/app_profiler/base_profile.rb', line 92

def view(params = {})
  raise NotImplementedError
end