11# frozen_string_literal: true
22
33#
4- # Copyright 2017-2020 , Optimizely and contributors
4+ # Copyright 2017-2021 , Optimizely and contributors
55#
66# Licensed under the Apache License, Version 2.0 (the "License");
77# you may not use this file except in compliance with the License.
@@ -52,11 +52,11 @@ def initialize(logger, user_profile_service = nil)
5252 @forced_variation_map = { }
5353 end
5454
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 = [ ] )
5656 # Determines variation into which user will be bucketed.
5757 #
5858 # 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
6060 # user_id - String ID for user
6161 # attributes - Hash representing user attributes
6262 #
@@ -68,10 +68,10 @@ def get_variation(project_config, experiment_key, user_id, attributes = nil, dec
6868 bucketing_id , bucketing_id_reasons = get_bucketing_id ( user_id , attributes )
6969 decide_reasons . push ( *bucketing_id_reasons )
7070 # 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 )
7272 return nil , decide_reasons if experiment . nil?
7373
74- experiment_id = experiment [ 'id ' ]
74+ experiment_key = experiment [ 'key ' ]
7575 unless project_config . experiment_running? ( experiment )
7676 message = "Experiment '#{ experiment_key } ' is not running."
7777 @logger . log ( Logger ::INFO , message )
@@ -80,12 +80,12 @@ def get_variation(project_config, experiment_key, user_id, attributes = nil, dec
8080 end
8181
8282 # 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 )
8484 decide_reasons . push ( *reasons_received )
8585 return forced_variation [ 'id' ] , decide_reasons if forced_variation
8686
8787 # 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 )
8989 decide_reasons . push ( *reasons_received )
9090 return whitelisted_variation_id , decide_reasons if whitelisted_variation_id
9191
@@ -122,7 +122,7 @@ def get_variation(project_config, experiment_key, user_id, attributes = nil, dec
122122 message = ''
123123 if variation_id
124124 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 } '."
126126 else
127127 message = "User '#{ user_id } ' is in no variation."
128128 end
@@ -186,13 +186,13 @@ def get_variation_for_feature_experiment(project_config, feature_flag, user_id,
186186 return nil , decide_reasons
187187 end
188188
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 )
191191 decide_reasons . push ( *reasons_received )
192192
193193 next unless variation_id
194194
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 )
196196
197197 return Decision . new ( experiment , variation , DECISION_SOURCES [ 'FEATURE_TEST' ] ) , decide_reasons
198198 end
@@ -315,7 +315,7 @@ def set_forced_variation(project_config, experiment_key, user_id, variation_key)
315315 return true
316316 end
317317
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 )
319319
320320 # check if the variation exists in the datafile
321321 unless variation_id
@@ -334,7 +334,7 @@ def get_forced_variation(project_config, experiment_key, user_id)
334334 # Gets the forced variation for the given user and experiment.
335335 #
336336 # project_config - Instance of ProjectConfig
337- # experiment_key - String Key for experiment
337+ # experiment_key - String key for experiment
338338 # user_id - String ID for user
339339 #
340340 # 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)
354354 return nil , decide_reasons if experiment_id . nil? || experiment_id . empty?
355355
356356 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."
358358 @logger . log ( Logger ::DEBUG , message )
359359 decide_reasons . push ( message )
360360 return nil , decide_reasons
361361 end
362362
363363 variation_id = experiment_to_variation_map [ experiment_id ]
364364 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 )
366366 variation_key = variation [ 'key' ] if variation
367367
368368 # check if the variation exists in the datafile
369369 # this case is logged in get_variation_from_id
370370 return nil , decide_reasons if variation_key . empty?
371371
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"
373373 @logger . log ( Logger ::DEBUG , message )
374374 decide_reasons . push ( message )
375375
@@ -378,7 +378,7 @@ def get_forced_variation(project_config, experiment_key, user_id)
378378
379379 private
380380
381- def get_whitelisted_variation_id ( project_config , experiment_key , user_id )
381+ def get_whitelisted_variation_id ( project_config , experiment_id , user_id )
382382 # Determine if a user is whitelisted into a variation for the given experiment and return the ID of that variation
383383 #
384384 # project_config - project_config - Instance of ProjectConfig
@@ -387,23 +387,23 @@ def get_whitelisted_variation_id(project_config, experiment_key, user_id)
387387 #
388388 # Returns variation ID into which user_id is whitelisted (nil if no variation)
389389
390- whitelisted_variations = project_config . get_whitelisted_variations ( experiment_key )
390+ whitelisted_variations = project_config . get_whitelisted_variations ( experiment_id )
391391
392392 return nil , nil unless whitelisted_variations
393393
394394 whitelisted_variation_key = whitelisted_variations [ user_id ]
395395
396396 return nil , nil unless whitelisted_variation_key
397397
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 )
399399
400400 unless whitelisted_variation_id
401401 message = "User '#{ user_id } ' is whitelisted into variation '#{ whitelisted_variation_key } ', which is not in the datafile."
402402 @logger . log ( Logger ::INFO , message )
403403 return nil , message
404404 end
405405
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 } '."
407407 @logger . log ( Logger ::INFO , message )
408408
409409 [ whitelisted_variation_id , message ]
0 commit comments