1
1
# frozen_string_literal: true
2
2
3
3
#
4
- # Copyright 2017-2020 , Optimizely and contributors
4
+ # Copyright 2017-2021 , Optimizely and contributors
5
5
#
6
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
7
# you may not use this file except in compliance with the License.
@@ -52,11 +52,11 @@ def initialize(logger, user_profile_service = nil)
52
52
@forced_variation_map = { }
53
53
end
54
54
55
- def get_variation ( project_config , experiment_key , user_id , attributes = nil , decide_options = [ ] )
55
+ def get_variation ( project_config , experiment_id , user_id , attributes = nil , decide_options = [ ] )
56
56
# Determines variation into which user will be bucketed.
57
57
#
58
58
# project_config - project_config - Instance of ProjectConfig
59
- # experiment_key - Experiment for which visitor variation needs to be determined
59
+ # experiment_id - Experiment for which visitor variation needs to be determined
60
60
# user_id - String ID for user
61
61
# attributes - Hash representing user attributes
62
62
#
@@ -68,10 +68,10 @@ def get_variation(project_config, experiment_key, user_id, attributes = nil, dec
68
68
bucketing_id , bucketing_id_reasons = get_bucketing_id ( user_id , attributes )
69
69
decide_reasons . push ( *bucketing_id_reasons )
70
70
# Check to make sure experiment is active
71
- experiment = project_config . get_experiment_from_key ( experiment_key )
71
+ experiment = project_config . get_experiment_from_id ( experiment_id )
72
72
return nil , decide_reasons if experiment . nil?
73
73
74
- experiment_id = experiment [ 'id ' ]
74
+ experiment_key = experiment [ 'key ' ]
75
75
unless project_config . experiment_running? ( experiment )
76
76
message = "Experiment '#{ experiment_key } ' is not running."
77
77
@logger . log ( Logger ::INFO , message )
@@ -80,12 +80,12 @@ def get_variation(project_config, experiment_key, user_id, attributes = nil, dec
80
80
end
81
81
82
82
# Check if a forced variation is set for the user
83
- forced_variation , reasons_received = get_forced_variation ( project_config , experiment_key , user_id )
83
+ forced_variation , reasons_received = get_forced_variation ( project_config , experiment [ 'key' ] , user_id )
84
84
decide_reasons . push ( *reasons_received )
85
85
return forced_variation [ 'id' ] , decide_reasons if forced_variation
86
86
87
87
# Check if user is in a white-listed variation
88
- whitelisted_variation_id , reasons_received = get_whitelisted_variation_id ( project_config , experiment_key , user_id )
88
+ whitelisted_variation_id , reasons_received = get_whitelisted_variation_id ( project_config , experiment_id , user_id )
89
89
decide_reasons . push ( *reasons_received )
90
90
return whitelisted_variation_id , decide_reasons if whitelisted_variation_id
91
91
@@ -122,7 +122,7 @@ def get_variation(project_config, experiment_key, user_id, attributes = nil, dec
122
122
message = ''
123
123
if variation_id
124
124
variation_key = variation [ 'key' ]
125
- message = "User '#{ user_id } ' is in variation '#{ variation_key } ' of experiment '#{ experiment_key } '."
125
+ message = "User '#{ user_id } ' is in variation '#{ variation_key } ' of experiment '#{ experiment_id } '."
126
126
else
127
127
message = "User '#{ user_id } ' is in no variation."
128
128
end
@@ -186,13 +186,13 @@ def get_variation_for_feature_experiment(project_config, feature_flag, user_id,
186
186
return nil , decide_reasons
187
187
end
188
188
189
- experiment_key = experiment [ 'key ' ]
190
- variation_id , reasons_received = get_variation ( project_config , experiment_key , user_id , attributes , decide_options )
189
+ experiment_id = experiment [ 'id ' ]
190
+ variation_id , reasons_received = get_variation ( project_config , experiment_id , user_id , attributes , decide_options )
191
191
decide_reasons . push ( *reasons_received )
192
192
193
193
next unless variation_id
194
194
195
- variation = project_config . variation_id_map [ experiment_key ] [ variation_id ]
195
+ variation = project_config . get_variation_from_id_by_experiment_id ( experiment_id , variation_id )
196
196
197
197
return Decision . new ( experiment , variation , DECISION_SOURCES [ 'FEATURE_TEST' ] ) , decide_reasons
198
198
end
@@ -315,7 +315,7 @@ def set_forced_variation(project_config, experiment_key, user_id, variation_key)
315
315
return true
316
316
end
317
317
318
- variation_id = project_config . get_variation_id_from_key ( experiment_key , variation_key )
318
+ variation_id = project_config . get_variation_id_from_key_by_experiment_id ( experiment_id , variation_key )
319
319
320
320
# check if the variation exists in the datafile
321
321
unless variation_id
@@ -334,7 +334,7 @@ def get_forced_variation(project_config, experiment_key, user_id)
334
334
# Gets the forced variation for the given user and experiment.
335
335
#
336
336
# project_config - Instance of ProjectConfig
337
- # experiment_key - String Key for experiment
337
+ # experiment_key - String key for experiment
338
338
# user_id - String ID for user
339
339
#
340
340
# Returns Variation The variation which the given user and experiment should be forced into
@@ -354,22 +354,22 @@ def get_forced_variation(project_config, experiment_key, user_id)
354
354
return nil , decide_reasons if experiment_id . nil? || experiment_id . empty?
355
355
356
356
unless experiment_to_variation_map . key? experiment_id
357
- message = "No experiment '#{ experiment_key } ' mapped to user '#{ user_id } ' in the forced variation map."
357
+ message = "No experiment '#{ experiment_id } ' mapped to user '#{ user_id } ' in the forced variation map."
358
358
@logger . log ( Logger ::DEBUG , message )
359
359
decide_reasons . push ( message )
360
360
return nil , decide_reasons
361
361
end
362
362
363
363
variation_id = experiment_to_variation_map [ experiment_id ]
364
364
variation_key = ''
365
- variation = project_config . get_variation_from_id ( experiment_key , variation_id )
365
+ variation = project_config . get_variation_from_id_by_experiment_id ( experiment_id , variation_id )
366
366
variation_key = variation [ 'key' ] if variation
367
367
368
368
# check if the variation exists in the datafile
369
369
# this case is logged in get_variation_from_id
370
370
return nil , decide_reasons if variation_key . empty?
371
371
372
- message = "Variation '#{ variation_key } ' is mapped to experiment '#{ experiment_key } ' and user '#{ user_id } ' in the forced variation map"
372
+ message = "Variation '#{ variation_key } ' is mapped to experiment '#{ experiment_id } ' and user '#{ user_id } ' in the forced variation map"
373
373
@logger . log ( Logger ::DEBUG , message )
374
374
decide_reasons . push ( message )
375
375
@@ -378,7 +378,7 @@ def get_forced_variation(project_config, experiment_key, user_id)
378
378
379
379
private
380
380
381
- def get_whitelisted_variation_id ( project_config , experiment_key , user_id )
381
+ def get_whitelisted_variation_id ( project_config , experiment_id , user_id )
382
382
# Determine if a user is whitelisted into a variation for the given experiment and return the ID of that variation
383
383
#
384
384
# project_config - project_config - Instance of ProjectConfig
@@ -387,23 +387,23 @@ def get_whitelisted_variation_id(project_config, experiment_key, user_id)
387
387
#
388
388
# Returns variation ID into which user_id is whitelisted (nil if no variation)
389
389
390
- whitelisted_variations = project_config . get_whitelisted_variations ( experiment_key )
390
+ whitelisted_variations = project_config . get_whitelisted_variations ( experiment_id )
391
391
392
392
return nil , nil unless whitelisted_variations
393
393
394
394
whitelisted_variation_key = whitelisted_variations [ user_id ]
395
395
396
396
return nil , nil unless whitelisted_variation_key
397
397
398
- whitelisted_variation_id = project_config . get_variation_id_from_key ( experiment_key , whitelisted_variation_key )
398
+ whitelisted_variation_id = project_config . get_variation_id_from_key_by_experiment_id ( experiment_id , whitelisted_variation_key )
399
399
400
400
unless whitelisted_variation_id
401
401
message = "User '#{ user_id } ' is whitelisted into variation '#{ whitelisted_variation_key } ', which is not in the datafile."
402
402
@logger . log ( Logger ::INFO , message )
403
403
return nil , message
404
404
end
405
405
406
- message = "User '#{ user_id } ' is whitelisted into variation '#{ whitelisted_variation_key } ' of experiment '#{ experiment_key } '."
406
+ message = "User '#{ user_id } ' is whitelisted into variation '#{ whitelisted_variation_key } ' of experiment '#{ experiment_id } '."
407
407
@logger . log ( Logger ::INFO , message )
408
408
409
409
[ whitelisted_variation_id , message ]
0 commit comments