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(notification-center): Add LogEvent notification #213

Merged
merged 103 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
232f0a0
feat: Add event_batch datamodels.
Jul 19, 2019
2320939
fix: unit test fixes.
Jul 22, 2019
fbb5a68
Merge branch 'master' into epmodels
mnoman09 Jul 22, 2019
c162465
feat: add event_factory and user_event_factory for event processing.
Jul 24, 2019
633b095
Removed third party dependency
msohailhussain Jul 26, 2019
8a5f531
Merge branch 'master' into sohail/pr-189
mnoman09 Aug 1, 2019
35fcd82
Merge remote-tracking branch 'origin/sohail/pr-189' into sohail/pr-193
mnoman09 Aug 1, 2019
d7201cb
Removed HasAttr
mnoman09 Aug 1, 2019
c829fca
Merge branch 'master' into sohail/pr-189
mnoman09 Aug 16, 2019
1d858b7
feat(eventProcessor): Adds EventProcessor and BatchEventProcessor
mnoman09 Aug 16, 2019
c3c9d46
Addressed feedback. Restructured classes.
msohailhussain Aug 20, 2019
a2eeb85
Merge branch 'sohail/pr-189' into sohail/pr-193
mnoman09 Aug 20, 2019
a045291
update: structural changes.
Aug 20, 2019
f70a1c9
Merge branch 'sohail/pr-193' into mnoman/AddBatchEP
mnoman09 Aug 20, 2019
faff070
update: add check for integer values and insert logs for default values.
mnoman09 Aug 20, 2019
c931956
replaced comma with as
mnoman09 Aug 20, 2019
2dea7fe
conditions reordered
mnoman09 Aug 20, 2019
d7f69ec
Added Log event notification
mnoman09 Aug 20, 2019
8853cb3
fix: fix imports in test_event_processor.
mnoman09 Aug 21, 2019
5934764
fix: linter issues
mnoman09 Aug 21, 2019
23ab6ce
fix: addressed more feedback
Aug 21, 2019
6474e98
update: removed print statements.
mnoman09 Aug 21, 2019
1184568
merged PR 189
mnoman09 Aug 21, 2019
592a306
fix: use VisitorAttribute class to create visitor attributes list.
Aug 21, 2019
c4a412a
fix: fix log_event payload.
Aug 22, 2019
2a3374c
removed commented code
mnoman09 Aug 22, 2019
ce44c11
fix linter error.
mnoman09 Aug 22, 2019
00efd1b
Merge branch 'sohail/pr-193' into mnoman/AddBatchEP
mnoman09 Aug 22, 2019
54333d7
update: log_event params.
mnoman09 Aug 22, 2019
af497f9
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
mnoman09 Aug 22, 2019
d2f7126
fix: update module names according to structural changes.
Aug 22, 2019
f21c94b
Merge remote-tracking branch 'remotes/main_origin/mnoman/AddBatchEP' …
Aug 22, 2019
3fd5788
fix: update module names according to structural changes.
Aug 22, 2019
ebe833c
Merge branch 'mnoman/AddBatchEP' of github.com:optimizely/python-sdk …
Aug 22, 2019
ddef208
fix: remove print statement.
Aug 23, 2019
e964048
fix: remove print statement.
Aug 23, 2019
8953a43
Merge branch 'mnoman/log_event_notification' of github.com:optimizely…
Aug 23, 2019
d4f2c66
Merge branch 'master' into sohail/pr-189
rashidsp Aug 28, 2019
80b0963
fix: addressed minor feedback.
Aug 28, 2019
071460a
fix: addressed minor feedback.
Aug 28, 2019
900d96d
Merge branch 'sohail/pr-189' of github.com:optimizely/python-sdk into…
Aug 28, 2019
8cd8547
Merge branch 'sohail/pr-189' into sohail/pr-193
Aug 28, 2019
b730bfd
Merge branch 'sohail/pr-193' into mnoman/AddBatchEP
Aug 28, 2019
752e35f
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
Aug 28, 2019
f9a2cb6
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
Aug 28, 2019
54446b9
Merge branch 'mnoman/log_event_notification' of github.com:optimizely…
Aug 28, 2019
e628e0f
fix: addressed review comments.
mariamjamal94 Sep 2, 2019
855c73f
Merge branch 'sohail/pr-193' into mnoman/AddBatchEP
mariamjamal94 Sep 2, 2019
c92f79d
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
mariamjamal94 Sep 2, 2019
c18e359
Merge branch 'sohail/pr-193' into mnoman/AddBatchEP
mariamjamal94 Sep 2, 2019
56408dc
Merge branches 'mnoman/AddBatchEP' and 'mnoman/AddBatchEP' of github.…
mariamjamal94 Sep 2, 2019
f851754
fix: addresses review comments.
mariamjamal94 Sep 4, 2019
8f7be2b
Merge branch 'sohail/pr-193' into mnoman/AddBatchEP
mariamjamal94 Sep 4, 2019
1486fa4
Merge branch 'sohail/pr-193' into mnoman/AddBatchEP
mariamjamal94 Sep 4, 2019
b9d5fd9
Merge branch 'mnoman/AddBatchEP' of github.com:optimizely/python-sdk …
mariamjamal94 Sep 4, 2019
d42d14b
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
mariamjamal94 Sep 4, 2019
65a752b
fix: addresses review comment.
mariamjamal94 Sep 5, 2019
ab192a0
Merge branch 'sohail/pr-193' into mnoman/AddBatchEP
mariamjamal94 Sep 5, 2019
06ac3f0
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
mariamjamal94 Sep 5, 2019
641d09a
update: allow float values for intervals.
mariamjamal94 Sep 5, 2019
9630139
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
mariamjamal94 Sep 5, 2019
8998580
fix: updated docstring for LogEvent.
mariamjamal94 Sep 6, 2019
a89a5bf
Merge branch 'sohail/pr-193' into mnoman/AddBatchEP
mariamjamal94 Sep 6, 2019
2582fb7
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
mariamjamal94 Sep 6, 2019
cfb2d74
fix: restore config_manager and remove unnecessary warnings from tox.…
mariamjamal94 Sep 6, 2019
75725bd
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
mariamjamal94 Sep 6, 2019
751ee64
fix: remove closeable and introduce stop method.
mariamjamal94 Sep 6, 2019
cb07348
fix: address review comments.
mariamjamal94 Sep 12, 2019
c3d5b07
Merge branch 'master' into sohail/pr-193
mariamjamal94 Sep 12, 2019
f50e053
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
mariamjamal94 Sep 13, 2019
bb3f738
fix: remove close method from test_notification_center.
mariamjamal94 Sep 13, 2019
b5d461b
Merge branch 'master' into sohail/pr-193
aliabbasrizvi Sep 16, 2019
ca208a6
Merge remote-tracking branch 'remotes/main_origin/master' into mnoman…
mariamjamal94 Sep 17, 2019
2c33344
update: update documentation and set default time interval unit to sec.
mariamjamal94 Sep 17, 2019
46cc51d
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
mariamjamal94 Sep 17, 2019
1ab8b32
fix: linting error.
mariamjamal94 Sep 17, 2019
67e0424
fix: compare times directly in seconds.
mariamjamal94 Sep 17, 2019
f0dd1fd
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
mariamjamal94 Sep 17, 2019
4d4e7d1
update: update documentation for event_processor.
mariamjamal94 Sep 17, 2019
477bbd5
Merge branch 'sohail/pr-193' into mnoman/AddBatchEP
oakbani Sep 17, 2019
e299dce
update: update sleeping time to 5sec.
mariamjamal94 Sep 17, 2019
758ffc2
Merge branch 'mnoman/AddBatchEP' of github.com:optimizely/python-sdk …
mariamjamal94 Sep 17, 2019
304ac00
update: update sleeping time to 5 sec.
mariamjamal94 Sep 17, 2019
cbd918a
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
mariamjamal94 Sep 17, 2019
5ef24de
fix: address import order issues.
mariamjamal94 Sep 18, 2019
1edb7c2
Merge branch 'sohail/pr-193' into mnoman/AddBatchEP
mariamjamal94 Sep 18, 2019
4c8b7f3
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
mariamjamal94 Sep 18, 2019
443b7aa
update: add documentation.
mariamjamal94 Sep 18, 2019
3b2cb4a
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
mariamjamal94 Sep 18, 2019
1d5aeca
Merge branch 'master' into sohail/pr-193
oakbani Sep 20, 2019
b3883d6
Merge remote-tracking branch 'remotes/origin/sohail/pr-193' into mnom…
oakbani Sep 20, 2019
655a859
Merge branch 'mnoman/AddBatchEP' of github.com:optimizely/python-sdk …
oakbani Sep 20, 2019
4753c0b
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
oakbani Sep 20, 2019
86e4b7f
Merge branch 'master' into mnoman/AddBatchEP
msohailhussain Sep 20, 2019
65fb6fa
fix: address review comments.
mariamjamal94 Sep 24, 2019
01a8ea1
Merge branch 'mnoman/AddBatchEP' of github.com:optimizely/python-sdk …
mariamjamal94 Sep 24, 2019
3baf024
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
mariamjamal94 Sep 24, 2019
7e20be7
Merge branch 'mnoman/log_event_notification' of github.com:optimizely…
mariamjamal94 Sep 24, 2019
498db55
update: add docstring.
mariamjamal94 Sep 25, 2019
614a3d4
Merge branch 'mnoman/AddBatchEP' into mnoman/log_event_notification
mariamjamal94 Sep 25, 2019
2c67253
fix: address review comments.
mariamjamal94 Sep 25, 2019
9ebf1a4
fix notification center impoer
oakbani Oct 3, 2019
6f84f08
Merge remote-tracking branch 'remotes/origin/master' into mnoman/log_…
oakbani Oct 4, 2019
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
20 changes: 18 additions & 2 deletions optimizely/event/event_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
from six.moves import queue

from optimizely import logger as _logging
from optimizely import notification_center as _notification_center
from optimizely.event_dispatcher import EventDispatcher as default_event_dispatcher
from optimizely.helpers import enums
from optimizely.helpers import validator
from .event_factory import EventFactory
from .user_event import UserEvent
Expand Down Expand Up @@ -62,8 +64,10 @@ def __init__(self,
event_queue=None,
batch_size=None,
flush_interval=None,
timeout_interval=None):
""" BatchEventProcessor init method to configure event batching.
timeout_interval=None,
notification_center=None):
""" EventProcessor init method to configure event batching.

Args:
event_dispatcher: Provides a dispatch_event method which if given a URL and params sends a request to it.
logger: Provides a log method to log messages. By default nothing would be logged.
Expand All @@ -76,6 +80,7 @@ def __init__(self,
be flushed.
timeout_interval: Optional floating point number representing time interval in seconds before joining the consumer
thread.
notification_center: Optional instance of notification_center.NotificationCenter.
"""
self.event_dispatcher = event_dispatcher or default_event_dispatcher
self.logger = _logging.adapt_logger(logger or _logging.NoOpLogger())
Expand All @@ -88,8 +93,13 @@ def __init__(self,
self.timeout_interval = timedelta(seconds=timeout_interval) \
if self._validate_intantiation_props(timeout_interval, 'timeout_interval') \
else self._DEFAULT_TIMEOUT_INTERVAL
self.notification_center = notification_center
self._current_batch = list()

if not validator.is_notification_center_valid(self.notification_center):
self.logger.error(enums.Errors.INVALID_INPUT.format('notification_center'))
self.notification_center = _notification_center.NotificationCenter()

if start_on_init is True:
self.start()

Expand Down Expand Up @@ -195,6 +205,12 @@ def _flush_queue(self):

log_event = EventFactory.create_log_event(to_process_batch, self.logger)

if self.notification_center is not None:
self.notification_center.send_notifications(
enums.NotificationTypes.LOG_EVENT,
log_event
)

try:
self.event_dispatcher.dispatch_event(log_event)
except Exception as e:
Expand Down
4 changes: 4 additions & 0 deletions optimizely/helpers/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ class NotificationTypes(object):

TRACK notification listener has the following parameters:
str event_key, str user_id, dict attributes (can be None), event_tags (can be None), Event event

LOG_EVENT notification listener has the following parameter(s):
LogEvent log_event
"""
ACTIVATE = 'ACTIVATE:experiment, user_id, attributes, variation, event'
DECISION = 'DECISION:type, user_id, attributes, decision_info'
OPTIMIZELY_CONFIG_UPDATE = 'OPTIMIZELY_CONFIG_UPDATE'
TRACK = 'TRACK:event_key, user_id, attributes, event_tags, event'
LOG_EVENT = 'LOG_EVENT:log_event'
36 changes: 33 additions & 3 deletions tests/test_event_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
from six.moves import queue

from . import base
from optimizely.logger import SimpleLogger
from optimizely.event.payload import Decision, Visitor
from optimizely.event.user_event_factory import UserEventFactory
from optimizely.event.event_processor import BatchEventProcessor
from optimizely.event.log_event import LogEvent
from optimizely.event.user_event_factory import UserEventFactory
from optimizely.helpers import enums
from optimizely.logger import SimpleLogger


class CanonicalEvent(object):
Expand Down Expand Up @@ -110,6 +112,7 @@ def setUp(self, *args, **kwargs):
self.event_name = 'test_event'
self.event_queue = queue.Queue(maxsize=self.DEFAULT_QUEUE_CAPACITY)
self.optimizely.logger = SimpleLogger()
self.notification_center = self.optimizely.notification_center

def tearDown(self):
self._event_processor.stop()
Expand All @@ -125,7 +128,8 @@ def _set_event_processor(self, event_dispatcher, logger):
self.event_queue,
self.MAX_BATCH_SIZE,
self.MAX_DURATION_SEC,
self.MAX_TIMEOUT_INTERVAL_SEC
self.MAX_TIMEOUT_INTERVAL_SEC,
self.optimizely.notification_center
)

def test_drain_on_stop(self):
Expand Down Expand Up @@ -371,3 +375,29 @@ def test_init__NaN_timeout_interval(self):
# default timeout interval is 5s.
self.assertEqual(self._event_processor.timeout_interval, timedelta(seconds=5))
mock_config_logging.info.assert_called_with('Using default value for timeout_interval.')

def test_notification_center__on_log_event(self):

mock_event_dispatcher = mock.Mock()
callback_hit = [False]

def on_log_event(log_event):
self.assertStrictTrue(isinstance(log_event, LogEvent))
callback_hit[0] = True

self.optimizely.notification_center.add_notification_listener(
enums.NotificationTypes.LOG_EVENT, on_log_event
)

with mock.patch.object(self.optimizely, 'logger') as mock_config_logging:
self._set_event_processor(mock_event_dispatcher, mock_config_logging)

user_event = self._build_conversion_event(self.event_name, self.project_config)
self._event_processor.process(user_event)

self._event_processor.stop()

self.assertEqual(True, callback_hit[0])
self.assertEqual(1, len(self.optimizely.notification_center.notification_listeners[
enums.NotificationTypes.LOG_EVENT
]))
16 changes: 16 additions & 0 deletions tests/test_notification_center.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def on_track_listener(*args):
pass


def on_log_event_listener(*args):
pass


class NotificationCenterTest(unittest.TestCase):

def test_add_notification_listener__valid_type(self):
Expand All @@ -59,6 +63,11 @@ def test_add_notification_listener__valid_type(self):
4, test_notification_center.add_notification_listener(enums.NotificationTypes.TRACK, on_track_listener)
)

self.assertEqual(
5, test_notification_center.add_notification_listener(enums.NotificationTypes.LOG_EVENT,
on_log_event_listener)
)

def test_add_notification_listener__multiple_listeners(self):
""" Test that multiple listeners of the same type can be successfully added. """

Expand Down Expand Up @@ -138,6 +147,7 @@ def another_on_activate_listener(*args):
self.assertEqual(2, len(test_notification_center.notification_listeners[enums.NotificationTypes.ACTIVATE]))
self.assertEqual(1, len(test_notification_center.notification_listeners[enums.NotificationTypes.DECISION]))
self.assertEqual(0, len(test_notification_center.notification_listeners[enums.NotificationTypes.TRACK]))
self.assertEqual(0, len(test_notification_center.notification_listeners[enums.NotificationTypes.LOG_EVENT]))

# Remove one of the activate listeners and assert.
self.assertTrue(test_notification_center.remove_notification_listener(3))
Expand All @@ -164,6 +174,10 @@ def another_on_activate_listener(*args):
3, test_notification_center.add_notification_listener(enums.NotificationTypes.ACTIVATE,
another_on_activate_listener)
)
self.assertEqual(
4, test_notification_center.add_notification_listener(enums.NotificationTypes.LOG_EVENT,
on_log_event_listener)
)

# Try removing a listener which does not exist.
self.assertFalse(test_notification_center.remove_notification_listener(42))
Expand All @@ -180,6 +194,7 @@ def test_clear_notification_listeners(self):
on_config_update_listener)
test_notification_center.add_notification_listener(enums.NotificationTypes.DECISION, on_decision_listener)
test_notification_center.add_notification_listener(enums.NotificationTypes.TRACK, on_track_listener)
test_notification_center.add_notification_listener(enums.NotificationTypes.LOG_EVENT, on_log_event_listener)

# Assert all listeners are there:
for notification_type in notification_center.NOTIFICATION_TYPES:
Expand Down Expand Up @@ -210,6 +225,7 @@ def test_clear_all_notification_listeners(self):
on_config_update_listener)
test_notification_center.add_notification_listener(enums.NotificationTypes.DECISION, on_decision_listener)
test_notification_center.add_notification_listener(enums.NotificationTypes.TRACK, on_track_listener)
test_notification_center.add_notification_listener(enums.NotificationTypes.LOG_EVENT, on_log_event_listener)

# Assert all listeners are there:
for notification_type in notification_center.NOTIFICATION_TYPES:
Expand Down