Skip to content

Commit

Permalink
refactor: sent event direclty to event bus
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Oct 26, 2023
1 parent 77c5f5f commit c550b78
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 58 deletions.
47 changes: 34 additions & 13 deletions eventtracking/backends/event_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,53 @@
from openedx_events.analytics.signals import TRACKING_EVENT_EMITTED

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

EVENT_BUS_SOURCE = "openedx/eventtracking"
logger = logging.getLogger(__name__)

EVENT_BUS_SOURCE = "openedx/eventtracking"

class EventBusRoutingBackend(RoutingBackend):
"""
Event tracker backend that emits an Open edX public signal.
"""

def __init__(self, processors=None, backends=None, backend_name=""):
self.backend_name = backend_name
super().__init__(processors=processors, backends=backends)

def send(self, event):
"""
Emit the TRACKING_EVENT_EMITTED Open edX public signal to allow
other apps to listen for tracking events.
"""
data = json.dumps(event.get("data"))
context = json.dumps(event.get("context"))
# .. event_implemented_name: TRACKING_EVENT_EMITTED
TRACKING_EVENT_EMITTED.send_event(
tracking_log=TrackingLogData(
name=event.get("name"),
timestamp=event.get("timestamp"),
data=data,
context=context,
)

tracking_log=TrackingLogData(
name=event.get("name"),
timestamp=event.get("timestamp"),
data=data,
context=context,
)

if SEND_TRACKING_EVENT_EMITTED_SIGNAL.is_enabled():
logger.info(f"Sending tracking event emitted signal for event for {tracking_log.name}")
get_producer().send(
signal=TRACKING_EVENT_EMITTED,
topic="analytics",
event_key_field="tracking_log.name",
event_data={"tracking_log": tracking_log},
event_metadata=generate_signal_metadata()
)


def generate_signal_metadata():
"""
Generate the metadata for the signal with a custom source.
"""
metadata = TRACKING_EVENT_EMITTED.generate_signal_metadata()
medata_dict = asdict(metadata)
medata_dict["source"] = EVENT_BUS_SOURCE
metadata = EventsMetadata(**medata_dict)
return metadata
45 changes: 0 additions & 45 deletions eventtracking/handlers.py

This file was deleted.

0 comments on commit c550b78

Please sign in to comment.