Update modules/database/discourse_mongo_db.py
Browse files
modules/database/discourse_mongo_db.py
CHANGED
|
@@ -1,23 +1,11 @@
|
|
| 1 |
# modules/database/discourse_mongo_db.py
|
| 2 |
-
# Importaciones estándar
|
| 3 |
-
import io
|
| 4 |
import base64
|
| 5 |
-
from datetime import datetime, timezone
|
| 6 |
import logging
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Importaciones de terceros
|
| 9 |
-
import matplotlib.pyplot as plt
|
| 10 |
-
|
| 11 |
-
from .mongo_db import (
|
| 12 |
-
get_collection,
|
| 13 |
-
insert_document,
|
| 14 |
-
find_documents,
|
| 15 |
-
update_document,
|
| 16 |
-
delete_document
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
# Configuración del logger
|
| 20 |
logger = logging.getLogger(__name__)
|
|
|
|
| 21 |
COLLECTION_NAME = 'student_discourse_analysis'
|
| 22 |
|
| 23 |
########################################################################
|
|
@@ -27,11 +15,13 @@ def store_student_discourse_result(username, text1, text2, analysis_result):
|
|
| 27 |
Guarda el resultado del análisis de discurso en MongoDB.
|
| 28 |
"""
|
| 29 |
try:
|
| 30 |
-
# Verificar que el resultado
|
| 31 |
if not analysis_result.get('success', False):
|
| 32 |
logger.error("No se puede guardar un análisis fallido")
|
| 33 |
return False
|
| 34 |
|
|
|
|
|
|
|
| 35 |
# Preparar el documento para MongoDB
|
| 36 |
document = {
|
| 37 |
'username': username,
|
|
@@ -42,19 +32,20 @@ def store_student_discourse_result(username, text1, text2, analysis_result):
|
|
| 42 |
'key_concepts2': analysis_result.get('key_concepts2', [])
|
| 43 |
}
|
| 44 |
|
| 45 |
-
# Codificar gráficos a base64
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
if not collection:
|
| 59 |
logger.error("No se pudo obtener la colección")
|
| 60 |
return False
|
|
@@ -76,26 +67,29 @@ def get_student_discourse_analysis(username, limit=10):
|
|
| 76 |
Recupera los análisis del discurso de un estudiante.
|
| 77 |
"""
|
| 78 |
try:
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
if not collection:
|
| 81 |
logger.error("No se pudo obtener la colección")
|
| 82 |
return []
|
| 83 |
|
| 84 |
query = {"username": username}
|
| 85 |
documents = list(collection.find(query).sort("timestamp", -1).limit(limit))
|
|
|
|
| 86 |
|
| 87 |
-
# Decodificar gráficos
|
| 88 |
for doc in documents:
|
| 89 |
-
|
| 90 |
-
if
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
return documents
|
| 100 |
|
| 101 |
except Exception as e:
|
|
|
|
| 1 |
# modules/database/discourse_mongo_db.py
|
|
|
|
|
|
|
| 2 |
import base64
|
|
|
|
| 3 |
import logging
|
| 4 |
+
from datetime import datetime, timezone
|
| 5 |
+
from ..database.mongo_db import get_collection, insert_document, find_documents
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
+
|
| 9 |
COLLECTION_NAME = 'student_discourse_analysis'
|
| 10 |
|
| 11 |
########################################################################
|
|
|
|
| 15 |
Guarda el resultado del análisis de discurso en MongoDB.
|
| 16 |
"""
|
| 17 |
try:
|
| 18 |
+
# Verificar que el resultado sea válido
|
| 19 |
if not analysis_result.get('success', False):
|
| 20 |
logger.error("No se puede guardar un análisis fallido")
|
| 21 |
return False
|
| 22 |
|
| 23 |
+
logger.info(f"Almacenando análisis de discurso para {username}")
|
| 24 |
+
|
| 25 |
# Preparar el documento para MongoDB
|
| 26 |
document = {
|
| 27 |
'username': username,
|
|
|
|
| 32 |
'key_concepts2': analysis_result.get('key_concepts2', [])
|
| 33 |
}
|
| 34 |
|
| 35 |
+
# Codificar gráficos a base64 para almacenamiento
|
| 36 |
+
for graph_key in ['graph1', 'graph2', 'combined_graph']:
|
| 37 |
+
if graph_key in analysis_result and analysis_result[graph_key] is not None:
|
| 38 |
+
if isinstance(analysis_result[graph_key], bytes):
|
| 39 |
+
logger.info(f"Codificando {graph_key} como base64")
|
| 40 |
+
document[graph_key] = base64.b64encode(analysis_result[graph_key]).decode('utf-8')
|
| 41 |
+
logger.info(f"{graph_key} codificado correctamente, longitud: {len(document[graph_key])}")
|
| 42 |
+
else:
|
| 43 |
+
logger.warning(f"{graph_key} no es de tipo bytes, es: {type(analysis_result[graph_key])}")
|
| 44 |
+
else:
|
| 45 |
+
logger.info(f"{graph_key} no presente en el resultado del análisis")
|
| 46 |
+
|
| 47 |
+
# Almacenar el documento en MongoDB
|
| 48 |
+
collection = get_collection(COLLECTION_NAME)
|
| 49 |
if not collection:
|
| 50 |
logger.error("No se pudo obtener la colección")
|
| 51 |
return False
|
|
|
|
| 67 |
Recupera los análisis del discurso de un estudiante.
|
| 68 |
"""
|
| 69 |
try:
|
| 70 |
+
logger.info(f"Recuperando análisis de discurso para {username}")
|
| 71 |
+
|
| 72 |
+
collection = get_collection(COLLECTION_NAME)
|
| 73 |
if not collection:
|
| 74 |
logger.error("No se pudo obtener la colección")
|
| 75 |
return []
|
| 76 |
|
| 77 |
query = {"username": username}
|
| 78 |
documents = list(collection.find(query).sort("timestamp", -1).limit(limit))
|
| 79 |
+
logger.info(f"Recuperados {len(documents)} documentos")
|
| 80 |
|
| 81 |
+
# Decodificar gráficos para uso en la aplicación
|
| 82 |
for doc in documents:
|
| 83 |
+
for graph_key in ['graph1', 'graph2', 'combined_graph']:
|
| 84 |
+
if graph_key in doc and doc[graph_key]:
|
| 85 |
+
try:
|
| 86 |
+
logger.info(f"Decodificando {graph_key} de base64 a bytes")
|
| 87 |
+
doc[graph_key] = base64.b64decode(doc[graph_key])
|
| 88 |
+
logger.info(f"{graph_key} decodificado correctamente")
|
| 89 |
+
except Exception as decode_error:
|
| 90 |
+
logger.error(f"Error decodificando {graph_key}: {str(decode_error)}")
|
| 91 |
+
doc[graph_key] = None
|
| 92 |
+
|
| 93 |
return documents
|
| 94 |
|
| 95 |
except Exception as e:
|