Skip to content

feat(DecisionListener): Adds feature variables decision listener. #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 4, 2019
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
5 changes: 3 additions & 2 deletions optimizely/helpers/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,6 @@ class NotificationTypes(object):


class DecisionInfoTypes(object):
EXPERIMENT = "experiment"
FEATURE = "feature"
EXPERIMENT = "experiment"
FEATURE = "feature"
FEATURE_VARIABLE = "feature_variable"
25 changes: 24 additions & 1 deletion optimizely/optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ def _get_feature_variable_for_type(self, feature_key, variable_key, variable_typ
)
return None

feature_enabled = False
variable_value = variable.defaultValue

decision = self.decision_service.get_variation_for_feature(feature_flag, user_id, attributes)
if decision.variation:

Expand All @@ -232,12 +232,35 @@ def _get_feature_variable_for_type(self, feature_key, variable_key, variable_typ
'Returning default value for variable "%s" of feature flag "%s".' % (user_id, variable_key, feature_key)
)

experiment_key = None
variation_key = None

if decision.source == decision_service.DECISION_SOURCE_EXPERIMENT:
experiment_key = decision.experiment.key
variation_key = decision.variation.key

try:
actual_value = self.config.get_typecast_value(variable_value, variable_type)
except:
self.logger.error('Unable to cast value. Returning None.')
actual_value = None

self.notification_center.send_notifications(
enums.NotificationTypes.DECISION,
enums.DecisionInfoTypes.FEATURE_VARIABLE,
user_id,
attributes or {},
{
'feature_key': feature_key,
'feature_enabled': feature_enabled,
'variable_key': variable_key,
'variable_value': actual_value,
'variable_type': variable_type,
'source': decision.source,
'source_experiment_key': experiment_key,
'source_variation_key': variation_key
}
)
return actual_value

def activate(self, experiment_key, user_id, attributes=None):
Expand Down
12 changes: 6 additions & 6 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,24 +311,24 @@ def setUp(self, config_dict='config_dict'):
'variables': [{
'id': '132', 'value': 'true'
}, {
'id': '135', 'value': '395'
'id': '133', 'value': 'Hello audience'
}, {
'id': '134', 'value': '39.99'
}, {
'id': '133', 'value': 'Hello audience'
'id': '135', 'value': '399'
}]
}, {
'key': '211229',
'id': '211229',
'featureEnabled': False,
'variables': [{
'id': '132', 'value': 'true'
}, {
'id': '135', 'value': '395'
}, {
'id': '134', 'value': '39.99'
'id': '133', 'value': 'environment'
}, {
'id': '133', 'value': 'Hello audience'
'id': '134', 'value': '49.99'
}, {
'id': '135', 'value': '499'
}]
}]
}, {
Expand Down
10 changes: 5 additions & 5 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,11 +932,11 @@ def test_get_rollout_from_id__valid_rollout_id(self):
'variables': [{
'id': '132', 'value': 'true'
}, {
'id': '135', 'value': '395'
'id': '133', 'value': 'Hello audience'
}, {
'id': '134', 'value': '39.99'
}, {
'id': '133', 'value': 'Hello audience'
'id': '135', 'value': '399'
}]
}, {
'key': '211229',
Expand All @@ -945,11 +945,11 @@ def test_get_rollout_from_id__valid_rollout_id(self):
'variables': [{
'id': '132', 'value': 'true'
}, {
'id': '135', 'value': '395'
'id': '133', 'value': 'environment'
}, {
'id': '134', 'value': '39.99'
'id': '134', 'value': '49.99'
}, {
'id': '133', 'value': 'Hello audience'
'id': '135', 'value': '499'
}]
}]
}, {
Expand Down
Loading