Class: Labkit::UserExperienceSli::Experience
- Inherits:
-
Object
- Object
- Labkit::UserExperienceSli::Experience
- Extended by:
- Forwardable
- Defined in:
- lib/labkit/user_experience_sli/experience.rb
Overview
The Experience class represents a single User Experience event to be measured and reported.
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Instance Method Summary collapse
-
#checkpoint(**extra) ⇒ self
Checkpoint the User Experience.
-
#complete(**extra) ⇒ self
Complete the User Experience.
-
#error!(error) ⇒ self
Marks the experience as failed with an error.
- #has_error? ⇒ Boolean
- #id ⇒ Object
-
#initialize(definition) ⇒ Experience
constructor
A new instance of Experience.
-
#rehydrate(data = {}) ⇒ Experience
Rehydrate an Experience instance from serialized data.
-
#resume(**extra) {|self| ... } ⇒ Object
Resume the User Experience.
-
#start(**extra) {|self| ... } ⇒ self
Start the User Experience.
- #to_h ⇒ Object
Constructor Details
#initialize(definition) ⇒ Experience
Returns a new instance of Experience.
40 41 42 |
# File 'lib/labkit/user_experience_sli/experience.rb', line 40 def initialize(definition) @definition = definition end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
38 39 40 |
# File 'lib/labkit/user_experience_sli/experience.rb', line 38 def error @error end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
38 39 40 |
# File 'lib/labkit/user_experience_sli/experience.rb', line 38 def start_time @start_time end |
Instance Method Details
#checkpoint(**extra) ⇒ self
Checkpoint the User Experience.
94 95 96 97 98 99 100 101 102 |
# File 'lib/labkit/user_experience_sli/experience.rb', line 94 def checkpoint(**extra) return self unless ensure_started! @checkpoint_time = Time.now.utc checkpoint_counter.increment(checkpoint: "intermediate", **base_labels) log_event("intermediate", **extra) self end |
#complete(**extra) ⇒ self
Complete the User Experience.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/labkit/user_experience_sli/experience.rb', line 122 def complete(**extra) return self unless ensure_started! && ensure_incomplete! begin @end_time = Time.now.utc ensure checkpoint_counter.increment(checkpoint: "end", **base_labels) total_counter.increment(error: has_error?, **base_labels) apdex_counter.increment(success: apdex_success?, **base_labels) unless has_error? log_event("end", **extra) Labkit::UserExperienceSli::Current.active_experiences.delete(id) end self end |
#error!(error) ⇒ self
Marks the experience as failed with an error
142 143 144 145 |
# File 'lib/labkit/user_experience_sli/experience.rb', line 142 def error!(error) @error = error self end |
#has_error? ⇒ Boolean
147 148 149 |
# File 'lib/labkit/user_experience_sli/experience.rb', line 147 def has_error? !!@error end |
#id ⇒ Object
44 45 46 |
# File 'lib/labkit/user_experience_sli/experience.rb', line 44 def id @definition.user_experience_id end |
#rehydrate(data = {}) ⇒ Experience
Rehydrate an Experience instance from serialized data.
52 53 54 55 56 57 58 |
# File 'lib/labkit/user_experience_sli/experience.rb', line 52 def rehydrate(data = {}) @start_time = Time.iso8601(data["start_time"]) if data&.has_key?("start_time") && data["start_time"] self rescue ArgumentError warn("Invalid #{id}, start_time: #{data['start_time']}") self end |
#resume(**extra) {|self| ... } ⇒ Object
Resume the User Experience.
108 109 110 111 112 113 114 115 116 |
# File 'lib/labkit/user_experience_sli/experience.rb', line 108 def resume(**extra, &) return self unless ensure_started! checkpoint(checkpoint_action: 'resume', **extra) return self unless block_given? completable(**extra, &) end |
#start(**extra) {|self| ... } ⇒ self
Start the User Experience.
Usage:
UserExperience.new(definition).start do |experience|
experience.checkpoint
experience.checkpoint
end
experience = UserExperience.new(definition)
experience.start
experience.checkpoint
experience.complete
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/labkit/user_experience_sli/experience.rb', line 78 def start(**extra, &) @start_time = Time.now.utc checkpoint_counter.increment(checkpoint: "start", **base_labels) log_event("start", **extra) Labkit::UserExperienceSli::Current.active_experiences[id] = self return self unless block_given? completable(**extra, &) end |
#to_h ⇒ Object
151 152 153 154 155 |
# File 'lib/labkit/user_experience_sli/experience.rb', line 151 def to_h return {} unless ensure_started! { id => { "start_time" => @start_time&.iso8601(3) } } end |