Skip to content

Commit

Permalink
Merge branch 'master' into mpirnovar/update_requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mat001 committed Jun 28, 2021
2 parents 75041ba + 2ee24cf commit 1963182
Show file tree
Hide file tree
Showing 10 changed files with 385 additions and 61 deletions.
4 changes: 2 additions & 2 deletions optimizely/event/event_processor.py
@@ -1,4 +1,4 @@
# Copyright 2019-2020 Optimizely
# Copyright 2019-2021 Optimizely
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand Down Expand Up @@ -120,7 +120,7 @@ def __init__(
@property
def is_running(self):
""" Property to check if consumer thread is alive or not. """
return self.executor.isAlive() if self.executor else False
return self.executor.is_alive() if self.executor else False

def _validate_instantiation_props(self, prop, prop_name, default_value):
""" Method to determine if instantiation properties like batch_size, flush_interval
Expand Down
102 changes: 99 additions & 3 deletions optimizely/optimizely_config.py
@@ -1,4 +1,4 @@
# Copyright 2020, Optimizely
# Copyright 2020-2021, Optimizely
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand All @@ -17,11 +17,16 @@


class OptimizelyConfig(object):
def __init__(self, revision, experiments_map, features_map, datafile=None):
def __init__(self, revision, experiments_map, features_map, datafile=None,
sdk_key=None, environment_key=None, attributes=None, events=None):
self.revision = revision
self.experiments_map = experiments_map
self.features_map = features_map
self._datafile = datafile
self.sdk_key = sdk_key
self.environment_key = environment_key
self.attributes = attributes or []
self.events = events or []

def get_datafile(self):
""" Get the datafile associated with OptimizelyConfig.
Expand All @@ -31,6 +36,38 @@ def get_datafile(self):
"""
return self._datafile

def get_sdk_key(self):
""" Get the sdk key associated with OptimizelyConfig.
Returns:
A string containing sdk key.
"""
return self.sdk_key

def get_environment_key(self):
""" Get the environemnt key associated with OptimizelyConfig.
Returns:
A string containing environment key.
"""
return self.environment_key

def get_attributes(self):
""" Get the attributes associated with OptimizelyConfig
returns:
A list of attributes.
"""
return self.attributes

def get_events(self):
""" Get the events associated with OptimizelyConfig
returns:
A list of events.
"""
return self.events


class OptimizelyExperiment(object):
def __init__(self, id, key, variations_map):
Expand Down Expand Up @@ -63,6 +100,19 @@ def __init__(self, id, key, variable_type, value):
self.value = value


class OptimizelyAttribute(object):
def __init__(self, id, key):
self.id = id
self.key = key


class OptimizelyEvent(object):
def __init__(self, id, key, experiment_ids):
self.id = id
self.key = key
self.experiment_ids = experiment_ids


class OptimizelyConfigService(object):
""" Class encapsulating methods to be used in creating instance of OptimizelyConfig. """

Expand All @@ -82,6 +132,10 @@ def __init__(self, project_config):
self.feature_flags = project_config.feature_flags
self.groups = project_config.groups
self.revision = project_config.revision
self.sdk_key = project_config.sdk_key
self.environment_key = project_config.environment_key
self.attributes = project_config.attributes
self.events = project_config.events

self._create_lookup_maps()

Expand All @@ -98,7 +152,15 @@ def get_config(self):
experiments_key_map, experiments_id_map = self._get_experiments_maps()
features_map = self._get_features_map(experiments_id_map)

return OptimizelyConfig(self.revision, experiments_key_map, features_map, self._datafile)
return OptimizelyConfig(
self.revision,
experiments_key_map,
features_map,
self._datafile,
self.sdk_key,
self.environment_key,
self.attributes,
self.events)

def _create_lookup_maps(self):
""" Creates lookup maps to avoid redundant iteration of config objects. """
Expand Down Expand Up @@ -233,3 +295,37 @@ def _get_features_map(self, experiments_id_map):
features_map[feature['key']] = optly_feature

return features_map

def get_attributes_map(self):
""" Gets attributes map for the project config.
Returns:
dict -- Attribute key, OptimizelyAttribute map
"""

attributes_map = {}

for attribute in self.attributes:
optly_attribute = OptimizelyAttribute(
attribute['id'], attribute['key']
)
attributes_map[attribute['key']] = optly_attribute

return attributes_map

def get_events_map(self):
""" Gets events map for the project config.
Returns:
dict -- Event key, OptimizelyEvent map
"""

events_map = {}

for event in self.events:
optly_event = OptimizelyEvent(
event['id'], event['key'], event.get('experimentIds', [])
)
events_map[event['key']] = optly_event

return events_map
1 change: 0 additions & 1 deletion optimizely/optimizely_factory.py
Expand Up @@ -113,7 +113,6 @@ def default_instance_with_config_manager(config_manager):
def custom_instance(sdk_key, datafile=None, event_dispatcher=None, logger=None, error_handler=None,
skip_json_validation=None, user_profile_service=None, config_manager=None,
notification_center=None):

""" Returns a new optimizely instance.
if max_event_batch_size and max_event_flush_interval are None then default batch_size and flush_interval
will be used to setup BatchEventProcessor.
Expand Down
22 changes: 21 additions & 1 deletion optimizely/project_config.py
@@ -1,4 +1,4 @@
# Copyright 2016-2019, Optimizely
# Copyright 2016-2019, 2021, Optimizely
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand Down Expand Up @@ -52,6 +52,8 @@ def __init__(self, datafile, logger, error_handler):
self.account_id = config.get('accountId')
self.project_id = config.get('projectId')
self.revision = config.get('revision')
self.sdk_key = config.get('sdkKey', None)
self.environment_key = config.get('environmentKey', None)
self.groups = config.get('groups', [])
self.experiments = config.get('experiments', [])
self.events = config.get('events', [])
Expand Down Expand Up @@ -213,6 +215,24 @@ def get_revision(self):

return self.revision

def get_sdk_key(self):
""" Get sdk key from the datafile.
Returns:
Revision of the sdk key.
"""

return self.sdk_key

def get_environment_key(self):
""" Get environment key from the datafile.
Returns:
Revision of the environment key.
"""

return self.environment_key

def get_account_id(self):
""" Get account ID from the config.
Expand Down
8 changes: 7 additions & 1 deletion tests/base.py
@@ -1,4 +1,4 @@
# Copyright 2016-2020, Optimizely
# Copyright 2016-2021, Optimizely
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand All @@ -22,6 +22,12 @@
def long(a):
raise NotImplementedError('Tests should only call `long` if running in PY2')

# Check to verify if TestCase has the attribute assertRasesRegex or assertRaisesRegexp
# This check depends on the version of python with assertRaisesRegexp being used by
# python2.7. Later versions of python are using the non-deprecated assertRaisesRegex.
if not hasattr(unittest.TestCase, 'assertRaisesRegex'):
unittest.TestCase.assertRaisesRegex = getattr(unittest.TestCase, 'assertRaisesRegexp')


class BaseTest(unittest.TestCase):
def assertStrictTrue(self, to_assert):
Expand Down
20 changes: 10 additions & 10 deletions tests/test_config.py
@@ -1,4 +1,4 @@
# Copyright 2016-2019, Optimizely
# Copyright 2016-2019, 2021, Optimizely
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand Down Expand Up @@ -1117,7 +1117,7 @@ def setUp(self):
def test_get_experiment_from_key__invalid_key(self):
""" Test that exception is raised when provided experiment key is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidExperimentException,
enums.Errors.INVALID_EXPERIMENT_KEY,
self.project_config.get_experiment_from_key,
Expand All @@ -1127,14 +1127,14 @@ def test_get_experiment_from_key__invalid_key(self):
def test_get_audience__invalid_id(self):
""" Test that message is logged when provided audience ID is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidAudienceException, enums.Errors.INVALID_AUDIENCE, self.project_config.get_audience, '42',
)

def test_get_variation_from_key__invalid_experiment_key(self):
""" Test that exception is raised when provided experiment key is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidExperimentException,
enums.Errors.INVALID_EXPERIMENT_KEY,
self.project_config.get_variation_from_key,
Expand All @@ -1145,7 +1145,7 @@ def test_get_variation_from_key__invalid_experiment_key(self):
def test_get_variation_from_key__invalid_variation_key(self):
""" Test that exception is raised when provided variation key is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidVariationException,
enums.Errors.INVALID_VARIATION,
self.project_config.get_variation_from_key,
Expand All @@ -1156,7 +1156,7 @@ def test_get_variation_from_key__invalid_variation_key(self):
def test_get_variation_from_id__invalid_experiment_key(self):
""" Test that exception is raised when provided experiment key is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidExperimentException,
enums.Errors.INVALID_EXPERIMENT_KEY,
self.project_config.get_variation_from_id,
Expand All @@ -1167,7 +1167,7 @@ def test_get_variation_from_id__invalid_experiment_key(self):
def test_get_variation_from_id__invalid_variation_id(self):
""" Test that exception is raised when provided variation ID is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidVariationException,
enums.Errors.INVALID_VARIATION,
self.project_config.get_variation_from_key,
Expand All @@ -1178,7 +1178,7 @@ def test_get_variation_from_id__invalid_variation_id(self):
def test_get_event__invalid_key(self):
""" Test that exception is raised when provided event key is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidEventException,
enums.Errors.INVALID_EVENT_KEY,
self.project_config.get_event,
Expand All @@ -1188,7 +1188,7 @@ def test_get_event__invalid_key(self):
def test_get_attribute_id__invalid_key(self):
""" Test that exception is raised when provided attribute key is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidAttributeException,
enums.Errors.INVALID_ATTRIBUTE,
self.project_config.get_attribute_id,
Expand All @@ -1198,7 +1198,7 @@ def test_get_attribute_id__invalid_key(self):
def test_get_group__invalid_id(self):
""" Test that exception is raised when provided group ID is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidGroupException, enums.Errors.INVALID_GROUP_ID, self.project_config.get_group, '42',
)

Expand Down

0 comments on commit 1963182

Please sign in to comment.