Class: Optimizely::ProjectConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely/project_config.rb

Constant Summary collapse

RUNNING_EXPERIMENT_STATUS =

Representation of the Optimizely project config.

['Running']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datafile, logger, error_handler) ⇒ ProjectConfig

Returns a new instance of ProjectConfig.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/optimizely/project_config.rb', line 61

def initialize(datafile, logger, error_handler)
  # ProjectConfig init method to fetch and set project config data
  #
  # datafile - JSON string representing the project

  config = JSON.load(datafile)

  @parsing_succeeded = false
  @error_handler = error_handler
  @logger = logger
  @version = config['version']

  if UNSUPPORTED_VERSIONS.include?(@version)
    return
  end

  @account_id = config['accountId']
  @project_id = config['projectId']
  @attributes = config['attributes']
  @events = config['events']
  @experiments = config['experiments']
  @revision = config['revision']
  @audiences = config['audiences']
  @groups = config.fetch('groups', [])

  # Utility maps for quick lookup
  @attribute_key_map = generate_key_map(@attributes, 'key')
  @event_key_map = generate_key_map(@events, 'key')
  @group_key_map = generate_key_map(@groups, 'id')
  @group_key_map.each do |key, group|
    exps = group.fetch('experiments')
    exps.each do |exp|
      @experiments.push(exp.merge('groupId' => key))
    end
  end
  @experiment_key_map = generate_key_map(@experiments, 'key')
  @experiment_id_map = generate_key_map(@experiments, 'id')
  @audience_id_map = generate_key_map(@audiences, 'id')
  @variation_id_map = {}
  @variation_key_map = {}
  @forced_variation_map = {}
  @variation_id_to_variable_usage_map = {}
  @variation_id_to_experiment_map = {}
  @experiment_key_map.each do |key, exp|
    variations = exp.fetch('variations')
    @variation_id_map[key] = generate_key_map(variations, 'id')
    @variation_key_map[key] = generate_key_map(variations, 'key')
  end
  @parsing_succeeded = true
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



36
37
38
# File 'lib/optimizely/project_config.rb', line 36

def 
  @account_id
end

#attribute_key_mapObject (readonly)

Returns the value of attribute attribute_key_map.



45
46
47
# File 'lib/optimizely/project_config.rb', line 45

def attribute_key_map
  @attribute_key_map
end

#attributesObject (readonly)

Returns the value of attribute attributes.



38
39
40
# File 'lib/optimizely/project_config.rb', line 38

def attributes
  @attributes
end

#audience_id_mapObject (readonly)

Returns the value of attribute audience_id_map.



46
47
48
# File 'lib/optimizely/project_config.rb', line 46

def audience_id_map
  @audience_id_map
end

#audiencesObject (readonly)

Returns the value of attribute audiences.



43
44
45
# File 'lib/optimizely/project_config.rb', line 43

def audiences
  @audiences
end

#error_handlerObject (readonly)

Gets project config attributes.



31
32
33
# File 'lib/optimizely/project_config.rb', line 31

def error_handler
  @error_handler
end

#event_key_mapObject (readonly)

Returns the value of attribute event_key_map.



47
48
49
# File 'lib/optimizely/project_config.rb', line 47

def event_key_map
  @event_key_map
end

#eventsObject (readonly)

Returns the value of attribute events.



39
40
41
# File 'lib/optimizely/project_config.rb', line 39

def events
  @events
end

#experiment_id_mapObject (readonly)

Returns the value of attribute experiment_id_map.



48
49
50
# File 'lib/optimizely/project_config.rb', line 48

def experiment_id_map
  @experiment_id_map
end

#experiment_key_mapObject (readonly)

Returns the value of attribute experiment_key_map.



49
50
51
# File 'lib/optimizely/project_config.rb', line 49

def experiment_key_map
  @experiment_key_map
end

#experimentsObject (readonly)

Returns the value of attribute experiments.



40
41
42
# File 'lib/optimizely/project_config.rb', line 40

def experiments
  @experiments
end

#forced_variation_mapObject (readonly)

Hash of user IDs to a Hash of experiments to variations. This contains all the forced variations set by the user by calling setForcedVariation (it is not the same as the whitelisting forcedVariations data structure in the Experiments class).



59
60
61
# File 'lib/optimizely/project_config.rb', line 59

def forced_variation_map
  @forced_variation_map
end

#group_key_mapObject (readonly)

Returns the value of attribute group_key_map.



50
51
52
# File 'lib/optimizely/project_config.rb', line 50

def group_key_map
  @group_key_map
end

#groupsObject (readonly)

Returns the value of attribute groups.



41
42
43
# File 'lib/optimizely/project_config.rb', line 41

def groups
  @groups
end

#loggerObject (readonly)

Returns the value of attribute logger.



32
33
34
# File 'lib/optimizely/project_config.rb', line 32

def logger
  @logger
end

#parsing_succeededObject (readonly)

Returns the value of attribute parsing_succeeded.



34
35
36
# File 'lib/optimizely/project_config.rb', line 34

def parsing_succeeded
  @parsing_succeeded
end

#project_idObject (readonly)

Returns the value of attribute project_id.



37
38
39
# File 'lib/optimizely/project_config.rb', line 37

def project_id
  @project_id
end

#revisionObject (readonly)

Returns the value of attribute revision.



42
43
44
# File 'lib/optimizely/project_config.rb', line 42

def revision
  @revision
end

#variation_id_mapObject (readonly)

Returns the value of attribute variation_id_map.



52
53
54
# File 'lib/optimizely/project_config.rb', line 52

def variation_id_map
  @variation_id_map
end

#variation_key_mapObject (readonly)

Returns the value of attribute variation_key_map.



53
54
55
# File 'lib/optimizely/project_config.rb', line 53

def variation_key_map
  @variation_key_map
end

#versionObject (readonly)

Returns the value of attribute version.



35
36
37
# File 'lib/optimizely/project_config.rb', line 35

def version
  @version
end

Instance Method Details

#experiment_running?(experiment) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
# File 'lib/optimizely/project_config.rb', line 112

def experiment_running?(experiment)
  # Determine if experiment corresponding to given key is running
  #
  # experiment - Experiment
  #
  # Returns true if experiment is running
  return RUNNING_EXPERIMENT_STATUS.include?(experiment['status'])
end

#get_attribute_id(attribute_key) ⇒ Object



350
351
352
353
354
355
356
# File 'lib/optimizely/project_config.rb', line 350

def get_attribute_id(attribute_key)
  attribute = @attribute_key_map[attribute_key]
  return attribute['id'] if attribute
  @logger.log Logger::ERROR, "Attribute key '#{attribute_key}' is not in datafile."
  @error_handler.handle_error InvalidAttributeError
  nil
end

#get_audience_conditions_from_id(audience_id) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/optimizely/project_config.rb', line 163

def get_audience_conditions_from_id(audience_id)
  # Get audience conditions for the provided audience ID
  #
  # audience_id - ID of the audience
  #
  # Returns conditions for the audience

  audience = @audience_id_map[audience_id]
  return audience['conditions'] if audience
  @logger.log Logger::ERROR, "Audience '#{audience_id}' is not in datafile."
  @error_handler.handle_error InvalidAudienceError
  nil
end

#get_experiment_from_key(experiment_key) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/optimizely/project_config.rb', line 121

def get_experiment_from_key(experiment_key)
  # Retrieves experiment ID for a given key
  #
  # experiment_key - String key representing the experiment
  #
  # Returns Experiment

  experiment = @experiment_key_map[experiment_key]
  return experiment if experiment
  @logger.log Logger::ERROR, "Experiment key '#{experiment_key}' is not in datafile."
  @error_handler.handle_error InvalidExperimentError
  nil
end

#get_experiment_ids_for_event(event_key) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/optimizely/project_config.rb', line 149

def get_experiment_ids_for_event(event_key)
  # Get experiment IDs for the provided event key.
  #
  # event_key - Event key for which experiment IDs are to be retrieved.
  #
  # Returns array of all experiment IDs for the event.

  event = @event_key_map[event_key]
  return event['experimentIds'] if event
  @logger.log Logger::ERROR, "Event '#{event_key}' is not in datafile."
  @error_handler.handle_error InvalidEventError
  []
end

#get_experiment_key(experiment_id) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/optimizely/project_config.rb', line 135

def get_experiment_key(experiment_id)
  # Retrieves experiment key for a given ID.
  #
  # experiment_id - String ID representing the experiment.
  #
  # Returns String key.

  experiment = @experiment_id_map[experiment_id]
  return experiment['key'] unless experiment.nil?
  @logger.log Logger::ERROR, "Experiment id '#{experiment_id}' is not in datafile."
  @error_handler.handle_error InvalidExperimentError
  nil
end

#get_forced_variation(experiment_key, user_id) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/optimizely/project_config.rb', line 256

def get_forced_variation(experiment_key, user_id)
  # Gets the forced variation for the given user and experiment.
  #
  # experiment_key - String Key for experiment.
  # user_id - String ID for user
  #
  # Returns Variation The variation which the given user and experiment should be forced into.

  # check for nil and empty string user ID
  if user_id.nil? or user_id.empty?
    @logger.log(Logger::DEBUG, "User ID is invalid")
    return nil
  end

  unless @forced_variation_map.has_key? (user_id)
    @logger.log(Logger::DEBUG, "User '#{user_id}' is not in the forced variation map.")
    return nil
  end

  experimentToVariationMap = @forced_variation_map[user_id]
  experiment = get_experiment_from_key(experiment_key)
  experiment_id = experiment["id"] if experiment
  # check for nil and empty string experiment ID
  if experiment_id.nil? or experiment_id.empty?
    # this case is logged in get_experiment_from_key
    return nil
  end

  unless experimentToVariationMap.has_key? (experiment_id)
    @logger.log(Logger::DEBUG, "No experiment '#{experiment_key}' mapped to user '#{user_id}' in the forced variation map.")
    return nil
  end

  variation_id = experimentToVariationMap[experiment_id]
  variation_key = ""
  variation = get_variation_from_id(experiment_key,variation_id)
  variation_key = variation["key"] if variation

  # check if the variation exists in the datafile
  if variation_key.empty?
    # this case is logged in get_variation_from_id
    return nil
  end

  @logger.log(Logger::DEBUG, "Variation '#{variation_key}' is mapped to experiment '#{experiment_key}' and user '#{user_id}' in the forced variation map")

  variation
end

#get_variation_from_id(experiment_key, variation_id) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/optimizely/project_config.rb', line 199

def get_variation_from_id(experiment_key, variation_id)
  # Get variation given experiment key and variation ID
  #
  # experiment_key - Key representing parent experiment of variation
  # variation_id - ID of the variation
  #
  # Returns the variation or nil if not found

  variation_id_map = @variation_id_map[experiment_key]
  if variation_id_map
    variation = variation_id_map[variation_id]
    return variation if variation
    @logger.log Logger::ERROR, "Variation id '#{variation_id}' is not in datafile."
    @error_handler.handle_error InvalidVariationError
    return nil
  end

  @logger.log Logger::ERROR, "Experiment key '#{experiment_key}' is not in datafile."
  @error_handler.handle_error InvalidExperimentError
  nil
end

#get_variation_id_from_key(experiment_key, variation_key) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/optimizely/project_config.rb', line 221

def get_variation_id_from_key(experiment_key, variation_key)
  # Get variation ID given experiment key and variation key
  #
  # experiment_key - Key representing parent experiment of variation
  # variation_key - Key of the variation
  #
  # Returns ID of the variation

  variation_key_map = @variation_key_map[experiment_key]
  if variation_key_map
    variation = variation_key_map[variation_key]
    return variation['id'] if variation
    @logger.log Logger::ERROR, "Variation key '#{variation_key}' is not in datafile."
    @error_handler.handle_error InvalidVariationError
    return nil
  end

  @logger.log Logger::ERROR, "Experiment key '#{experiment_key}' is not in datafile."
  @error_handler.handle_error InvalidExperimentError
  nil
end

#get_variation_key_from_id(experiment_key, variation_id) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/optimizely/project_config.rb', line 177

def get_variation_key_from_id(experiment_key, variation_id)
  # Get variation key given experiment key and variation ID
  #
  # experiment_key - Key representing parent experiment of variation
  # variation_id - ID of the variation
  #
  # Returns key of the variation

  variation_id_map = @variation_id_map[experiment_key]
  if variation_id_map
    variation = variation_id_map[variation_id]
    return variation['key'] if variation
    @logger.log Logger::ERROR, "Variation id '#{variation_id}' is not in datafile."
    @error_handler.handle_error InvalidVariationError
    return nil
  end

  @logger.log Logger::ERROR, "Experiment key '#{experiment_key}' is not in datafile."
  @error_handler.handle_error InvalidExperimentError
  nil
end

#get_whitelisted_variations(experiment_key) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/optimizely/project_config.rb', line 243

def get_whitelisted_variations(experiment_key)
  # Retrieves whitelisted variations for a given experiment Key
  #
  # experiment_key - String Key representing the experiment
  #
  # Returns whitelisted variations for the experiment or nil

  experiment = @experiment_key_map[experiment_key]
  return experiment['forcedVariations'] if experiment
  @logger.log Logger::ERROR, "Experiment key '#{experiment_key}' is not in datafile."
  @error_handler.handle_error InvalidExperimentError
end

#parsing_succeeded?Boolean

Returns:

  • (Boolean)


358
359
360
361
362
363
364
# File 'lib/optimizely/project_config.rb', line 358

def parsing_succeeded?
  # Helper method to determine if parsing the datafile was successful.
  #
  # Returns Boolean depending on whether parsing the datafile succeeded or not.

  @parsing_succeeded
end

#set_forced_variation(experiment_key, user_id, variation_key) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/optimizely/project_config.rb', line 305

def set_forced_variation(experiment_key, user_id, variation_key)
  # Sets a Hash of user IDs to a Hash of experiments to forced variations.
  #
  # experiment_key - String Key for experiment.
  # user_id - String ID for user.
  # variation_key - String Key for variation. If null, then clear the existing experiment-to-variation mapping.
  #
  # Returns a boolean value that indicates if the set completed successfully.

  #  check for null and empty string user ID
  if user_id.nil? or user_id.empty?
    @logger.log(Logger::DEBUG, "User ID is invalid")
    return false
  end

  experiment = get_experiment_from_key(experiment_key)
  experiment_id = experiment["id"] if experiment
  #  check if the experiment exists in the datafile
  if experiment_id.nil? or experiment_id.empty?
    return false
  end

  #  clear the forced variation if the variation key is null
  if variation_key.nil? or variation_key.empty?
    @forced_variation_map[user_id].delete(experiment_id) if @forced_variation_map.has_key? (user_id)
    @logger.log(Logger::DEBUG, "Variation mapped to experiment '#{experiment_key}' has been removed for user '#{user_id}'.")
    return true
  end

  variation_id = get_variation_id_from_key(experiment_key, variation_key)

  #  check if the variation exists in the datafile
  unless variation_id
    #  this case is logged in get_variation_id_from_key
    return false
  end

  unless @forced_variation_map.has_key? user_id
    @forced_variation_map[user_id] = {}
  end
  @forced_variation_map[user_id][experiment_id] = variation_id
  @logger.log(Logger::DEBUG, "Set variation '#{variation_id}' for experiment '#{experiment_id}' and user '#{user_id}' in the forced variation map.")
  return true
end

#variation_id_exists?(experiment_id, variation_id) ⇒ Boolean

Returns:

  • (Boolean)


366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/optimizely/project_config.rb', line 366

def variation_id_exists?(experiment_id, variation_id)
  # Determines if a given experiment ID / variation ID pair exists in the datafile
  #
  # experiment_id - String experiment ID
  # variation_id - String variation ID
  #
  # Returns true if variation is in datafile

  experiment_key = get_experiment_key(experiment_id)
  variation_id_map = @variation_id_map[experiment_key]
  if variation_id_map
    variation = variation_id_map[variation_id]
    return true if variation
    @logger.log Logger::ERROR, "Variation ID '#{variation_id}' is not in datafile."
    @error_handler.handle_error InvalidVariationError
  end

  false
end