Spaces:
Sleeping
Sleeping
Girish Jeswani commited on
Commit ·
396b902
1
Parent(s): 5542d2b
update session management
Browse files
multi_llm_chatbot_backend/app/api/routes/phd_canvas.py
CHANGED
|
@@ -2,10 +2,12 @@ from fastapi import APIRouter, HTTPException, Depends, status, BackgroundTasks
|
|
| 2 |
from typing import Dict, Optional
|
| 3 |
from datetime import datetime
|
| 4 |
import logging
|
|
|
|
| 5 |
|
| 6 |
from app.models.user import User
|
| 7 |
from app.models.phd_canvas import PhdCanvas, CanvasResponse, UpdateCanvasRequest
|
| 8 |
from app.core.auth import get_current_active_user
|
|
|
|
| 9 |
|
| 10 |
from pydantic import BaseModel
|
| 11 |
|
|
@@ -168,6 +170,7 @@ async def trigger_auto_update(
|
|
| 168 |
"""Trigger automatic canvas update (typically called when user opens canvas page)"""
|
| 169 |
try:
|
| 170 |
canvas_manager = get_canvas_manager()
|
|
|
|
| 171 |
|
| 172 |
# Get current canvas to check if auto-update is needed
|
| 173 |
canvas = await canvas_manager.get_or_create_canvas(str(current_user.id))
|
|
@@ -181,13 +184,6 @@ async def trigger_auto_update(
|
|
| 181 |
canvas.total_insights == 0
|
| 182 |
)
|
| 183 |
|
| 184 |
-
# Check if we need to update (has been more than 1 hour since last update)
|
| 185 |
-
from datetime import timedelta
|
| 186 |
-
needs_regular_update = (
|
| 187 |
-
canvas.last_updated is None or
|
| 188 |
-
(datetime.utcnow() - canvas.last_updated) > timedelta(hours=1)
|
| 189 |
-
)
|
| 190 |
-
|
| 191 |
if is_first_time_canvas:
|
| 192 |
logger.info(f"First-time canvas detected for user {current_user.id}, triggering full update")
|
| 193 |
|
|
@@ -203,11 +199,29 @@ async def trigger_auto_update(
|
|
| 203 |
"status": "processing",
|
| 204 |
"type": "full_update"
|
| 205 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
-
|
| 208 |
-
logger.info(f"Regular auto-updating canvas for user {current_user.id}")
|
| 209 |
-
|
| 210 |
-
# For existing canvas, do incremental update
|
| 211 |
background_tasks.add_task(
|
| 212 |
_background_canvas_update,
|
| 213 |
str(current_user.id),
|
|
@@ -215,14 +229,17 @@ async def trigger_auto_update(
|
|
| 215 |
)
|
| 216 |
|
| 217 |
return {
|
| 218 |
-
"message": "Canvas update queued",
|
| 219 |
"status": "updating",
|
| 220 |
-
"type": "incremental_update"
|
|
|
|
| 221 |
}
|
| 222 |
else:
|
|
|
|
| 223 |
return {
|
| 224 |
"message": "Canvas is up to date",
|
| 225 |
-
"status": "current"
|
|
|
|
| 226 |
}
|
| 227 |
|
| 228 |
except Exception as e:
|
|
@@ -296,22 +313,4 @@ async def _background_canvas_update(user_id: str, request: UpdateCanvasRequest):
|
|
| 296 |
except Exception as e:
|
| 297 |
logger.error(f"Error in background canvas update for user {user_id}: {e}")
|
| 298 |
import traceback
|
| 299 |
-
logger.error(f"
|
| 300 |
-
|
| 301 |
-
# Health check endpoint
|
| 302 |
-
@router.get("/phd-canvas/health")
|
| 303 |
-
async def canvas_health_check():
|
| 304 |
-
"""Health check for canvas service"""
|
| 305 |
-
try:
|
| 306 |
-
canvas_manager = get_canvas_manager()
|
| 307 |
-
return {
|
| 308 |
-
"status": "healthy",
|
| 309 |
-
"service": "phd-canvas",
|
| 310 |
-
"timestamp": datetime.utcnow().isoformat()
|
| 311 |
-
}
|
| 312 |
-
except Exception as e:
|
| 313 |
-
logger.error(f"Canvas health check failed: {e}")
|
| 314 |
-
raise HTTPException(
|
| 315 |
-
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
| 316 |
-
detail="Canvas service is not healthy"
|
| 317 |
-
)
|
|
|
|
| 2 |
from typing import Dict, Optional
|
| 3 |
from datetime import datetime
|
| 4 |
import logging
|
| 5 |
+
from bson import ObjectId
|
| 6 |
|
| 7 |
from app.models.user import User
|
| 8 |
from app.models.phd_canvas import PhdCanvas, CanvasResponse, UpdateCanvasRequest
|
| 9 |
from app.core.auth import get_current_active_user
|
| 10 |
+
from app.core.database import get_database
|
| 11 |
|
| 12 |
from pydantic import BaseModel
|
| 13 |
|
|
|
|
| 170 |
"""Trigger automatic canvas update (typically called when user opens canvas page)"""
|
| 171 |
try:
|
| 172 |
canvas_manager = get_canvas_manager()
|
| 173 |
+
db = get_database()
|
| 174 |
|
| 175 |
# Get current canvas to check if auto-update is needed
|
| 176 |
canvas = await canvas_manager.get_or_create_canvas(str(current_user.id))
|
|
|
|
| 184 |
canvas.total_insights == 0
|
| 185 |
)
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
if is_first_time_canvas:
|
| 188 |
logger.info(f"First-time canvas detected for user {current_user.id}, triggering full update")
|
| 189 |
|
|
|
|
| 199 |
"status": "processing",
|
| 200 |
"type": "full_update"
|
| 201 |
}
|
| 202 |
+
|
| 203 |
+
# For existing canvas, check if there are actually new chats
|
| 204 |
+
user_object_id = ObjectId(str(current_user.id))
|
| 205 |
+
|
| 206 |
+
# Count chats newer than last processed
|
| 207 |
+
query_filter = {
|
| 208 |
+
"user_id": user_object_id,
|
| 209 |
+
"is_active": {"$ne": False},
|
| 210 |
+
"deleted_at": {"$exists": False}
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
if canvas.last_chat_processed:
|
| 214 |
+
query_filter["$or"] = [
|
| 215 |
+
{"created_at": {"$gt": canvas.last_chat_processed}},
|
| 216 |
+
{"updated_at": {"$gt": canvas.last_chat_processed}}
|
| 217 |
+
]
|
| 218 |
+
|
| 219 |
+
new_chat_count = await db.chat_sessions.count_documents(query_filter)
|
| 220 |
+
|
| 221 |
+
if new_chat_count > 0:
|
| 222 |
+
logger.info(f"Found {new_chat_count} new/updated chats for user {current_user.id}, triggering incremental update")
|
| 223 |
|
| 224 |
+
# Only update if there are actually new chats
|
|
|
|
|
|
|
|
|
|
| 225 |
background_tasks.add_task(
|
| 226 |
_background_canvas_update,
|
| 227 |
str(current_user.id),
|
|
|
|
| 229 |
)
|
| 230 |
|
| 231 |
return {
|
| 232 |
+
"message": f"Canvas update queued for {new_chat_count} new chats",
|
| 233 |
"status": "updating",
|
| 234 |
+
"type": "incremental_update",
|
| 235 |
+
"new_chats": new_chat_count
|
| 236 |
}
|
| 237 |
else:
|
| 238 |
+
logger.info(f"No new chats found for user {current_user.id}, skipping update")
|
| 239 |
return {
|
| 240 |
"message": "Canvas is up to date",
|
| 241 |
+
"status": "current",
|
| 242 |
+
"new_chats": 0
|
| 243 |
}
|
| 244 |
|
| 245 |
except Exception as e:
|
|
|
|
| 313 |
except Exception as e:
|
| 314 |
logger.error(f"Error in background canvas update for user {user_id}: {e}")
|
| 315 |
import traceback
|
| 316 |
+
logger.error(f"Traceback: {traceback.format_exc()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|