Files
bakery-ia/shared/messaging/events.py

105 lines
3.6 KiB
Python
Raw Normal View History

"""
2025-07-18 14:18:52 +02:00
shared/messaging/events.py
Event definitions for microservices communication
"""
2025-07-18 13:39:40 +02:00
from datetime import datetime, timezone
from typing import Dict, Any, Optional
import uuid
class BaseEvent:
2025-07-18 14:18:52 +02:00
"""Base event class - FIXED"""
2025-07-17 19:03:11 +02:00
def __init__(self, service_name: str, data: Dict[str, Any], event_type: str = "", correlation_id: Optional[str] = None):
self.service_name = service_name
self.data = data
self.event_type = event_type
self.event_id = str(uuid.uuid4())
2025-07-18 13:39:40 +02:00
self.timestamp = datetime.now(timezone.utc)
2025-07-17 19:03:11 +02:00
self.correlation_id = correlation_id
2025-07-18 13:39:40 +02:00
2025-07-18 14:18:52 +02:00
def to_dict(self) -> Dict[str, Any]:
"""Converts the event object to a dictionary for JSON serialization - FIXED"""
2025-07-18 13:39:40 +02:00
return {
"service_name": self.service_name,
"data": self.data,
"event_type": self.event_type,
"event_id": self.event_id,
2025-07-18 14:18:52 +02:00
"timestamp": self.timestamp.isoformat(), # Convert datetime to ISO string
2025-07-18 13:39:40 +02:00
"correlation_id": self.correlation_id
}
2025-07-18 14:18:52 +02:00
# Auth Events - FIXED
class UserRegisteredEvent(BaseEvent):
2025-07-17 19:03:11 +02:00
def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
super().__init__(
service_name=service_name,
data=data,
2025-07-18 14:18:52 +02:00
event_type="user.registered",
2025-07-17 19:03:11 +02:00
correlation_id=correlation_id
)
2025-07-18 14:18:52 +02:00
class UserLoginEvent(BaseEvent):
2025-07-17 19:03:11 +02:00
def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
super().__init__(
service_name=service_name,
data=data,
2025-07-18 14:18:52 +02:00
event_type="user.login",
2025-07-17 19:03:11 +02:00
correlation_id=correlation_id
)
2025-07-18 14:18:52 +02:00
class UserLogoutEvent(BaseEvent):
2025-07-17 19:03:11 +02:00
def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
super().__init__(
service_name=service_name,
data=data,
2025-07-18 14:18:52 +02:00
event_type="user.logout",
2025-07-17 19:03:11 +02:00
correlation_id=correlation_id
)
2025-07-18 14:18:52 +02:00
# Training Events
class TrainingStartedEvent(BaseEvent):
2025-07-17 19:03:11 +02:00
def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
super().__init__(
service_name=service_name,
data=data,
2025-07-18 14:18:52 +02:00
event_type="training.started",
2025-07-17 19:03:11 +02:00
correlation_id=correlation_id
)
2025-07-18 14:18:52 +02:00
class TrainingCompletedEvent(BaseEvent):
2025-07-17 19:03:11 +02:00
def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
super().__init__(
service_name=service_name,
data=data,
2025-07-18 14:18:52 +02:00
event_type="training.completed",
2025-07-17 19:03:11 +02:00
correlation_id=correlation_id
)
2025-07-18 14:18:52 +02:00
class TrainingFailedEvent(BaseEvent):
2025-07-17 19:03:11 +02:00
def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
super().__init__(
service_name=service_name,
data=data,
2025-07-18 14:18:52 +02:00
event_type="training.failed",
2025-07-17 19:03:11 +02:00
correlation_id=correlation_id
)
2025-07-18 14:18:52 +02:00
# Forecasting Events
class ForecastGeneratedEvent(BaseEvent):
2025-07-17 19:03:11 +02:00
def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
super().__init__(
service_name=service_name,
data=data,
2025-07-18 14:18:52 +02:00
event_type="forecast.generated",
2025-07-17 19:03:11 +02:00
correlation_id=correlation_id
)
2025-07-18 14:18:52 +02:00
# Data Events
class DataImportedEvent(BaseEvent):
2025-07-17 19:03:11 +02:00
def __init__(self, service_name: str, data: Dict[str, Any], correlation_id: Optional[str] = None):
super().__init__(
service_name=service_name,
data=data,
2025-07-18 14:18:52 +02:00
event_type="data.imported",
2025-07-17 19:03:11 +02:00
correlation_id=correlation_id
)