Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 31 additions & 32 deletions splitio/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from splitio.models.events import Event, EventWrapper
from splitio.models.telemetry import get_latency_bucket_index, MethodExceptionsAndLatencies
from splitio.client import input_validator
from splitio.client.util import get_fallback_treatment_and_label
from splitio.util.time import get_current_epoch_time_ms, utctime_ms


Expand Down Expand Up @@ -41,7 +40,7 @@ class ClientBase(object): # pylint: disable=too-many-instance-attributes
'impressions_disabled': False
}

def __init__(self, factory, recorder, labels_enabled=True, fallback_treatments_configuration=None):
def __init__(self, factory, recorder, labels_enabled=True, fallback_treatment_calculator=None):
"""
Construct a Client instance.

Expand All @@ -63,10 +62,10 @@ def __init__(self, factory, recorder, labels_enabled=True, fallback_treatments_c
self._feature_flag_storage = factory._get_storage('splits') # pylint: disable=protected-access
self._segment_storage = factory._get_storage('segments') # pylint: disable=protected-access
self._events_storage = factory._get_storage('events') # pylint: disable=protected-access
self._evaluator = Evaluator(self._splitter, fallback_treatments_configuration)
self._evaluator = Evaluator(self._splitter, fallback_treatment_calculator)
self._telemetry_evaluation_producer = self._factory._telemetry_evaluation_producer
self._telemetry_init_producer = self._factory._telemetry_init_producer
self._fallback_treatments_configuration = fallback_treatments_configuration
self._fallback_treatment_calculator = fallback_treatment_calculator

@property
def ready(self):
Expand Down Expand Up @@ -206,17 +205,17 @@ def _validate_track(self, key, traffic_type, event_type, value=None, properties=
def _get_properties(self, evaluation_options):
return evaluation_options.properties if evaluation_options != None else None

def _get_fallback_treatment_with_config(self, treatment, feature):
label = ""

label, treatment, config = get_fallback_treatment_and_label(self._fallback_treatments_configuration,
feature, treatment, label, _LOGGER)
return treatment, config
def _get_fallback_treatment_with_config(self, feature):
fallback_treatment = self._fallback_treatment_calculator.resolve(feature, "")
return fallback_treatment.treatment, fallback_treatment.config

def _get_fallback_eval_results(self, eval_result, feature):
result = copy.deepcopy(eval_result)
result["impression"]["label"], result["treatment"], result["configurations"] = get_fallback_treatment_and_label(self._fallback_treatments_configuration,
feature, result["treatment"], result["impression"]["label"], _LOGGER)
fallback_treatment = self._fallback_treatment_calculator.resolve(feature, result["impression"]["label"])
result["impression"]["label"] = fallback_treatment.label
result["treatment"] = fallback_treatment.treatment
result["configurations"] = fallback_treatment.config

return result

def _check_impression_label(self, result):
Expand All @@ -225,7 +224,7 @@ def _check_impression_label(self, result):
class Client(ClientBase): # pylint: disable=too-many-instance-attributes
"""Entry point for the split sdk."""

def __init__(self, factory, recorder, labels_enabled=True, fallback_treatments_configuration=None):
def __init__(self, factory, recorder, labels_enabled=True, fallback_treatment_calculator=None):
"""
Construct a Client instance.

Expand All @@ -240,7 +239,7 @@ def __init__(self, factory, recorder, labels_enabled=True, fallback_treatments_c

:rtype: Client
"""
ClientBase.__init__(self, factory, recorder, labels_enabled, fallback_treatments_configuration)
ClientBase.__init__(self, factory, recorder, labels_enabled, fallback_treatment_calculator)
self._context_factory = EvaluationDataFactory(factory._get_storage('splits'), factory._get_storage('segments'), factory._get_storage('rule_based_segments'))

def destroy(self):
Expand Down Expand Up @@ -275,7 +274,7 @@ def get_treatment(self, key, feature_flag_name, attributes=None, evaluation_opti

except:
_LOGGER.error('get_treatment failed')
treatment, _ = self._get_fallback_treatment_with_config(CONTROL, feature_flag_name)
treatment, _ = self._get_fallback_treatment_with_config(feature_flag_name)
return treatment

def get_treatment_with_config(self, key, feature_flag_name, attributes=None, evaluation_options=None):
Expand All @@ -301,7 +300,7 @@ def get_treatment_with_config(self, key, feature_flag_name, attributes=None, eva

except Exception:
_LOGGER.error('get_treatment_with_config failed')
return self._get_fallback_treatment_with_config(CONTROL, feature_flag_name)
return self._get_fallback_treatment_with_config(feature_flag_name)

def _get_treatment(self, method, key, feature, attributes=None, evaluation_options=None):
"""
Expand All @@ -321,7 +320,7 @@ def _get_treatment(self, method, key, feature, attributes=None, evaluation_optio
:rtype: dict
"""
if not self._client_is_usable(): # not destroyed & not waiting for a fork
return self._get_fallback_treatment_with_config(CONTROL, feature)
return self._get_fallback_treatment_with_config(feature)

start = get_current_epoch_time_ms()
if not self.ready:
Expand All @@ -331,7 +330,7 @@ def _get_treatment(self, method, key, feature, attributes=None, evaluation_optio
try:
key, bucketing, feature, attributes, evaluation_options = self._validate_treatment_input(key, feature, attributes, method, evaluation_options)
except _InvalidInputError:
return self._get_fallback_treatment_with_config(CONTROL, feature)
return self._get_fallback_treatment_with_config(feature)

result = self._get_fallback_eval_results(self._NON_READY_EVAL_RESULT, feature)

Expand Down Expand Up @@ -376,7 +375,7 @@ def get_treatments(self, key, feature_flag_names, attributes=None, evaluation_op
return {feature_flag: result[0] for (feature_flag, result) in with_config.items()}

except Exception:
return {feature: self._get_fallback_treatment_with_config(CONTROL, feature)[0] for feature in feature_flag_names}
return {feature: self._get_fallback_treatment_with_config(feature)[0] for feature in feature_flag_names}

def get_treatments_with_config(self, key, feature_flag_names, attributes=None, evaluation_options=None):
"""
Expand All @@ -400,7 +399,7 @@ def get_treatments_with_config(self, key, feature_flag_names, attributes=None, e
return self._get_treatments(key, feature_flag_names, MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG, attributes, evaluation_options)

except Exception:
return {feature: (self._get_fallback_treatment_with_config(CONTROL, feature)) for feature in feature_flag_names}
return {feature: (self._get_fallback_treatment_with_config(feature)) for feature in feature_flag_names}

def get_treatments_by_flag_set(self, key, flag_set, attributes=None, evaluation_options=None):
"""
Expand Down Expand Up @@ -624,7 +623,7 @@ def _get_treatments(self, key, features, method, attributes=None, evaluation_opt
"""
start = get_current_epoch_time_ms()
if not self._client_is_usable():
return input_validator.generate_control_treatments(features, self._fallback_treatments_configuration)
return input_validator.generate_control_treatments(features, self._fallback_treatment_calculator)

if not self.ready:
_LOGGER.error("Client is not ready - no calls possible")
Expand All @@ -633,7 +632,7 @@ def _get_treatments(self, key, features, method, attributes=None, evaluation_opt
try:
key, bucketing, features, attributes, evaluation_options = self._validate_treatments_input(key, features, attributes, method, evaluation_options)
except _InvalidInputError:
return input_validator.generate_control_treatments(features, self._fallback_treatments_configuration)
return input_validator.generate_control_treatments(features, self._fallback_treatment_calculator)

results = {n: self._get_fallback_eval_results(self._NON_READY_EVAL_RESULT, n) for n in features}
if self.ready:
Expand Down Expand Up @@ -726,7 +725,7 @@ def track(self, key, traffic_type, event_type, value=None, properties=None):
class ClientAsync(ClientBase): # pylint: disable=too-many-instance-attributes
"""Entry point for the split sdk."""

def __init__(self, factory, recorder, labels_enabled=True, fallback_treatments_configuration=None):
def __init__(self, factory, recorder, labels_enabled=True, fallback_treatment_calculator=None):
"""
Construct a Client instance.

Expand All @@ -741,7 +740,7 @@ def __init__(self, factory, recorder, labels_enabled=True, fallback_treatments_c

:rtype: Client
"""
ClientBase.__init__(self, factory, recorder, labels_enabled, fallback_treatments_configuration)
ClientBase.__init__(self, factory, recorder, labels_enabled, fallback_treatment_calculator)
self._context_factory = AsyncEvaluationDataFactory(factory._get_storage('splits'), factory._get_storage('segments'), factory._get_storage('rule_based_segments'))

async def destroy(self):
Expand Down Expand Up @@ -776,7 +775,7 @@ async def get_treatment(self, key, feature_flag_name, attributes=None, evaluatio

except:
_LOGGER.error('get_treatment failed')
treatment, _ = self._get_fallback_treatment_with_config(CONTROL, feature_flag_name)
treatment, _ = self._get_fallback_treatment_with_config(feature_flag_name)
return treatment

async def get_treatment_with_config(self, key, feature_flag_name, attributes=None, evaluation_options=None):
Expand All @@ -802,7 +801,7 @@ async def get_treatment_with_config(self, key, feature_flag_name, attributes=Non

except Exception:
_LOGGER.error('get_treatment_with_config failed')
return self._get_fallback_treatment_with_config(CONTROL, feature_flag_name)
return self._get_fallback_treatment_with_config(feature_flag_name)

async def _get_treatment(self, method, key, feature, attributes=None, evaluation_options=None):
"""
Expand All @@ -822,7 +821,7 @@ async def _get_treatment(self, method, key, feature, attributes=None, evaluation
:rtype: dict
"""
if not self._client_is_usable(): # not destroyed & not waiting for a fork
return self._get_fallback_treatment_with_config(CONTROL, feature)
return self._get_fallback_treatment_with_config(feature)

start = get_current_epoch_time_ms()
if not self.ready:
Expand All @@ -832,7 +831,7 @@ async def _get_treatment(self, method, key, feature, attributes=None, evaluation
try:
key, bucketing, feature, attributes, evaluation_options = self._validate_treatment_input(key, feature, attributes, method, evaluation_options)
except _InvalidInputError:
return self._get_fallback_treatment_with_config(CONTROL, feature)
return self._get_fallback_treatment_with_config(feature)

result = self._get_fallback_eval_results(self._NON_READY_EVAL_RESULT, feature)
if self.ready:
Expand Down Expand Up @@ -875,7 +874,7 @@ async def get_treatments(self, key, feature_flag_names, attributes=None, evaluat
return {feature_flag: result[0] for (feature_flag, result) in with_config.items()}

except Exception:
return {feature: self._get_fallback_treatment_with_config(CONTROL, feature)[0] for feature in feature_flag_names}
return {feature: self._get_fallback_treatment_with_config(feature)[0] for feature in feature_flag_names}

async def get_treatments_with_config(self, key, feature_flag_names, attributes=None, evaluation_options=None):
"""
Expand All @@ -899,7 +898,7 @@ async def get_treatments_with_config(self, key, feature_flag_names, attributes=N
return await self._get_treatments(key, feature_flag_names, MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG, attributes, evaluation_options)

except Exception:
return {feature: (self._get_fallback_treatment_with_config(CONTROL, feature)) for feature in feature_flag_names}
return {feature: (self._get_fallback_treatment_with_config(feature)) for feature in feature_flag_names}

async def get_treatments_by_flag_set(self, key, flag_set, attributes=None, evaluation_options=None):
"""
Expand Down Expand Up @@ -1037,7 +1036,7 @@ async def _get_treatments(self, key, features, method, attributes=None, evaluati
"""
start = get_current_epoch_time_ms()
if not self._client_is_usable():
return input_validator.generate_control_treatments(features, self._fallback_treatments_configuration)
return input_validator.generate_control_treatments(features, self._fallback_treatment_calculator)

if not self.ready:
_LOGGER.error("Client is not ready - no calls possible")
Expand All @@ -1046,7 +1045,7 @@ async def _get_treatments(self, key, features, method, attributes=None, evaluati
try:
key, bucketing, features, attributes, evaluation_options = self._validate_treatments_input(key, features, attributes, method, evaluation_options)
except _InvalidInputError:
return input_validator.generate_control_treatments(features, self._fallback_treatments_configuration)
return input_validator.generate_control_treatments(features, self._fallback_treatment_calculator)

results = {n: self._get_fallback_eval_results(self._NON_READY_EVAL_RESULT, n) for n in features}
if self.ready:
Expand Down
Loading