Improve teh securty of teh DB
This commit is contained in:
@@ -181,11 +181,30 @@ async def events_stream(request: Request, tenant_id: str):
|
||||
pubsub = redis_client.pubsub()
|
||||
channel_name = f"alerts:{tenant_id}"
|
||||
await pubsub.subscribe(channel_name)
|
||||
|
||||
|
||||
# Send initial connection event
|
||||
yield f"event: connection\n"
|
||||
yield f"data: {json.dumps({'type': 'connected', 'message': 'SSE connection established', 'timestamp': time.time()})}\n\n"
|
||||
|
||||
|
||||
# Fetch and send initial active alerts from Redis cache
|
||||
try:
|
||||
cache_key = f"active_alerts:{tenant_id}"
|
||||
cached_alerts = await redis_client.get(cache_key)
|
||||
if cached_alerts:
|
||||
active_items = json.loads(cached_alerts)
|
||||
logger.info(f"Sending initial_items to tenant {tenant_id}, count: {len(active_items)}")
|
||||
yield f"event: initial_items\n"
|
||||
yield f"data: {json.dumps(active_items)}\n\n"
|
||||
else:
|
||||
logger.info(f"No cached alerts found for tenant {tenant_id}")
|
||||
yield f"event: initial_items\n"
|
||||
yield f"data: {json.dumps([])}\n\n"
|
||||
except Exception as e:
|
||||
logger.error(f"Error fetching initial items for tenant {tenant_id}: {e}")
|
||||
# Still send empty initial_items event
|
||||
yield f"event: initial_items\n"
|
||||
yield f"data: {json.dumps([])}\n\n"
|
||||
|
||||
heartbeat_counter = 0
|
||||
|
||||
while True:
|
||||
|
||||
Reference in New Issue
Block a user