round 3
This commit is contained in:
10
events/broadcaster.py
Normal file
10
events/broadcaster.py
Normal file
@@ -0,0 +1,10 @@
|
||||
import time
|
||||
from threading import Thread
|
||||
|
||||
class Broadcaster(Thread):
|
||||
"""Background thread sending periodic notifications."""
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
#push event to all users
|
||||
time.sleep(5) # Send every 5 seconds
|
||||
74
events/sse.py
Normal file
74
events/sse.py
Normal file
@@ -0,0 +1,74 @@
|
||||
import json
|
||||
import queue
|
||||
import uuid
|
||||
from typing import Dict, Any
|
||||
|
||||
from flask import Response
|
||||
|
||||
from events.types.event import Event
|
||||
|
||||
# Maps user_id → dict of {connection_id: queue}
|
||||
user_queues: Dict[str, Dict[str, queue.Queue]] = {}
|
||||
|
||||
|
||||
def get_queue(user_id: str, connection_id: str) -> queue.Queue:
|
||||
"""Get or create a queue for a specific user connection."""
|
||||
if user_id not in user_queues:
|
||||
user_queues[user_id] = {}
|
||||
|
||||
if connection_id not in user_queues[user_id]:
|
||||
user_queues[user_id][connection_id] = queue.Queue()
|
||||
|
||||
return user_queues[user_id][connection_id]
|
||||
|
||||
|
||||
def send_to_user(user_id: str, data: Dict[str, Any]):
|
||||
"""Send data to all connections for a specific user."""
|
||||
if user_id in user_queues:
|
||||
# Format as SSE message once
|
||||
message = f"data: {json.dumps(data)}\n\n".encode('utf-8')
|
||||
|
||||
# Send to all connections for this user
|
||||
for connection_id, q in user_queues[user_id].items():
|
||||
try:
|
||||
q.put(message, block=False)
|
||||
except queue.Full:
|
||||
# Skip if queue is full (connection might be dead)
|
||||
pass
|
||||
|
||||
|
||||
def send_event_to_user(user_id: str, event: Event):
|
||||
"""Send an Event to all connections for a specific user."""
|
||||
send_to_user(user_id, event.to_dict())
|
||||
|
||||
|
||||
def sse_response_for_user(user_id: str):
|
||||
"""Create SSE response for a user connection."""
|
||||
# Generate unique connection ID
|
||||
connection_id = str(uuid.uuid4())
|
||||
user_queue = get_queue(user_id, connection_id)
|
||||
|
||||
def generate():
|
||||
try:
|
||||
while True:
|
||||
# Get message from queue (blocks until available)
|
||||
message = user_queue.get()
|
||||
yield message
|
||||
except GeneratorExit:
|
||||
# Clean up when client disconnects
|
||||
if user_id in user_queues and connection_id in user_queues[user_id]:
|
||||
del user_queues[user_id][connection_id]
|
||||
|
||||
# Remove user entry if no connections remain
|
||||
if not user_queues[user_id]:
|
||||
del user_queues[user_id]
|
||||
|
||||
return Response(
|
||||
generate(),
|
||||
mimetype='text/event-stream',
|
||||
headers={
|
||||
'Cache-Control': 'no-cache',
|
||||
'X-Accel-Buffering': 'no',
|
||||
'Connection': 'keep-alive'
|
||||
}
|
||||
)
|
||||
18
events/types/child_add.py
Normal file
18
events/types/child_add.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from events.types.payload import Payload
|
||||
|
||||
|
||||
class ChildAdd(Payload):
|
||||
def __init__(self, child_id: str, status: str):
|
||||
super().__init__({
|
||||
'child_id': child_id,
|
||||
'status': status,
|
||||
})
|
||||
|
||||
@property
|
||||
def child_id(self) -> str:
|
||||
return self.get("child_id")
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
return self.get("status")
|
||||
|
||||
18
events/types/child_delete.py
Normal file
18
events/types/child_delete.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from events.types.payload import Payload
|
||||
|
||||
|
||||
class ChildDelete(Payload):
|
||||
def __init__(self, child_id: str, status: str):
|
||||
super().__init__({
|
||||
'child_id': child_id,
|
||||
'status': status,
|
||||
})
|
||||
|
||||
@property
|
||||
def child_id(self) -> str:
|
||||
return self.get("child_id")
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
return self.get("status")
|
||||
|
||||
18
events/types/child_update.py
Normal file
18
events/types/child_update.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from events.types.payload import Payload
|
||||
|
||||
|
||||
class ChildUpdate(Payload):
|
||||
def __init__(self, child_id: str, status: str):
|
||||
super().__init__({
|
||||
'child_id': child_id,
|
||||
'status': status,
|
||||
})
|
||||
|
||||
@property
|
||||
def child_id(self) -> str:
|
||||
return self.get("child_id")
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
return self.get("status")
|
||||
|
||||
18
events/types/event.py
Normal file
18
events/types/event.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from dataclasses import dataclass, field
|
||||
import time
|
||||
|
||||
from events.types.payload import Payload
|
||||
|
||||
|
||||
@dataclass
|
||||
class Event:
|
||||
type: str
|
||||
payload: Payload
|
||||
timestamp: float = field(default_factory=time.time)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'type': self.type,
|
||||
'payload': self.payload.data,
|
||||
'timestamp': self.timestamp
|
||||
}
|
||||
19
events/types/event_types.py
Normal file
19
events/types/event_types.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class EventType(Enum):
|
||||
TASK_UPDATE = "task_update"
|
||||
TASK_SET = "task_set"
|
||||
TASK_TEST = "task_test"
|
||||
REWARD_UPDATE = "reward_update"
|
||||
REWARD_SET = "reward_set"
|
||||
CHILD_UPDATE = "child_update"
|
||||
CHILD_ADD = "child_add"
|
||||
CHILD_DELETE = "child_delete"
|
||||
TASK_CREATED = "task_created"
|
||||
TASK_DELETED = "task_deleted"
|
||||
TASK_EDITED = "task_edited"
|
||||
REWARD_CREATED = "reward_created"
|
||||
REWARD_DELETED = "reward_deleted"
|
||||
REWARD_EDITED = "reward_edited"
|
||||
# Add more event types here
|
||||
6
events/types/payload.py
Normal file
6
events/types/payload.py
Normal file
@@ -0,0 +1,6 @@
|
||||
class Payload:
|
||||
def __init__(self, data: dict):
|
||||
self.data = data
|
||||
|
||||
def get(self, key: str, default=None):
|
||||
return self.data.get(key, default)
|
||||
19
events/types/reward_created.py
Normal file
19
events/types/reward_created.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from events.types.payload import Payload
|
||||
|
||||
|
||||
class RewardCreated(Payload):
|
||||
def __init__(self, reward_id: str, status: str):
|
||||
super().__init__({
|
||||
'reward_id': reward_id,
|
||||
'status': status
|
||||
})
|
||||
|
||||
@property
|
||||
def reward_id(self) -> str:
|
||||
return self.get("reward_id")
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
return self.get("status")
|
||||
|
||||
|
||||
19
events/types/reward_deleted.py
Normal file
19
events/types/reward_deleted.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from events.types.payload import Payload
|
||||
|
||||
|
||||
class RewardDeleted(Payload):
|
||||
def __init__(self, reward_id: str, status: str):
|
||||
super().__init__({
|
||||
'reward_id': reward_id,
|
||||
'status': status
|
||||
})
|
||||
|
||||
@property
|
||||
def reward_id(self) -> str:
|
||||
return self.get("reward_id")
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
return self.get("status")
|
||||
|
||||
|
||||
19
events/types/reward_edited.py
Normal file
19
events/types/reward_edited.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from events.types.payload import Payload
|
||||
|
||||
|
||||
class RewardEdited(Payload):
|
||||
def __init__(self, reward_id: str, status: str):
|
||||
super().__init__({
|
||||
'reward_id': reward_id,
|
||||
'status': status
|
||||
})
|
||||
|
||||
@property
|
||||
def reward_id(self) -> str:
|
||||
return self.get("reward_id")
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
return self.get("status")
|
||||
|
||||
|
||||
19
events/types/reward_set.py
Normal file
19
events/types/reward_set.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from events.types.payload import Payload
|
||||
|
||||
|
||||
class RewardSet(Payload):
|
||||
def __init__(self, child_id: str, status: str):
|
||||
super().__init__({
|
||||
'child_id': child_id,
|
||||
'status': status
|
||||
})
|
||||
|
||||
@property
|
||||
def child_id(self) -> str:
|
||||
return self.get("child_id")
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
return self.get("status")
|
||||
|
||||
|
||||
29
events/types/reward_update.py
Normal file
29
events/types/reward_update.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from events.types.payload import Payload
|
||||
|
||||
|
||||
class RewardUpdate(Payload):
|
||||
def __init__(self, reward_id: str, child_id: str, status: str, points: int):
|
||||
super().__init__({
|
||||
'reward_id': reward_id,
|
||||
'child_id': child_id,
|
||||
'status': status,
|
||||
'points': points
|
||||
})
|
||||
|
||||
@property
|
||||
def reward_id(self) -> str:
|
||||
return self.get("reward_id")
|
||||
|
||||
@property
|
||||
def child_id(self) -> str:
|
||||
return self.get("child_id")
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
return self.get("status")
|
||||
|
||||
@property
|
||||
def points(self) -> int:
|
||||
return self.get("points", 0)
|
||||
|
||||
|
||||
19
events/types/task_created.py
Normal file
19
events/types/task_created.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from events.types.payload import Payload
|
||||
|
||||
|
||||
class TaskCreated(Payload):
|
||||
def __init__(self, task_id: str, status: str):
|
||||
super().__init__({
|
||||
'task_id': task_id,
|
||||
'status': status
|
||||
})
|
||||
|
||||
@property
|
||||
def task_id(self) -> str:
|
||||
return self.get("task_id")
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
return self.get("status")
|
||||
|
||||
|
||||
19
events/types/task_deleted.py
Normal file
19
events/types/task_deleted.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from events.types.payload import Payload
|
||||
|
||||
|
||||
class TaskDeleted(Payload):
|
||||
def __init__(self, task_id: str, status: str):
|
||||
super().__init__({
|
||||
'task_id': task_id,
|
||||
'status': status
|
||||
})
|
||||
|
||||
@property
|
||||
def task_id(self) -> str:
|
||||
return self.get("task_id")
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
return self.get("status")
|
||||
|
||||
|
||||
19
events/types/task_edited.py
Normal file
19
events/types/task_edited.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from events.types.payload import Payload
|
||||
|
||||
|
||||
class TaskEdited(Payload):
|
||||
def __init__(self, task_id: str, status: str):
|
||||
super().__init__({
|
||||
'task_id': task_id,
|
||||
'status': status
|
||||
})
|
||||
|
||||
@property
|
||||
def task_id(self) -> str:
|
||||
return self.get("task_id")
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
return self.get("status")
|
||||
|
||||
|
||||
19
events/types/task_set.py
Normal file
19
events/types/task_set.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from events.types.payload import Payload
|
||||
|
||||
|
||||
class TaskSet(Payload):
|
||||
def __init__(self, child_id: str, status: str):
|
||||
super().__init__({
|
||||
'child_id': child_id,
|
||||
'status': status
|
||||
})
|
||||
|
||||
@property
|
||||
def child_id(self) -> str:
|
||||
return self.get("child_id")
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
return self.get("status")
|
||||
|
||||
|
||||
17
events/types/task_test.py
Normal file
17
events/types/task_test.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from events.types.payload import Payload
|
||||
|
||||
|
||||
class TaskTest(Payload):
|
||||
def __init__(self, task_id: str, message: str):
|
||||
super().__init__({
|
||||
'task_id': task_id,
|
||||
'message': message
|
||||
})
|
||||
|
||||
@property
|
||||
def task_id(self) -> str:
|
||||
return self.get("task_id")
|
||||
|
||||
@property
|
||||
def message(self) -> str:
|
||||
return self.get("message")
|
||||
29
events/types/task_update.py
Normal file
29
events/types/task_update.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from events.types.payload import Payload
|
||||
|
||||
|
||||
class TaskUpdate(Payload):
|
||||
def __init__(self, task_id: str, child_id: str, status: str, points: int):
|
||||
super().__init__({
|
||||
'task_id': task_id,
|
||||
'child_id': child_id,
|
||||
'status': status,
|
||||
'points': points
|
||||
})
|
||||
|
||||
@property
|
||||
def task_id(self) -> str:
|
||||
return self.get("task_id")
|
||||
|
||||
@property
|
||||
def child_id(self) -> str:
|
||||
return self.get("child_id")
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
return self.get("status")
|
||||
|
||||
@property
|
||||
def points(self) -> int:
|
||||
return self.get("points", 0)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user