10
10
from splitio .models .events import Event , EventWrapper
11
11
from splitio .models .telemetry import get_latency_bucket_index , MethodExceptionsAndLatencies
12
12
from splitio .client import input_validator
13
- from splitio .client .util import get_fallback_treatment_and_label
14
13
from splitio .util .time import get_current_epoch_time_ms , utctime_ms
15
14
16
15
@@ -41,7 +40,7 @@ class ClientBase(object): # pylint: disable=too-many-instance-attributes
41
40
'impressions_disabled' : False
42
41
}
43
42
44
- def __init__ (self , factory , recorder , labels_enabled = True , fallback_treatments_configuration = None ):
43
+ def __init__ (self , factory , recorder , labels_enabled = True , fallback_treatment_calculator = None ):
45
44
"""
46
45
Construct a Client instance.
47
46
@@ -63,10 +62,10 @@ def __init__(self, factory, recorder, labels_enabled=True, fallback_treatments_c
63
62
self ._feature_flag_storage = factory ._get_storage ('splits' ) # pylint: disable=protected-access
64
63
self ._segment_storage = factory ._get_storage ('segments' ) # pylint: disable=protected-access
65
64
self ._events_storage = factory ._get_storage ('events' ) # pylint: disable=protected-access
66
- self ._evaluator = Evaluator (self ._splitter , fallback_treatments_configuration )
65
+ self ._evaluator = Evaluator (self ._splitter , fallback_treatment_calculator )
67
66
self ._telemetry_evaluation_producer = self ._factory ._telemetry_evaluation_producer
68
67
self ._telemetry_init_producer = self ._factory ._telemetry_init_producer
69
- self ._fallback_treatments_configuration = fallback_treatments_configuration
68
+ self ._fallback_treatment_calculator = fallback_treatment_calculator
70
69
71
70
@property
72
71
def ready (self ):
@@ -206,17 +205,17 @@ def _validate_track(self, key, traffic_type, event_type, value=None, properties=
206
205
def _get_properties (self , evaluation_options ):
207
206
return evaluation_options .properties if evaluation_options != None else None
208
207
209
- def _get_fallback_treatment_with_config (self , treatment , feature ):
210
- label = ""
211
-
212
- label , treatment , config = get_fallback_treatment_and_label (self ._fallback_treatments_configuration ,
213
- feature , treatment , label , _LOGGER )
214
- return treatment , config
208
+ def _get_fallback_treatment_with_config (self , feature ):
209
+ fallback_treatment = self ._fallback_treatment_calculator .resolve (feature , "" )
210
+ return fallback_treatment .treatment , fallback_treatment .config
215
211
216
212
def _get_fallback_eval_results (self , eval_result , feature ):
217
213
result = copy .deepcopy (eval_result )
218
- result ["impression" ]["label" ], result ["treatment" ], result ["configurations" ] = get_fallback_treatment_and_label (self ._fallback_treatments_configuration ,
219
- feature , result ["treatment" ], result ["impression" ]["label" ], _LOGGER )
214
+ fallback_treatment = self ._fallback_treatment_calculator .resolve (feature , result ["impression" ]["label" ])
215
+ result ["impression" ]["label" ] = fallback_treatment .label
216
+ result ["treatment" ] = fallback_treatment .treatment
217
+ result ["configurations" ] = fallback_treatment .config
218
+
220
219
return result
221
220
222
221
def _check_impression_label (self , result ):
@@ -225,7 +224,7 @@ def _check_impression_label(self, result):
225
224
class Client (ClientBase ): # pylint: disable=too-many-instance-attributes
226
225
"""Entry point for the split sdk."""
227
226
228
- def __init__ (self , factory , recorder , labels_enabled = True , fallback_treatments_configuration = None ):
227
+ def __init__ (self , factory , recorder , labels_enabled = True , fallback_treatment_calculator = None ):
229
228
"""
230
229
Construct a Client instance.
231
230
@@ -240,7 +239,7 @@ def __init__(self, factory, recorder, labels_enabled=True, fallback_treatments_c
240
239
241
240
:rtype: Client
242
241
"""
243
- ClientBase .__init__ (self , factory , recorder , labels_enabled , fallback_treatments_configuration )
242
+ ClientBase .__init__ (self , factory , recorder , labels_enabled , fallback_treatment_calculator )
244
243
self ._context_factory = EvaluationDataFactory (factory ._get_storage ('splits' ), factory ._get_storage ('segments' ), factory ._get_storage ('rule_based_segments' ))
245
244
246
245
def destroy (self ):
@@ -275,7 +274,7 @@ def get_treatment(self, key, feature_flag_name, attributes=None, evaluation_opti
275
274
276
275
except :
277
276
_LOGGER .error ('get_treatment failed' )
278
- treatment , _ = self ._get_fallback_treatment_with_config (CONTROL , feature_flag_name )
277
+ treatment , _ = self ._get_fallback_treatment_with_config (feature_flag_name )
279
278
return treatment
280
279
281
280
def get_treatment_with_config (self , key , feature_flag_name , attributes = None , evaluation_options = None ):
@@ -301,7 +300,7 @@ def get_treatment_with_config(self, key, feature_flag_name, attributes=None, eva
301
300
302
301
except Exception :
303
302
_LOGGER .error ('get_treatment_with_config failed' )
304
- return self ._get_fallback_treatment_with_config (CONTROL , feature_flag_name )
303
+ return self ._get_fallback_treatment_with_config (feature_flag_name )
305
304
306
305
def _get_treatment (self , method , key , feature , attributes = None , evaluation_options = None ):
307
306
"""
@@ -321,7 +320,7 @@ def _get_treatment(self, method, key, feature, attributes=None, evaluation_optio
321
320
:rtype: dict
322
321
"""
323
322
if not self ._client_is_usable (): # not destroyed & not waiting for a fork
324
- return self ._get_fallback_treatment_with_config (CONTROL , feature )
323
+ return self ._get_fallback_treatment_with_config (feature )
325
324
326
325
start = get_current_epoch_time_ms ()
327
326
if not self .ready :
@@ -331,7 +330,7 @@ def _get_treatment(self, method, key, feature, attributes=None, evaluation_optio
331
330
try :
332
331
key , bucketing , feature , attributes , evaluation_options = self ._validate_treatment_input (key , feature , attributes , method , evaluation_options )
333
332
except _InvalidInputError :
334
- return self ._get_fallback_treatment_with_config (CONTROL , feature )
333
+ return self ._get_fallback_treatment_with_config (feature )
335
334
336
335
result = self ._get_fallback_eval_results (self ._NON_READY_EVAL_RESULT , feature )
337
336
@@ -376,7 +375,7 @@ def get_treatments(self, key, feature_flag_names, attributes=None, evaluation_op
376
375
return {feature_flag : result [0 ] for (feature_flag , result ) in with_config .items ()}
377
376
378
377
except Exception :
379
- return {feature : self ._get_fallback_treatment_with_config (CONTROL , feature )[0 ] for feature in feature_flag_names }
378
+ return {feature : self ._get_fallback_treatment_with_config (feature )[0 ] for feature in feature_flag_names }
380
379
381
380
def get_treatments_with_config (self , key , feature_flag_names , attributes = None , evaluation_options = None ):
382
381
"""
@@ -400,7 +399,7 @@ def get_treatments_with_config(self, key, feature_flag_names, attributes=None, e
400
399
return self ._get_treatments (key , feature_flag_names , MethodExceptionsAndLatencies .TREATMENTS_WITH_CONFIG , attributes , evaluation_options )
401
400
402
401
except Exception :
403
- return {feature : (self ._get_fallback_treatment_with_config (CONTROL , feature )) for feature in feature_flag_names }
402
+ return {feature : (self ._get_fallback_treatment_with_config (feature )) for feature in feature_flag_names }
404
403
405
404
def get_treatments_by_flag_set (self , key , flag_set , attributes = None , evaluation_options = None ):
406
405
"""
@@ -624,7 +623,7 @@ def _get_treatments(self, key, features, method, attributes=None, evaluation_opt
624
623
"""
625
624
start = get_current_epoch_time_ms ()
626
625
if not self ._client_is_usable ():
627
- return input_validator .generate_control_treatments (features , self ._fallback_treatments_configuration )
626
+ return input_validator .generate_control_treatments (features , self ._fallback_treatment_calculator )
628
627
629
628
if not self .ready :
630
629
_LOGGER .error ("Client is not ready - no calls possible" )
@@ -633,7 +632,7 @@ def _get_treatments(self, key, features, method, attributes=None, evaluation_opt
633
632
try :
634
633
key , bucketing , features , attributes , evaluation_options = self ._validate_treatments_input (key , features , attributes , method , evaluation_options )
635
634
except _InvalidInputError :
636
- return input_validator .generate_control_treatments (features , self ._fallback_treatments_configuration )
635
+ return input_validator .generate_control_treatments (features , self ._fallback_treatment_calculator )
637
636
638
637
results = {n : self ._get_fallback_eval_results (self ._NON_READY_EVAL_RESULT , n ) for n in features }
639
638
if self .ready :
@@ -726,7 +725,7 @@ def track(self, key, traffic_type, event_type, value=None, properties=None):
726
725
class ClientAsync (ClientBase ): # pylint: disable=too-many-instance-attributes
727
726
"""Entry point for the split sdk."""
728
727
729
- def __init__ (self , factory , recorder , labels_enabled = True , fallback_treatments_configuration = None ):
728
+ def __init__ (self , factory , recorder , labels_enabled = True , fallback_treatment_calculator = None ):
730
729
"""
731
730
Construct a Client instance.
732
731
@@ -741,7 +740,7 @@ def __init__(self, factory, recorder, labels_enabled=True, fallback_treatments_c
741
740
742
741
:rtype: Client
743
742
"""
744
- ClientBase .__init__ (self , factory , recorder , labels_enabled , fallback_treatments_configuration )
743
+ ClientBase .__init__ (self , factory , recorder , labels_enabled , fallback_treatment_calculator )
745
744
self ._context_factory = AsyncEvaluationDataFactory (factory ._get_storage ('splits' ), factory ._get_storage ('segments' ), factory ._get_storage ('rule_based_segments' ))
746
745
747
746
async def destroy (self ):
@@ -776,7 +775,7 @@ async def get_treatment(self, key, feature_flag_name, attributes=None, evaluatio
776
775
777
776
except :
778
777
_LOGGER .error ('get_treatment failed' )
779
- treatment , _ = self ._get_fallback_treatment_with_config (CONTROL , feature_flag_name )
778
+ treatment , _ = self ._get_fallback_treatment_with_config (feature_flag_name )
780
779
return treatment
781
780
782
781
async def get_treatment_with_config (self , key , feature_flag_name , attributes = None , evaluation_options = None ):
@@ -802,7 +801,7 @@ async def get_treatment_with_config(self, key, feature_flag_name, attributes=Non
802
801
803
802
except Exception :
804
803
_LOGGER .error ('get_treatment_with_config failed' )
805
- return self ._get_fallback_treatment_with_config (CONTROL , feature_flag_name )
804
+ return self ._get_fallback_treatment_with_config (feature_flag_name )
806
805
807
806
async def _get_treatment (self , method , key , feature , attributes = None , evaluation_options = None ):
808
807
"""
@@ -822,7 +821,7 @@ async def _get_treatment(self, method, key, feature, attributes=None, evaluation
822
821
:rtype: dict
823
822
"""
824
823
if not self ._client_is_usable (): # not destroyed & not waiting for a fork
825
- return self ._get_fallback_treatment_with_config (CONTROL , feature )
824
+ return self ._get_fallback_treatment_with_config (feature )
826
825
827
826
start = get_current_epoch_time_ms ()
828
827
if not self .ready :
@@ -832,7 +831,7 @@ async def _get_treatment(self, method, key, feature, attributes=None, evaluation
832
831
try :
833
832
key , bucketing , feature , attributes , evaluation_options = self ._validate_treatment_input (key , feature , attributes , method , evaluation_options )
834
833
except _InvalidInputError :
835
- return self ._get_fallback_treatment_with_config (CONTROL , feature )
834
+ return self ._get_fallback_treatment_with_config (feature )
836
835
837
836
result = self ._get_fallback_eval_results (self ._NON_READY_EVAL_RESULT , feature )
838
837
if self .ready :
@@ -875,7 +874,7 @@ async def get_treatments(self, key, feature_flag_names, attributes=None, evaluat
875
874
return {feature_flag : result [0 ] for (feature_flag , result ) in with_config .items ()}
876
875
877
876
except Exception :
878
- return {feature : self ._get_fallback_treatment_with_config (CONTROL , feature )[0 ] for feature in feature_flag_names }
877
+ return {feature : self ._get_fallback_treatment_with_config (feature )[0 ] for feature in feature_flag_names }
879
878
880
879
async def get_treatments_with_config (self , key , feature_flag_names , attributes = None , evaluation_options = None ):
881
880
"""
@@ -899,7 +898,7 @@ async def get_treatments_with_config(self, key, feature_flag_names, attributes=N
899
898
return await self ._get_treatments (key , feature_flag_names , MethodExceptionsAndLatencies .TREATMENTS_WITH_CONFIG , attributes , evaluation_options )
900
899
901
900
except Exception :
902
- return {feature : (self ._get_fallback_treatment_with_config (CONTROL , feature )) for feature in feature_flag_names }
901
+ return {feature : (self ._get_fallback_treatment_with_config (feature )) for feature in feature_flag_names }
903
902
904
903
async def get_treatments_by_flag_set (self , key , flag_set , attributes = None , evaluation_options = None ):
905
904
"""
@@ -1037,7 +1036,7 @@ async def _get_treatments(self, key, features, method, attributes=None, evaluati
1037
1036
"""
1038
1037
start = get_current_epoch_time_ms ()
1039
1038
if not self ._client_is_usable ():
1040
- return input_validator .generate_control_treatments (features , self ._fallback_treatments_configuration )
1039
+ return input_validator .generate_control_treatments (features , self ._fallback_treatment_calculator )
1041
1040
1042
1041
if not self .ready :
1043
1042
_LOGGER .error ("Client is not ready - no calls possible" )
@@ -1046,7 +1045,7 @@ async def _get_treatments(self, key, features, method, attributes=None, evaluati
1046
1045
try :
1047
1046
key , bucketing , features , attributes , evaluation_options = self ._validate_treatments_input (key , features , attributes , method , evaluation_options )
1048
1047
except _InvalidInputError :
1049
- return input_validator .generate_control_treatments (features , self ._fallback_treatments_configuration )
1048
+ return input_validator .generate_control_treatments (features , self ._fallback_treatment_calculator )
1050
1049
1051
1050
results = {n : self ._get_fallback_eval_results (self ._NON_READY_EVAL_RESULT , n ) for n in features }
1052
1051
if self .ready :
0 commit comments