Dama12 commited on
Commit
766e6f5
·
1 Parent(s): a4aa7df

Fix NameError, improve logging and UUID handling

Browse files
app/api/routes/analysis.py CHANGED
@@ -10,7 +10,10 @@ from app.schemas.all_schemas import AnalysisRequest, AnalysisResponse
10
  from uuid import UUID
11
  import uuid
12
  from typing import List, Optional
 
13
  import os
 
 
14
  import glob
15
  from app.services.mock_intelligence import MockIntelligenceService
16
  from app.rag.pdf_parser import PDFParser
@@ -130,6 +133,7 @@ def _normalize_k2_result_for_frontend(result_dict: dict) -> dict:
130
  from app.core.logging import logger
131
 
132
  logger.info(f"NORMALIZE: Input result keys: {list(result_dict.keys())}")
 
133
 
134
  # Ensure comparative_analysis exists and has required structure
135
  if 'comparative_analysis' not in result_dict or not result_dict['comparative_analysis']:
@@ -211,13 +215,21 @@ async def get_chat_history(
211
  db: Session = Depends(get_db)
212
  ):
213
  """Récupère l'historique de chat pour une analyse"""
214
- if analysis_id.startswith("demo_"):
215
  return []
216
 
217
  try:
218
  from app.db.repositories.chat_repo import ChatRepository
219
  chat_repo = ChatRepository(db)
220
- messages = chat_repo.get_history(UUID(analysis_id))
 
 
 
 
 
 
 
 
221
 
222
  return [
223
  {
 
10
  from uuid import UUID
11
  import uuid
12
  from typing import List, Optional
13
+ import json
14
  import os
15
+ import re
16
+ import traceback
17
  import glob
18
  from app.services.mock_intelligence import MockIntelligenceService
19
  from app.rag.pdf_parser import PDFParser
 
133
  from app.core.logging import logger
134
 
135
  logger.info(f"NORMALIZE: Input result keys: {list(result_dict.keys())}")
136
+ logger.info(f"NORMALIZE: Status is: {result_dict.get('status')}")
137
 
138
  # Ensure comparative_analysis exists and has required structure
139
  if 'comparative_analysis' not in result_dict or not result_dict['comparative_analysis']:
 
215
  db: Session = Depends(get_db)
216
  ):
217
  """Récupère l'historique de chat pour une analyse"""
218
+ if str(analysis_id).startswith("demo_") or not any(c in str(analysis_id) for c in "0123456789abcdef"):
219
  return []
220
 
221
  try:
222
  from app.db.repositories.chat_repo import ChatRepository
223
  chat_repo = ChatRepository(db)
224
+
225
+ # Validate UUID format
226
+ try:
227
+ target_id = UUID(analysis_id)
228
+ except ValueError:
229
+ logger.warning(f"Invalid UUID for chat history: {analysis_id}")
230
+ return []
231
+
232
+ messages = chat_repo.get_history(target_id)
233
 
234
  return [
235
  {
app/services/analysis_service.py CHANGED
@@ -175,17 +175,24 @@ class AnalysisService:
175
 
176
  except Exception as e:
177
  db.rollback() # CLEAN TRANSACTION
 
178
  import traceback
179
- logger.error(f"Background analysis error for {analysis_id}: {str(e)}")
180
  logger.error(traceback.format_exc())
 
181
  try:
182
  error_data = {
183
  "status": "FAILED",
184
- "reasoning_summary": f"Erreur technique lors de l'analyse : {str(e)}",
185
  "confidence_overall": 0
186
  }
187
- self.complete_analysis(UUID(analysis_id), status="FAILED", result=error_data)
188
- db.commit()
 
 
 
 
 
 
189
  except Exception as final_err:
190
  logger.error(f"Failed to even mark analysis as FAILED: {final_err}")
191
  db.rollback()
 
175
 
176
  except Exception as e:
177
  db.rollback() # CLEAN TRANSACTION
178
+ logger.error(f"FATAL ERROR in process_analysis for {analysis_id}: {str(e)}")
179
  import traceback
 
180
  logger.error(traceback.format_exc())
181
+
182
  try:
183
  error_data = {
184
  "status": "FAILED",
185
+ "reasoning_summary": f"Erreur technique lors de l'analyse : {str(e)}\n\nTrace: {traceback.format_exc()[:500]}...",
186
  "confidence_overall": 0
187
  }
188
+ # Use a safe UUID conversion
189
+ try:
190
+ target_uuid = UUID(analysis_id) if isinstance(analysis_id, str) else analysis_id
191
+ self.complete_analysis(target_uuid, status="FAILED", result=error_data)
192
+ db.commit()
193
+ logger.info(f"Marked analysis {analysis_id} as FAILED in DB")
194
+ except Exception as uuid_err:
195
+ logger.error(f"Could not convert {analysis_id} to UUID or save failure: {uuid_err}")
196
  except Exception as final_err:
197
  logger.error(f"Failed to even mark analysis as FAILED: {final_err}")
198
  db.rollback()
app/services/k2_think_engine.py CHANGED
@@ -13,6 +13,7 @@ from app.reasoning.k2_client import K2ThinkClient
13
  from app.core.settings import settings
14
  from app.core.logging import logger
15
  import json
 
16
 
17
 
18
  class K2ThinkEngine:
 
13
  from app.core.settings import settings
14
  from app.core.logging import logger
15
  import json
16
+ import os
17
 
18
 
19
  class K2ThinkEngine: