-
Notifications
You must be signed in to change notification settings - Fork 37
Add Audiences to OptimizelyConfig and expose in OptimizelyExperiment #342
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
Changes from 1 commit
e80fdfb
d58b71a
a583ea5
5b2196c
f964f94
f60ab0a
df1b081
9c9bcea
61d5c24
13703ca
ef95cd8
b8828b9
c3bc816
f1bd533
0ad43d0
942bfe9
528b8f2
c3c3d2c
2fef52a
d3b2a46
dda6e76
4f2d7fb
f90e3e3
4471555
bb98741
48f604b
8179f2b
d694c7f
ec39096
a687a9b
11be430
ecf2e45
f568aad
3b8706f
2504f98
1cd6aab
bc98adc
98ff2b6
d6fa32d
16059d7
0320627
8187f6f
0804bb8
ba3bbef
2567bac
d401c72
6b4a00d
a1ed815
ec4218a
508f82f
26ad430
1f35dda
a4e497f
bf92fd7
2d6def3
bec791a
286f178
c3faba3
199a35f
4fa5d05
4356a4d
a273d11
c8d68f7
a9ac50c
6ce98e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…utes list and events list.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,33 +162,32 @@ def __init__(self, project_config): | |
The typed_audiences has higher presidence. | ||
''' | ||
|
||
typed_audiences = project_config.typed_audiences or [] | ||
typed_audiences = project_config.typed_audiences.copy() | ||
optly_typed_audiences = [] | ||
for typed_audience in typed_audiences: | ||
optly_audience = OptimizelyAudience( | ||
typed_audience.get('id'), | ||
The-inside-man marked this conversation as resolved.
Show resolved
Hide resolved
|
||
typed_audience.get('name'), | ||
typed_audience.get('conditions') | ||
) | ||
optly_typed_audiences.append(optly_audience) | ||
|
||
for old_audience in project_config.audiences: | ||
# check if old_audience.id exists in new_audiences.id from typed_audiences | ||
if len([new_audience for new_audience in typed_audiences | ||
if len([new_audience for new_audience in project_config.typed_audiences | ||
if new_audience.get('id') == old_audience.get('id')]) == 0: | ||
if old_audience.get('id') == "$opt_dummy_audience": | ||
continue | ||
else: | ||
typed_audiences.append(old_audience) | ||
|
||
self.audiences = typed_audiences | ||
|
||
audiences_map = {} | ||
|
||
for audience in self.audiences: | ||
audiences_map[audience.get('id')] = audience.get('name') | ||
# Convert audiences array to OptimizelyAudience array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Python uses the term lists instead of array (minor thing) |
||
optly_audience = OptimizelyAudience( | ||
old_audience.get('id'), | ||
old_audience.get('name'), | ||
old_audience.get('conditions') | ||
) | ||
optly_typed_audiences.append(optly_audience) | ||
|
||
# Updating each OptimizelyExperiment based on the ID from entities.Experiment found in the experiment_key_map | ||
for ent_exp in project_config.experiment_key_map.values(): | ||
experiments_by_key, experiments_by_id = self._get_experiments_maps() | ||
try: | ||
optly_experiment = experiments_by_id[ent_exp.id] | ||
self.update_experiment(optly_experiment, ent_exp.audienceConditions, audiences_map) | ||
except KeyError: | ||
# ID not in map | ||
continue | ||
self.audiences = optly_typed_audiences | ||
|
||
def update_experiment(self, experiment, conditions, audiences_map): | ||
''' | ||
|
@@ -243,7 +242,7 @@ def stringify_conditions(self, conditions, audiences_map): | |
''' | ||
ARGS = ConditionOperatorTypes.operators | ||
The-inside-man marked this conversation as resolved.
Show resolved
Hide resolved
|
||
condition = "" | ||
ret = "(" | ||
ret = "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd use more descriptive variable name (code maintenance reasons) |
||
length = len(conditions) | ||
|
||
if length == 0: | ||
|
@@ -254,20 +253,33 @@ def stringify_conditions(self, conditions, audiences_map): | |
''' | ||
audience_name = self.lookup_name_from_id(conditions[0], audiences_map) | ||
|
||
return audience_name | ||
return '"' + audience_name + '"' | ||
|
||
if length == 2: | ||
if conditions[0] in ARGS: | ||
condition = conditions[0] | ||
audience_name = self.lookup_name_from_id(conditions[1], audiences_map) | ||
return condition.upper() + ' "' + audience_name + '"' | ||
else: | ||
condition = 'OR' | ||
return ('"' + self.lookup_name_from_id(conditions[0], audiences_map) + '" ' | ||
+ condition.upper() + ' "' | ||
+ self.lookup_name_from_id(conditions[1], audiences_map) + '"') | ||
|
||
if length > 1: | ||
if length > 2: | ||
for i in range(length): | ||
if conditions[i] in ARGS: | ||
condition = conditions[i] | ||
else: | ||
if condition == "": | ||
condition = 'OR' | ||
if type(conditions[i]) == list: | ||
# If the next item is a list, recursively call function on list | ||
if i + 1 < length: | ||
ret += self.stringify_conditions(conditions[i], | ||
audiences_map) + ' ' + condition.upper() + ' ' | ||
ret += '(' + self.stringify_conditions(conditions[i], | ||
audiences_map) + ') ' + condition.upper() + ' ' | ||
else: | ||
ret += self.stringify_conditions(conditions[i], audiences_map) | ||
ret += '(' + self.stringify_conditions(conditions[i], audiences_map) + ')' | ||
else: | ||
# Handle ID's here - Lookup name based on ID | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no apostrophe |
||
audience_name = self.lookup_name_from_id(conditions[i], audiences_map) | ||
|
@@ -277,7 +289,7 @@ def stringify_conditions(self, conditions, audiences_map): | |
else: | ||
ret += '"' + audience_name + '"' | ||
|
||
return ret + ")" | ||
return ret + "" | ||
|
||
def get_config(self): | ||
""" Gets instance of OptimizelyConfig | ||
|
@@ -299,8 +311,8 @@ def get_config(self): | |
self._datafile, | ||
self.sdk_key, | ||
self.environment_key, | ||
self.attributes, | ||
self.events, | ||
self._get_attributes_list(self.attributes), | ||
self._get_events_list(self.events), | ||
self.audiences) | ||
|
||
def _create_lookup_maps(self): | ||
|
@@ -389,7 +401,8 @@ def _get_all_experiments(self): | |
return experiments | ||
|
||
def _get_experiments_maps(self): | ||
""" Gets maps for all the experiments in the project config. | ||
""" Gets maps for all the experiments in the project config and | ||
updates the experiment with updated experiment audiences string. | ||
|
||
Returns: | ||
dict, dict -- experiment key/id to OptimizelyExperiment maps. | ||
|
@@ -398,12 +411,20 @@ def _get_experiments_maps(self): | |
experiments_key_map = {} | ||
# Id map comes in handy to figure out feature experiment. | ||
experiments_id_map = {} | ||
# Audiences map to use for updating experiments with new audience conditions string | ||
audiences_map = {} | ||
|
||
# Build map from OptimizelyAudience array | ||
for optly_audience in self.audiences: | ||
audiences_map[optly_audience.id] = optly_audience.name | ||
|
||
all_experiments = self._get_all_experiments() | ||
for exp in all_experiments: | ||
optly_exp = OptimizelyExperiment( | ||
exp['id'], exp['key'], self._get_variations_map(exp) | ||
) | ||
# Updating each OptimizelyExperiment based on the ID from entities.Experiment found in the experiment_key_map | ||
self.update_experiment(optly_exp, exp.get('audienceConditions', []), audiences_map) | ||
|
||
experiments_key_map[exp['key']] = optly_exp | ||
experiments_id_map[exp['id']] = optly_exp | ||
|
@@ -436,3 +457,38 @@ def _get_features_map(self, experiments_id_map): | |
features_map[feature['key']] = optly_feature | ||
|
||
return features_map | ||
|
||
def _get_attributes_list(self, attributes): | ||
""" Gets attributes list for the project config | ||
|
||
Returns: | ||
List - OptimizelyAttributes | ||
""" | ||
attributes_list = [] | ||
|
||
for attribute in attributes: | ||
optly_attribute = OptimizelyAttribute( | ||
attribute['id'], | ||
attribute['key'] | ||
) | ||
attributes_list.append(optly_attribute) | ||
|
||
return attributes_list | ||
|
||
def _get_events_list(self, events): | ||
""" Gets events list for the project_config | ||
|
||
Returns: | ||
List - OptimizelyEvents | ||
""" | ||
events_list = [] | ||
|
||
for event in events: | ||
optly_event = OptimizelyEvent( | ||
event['id'], | ||
event['key'], | ||
event['experimentIds'] | ||
) | ||
events_list.append(optly_event) | ||
|
||
return events_list |
Uh oh!
There was an error while loading. Please reload this page.