Skip to content

Commit 42f2eb0

Browse files
rashidspaliabbasrizvi
authored andcommitted
feat: decision listener for get feature variable * (#170)
1 parent e4d949a commit 42f2eb0

File tree

5 files changed

+367
-36
lines changed

5 files changed

+367
-36
lines changed

optimizely/helpers/enums.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,6 @@ class NotificationTypes(object):
9898

9999

100100
class DecisionInfoTypes(object):
101-
EXPERIMENT = "experiment"
102-
FEATURE = "feature"
101+
EXPERIMENT = "experiment"
102+
FEATURE = "feature"
103+
FEATURE_VARIABLE = "feature_variable"

optimizely/optimizely.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ def _get_feature_variable_for_type(self, feature_key, variable_key, variable_typ
208208
)
209209
return None
210210

211+
feature_enabled = False
211212
variable_value = variable.defaultValue
212-
213213
decision = self.decision_service.get_variation_for_feature(feature_flag, user_id, attributes)
214214
if decision.variation:
215215

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

235+
experiment_key = None
236+
variation_key = None
237+
238+
if decision.source == decision_service.DECISION_SOURCE_EXPERIMENT:
239+
experiment_key = decision.experiment.key
240+
variation_key = decision.variation.key
241+
235242
try:
236243
actual_value = self.config.get_typecast_value(variable_value, variable_type)
237244
except:
238245
self.logger.error('Unable to cast value. Returning None.')
239246
actual_value = None
240247

248+
self.notification_center.send_notifications(
249+
enums.NotificationTypes.DECISION,
250+
enums.DecisionInfoTypes.FEATURE_VARIABLE,
251+
user_id,
252+
attributes or {},
253+
{
254+
'feature_key': feature_key,
255+
'feature_enabled': feature_enabled,
256+
'variable_key': variable_key,
257+
'variable_value': actual_value,
258+
'variable_type': variable_type,
259+
'source': decision.source,
260+
'source_experiment_key': experiment_key,
261+
'source_variation_key': variation_key
262+
}
263+
)
241264
return actual_value
242265

243266
def activate(self, experiment_key, user_id, attributes=None):

tests/base.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,24 +311,24 @@ def setUp(self, config_dict='config_dict'):
311311
'variables': [{
312312
'id': '132', 'value': 'true'
313313
}, {
314-
'id': '135', 'value': '395'
314+
'id': '133', 'value': 'Hello audience'
315315
}, {
316316
'id': '134', 'value': '39.99'
317317
}, {
318-
'id': '133', 'value': 'Hello audience'
318+
'id': '135', 'value': '399'
319319
}]
320320
}, {
321321
'key': '211229',
322322
'id': '211229',
323323
'featureEnabled': False,
324324
'variables': [{
325325
'id': '132', 'value': 'true'
326-
}, {
327-
'id': '135', 'value': '395'
328326
}, {
329-
'id': '134', 'value': '39.99'
327+
'id': '133', 'value': 'environment'
330328
}, {
331-
'id': '133', 'value': 'Hello audience'
329+
'id': '134', 'value': '49.99'
330+
}, {
331+
'id': '135', 'value': '499'
332332
}]
333333
}]
334334
}, {

tests/test_config.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -932,11 +932,11 @@ def test_get_rollout_from_id__valid_rollout_id(self):
932932
'variables': [{
933933
'id': '132', 'value': 'true'
934934
}, {
935-
'id': '135', 'value': '395'
935+
'id': '133', 'value': 'Hello audience'
936936
}, {
937937
'id': '134', 'value': '39.99'
938938
}, {
939-
'id': '133', 'value': 'Hello audience'
939+
'id': '135', 'value': '399'
940940
}]
941941
}, {
942942
'key': '211229',
@@ -945,11 +945,11 @@ def test_get_rollout_from_id__valid_rollout_id(self):
945945
'variables': [{
946946
'id': '132', 'value': 'true'
947947
}, {
948-
'id': '135', 'value': '395'
948+
'id': '133', 'value': 'environment'
949949
}, {
950-
'id': '134', 'value': '39.99'
950+
'id': '134', 'value': '49.99'
951951
}, {
952-
'id': '133', 'value': 'Hello audience'
952+
'id': '135', 'value': '499'
953953
}]
954954
}]
955955
}, {

0 commit comments

Comments
 (0)