AIdeaText commited on
Commit
92df942
·
1 Parent(s): dcdbcfb

update store graph

Browse files
Files changed (1) hide show
  1. modules/database/semantic_mongo_db.py +56 -35
modules/database/semantic_mongo_db.py CHANGED
@@ -1,13 +1,15 @@
1
  #/modules/database/semantic_mongo_db.py
2
 
3
- # Importaciones estándar
4
  import io
5
  import base64
6
  from datetime import datetime, timezone
7
  import logging
 
8
 
9
  # Importaciones de terceros
10
  import matplotlib.pyplot as plt
 
11
 
12
  # Importaciones locales
13
  from .mongo_db import (
@@ -19,60 +21,79 @@ from .mongo_db import (
19
  )
20
 
21
  # Configuración del logger
22
- logger = logging.getLogger(__name__) # Cambiado de name a __name__
23
  COLLECTION_NAME = 'student_semantic_analysis'
24
 
25
- ####################################################################
26
- # modules/database/semantic_mongo_db.py
27
  def store_student_semantic_result(username, text, analysis_result, lang_code='en'):
28
  """
29
- Guarda el resultado del análisis semántico en MongoDB.
30
- Args:
31
- username: Nombre del usuario
32
- text: Texto completo analizado
33
- analysis_result: Diccionario con los resultados del análisis
34
- lang_code: Código de idioma (opcional, default 'en')
35
  """
36
  try:
37
- # Verificar datos mínimos requeridos
38
- if not username or not text or not analysis_result:
39
  logger.error("Datos insuficientes para guardar el análisis")
40
  return False
41
 
42
- # Preparar el gráfico conceptual
43
- concept_graph_data = None
44
- if 'concept_graph' in analysis_result and analysis_result['concept_graph'] is not None:
 
 
 
 
 
 
 
45
  try:
46
- if isinstance(analysis_result['concept_graph'], bytes):
47
- concept_graph_data = base64.b64encode(analysis_result['concept_graph']).decode('utf-8')
 
48
  else:
49
- logger.warning("El gráfico conceptual no está en formato bytes")
 
 
 
 
 
 
 
 
 
 
 
 
50
  except Exception as e:
51
- logger.error(f"Error al codificar gráfico conceptual: {str(e)}")
 
 
 
52
 
53
- # Crear documento para MongoDB
54
  analysis_document = {
55
  'username': username,
56
- 'timestamp': datetime.now(timezone.utc), # Mejor práctica
57
- 'text': text,
58
- 'analysis_type': 'semantic',
 
59
  'key_concepts': analysis_result.get('key_concepts', []),
60
- 'concept_centrality': analysis_result.get('concept_centrality', {}),
61
- 'concept_graph': concept_graph_data,
62
- 'language': lang_code # Usamos el parámetro directamente
63
  }
64
 
65
- # Insertar en MongoDB
66
- result = insert_document(COLLECTION_NAME, analysis_document)
67
- if result:
68
- logger.info(f"Análisis semántico guardado para {username}")
69
- return True
70
-
71
- logger.error("No se pudo insertar el documento en MongoDB")
72
- return False
 
 
73
 
74
  except Exception as e:
75
- logger.error(f"Error al guardar el análisis semántico: {str(e)}", exc_info=True)
76
  return False
77
 
78
  ####################################################################################
 
1
  #/modules/database/semantic_mongo_db.py
2
 
3
+ # Importaciones estándar corregidas y optimizadas
4
  import io
5
  import base64
6
  from datetime import datetime, timezone
7
  import logging
8
+ from PIL import Image # Necesario para la optimización
9
 
10
  # Importaciones de terceros
11
  import matplotlib.pyplot as plt
12
+ from pymongo.errors import PyMongoError
13
 
14
  # Importaciones locales
15
  from .mongo_db import (
 
21
  )
22
 
23
  # Configuración del logger
24
+ logger = logging.getLogger(__name__)
25
  COLLECTION_NAME = 'student_semantic_analysis'
26
 
 
 
27
  def store_student_semantic_result(username, text, analysis_result, lang_code='en'):
28
  """
29
+ NOMBRE PRESERVADO. Guarda el resultado del análisis semántico en MongoDB.
30
+ Optimizado para evitar el error 413 (Request size too large) mediante compresión.
 
 
 
 
31
  """
32
  try:
33
+ # 1. Validación de datos mínimos
34
+ if not all([username, text, analysis_result]):
35
  logger.error("Datos insuficientes para guardar el análisis")
36
  return False
37
 
38
+ collection = get_collection(COLLECTION_NAME)
39
+ if collection is None:
40
+ logger.error(f"No se pudo obtener la colección {COLLECTION_NAME}")
41
+ return False
42
+
43
+ # 2. Procesamiento Único del Gráfico (Optimizado y sin redundancias)
44
+ concept_graph_data = analysis_result.get('concept_graph')
45
+ final_graph_bytes = None
46
+
47
+ if concept_graph_data:
48
  try:
49
+ # Convertir de base64 a bytes si es necesario
50
+ if isinstance(concept_graph_data, str):
51
+ final_graph_bytes = base64.b64decode(concept_graph_data)
52
  else:
53
+ final_graph_bytes = concept_graph_data
54
+
55
+ # --- COMPRESIÓN PARA EVITAR ERROR 413 ---
56
+ img = Image.open(io.BytesIO(final_graph_bytes))
57
+ if img.mode != 'RGB':
58
+ img = img.convert('RGB')
59
+
60
+ output = io.BytesIO()
61
+ # Calidad 75% reduce el peso drásticamente sin perder claridad en los nodos
62
+ img.save(output, format="JPEG", quality=75, optimize=True)
63
+ final_graph_bytes = output.getvalue()
64
+
65
+ logger.info(f"Grafo optimizado: {len(final_graph_bytes)} bytes para usuario {username}")
66
  except Exception as e:
67
+ logger.warning(f"Error procesando gráfico, se intentará guardar original: {str(e)}")
68
+ # Si falla la compresión, mantenemos los bytes originales si existen
69
+ if not final_graph_bytes and isinstance(concept_graph_data, bytes):
70
+ final_graph_bytes = concept_graph_data
71
 
72
+ # 3. Preparar el documento (Estructura limpia)
73
  analysis_document = {
74
  'username': username,
75
+ 'timestamp': datetime.now(timezone.utc),
76
+ 'text': text[:50000], # Límite de seguridad para textos extremadamente largos
77
+ 'language': lang_code,
78
+ 'analysis_type': 'standard_semantic',
79
  'key_concepts': analysis_result.get('key_concepts', []),
80
+ 'entities': analysis_result.get('entities', []),
81
+ 'concept_graph': final_graph_bytes # Los bytes ya procesados/comprimidos
 
82
  }
83
 
84
+ # 4. Inserción en MongoDB
85
+ try:
86
+ result = collection.insert_one(analysis_document)
87
+ if result.inserted_id:
88
+ logger.info(f"Análisis guardado exitosamente. ID: {result.inserted_id}")
89
+ return True
90
+ return False
91
+ except PyMongoError as e:
92
+ logger.error(f"Error de inserción en MongoDB: {str(e)}")
93
+ return False
94
 
95
  except Exception as e:
96
+ logger.error(f"Error inesperado en store_student_semantic_result: {str(e)}", exc_info=True)
97
  return False
98
 
99
  ####################################################################################