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
41
42
43
44
# File 'lib/app_profiler/base_profile.rb', line 36

def initialize(data, id: nil, context: nil)
  ProfileId.current = id if id.present?

  @context = context
  @data    = data

  [PROFILE_BACKEND_METADATA_KEY] = self.class.backend_name
  [PROFILE_ID_METADATA_KEY] = ProfileId.current
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

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

#durationObject



50
51
52
# File 'lib/app_profiler/base_profile.rb', line 50

def duration
  [:duration]
end

#enqueue_uploadObject



73
74
75
# File 'lib/app_profiler/base_profile.rb', line 73

def enqueue_upload
  AppProfiler.storage.enqueue_upload(self)
end

#fileObject



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

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

#formatObject

Raises:

  • (NotImplementedError)


100
101
102
# File 'lib/app_profiler/base_profile.rb', line 100

def format
  raise NotImplementedError
end

#idObject



46
47
48
# File 'lib/app_profiler/base_profile.rb', line 46

def id
  [PROFILE_ID_METADATA_KEY]
end

#metadataObject

Raises:

  • (NotImplementedError)


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

def 
  raise NotImplementedError
end

#modeObject

Raises:

  • (NotImplementedError)


96
97
98
# File 'lib/app_profiler/base_profile.rb', line 96

def mode
  raise NotImplementedError
end

#to_hObject



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

def to_h
  @data
end

#uploadObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/app_profiler/base_profile.rb', line 54

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)


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

def valid?
  mode.present?
end

#view(params = {}) ⇒ Object

Raises:

  • (NotImplementedError)


104
105
106
# File 'lib/app_profiler/base_profile.rb', line 104

def view(params = {})
  raise NotImplementedError
end