8000 Add forced-decisions APIs to OptimizelyUserContext by Mat001 · Pull Request #361 · optimizely/python-sdk · GitHub
[go: up one dir, main page]

Skip to content

Add forced-decisions APIs to OptimizelyUserContext #361

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 42 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
c5d9dae
add maps to project config
Mat001 Sep 9, 2021
17ad742
initial code
Mat001 Sep 15, 2021
cee1fb8
Merge branch 'master' of github.com:optimizely/python-sdk into mpirno…
Mat001 Sep 16, 2021
58977d2
feat: add remaining implementation
Mat001 Oct 1, 2021
340cbce
WIP: addressed implementation PR comments and fixed failing unit tests
Mat001 Oct 19, 2021
c81a425
Fixed lint errors
Mat001 Oct 19, 2021
c89bc3c
fix failing tests in py 3.5
Mat001 Oct 20, 2021
2fe78ab
fixed failing logger import for Py2
Mat001 Oct 20, 2021
d80c555
add OptimizelyDecisionContext and OptmizelyForcedDecisions
Mat001 Oct 27, 2021
5ed2fb4
testcases added
ozayr-zaviar Nov 2, 2021
6003fdc
Update optimizely/optimizely_user_context.py
Mat001 Nov 2, 2021
e4dc745
Update optimizely/optimizely_user_context.py
Mat001 Nov 2, 2021
d75f389
Update optimizely/optimizely_user_context.py
Mat001 Nov 2, 2021
68146a1
make rule key optional in OptimizelyDecisionContext
Mat001 Nov 2, 2021
a261899
Mutex lock and testcases added
8000 ozayr-zaviar Nov 3, 2021
a837f03
Merge branch 'master' into mpirnovar/forced_decisions
Mat001 Nov 3, 2021
de4a31c
Update optimizely/optimizely_user_context.py
Mat001 Nov 5, 2021
0c52707
use get() vs [] in remove_forced_decision
Mat001 Nov 5, 2021
4116b43
add self lock and keys(0
Mat001 Nov 5, 2021
081cd79
add missing colon
Mat001 Nov 7, 2021
e061abc
fix displaying reasons
Mat001 Nov 11, 2021
337f8d9
Update optimizely/optimizely.py
Mat001 Nov 12, 2021
a71f50e
address PR comments
Mat001 Nov 16, 2021
981cbe5
updating
Mat001 Nov 16, 2021
94d5af9
more PR review fixes
Mat001 Nov 17, 2021
e9cd304
fixed few more PR comments
Mat001 Nov 17, 2021
2dff4c6
added bucket reasons
Mat001 Nov 17, 2021
e2f1db3
FSC fixes
ozayr-zaviar Nov 18, 2021
6849c33
addressed more PR comments, fixed FSC test failuer about impressin ev…
Mat001 Nov 19, 2021
55fe98f
address more PR comments
Mat001 Nov 19, 2021
94a0c26
use is_valid check on opti client
Mat001 Nov 19, 2021
e5aaccb
addressed more PR comments
Mat001 Nov 19, 2021
44373e9
reasons and key name fixed
ozayr-zaviar Nov 22, 2021
e6c1772
create get_default method for empty experiment object
Mat001 Nov 22, 2021
ab40d9e
fixed further PR comments
Mat001 Nov 24, 2021
795b41a
fix logger so we use the top logger in optimizely client
Mat001 Dec 1, 2021
dbbc051
Refact: Refactored Forced decision (#365)
msohailhussain Dec 2, 2021
75fe2bb
coupl of corrections
Mat001 Dec 2, 2021
243d447
remove check on config
Mat001 Dec 2, 2021
1010ece
remove redundant import
Mat001 Dec 2, 2021
17efc27
remove redundant test about invalid datafile
Mat001 Dec 3, 2021
201548f
add reasons to return
Mat001 Dec 4, 2021
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
Prev Previous commit
Next Next commit
addressed more PR comments
  • Loading branch information
Mat001 committed Nov 19, 2021
commit e5aaccb10efcc7f3cc48a34efcb143daa6fbc1cf
63 changes: 38 additions & 25 deletions optimizely/project_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,27 +124,26 @@ def __init__(self, datafile, logger, error_handler):
# As we cannot create json variables in datafile directly, here we convert
# the variables of string type and json subType to json type
# This is needed to fully support json variables
for feature in self.feature_key_map:
for variable in self.feature_key_map[feature].variables:
self.experiment_feature_map = {}
self.flag_rules_map = {}

for feature_key, feature_value in self.feature_key_map.items():
for variable in self.feature_key_map[feature_key].variables:
sub_type = variable.get('subType', '')
if variable['type'] == entities.Variable.Type.STRING and sub_type == entities.Variable.Type.JSON:
variable['type'] = entities.Variable.Type.JSON

# Dict containing map of experiment ID to feature ID.
# for checking that experiment is a feature experiment or not.
self.experiment_feature_map = {}
for feature in self.feature_key_map.values():
feature.variables = self._generate_key_map(feature.variables, 'key', entities.Variable)
for exp_id in feature.experimentIds:
# loop over features=flags already happening
# get feature variables for eacg flag/feature
feature_value.variables = self._generate_key_map(feature_value.variables, 'key', entities.Variable)
for exp_id in feature_value.experimentIds:
# Add this experiment in experiment-feature map.
self.experiment_feature_map[exp_id] = [feature.id]
self.experiment_feature_map[exp_id] = [feature_value.id]

# all rules(experiment rules and delivery rules) for each flag
self.flag_rules_map = {}
for flag in self.feature_flags:

experiments = []
if not flag['experimentIds'] == '':
if len(flag['experimentIds']) > 0:
for exp_id in flag['experimentIds']:
experiments.append(self.experiment_id_map[exp_id])
if not flag['rolloutId'] == '':
Expand All @@ -160,19 +159,7 @@ def __init__(self, datafile, logger, error_handler):
# All variations for each flag
# Datafile does not contain a separate entity for this.
# We collect variations used in each rule (experiment rules and delivery rules)
self.flag_variations_map = {}

for flag_key, rules in self.flag_rules_map.items():
variations = []
for rule in rules:
# get variations as objects (rule.variations gives list)

variation_objects = self.variation_id_map_by_experiment_id[rule.id].values()
for variation in variation_objects:
if variation.id not in [var.id for var in variations]:
variations.append(variation)

self.flag_variations_map[flag_key] = variations
self.flag_variations_map = self.get_all_variations_for_each_rule(self.flag_rules_map)

@staticmethod
def _generate_key_map(entity_list, key, entity_class):
Expand Down Expand Up @@ -674,3 +661,29 @@ def get_flag_variation_by_id(self, flag_key, variation_id):
return variation

return None

def get_all_variations_for_each_rule(self, flag_rules_map):
""" Helper method to get all variation objects from each flag.
collects variations used in each rule (experiment rules and delivery rules).

Args:
flag_rules_map: A dictionary. A map of all rules per flag.

Returns:
Map of flag variations.
"""
flag_variations_map = {}

for flag_key, rules in flag_rules_map.items():
variations = []
for rule in rules:
# get variations as objects (rule.variations gives list)
variation_objects = self.variation_id_map_by_experiment_id[rule.id].values()
for variation in variation_objects:
# append variation if it's not already in the list
if variation not in variations:
variations.append(variation)

flag_variations_map[flag_key] = variations

return flag_variations_map
7 changes: 4 additions & 3 deletions tests/test_user_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -1568,8 +1568,10 @@ def test_should_return_valid_decision_after_setting_invalid_delivery_rule_variat
'rule (211127) and user (test_user) in the forced decision map.'
])))

# TODO - JAE: Can we change the test name and description? Not clear which part is invalid. Also, I see the forced set flag and decide flag is different. Is it intentional?
def test_invalid_experiment_rule_return_decision__forced_decision(self): # TODO - CHECK WITH JAE if this test should return valid decision like docstring says!
# TODO - JAE: Can we change the test name and description? Not clear which part is invalid.
# Also, I see the forced set flag and decide flag is different. Is it intentional?
# TODO - CHECK WITH JAE if this test should return valid decision like docstring says!
def test_invalid_experiment_rule_return_decision__forced_decision(self):
"""
Should return valid decision after setting invalid experiment
rule variation in forced decision.
Expand All @@ -1596,7 +1598,6 @@ def test_invalid_experiment_rule_return_decision__forced_decision(self):
self.assertEqual(decide_decision.rule_key, None)
self.assertFalse(decide_decision.enabled)


self.assertEqual(decide_decision.flag_key, 'test_feature_in_rollout')
self.assertEqual(decide_decision.user_context.user_id, 'test_user')
self.assertEqual(decide_decision.user_context.get_user_attributes(), {})
Expand Down
0