@@ -18,7 +18,7 @@ class SplitClient
1818 # @param sdk_key [String] the SDK key for your split account
1919 #
2020 # @return [SplitIoClient] split.io client instance
21- def initialize ( sdk_key , repositories , status_manager , config , impressions_manager , telemetry_evaluation_producer , evaluator , split_validator )
21+ def initialize ( sdk_key , repositories , status_manager , config , impressions_manager , telemetry_evaluation_producer , evaluator , split_validator , fallback_treatment_calculator )
2222 @api_key = sdk_key
2323 @splits_repository = repositories [ :splits ]
2424 @segments_repository = repositories [ :segments ]
@@ -32,6 +32,7 @@ def initialize(sdk_key, repositories, status_manager, config, impressions_manage
3232 @telemetry_evaluation_producer = telemetry_evaluation_producer
3333 @split_validator = split_validator
3434 @evaluator = evaluator
35+ @fallback_treatment_calculator = fallback_treatment_calculator
3536 end
3637
3738 def get_treatment (
@@ -50,7 +51,9 @@ def get_treatment_with_config(
5051 multiple = false , evaluator = nil
5152 )
5253 log_deprecated_warning ( GET_TREATMENT , evaluator , 'evaluator' )
53- treatment ( key , split_name , attributes , split_data , store_impressions , GET_TREATMENT_WITH_CONFIG , multiple , evaluation_options )
54+ result = treatment ( key , split_name , attributes , split_data , store_impressions , GET_TREATMENT_WITH_CONFIG , multiple , evaluation_options )
55+
56+ { :config => result [ :config ] , :treatment => result [ :treatment ] }
5457 end
5558
5659 def get_treatments ( key , split_names , attributes = { } , evaluation_options = nil )
@@ -63,7 +66,11 @@ def get_treatments(key, split_names, attributes = {}, evaluation_options = nil)
6366 end
6467
6568 def get_treatments_with_config ( key , split_names , attributes = { } , evaluation_options = nil )
66- treatments ( key , split_names , attributes , evaluation_options , GET_TREATMENTS_WITH_CONFIG )
69+ results = treatments ( key , split_names , attributes , evaluation_options , GET_TREATMENTS_WITH_CONFIG )
70+
71+ results . map { |key , value |
72+ [ key , { treatment : value [ :treatment ] , config : value [ :config ] } ]
73+ } . to_h
6774 end
6875
6976 def get_treatments_by_flag_set ( key , flag_set , attributes = { } , evaluation_options = nil )
@@ -89,13 +96,21 @@ def get_treatments_by_flag_sets(key, flag_sets, attributes = {}, evaluation_opti
8996 def get_treatments_with_config_by_flag_set ( key , flag_set , attributes = { } , evaluation_options = nil )
9097 valid_flag_set = @split_validator . valid_flag_sets ( GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SET , [ flag_set ] )
9198 split_names = @splits_repository . get_feature_flags_by_sets ( valid_flag_set )
92- treatments ( key , split_names , attributes , evaluation_options , GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SET )
99+ results = treatments ( key , split_names , attributes , evaluation_options , GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SET )
100+
101+ results . map { |key , value |
102+ [ key , { treatment : value [ :treatment ] , config : value [ :config ] } ]
103+ } . to_h
93104 end
94105
95106 def get_treatments_with_config_by_flag_sets ( key , flag_sets , attributes = { } , evaluation_options = nil )
96107 valid_flag_set = @split_validator . valid_flag_sets ( GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SETS , flag_sets )
97108 split_names = @splits_repository . get_feature_flags_by_sets ( valid_flag_set )
98- treatments ( key , split_names , attributes , evaluation_options , GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SETS )
109+ results = treatments ( key , split_names , attributes , evaluation_options , GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SETS )
110+
111+ results . map { |key , value |
112+ [ key , { treatment : value [ :treatment ] , config : value [ :config ] } ]
113+ } . to_h
99114 end
100115
101116 def destroy
@@ -277,7 +292,7 @@ def treatments(key, feature_flag_names, attributes = {}, evaluation_options = ni
277292 if !@config . split_validator . valid_get_treatments_parameters ( calling_method , key , sanitized_feature_flag_names , matching_key , bucketing_key , attributes )
278293 to_return = Hash . new
279294 sanitized_feature_flag_names . each { |name |
280- to_return [ name . to_sym ] = control_treatment_with_config
295+ to_return [ name . to_sym ] = check_fallback_treatment ( name , '' )
281296 }
282297 return to_return
283298 end
@@ -286,9 +301,11 @@ def treatments(key, feature_flag_names, attributes = {}, evaluation_options = ni
286301 impressions = [ ]
287302 to_return = Hash . new
288303 sanitized_feature_flag_names . each { |name |
289- to_return [ name . to_sym ] = control_treatment_with_config
304+ treatment_data = check_fallback_treatment ( name , Engine ::Models ::Label ::NOT_READY )
305+ to_return [ name . to_sym ] = treatment_data
306+
290307 impressions << { :impression => @impressions_manager . build_impression ( matching_key , bucketing_key , name . to_sym ,
291- control_treatment_with_config . merge ( { :label => Engine :: Models :: Label :: NOT_READY } ) , false , { attributes : attributes , time : nil } ,
308+ get_treatment_without_config ( treatment_data ) , false , { attributes : attributes , time : nil } ,
292309 evaluation_options ) , :disabled => false }
293310 }
294311 @impressions_manager . track ( impressions )
@@ -308,7 +325,7 @@ def treatments(key, feature_flag_names, attributes = {}, evaluation_options = ni
308325 if feature_flag . nil?
309326 @config . logger . warn ( "#{ calling_method } : you passed #{ key } that " \
310327 'does not exist in this environment, please double check what feature flags exist in the Split user interface' )
311- invalid_treatments [ key ] = control_treatment_with_config
328+ invalid_treatments [ key ] = check_fallback_treatment ( key , Engine :: Models :: Label :: NOT_FOUND )
312329 next
313330 end
314331 treatments_labels_change_numbers , impressions = evaluate_treatment ( feature_flag , key , bucketing_key , matching_key , attributes , calling_method , false , evaluation_options )
@@ -344,7 +361,7 @@ def treatment(key, feature_flag_name, attributes = {}, split_data = nil, store_i
344361
345362 attributes = parsed_attributes ( attributes )
346363
347- return parsed_treatment ( control_treatment , multiple ) unless valid_client && @config . split_validator . valid_get_treatment_parameters ( calling_method , key , feature_flag_name , matching_key , bucketing_key , attributes )
364+ return parsed_treatment ( check_fallback_treatment ( feature_flag_name , "" ) , multiple ) unless valid_client && @config . split_validator . valid_get_treatment_parameters ( calling_method , key , feature_flag_name , matching_key , bucketing_key , attributes )
348365
349366 bucketing_key = bucketing_key ? bucketing_key . to_s : nil
350367 matching_key = matching_key . to_s
@@ -373,7 +390,7 @@ def evaluate_treatment(feature_flag, feature_flag_name, bucketing_key, matching_
373390 if feature_flag . nil? && ready?
374391 @config . logger . warn ( "#{ calling_method } : you passed #{ feature_flag_name } that " \
375392 'does not exist in this environment, please double check what feature flags exist in the Split user interface' )
376- return parsed_treatment ( control_treatment . merge ( { :label => Engine ::Models ::Label ::NOT_FOUND } ) , multiple ) , nil
393+ return check_fallback_treatment ( feature_flag_name , Engine ::Models ::Label ::NOT_FOUND ) , nil
377394 end
378395
379396 if !feature_flag . nil? && ready?
@@ -383,7 +400,7 @@ def evaluate_treatment(feature_flag, feature_flag_name, bucketing_key, matching_
383400 impressions_disabled = feature_flag [ :impressionsDisabled ]
384401 else
385402 @config . logger . error ( "#{ calling_method } : the SDK is not ready, results may be incorrect for feature flag #{ feature_flag_name } . Make sure to wait for SDK readiness before using this method." )
386- treatment_data = control_treatment . merge ( { :label => Engine ::Models ::Label ::NOT_READY } )
403+ treatment_data = check_fallback_treatment ( feature_flag_name , Engine ::Models ::Label ::NOT_READY )
387404 impressions_disabled = false
388405 end
389406
@@ -396,22 +413,16 @@ def evaluate_treatment(feature_flag, feature_flag_name, bucketing_key, matching_
396413 rescue StandardError => e
397414 @config . log_found_exception ( __method__ . to_s , e )
398415 record_exception ( calling_method )
399- impression_decorator = { :impression => @impressions_manager . build_impression ( matching_key , bucketing_key , feature_flag_name , control_treatment , false , { attributes : attributes , time : nil } , evaluation_options ) , :disabled => false }
416+ treatment_data = check_fallback_treatment ( feature_flag_name , Engine ::Models ::Label ::EXCEPTION )
417+ impression_decorator = { :impression => @impressions_manager . build_impression ( matching_key , bucketing_key , feature_flag_name , get_treatment_without_config ( treatment_data ) , false , { attributes : attributes , time : nil } , evaluation_options ) , :disabled => false }
418+
400419 impressions_decorator << impression_decorator unless impression_decorator . nil?
401420
402- return parsed_treatment ( control_treatment . merge ( { :label => Engine :: Models :: Label :: EXCEPTION } ) , multiple ) , impressions_decorator
421+ return parsed_treatment ( treatment_data , multiple ) , impressions_decorator
403422 end
404423 return parsed_treatment ( treatment_data , multiple ) , impressions_decorator
405424 end
406425
407- def control_treatment
408- { :treatment => Engine ::Models ::Treatment ::CONTROL }
409- end
410-
411- def control_treatment_with_config
412- { :treatment => Engine ::Models ::Treatment ::CONTROL , :config => nil }
413- end
414-
415426 def variable_size ( value )
416427 value . is_a? ( String ) ? value . length : 0
417428 end
@@ -472,5 +483,39 @@ def record_exception(method)
472483 @telemetry_evaluation_producer . record_exception ( Telemetry ::Domain ::Constants ::TRACK )
473484 end
474485 end
486+
487+ def check_fallback_treatment ( feature_name , label )
488+ return {
489+ label : ( label != '' ) ? label : nil ,
490+ treatment : Engine ::Models ::Treatment ::CONTROL ,
491+ config : nil ,
492+ change_number : nil
493+ } unless feature_name . is_a? ( Symbol ) || feature_name . is_a? ( String )
494+
495+ fallback_treatment = @fallback_treatment_calculator . resolve ( feature_name . to_sym , label )
496+
497+ {
498+ label : ( label != '' ) ? fallback_treatment . label : nil ,
499+ treatment : fallback_treatment . treatment ,
500+ config : get_fallback_config ( fallback_treatment ) ,
501+ change_number : nil
502+ }
503+ end
504+
505+ def get_treatment_without_config ( treatment )
506+ {
507+ label : treatment [ :label ] ,
508+ treatment : treatment [ :treatment ] ,
509+ }
510+ end
511+
512+ def get_fallback_config ( fallback_treatment )
513+ if fallback_treatment . config != nil
514+ return fallback_treatment . config
515+ end
516+
517+ return nil
518+ end
519+
475520 end
476521end
0 commit comments