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

feat: Optimizely factory added #325

Merged
merged 10 commits into from
Jun 4, 2021
Merged

Conversation

ozayr-zaviar
Copy link
Contributor

@ozayr-zaviar ozayr-zaviar commented May 6, 2021

Summary

  • optimizely_factory.py file added containing OptimizelyFactory class.
  • test_optimizely_factory.py file added in tests/ for test cases.

Test plan

  • All existed unit tests and FSC test should pass.
  • All unit tests should pass.

@coveralls
Copy link

coveralls commented May 6, 2021

Coverage Status

Coverage increased (+0.03%) to 95.871% when pulling 5c7ff3e on uzair/static-polling-interval into 7607cf4 on master.

@@ -196,7 +196,7 @@ def __init__(
self.datafile_url = self.get_datafile_url(
sdk_key, url, url_template or self.DATAFILE_URL_TEMPLATE
)
self.set_update_interval(update_interval)
Copy link
Contributor

Choose a reason for hiding this comment

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

you are not supposed to change method name.

""" Helper method to set frequency at which datafile has to be polled and ProjectConfig updated.

Args:
update_interval: Time in seconds after which to update datafile.
"""

logger = optimizely_logger.adapt_logger(optimizely_logger.NoOpLogger())
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to assign new logger.

if update_interval is None:
update_interval = enums.ConfigManager.DEFAULT_UPDATE_INTERVAL
self.logger.debug('Setting config update interval to default value {}.'.format(update_interval))
logger.debug('Setting config update interval to default value {}.'.format(update_interval))
Copy link
Contributor

Choose a reason for hiding this comment

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

revert this line.

@ozayr-zaviar ozayr-zaviar changed the title refactor: polling interval function conversion to static function feat: Optimizely factory added May 10, 2021
@@ -0,0 +1,180 @@
# Copyright 2016-2021, Optimizely
Copy link
Contributor

Choose a reason for hiding this comment

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

change the header year to 2021 only as this file is created in this year

"""

if isinstance(flush_interval, bool):
logger.error('Flush interval is invalid, setting to default flush interval # {0}'.format(
Copy link
Contributor

Choose a reason for hiding this comment

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

avoid logging and validation here as there should be logging and validation already done somewhere in SDK.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is fine to have some major validation here.

logger: Optional LoggerInterface Provides a log method to log messages.
"""

if isinstance(batch_size, bool):
Copy link
Contributor

Choose a reason for hiding this comment

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

we don't need this condition.

BatchEventProcessor._DEFAULT_BATCH_SIZE))
return

if not batch_size > 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

can't we use else or else if

return OptimizelyFactory.max_event_batch_size

@staticmethod
def set_flush_interval(flush_interval, logger=optimizely_logger.NoOpLogger().logger):
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above comments.

optimizely_instance = OptimizelyFactory.default_instance('sdk_key')
self.assertIsInstance(optimizely_instance.config_manager, PollingConfigManager)

def test_default_instance_should_create_config_manager_when_polling_interval_and_blocking_timeout_are_set_valid(
Copy link
Contributor

Choose a reason for hiding this comment

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

shorten the name of func

@msohailhussain msohailhussain marked this pull request as ready for review May 12, 2021 17:21
@msohailhussain msohailhussain requested a review from a team as a code owner May 12, 2021 17:21
Copy link
Contributor

@jaeopt jaeopt left a comment

Choose a reason for hiding this comment

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

A few suggestions and clarifications

@staticmethod
def default_instance_with_config_manager(config_manager):
return Optimizely(
None, None, None, None, None, None, None, config_manager
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
None, None, None, None, None, None, None, config_manager
config_manager=config_manager

Comment on lines 74 to 75
# validate through validator
self.assertTrue(validator.is_config_manager_valid(CustomConfigManager()))
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the point of validating one defined for test?

blocking_timeout = None

@staticmethod
def set_batch_size(batch_size, logger=optimizely_logger.NoOpLogger().logger):
Copy link
Contributor

Choose a reason for hiding this comment

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

passing a logger to set a parameter sounds weird. Can't remove all these validation with logging here since those parameters are validated again in event_processor.

config_manager = PollingConfigManager(**config_manager_options)

optimizely = Optimizely(
datafile, None, logger, error_handler, None, None, sdk_key, config_manager, notification_center
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we include "BatchEventProcessor" as a part of this factory method as a common configuration?

def set_batch_size(batch_size, logger=optimizely_logger.NoOpLogger().logger):
""" Convenience method for setting the maximum number of events contained within a batch.
Args:
batch_size: Sets size of EventQueue.
Copy link
Contributor

Choose a reason for hiding this comment

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

Change to lower case: event_queue.
I couldn't find class EentQueue in the sdk. Unless I missed smth

""" Convenience method for setting the maximum number of events contained within a batch.
Args:
batch_size: Sets size of EventQueue.
logger: Optional LoggerInterface Provides a log method to log messages.
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here: ----> loger interface.
Otherwise it may hint to a class which doesn't exist

Also lover case "provides"

""" Convenience method for setting the maximum time interval in milliseconds between event dispatches.
Args:
flush_interval: Time interval between event dispatches.
logger: Optional LoggerInterface Provides a log method to log messages.
Copy link
Contributor

Choose a reason for hiding this comment

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

Use lower case "logging interface", and lower case "provides"

def default_instance(sdk_key, datafile=None):
""" Returns a new optimizely instance..
Args:
sdk_key: Required String uniquely identifying the fallback datafile corresponding to project.
Copy link
Contributor

Choose a reason for hiding this comment

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

Lower case "string". Python doesn't have static typing


""" 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.
Copy link
Contributor

Choose a reason for hiding this comment

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

change to "BatchEventProcessor"

By default nothing would be logged.
error_handler: Optional ErrorHandlerInterface which provides a handle_error method to handle exceptions.
By default all exceptions will be suppressed.
skip_json_validation: Optional Boolean param to skip JSON schema validation of the provided datafile.
Copy link
Contributor

Choose a reason for hiding this comment

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

lower case boolean

error_handler: Optional ErrorHandlerInterface which provides a handle_error method to handle exceptions.
By default all exceptions will be suppressed.
skip_json_validation: Optional Boolean param to skip JSON schema validation of the provided datafile.
user_profile_service: Optional UserProfileServiceInterface Provides methods to store and retrieve
Copy link
Contributor

Choose a reason for hiding this comment

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

UserProfileService interface
provides

skip_json_validation: Optional Boolean param to skip JSON schema validation of the provided datafile.
user_profile_service: Optional UserProfileServiceInterface Provides methods to store and retrieve
user profiles.
config_manager: Optional ConfigManagerInterface Responds to 'config' method.
Copy link
Contributor

Choose a reason for hiding this comment

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

ConfigManager interface
responds

self.event_dispatcher = EventDispatcher()
self.user_profile_service = UserProfileService()

def test_default_instance_should_create_config_manager_when_sdk_key_is_given(self, _):
Copy link
Contributor

Choose a reason for hiding this comment

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

please use double underscore in test name after the main part of the test name. In All tests. For example:

  1. test_default_instance__should_create_config_manager_when_sdk_key_is_given
  2. test_default_instance__should_create_config_set_default_values_when_polling_int_and_blocking_time_invalid
  3. etc

This is for consistency with other test names. Double underscore clarifies the specifics of the test in the second part of the test name, so it's nice to have that double underscore mark.

self.assertEqual(optimizely_instance.config_manager.update_interval, 40)
self.assertEqual(optimizely_instance.config_manager.blocking_timeout, 5)

def test_default_instance_should_create_config_set_default_values_when_polling_int_and_blocking_time_invalid(
Copy link
Contributor

Choose a reason for hiding this comment

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

double underscore test_default_instance__...

Copy link
Contributor

@jaeopt jaeopt left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@msohailhussain msohailhussain left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Contributor

@Mat001 Mat001 left a comment

Choose a reason for hiding this comment

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

lgtm

@msohailhussain msohailhussain merged commit cdc652e into master Jun 4, 2021
@msohailhussain msohailhussain deleted the uzair/static-polling-interval branch June 4, 2021 05:16
The-inside-man pushed a commit that referenced this pull request Jun 15, 2021
* polling interval updated

* Revert "polling interval updated"

This reverts commit e696f1c.

* Create optimizely_factory.py

* linting fixed

* whitespaced removed

* comments addressed and unit tests added

* comments addressed

* comments addressed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants