Skip to content
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

wrong variation issue fix #346

Closed
Closed
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
2 changes: 1 addition & 1 deletion optimizely/bucketer.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def bucket(self, project_config, experiment, user_id, bucketing_id):
variation_id = self.find_bucket(project_config, bucketing_id,
experiment.id, experiment.trafficAllocation)
if variation_id:
variation = project_config.get_variation_from_id(experiment.key, variation_id)
variation = project_config.get_variation_from_id(experiment.id, variation_id)
return variation, decide_reasons

else:
Expand Down
5 changes: 2 additions & 3 deletions optimizely/decision_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from .helpers import validator
from .user_profile import UserProfile


Decision = namedtuple('Decision', 'experiment variation source')


Expand Down Expand Up @@ -342,8 +341,8 @@ def get_variation_for_rollout(self, project_config, rollout, user_id, attributes
if rollout and len(rollout.experiments) > 0:
for idx in range(len(rollout.experiments) - 1):
logging_key = str(idx + 1)
rollout_rule = project_config.get_experiment_from_key(rollout.experiments[idx].get('key'))

rollout_rule = project_config.get_experiment_from_id(rollout.experiments[idx].get('id'))
# rollout_rule = optimizely.entities.Experiment(**rollout.experiments[idx])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code (minor).

# Check if user meets audience conditions for targeting rule
audience_conditions = rollout_rule.get_audience_conditions_or_ids()
user_meets_audience_conditions, reasons_received = audience_helper.does_user_meet_audience_conditions(
Expand Down
37 changes: 33 additions & 4 deletions optimizely/project_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .helpers import enums
from . import entities
from . import exceptions
import copy

SUPPORTED_VERSIONS = [
enums.DatafileVersions.V2,
Expand Down Expand Up @@ -56,6 +57,7 @@ def __init__(self, datafile, logger, error_handler):
self.environment_key = config.get('environmentKey', None)
self.groups = config.get('groups', [])
self.experiments = config.get('experiments', [])
self.mapexperiment = copy.deepcopy(self.experiments)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't look ok to me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found some other issues as well with test cases. working through now.

self.events = config.get('events', [])
self.attributes = config.get('attributes', [])
self.audiences = config.get('audiences', [])
Expand All @@ -68,6 +70,7 @@ def __init__(self, datafile, logger, error_handler):

# Utility maps for quick lookup
self.group_id_map = self._generate_key_map(self.groups, 'id', entities.Group)
self.experimentID_map = self._generate_key_map(self.mapexperiment, 'id', entities.Experiment)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't mix camel cases with snake_case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why experiment's can't be passed? what's the need of mapexperiment?

self.experiment_key_map = self._generate_key_map(self.experiments, 'key', entities.Experiment)
self.event_key_map = self._generate_key_map(self.events, 'key', entities.Event)
self.attribute_key_map = self._generate_key_map(self.attributes, 'key', entities.Attribute)
Expand All @@ -85,18 +88,24 @@ def __init__(self, datafile, logger, error_handler):
for layer in self.rollout_id_map.values():
for experiment in layer.experiments:
self.experiment_key_map[experiment['key']] = entities.Experiment(**experiment)
self.experimentID_map[experiment['id']] = entities.Experiment(**experiment)

self.audience_id_map = self._deserialize_audience(self.audience_id_map)
for group in self.group_id_map.values():
experiments_in_group_key_map = self._generate_key_map(group.experiments, 'key', entities.Experiment)
for experiment in experiments_in_group_key_map.values():
experiment.__dict__.update({'groupId': group.id, 'groupPolicy': group.policy})
self.experiment_key_map.update(experiments_in_group_key_map)
experiments_in_group_id_map = self._generate_key_map(group.experiments, 'id', entities.Experiment)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid confusion maybe we should rename this function or create a new one for ids.

for experiment in experiments_in_group_id_map.values():
experiment.__dict__.update({'groupId': group.id, 'groupPolicy': group.policy})
self.experiment_key_map.update(experiments_in_group_id_map)

self.experiment_id_map = {}
self.variation_key_map = {}
self.variation_id_map = {}
self.variation_variable_usage_map = {}

for experiment in self.experiment_key_map.values():
self.experiment_id_map[experiment.id] = experiment
self.variation_key_map[experiment.key] = self._generate_key_map(
Expand All @@ -108,6 +117,26 @@ def __init__(self, datafile, logger, error_handler):
self.variation_variable_usage_map[variation.id] = self._generate_key_map(
variation.variables, 'id', entities.Variation.VariableUsage
)
self.correct_variation_id_map = {}
self.temp_variation_id_map = {}
for experiment in self.experimentID_map.values():
self.temp_variation_id_map[experiment.id] = self._generate_key_map(
experiment.variations, 'id', entities.Variation
)
self.correct_variation_id_map[experiment.id] = {}
for variation in self.temp_variation_id_map.get(experiment.id).values():
self.correct_variation_id_map[experiment.id][variation.id] = variation
self.variation_variable_usage_map[variation.id] = self._generate_key_map(
variation.variables, 'id', entities.Variation.VariableUsage
)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too many blank lines between code, should be removed to follow lint.








self.feature_key_map = self._generate_key_map(self.feature_flags, 'key', entities.FeatureFlag)

Expand Down Expand Up @@ -280,8 +309,8 @@ def get_experiment_from_id(self, experiment_id):
Experiment corresponding to the provided experiment ID.
"""

experiment = self.experiment_id_map.get(experiment_id)

# experiment = self.experiment_id_map.get(experiment_id)
experiment = self.experimentID_map.get(experiment_id)
if experiment:
return experiment

Expand Down Expand Up @@ -362,8 +391,8 @@ def get_variation_from_id(self, experiment_key, variation_id):
Object representing the variation.
"""

variation_map = self.variation_id_map.get(experiment_key)

#variation_map = self.variation_id_map.get(experiment_key)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove commented code

variation_map = self.correct_variation_id_map.get(experiment_key)
if variation_map:
variation = variation_map.get(variation_id)
if variation:
Expand Down
Loading