update store graph
Browse files
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__)
|
| 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 |
-
|
| 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 |
-
#
|
| 38 |
-
if not username
|
| 39 |
logger.error("Datos insuficientes para guardar el análisis")
|
| 40 |
return False
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
else:
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
-
logger.
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
#
|
| 54 |
analysis_document = {
|
| 55 |
'username': username,
|
| 56 |
-
'timestamp': datetime.now(timezone.utc),
|
| 57 |
-
'text': text,
|
| 58 |
-
'
|
|
|
|
| 59 |
'key_concepts': analysis_result.get('key_concepts', []),
|
| 60 |
-
'
|
| 61 |
-
'concept_graph':
|
| 62 |
-
'language': lang_code # Usamos el parámetro directamente
|
| 63 |
}
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
-
logger.error(f"Error
|
| 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 |
####################################################################################
|