2025-07-17 13:09:24 +02:00
|
|
|
"""
|
|
|
|
|
Event definitions for microservices communication
|
2025-07-17 19:03:11 +02:00
|
|
|
- Simple class-based approach to avoid dataclass issues
|
2025-07-17 13:09:24 +02:00
|
|
|
"""
|
|
|
|
|
|
2025-07-18 13:39:40 +02:00
|
|
|
from datetime import datetime, timezone
|
2025-07-17 13:09:24 +02:00
|
|
|
from typing import Dict, Any, Optional
|
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
class BaseEvent:
|
|
|
|
|
"""Base event class"""
|
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
|
|
|
|
|
|
|
|
def to_dict(self) -> Dict[str, Any]: # Add this method
|
|
|
|
|
"""Converts the event object to a dictionary for JSON serialization."""
|
|
|
|
|
return {
|
|
|
|
|
"service_name": self.service_name,
|
|
|
|
|
"data": self.data,
|
|
|
|
|
"event_type": self.event_type,
|
|
|
|
|
"event_id": self.event_id,
|
|
|
|
|
"timestamp": self.timestamp.isoformat(), # Convert datetime to ISO 8601 string
|
|
|
|
|
"correlation_id": self.correlation_id
|
|
|
|
|
}
|
2025-07-17 13:09:24 +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,
|
|
|
|
|
event_type="training.started",
|
|
|
|
|
correlation_id=correlation_id
|
|
|
|
|
)
|
2025-07-17 13:09:24 +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,
|
|
|
|
|
event_type="training.completed",
|
|
|
|
|
correlation_id=correlation_id
|
|
|
|
|
)
|
2025-07-17 13:09:24 +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,
|
|
|
|
|
event_type="training.failed",
|
|
|
|
|
correlation_id=correlation_id
|
|
|
|
|
)
|
2025-07-17 13:09:24 +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,
|
|
|
|
|
event_type="forecast.generated",
|
|
|
|
|
correlation_id=correlation_id
|
|
|
|
|
)
|
2025-07-17 13:09:24 +02:00
|
|
|
|
|
|
|
|
class ForecastRequestedEvent(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,
|
|
|
|
|
event_type="forecast.requested",
|
|
|
|
|
correlation_id=correlation_id
|
|
|
|
|
)
|
2025-07-17 13:09:24 +02:00
|
|
|
|
|
|
|
|
# User Events
|
|
|
|
|
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,
|
|
|
|
|
event_type="user.registered",
|
|
|
|
|
correlation_id=correlation_id
|
|
|
|
|
)
|
2025-07-17 13:09:24 +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,
|
|
|
|
|
event_type="user.login",
|
|
|
|
|
correlation_id=correlation_id
|
|
|
|
|
)
|
2025-07-17 13:09:24 +02:00
|
|
|
|
|
|
|
|
# Tenant Events
|
|
|
|
|
class TenantCreatedEvent(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,
|
|
|
|
|
event_type="tenant.created",
|
|
|
|
|
correlation_id=correlation_id
|
|
|
|
|
)
|
2025-07-17 13:09:24 +02:00
|
|
|
|
|
|
|
|
class TenantUpdatedEvent(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,
|
|
|
|
|
event_type="tenant.updated",
|
|
|
|
|
correlation_id=correlation_id
|
|
|
|
|
)
|
2025-07-17 13:09:24 +02:00
|
|
|
|
|
|
|
|
# Notification Events
|
|
|
|
|
class NotificationSentEvent(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,
|
|
|
|
|
event_type="notification.sent",
|
|
|
|
|
correlation_id=correlation_id
|
|
|
|
|
)
|
2025-07-17 13:09:24 +02:00
|
|
|
|
|
|
|
|
class NotificationFailedEvent(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,
|
|
|
|
|
event_type="notification.failed",
|
|
|
|
|
correlation_id=correlation_id
|
|
|
|
|
)
|