Skip to content

Commit

Permalink
chore: quality fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Oct 26, 2023
1 parent 715a98a commit b69e866
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 27 deletions.
13 changes: 7 additions & 6 deletions eventtracking/backends/event_bus.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
"""Event tracker backend that emits events to the event-bus."""
import json
import logging

from openedx_events.analytics.data import TrackingLogData
from openedx_events.analytics.signals import TRACKING_EVENT_EMITTED
from openedx_events.event_bus import get_producer

from eventtracking.backends.routing import RoutingBackend
from eventtracking.config import SEND_TRACKING_EVENT_EMITTED_SIGNAL
from openedx_events.data import EventsMetadata
from openedx_events.event_bus import get_producer
from attrs import asdict
import logging

logger = logging.getLogger(__name__)


class EventBusRoutingBackend(RoutingBackend):
"""
Event tracker backend that emits an Open edX public signal.
Expand All @@ -29,7 +28,7 @@ def send(self, event):
data = json.dumps(event.get("data"))
context = json.dumps(event.get("context"))

tracking_log=TrackingLogData(
tracking_log = TrackingLogData(
name=event.get("name"),
timestamp=event.get("timestamp"),
data=data,
Expand All @@ -38,7 +37,9 @@ def send(self, event):

metadata = TRACKING_EVENT_EMITTED.generate_signal_metadata()

logger.info(f"Sending tracking event emitted signal for event for {tracking_log.name}")
logger.info(
f"Sending tracking event emitted signal for event for {tracking_log.name}"
)
get_producer().send(
signal=TRACKING_EVENT_EMITTED,
topic="analytics",
Expand Down
4 changes: 2 additions & 2 deletions eventtracking/backends/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def process_event(self, event):
processed_event = modified_event
except EventEmissionExit:
raise
except Exception: # pylint: disable=broad-except
except Exception:
LOG.exception(
'Failed to execute processor: %s', str(processor)
)
Expand All @@ -142,7 +142,7 @@ def send_to_backends(self, event, raise_on_error=False):
LOG.info('[send_to_backends] Failed to send edx event "%s" to "%s" backend. "%s" backend has'
' not been enabled, [%s]', event["name"], name, name, repr(exc)
)
except Exception as exc: # pylint: disable=broad-except
except Exception as exc:
LOG.exception(
'Unable to send edx event "%s" to backend: %s', event["name"], name
)
Expand Down
38 changes: 21 additions & 17 deletions eventtracking/backends/tests/test_event_bus.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"""
Test the async routing backend.
"""
from django.test import override_settings
import json

from unittest import TestCase
from unittest.mock import sentinel, patch, Mock
from eventtracking.backends.event_bus import EventBusRoutingBackend
from unittest.mock import Mock, patch, sentinel

from django.test import override_settings
from openedx_events.analytics.data import TrackingLogData
from openedx_events.analytics.signals import TRACKING_EVENT_EMITTED

from eventtracking.backends.event_bus import EventBusRoutingBackend


class TestAsyncRoutingBackend(TestCase):
"""
Test the async routing backend.
Expand All @@ -18,20 +20,20 @@ class TestAsyncRoutingBackend(TestCase):
def setUp(self):
super().setUp()
self.sample_event = {
'name': str(sentinel.name),
'data': {'foo': 'bar'},
'timestamp': '2020-01-01T12:12:12.000000+00:00',
'context': {'baz': 'qux'},
"name": str(sentinel.name),
"data": {"foo": "bar"},
"timestamp": "2020-01-01T12:12:12.000000+00:00",
"context": {"baz": "qux"},
}

@patch('eventtracking.backends.event_bus.EventBusRoutingBackend.send')
@patch("eventtracking.backends.event_bus.EventBusRoutingBackend.send")
def test_successful_send(self, mocked_send_event):
backend = EventBusRoutingBackend()
backend.send(self.sample_event)
mocked_send_event.assert_called_once_with(self.sample_event)

@override_settings(SEND_TRACKING_EVENT_EMITTED_SIGNAL=True)
@patch('eventtracking.backends.event_bus.get_producer')
@patch("eventtracking.backends.event_bus.get_producer")
def test_successful_send_event(self, mock_get_producer):
mock_send = Mock()
mock_get_producer.return_value = Mock(send=mock_send)
Expand All @@ -48,18 +50,20 @@ def test_successful_send_event(self, mock_get_producer):
"event_key_field": "tracking_log.name",
"event_data": {
"tracking_log": TrackingLogData(
name=self.sample_event['name'],
timestamp=self.sample_event['timestamp'],
data=json.dumps(self.sample_event['data']),
context=json.dumps(self.sample_event['context']),
name=self.sample_event["name"],
timestamp=self.sample_event["timestamp"],
data=json.dumps(self.sample_event["data"]),
context=json.dumps(self.sample_event["context"]),
)
},
},
mock_send.call_args.kwargs
mock_send.call_args.kwargs,
)

@patch('eventtracking.backends.event_bus.SEND_TRACKING_EVENT_EMITTED_SIGNAL.is_enabled')
@patch('eventtracking.backends.event_bus.get_producer')
@patch(
"eventtracking.backends.event_bus.SEND_TRACKING_EVENT_EMITTED_SIGNAL.is_enabled"
)
@patch("eventtracking.backends.event_bus.get_producer")
def test_event_is_disabled(self, mock_get_producer, mock_is_enabled):
mock_is_enabled.return_value = False
backend = EventBusRoutingBackend()
Expand Down
9 changes: 7 additions & 2 deletions eventtracking/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@
# .. toggle_default: False
# .. toggle_description: When True, the system will publish `TRACKING_EVENT_EMITTED` signals to the event bus. The
# `TRACKING_EVENT_EMITTED` signal is emit when a tracking log is emitted.
# .. toggle_use_cases: publish
SEND_TRACKING_EVENT_EMITTED_SIGNAL = SettingToggle('SEND_TRACKING_EVENT_EMITTED_SIGNAL', default=True, module_name=__name__)
# .. toggle_use_cases: circuit_breaker
# .. toggle_creation_date: 2023-10-26
SEND_TRACKING_EVENT_EMITTED_SIGNAL = SettingToggle(
'SEND_TRACKING_EVENT_EMITTED_SIGNAL',
default=True,
module_name=__name__
)

0 comments on commit b69e866

Please sign in to comment.