Class: Context
- Inherits:
-
Object
- Object
- Context
- Defined in:
- lib/context.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#pending_count ⇒ Object
readonly
Returns the value of attribute pending_count.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #custom_assignment(experiment_name) ⇒ Object
- #experiments ⇒ Object
- #failed? ⇒ Boolean
-
#initialize(clock, config, data_future, data_provider, event_handler, event_logger, variable_parser, audience_matcher) ⇒ Context
constructor
A new instance of Context.
- #override(experiment_name) ⇒ Object
- #peek_treatment(experiment_name) ⇒ Object
- #peek_variable_value(key, default_value) ⇒ Object
- #publish ⇒ Object
- #queue_exposure(assignment) ⇒ Object
- #ready? ⇒ Boolean
- #refresh ⇒ Object
- #set_attribute(name, value) ⇒ Object
- #set_attributes(attributes) ⇒ Object
- #set_custom_assignment(experiment_name, variant) ⇒ Object
- #set_custom_assignments(custom_assignments) ⇒ Object
- #set_override(experiment_name, variant) ⇒ Object
- #set_overrides(overrides) ⇒ Object
- #set_unit(unit_type, uid) ⇒ Object
- #set_units(units) ⇒ Object
- #track(goal_name, properties) ⇒ Object
- #treatment(experiment_name) ⇒ Object
- #variable_keys ⇒ Object
- #variable_value(key, default_value) ⇒ Object
Constructor Details
#initialize(clock, config, data_future, data_provider, event_handler, event_logger, variable_parser, audience_matcher) ⇒ Context
Returns a new instance of Context.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/context.rb', line 21 def initialize(clock, config, data_future, data_provider, event_handler, event_logger, variable_parser, audience_matcher) @index = [] @achievements = [] @assignment_cache = {} @assignments = {} @clock = clock.is_a?(String) ? Time.iso8601(clock) : clock @publish_delay = config.publish_delay @refresh_interval = config.refresh_interval @event_handler = event_handler @event_logger = !config.event_logger.nil? ? config.event_logger : event_logger @data_provider = data_provider @variable_parser = variable_parser @audience_matcher = audience_matcher @closed = false @units = {} @attributes = [] @overrides = {} @cassignments = {} @assigners = {} @hashed_units = {} @pending_count = 0 @exposures ||= [] set_units(config.units) if config.units set_attributes(config.attributes) if config.attributes set_overrides(config.overrides) if config.overrides set_custom_assignments(config.custom_assignments) if config.custom_assignments if data_future.success? assign_data(data_future.data_future) log_event(ContextEventLogger::EVENT_TYPE::READY, data_future.data_future) else set_data_failed(data_future.exception) log_error(data_future.exception) end end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
13 14 15 |
# File 'lib/context.rb', line 13 def data @data end |
#pending_count ⇒ Object
Returns the value of attribute pending_count.
13 14 15 |
# File 'lib/context.rb', line 13 def pending_count @pending_count end |
Class Method Details
.create(clock, config, data_future, data_provider, event_handler, event_logger, variable_parser, audience_matcher) ⇒ Object
15 16 17 18 19 |
# File 'lib/context.rb', line 15 def self.create(clock, config, data_future, data_provider, event_handler, event_logger, variable_parser, audience_matcher) Context.new(clock, config, data_future, data_provider, event_handler, event_logger, variable_parser, audience_matcher) end |
Instance Method Details
#close ⇒ Object
251 252 253 254 255 256 257 258 259 |
# File 'lib/context.rb', line 251 def close unless @closed if @pending_count > 0 flush end @closed = true log_event(ContextEventLogger::EVENT_TYPE::CLOSE, nil) end end |
#closed? ⇒ Boolean
67 68 69 |
# File 'lib/context.rb', line 67 def closed? @closed end |
#custom_assignment(experiment_name) ⇒ Object
107 108 109 110 111 |
# File 'lib/context.rb', line 107 def custom_assignment(experiment_name) check_not_closed? @cassignments[experiment_name.to_s.to_sym] end |
#experiments ⇒ Object
71 72 73 74 75 |
# File 'lib/context.rb', line 71 def experiments check_ready?(true) @data.experiments.map(&:name) end |
#failed? ⇒ Boolean
63 64 65 |
# File 'lib/context.rb', line 63 def failed? @failed end |
#override(experiment_name) ⇒ Object
89 90 91 92 93 |
# File 'lib/context.rb', line 89 def override(experiment_name) check_not_closed? @overrides[experiment_name.to_s.to_sym] end |
#peek_treatment(experiment_name) ⇒ Object
180 181 182 183 184 |
# File 'lib/context.rb', line 180 def peek_treatment(experiment_name) check_ready?(true) assignment(experiment_name).variant end |
#peek_variable_value(key, default_value) ⇒ Object
206 207 208 209 210 211 212 213 214 215 |
# File 'lib/context.rb', line 206 def peek_variable_value(key, default_value) check_ready?(true) assignment = variable_assignment(key) return assignment.variables[key.to_s.to_sym] if !assignment.nil? && !assignment.variables.nil? && assignment.variables.key?(key.to_s.to_sym) default_value end |
#publish ⇒ Object
230 231 232 233 234 |
# File 'lib/context.rb', line 230 def publish check_not_closed? flush end |
#queue_exposure(assignment) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/context.rb', line 157 def queue_exposure(assignment) unless assignment.exposed assignment.exposed = true exposure = Exposure.new exposure.id = assignment.id || 0 exposure.name = assignment.name exposure.unit = assignment.unit_type exposure.variant = assignment.variant exposure.exposed_at = @clock.to_i exposure.assigned = assignment.assigned exposure.eligible = assignment.eligible exposure.overridden = assignment.overridden exposure.full_on = assignment.full_on exposure.custom = assignment.custom exposure.audience_mismatch = assignment.audience_mismatch @pending_count += 1 @exposures.push(exposure) log_event(ContextEventLogger::EVENT_TYPE::EXPOSURE, exposure) end end |
#ready? ⇒ Boolean
59 60 61 |
# File 'lib/context.rb', line 59 def ready? !@data.nil? end |
#refresh ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/context.rb', line 236 def refresh check_not_closed? unless @failed data_future = @data_provider.context_data if data_future.success? assign_data(data_future.data_future) log_event(ContextEventLogger::EVENT_TYPE::REFRESH, data_future.data_future) else set_data_failed(data_future.exception) log_error(data_future.exception) end end end |
#set_attribute(name, value) ⇒ Object
135 136 137 138 139 |
# File 'lib/context.rb', line 135 def set_attribute(name, value) check_not_closed? @attributes.push(Attribute.new(name, value, @clock.to_i)) end |
#set_attributes(attributes) ⇒ Object
141 142 143 144 145 |
# File 'lib/context.rb', line 141 def set_attributes(attributes) check_not_closed? attributes.each { |key, value| self.set_attribute(key, value) } end |
#set_custom_assignment(experiment_name, variant) ⇒ Object
95 96 97 98 99 |
# File 'lib/context.rb', line 95 def set_custom_assignment(experiment_name, variant) check_not_closed? @cassignments[experiment_name.to_s.to_sym] = variant end |
#set_custom_assignments(custom_assignments) ⇒ Object
101 102 103 104 105 |
# File 'lib/context.rb', line 101 def set_custom_assignments(custom_assignments) check_not_closed? @cassignments.merge!(custom_assignments.transform_keys(&:to_sym)) end |
#set_override(experiment_name, variant) ⇒ Object
77 78 79 80 81 |
# File 'lib/context.rb', line 77 def set_override(experiment_name, variant) check_not_closed? @overrides[experiment_name.to_s.to_sym] = variant end |
#set_overrides(overrides) ⇒ Object
83 84 85 86 87 |
# File 'lib/context.rb', line 83 def set_overrides(overrides) check_not_closed? @overrides.merge!(overrides.transform_keys(&:to_sym)) end |
#set_unit(unit_type, uid) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/context.rb', line 113 def set_unit(unit_type, uid) check_not_closed? previous = @units[unit_type.to_sym] if !previous.nil? && previous != uid raise IllegalStateException.new("Unit '#{unit_type}' already set.") end trimmed = uid.to_s.strip if trimmed.empty? raise IllegalStateException.new("Unit '#{unit_type}' UID must not be blank.") end @units[unit_type] = trimmed end |
#set_units(units) ⇒ Object
129 130 131 132 133 |
# File 'lib/context.rb', line 129 def set_units(units) check_not_closed? units.each { |key, value| self.set_unit(key, value) } end |
#track(goal_name, properties) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/context.rb', line 217 def track(goal_name, properties) check_not_closed? achievement = GoalAchievement.new achievement.achieved_at = @clock.to_i achievement.name = goal_name achievement.properties = properties @pending_count += 1 @achievements.push(achievement) log_event(ContextEventLogger::EVENT_TYPE::GOAL, achievement) end |
#treatment(experiment_name) ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/context.rb', line 147 def treatment(experiment_name) check_ready?(true) assignment = assignment(experiment_name) unless assignment.exposed queue_exposure(assignment) end assignment.variant end |
#variable_keys ⇒ Object
186 187 188 189 190 191 192 |
# File 'lib/context.rb', line 186 def variable_keys check_ready?(true) hsh = {} @index_variables.each { |key, value| hsh[key] = value.data.name } hsh end |
#variable_value(key, default_value) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/context.rb', line 194 def variable_value(key, default_value) check_ready?(true) assignment = variable_assignment(key) unless assignment.nil? || assignment.variables.nil? queue_exposure(assignment) unless assignment.exposed return assignment.variables[key.to_s.to_sym] if assignment.variables.key?(key.to_s.to_sym) end default_value end |