Update modules/database/discourse_mongo_db.py
Browse files
modules/database/discourse_mongo_db.py
CHANGED
|
@@ -21,120 +21,6 @@ logger = logging.getLogger(__name__)
|
|
| 21 |
COLLECTION_NAME = 'student_discourse_analysis'
|
| 22 |
|
| 23 |
########################################################################
|
| 24 |
-
def store_student_discourse_result(username, text1, text2, analysis_result):
|
| 25 |
-
"""
|
| 26 |
-
Guarda el resultado del análisis de discurso comparativo en MongoDB.
|
| 27 |
-
"""
|
| 28 |
-
try:
|
| 29 |
-
# Los gráficos ya vienen en bytes, solo necesitamos codificar a base64
|
| 30 |
-
graph1_data = None
|
| 31 |
-
graph2_data = None
|
| 32 |
-
combined_graph_data = None
|
| 33 |
-
|
| 34 |
-
if 'graph1' in analysis_result and analysis_result['graph1'] is not None:
|
| 35 |
-
try:
|
| 36 |
-
graph1_data = base64.b64encode(analysis_result['graph1']).decode('utf-8')
|
| 37 |
-
except Exception as e:
|
| 38 |
-
logger.error(f"Error al codificar gráfico 1: {str(e)}")
|
| 39 |
-
|
| 40 |
-
if 'graph2' in analysis_result and analysis_result['graph2'] is not None:
|
| 41 |
-
try:
|
| 42 |
-
graph2_data = base64.b64encode(analysis_result['graph2']).decode('utf-8')
|
| 43 |
-
except Exception as e:
|
| 44 |
-
logger.error(f"Error al codificar gráfico 2: {str(e)}")
|
| 45 |
-
|
| 46 |
-
if 'combined_graph' in analysis_result and analysis_result['combined_graph'] is not None:
|
| 47 |
-
try:
|
| 48 |
-
combined_graph_data = base64.b64encode(analysis_result['combined_graph']).decode('utf-8')
|
| 49 |
-
except Exception as e:
|
| 50 |
-
logger.error(f"Error al codificar gráfico combinado: {str(e)}")
|
| 51 |
-
|
| 52 |
-
# Crear documento para MongoDB
|
| 53 |
-
analysis_document = {
|
| 54 |
-
'username': username,
|
| 55 |
-
'timestamp': datetime.now(timezone.utc).isoformat(),
|
| 56 |
-
'text1': text1,
|
| 57 |
-
'text2': text2,
|
| 58 |
-
'analysis_type': 'discourse',
|
| 59 |
-
'key_concepts1': analysis_result.get('key_concepts1', []),
|
| 60 |
-
'key_concepts2': analysis_result.get('key_concepts2', []),
|
| 61 |
-
'graph1': graph1_data,
|
| 62 |
-
'graph2': graph2_data,
|
| 63 |
-
'combined_graph': combined_graph_data
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
# Insertar en MongoDB
|
| 67 |
-
result = insert_document(COLLECTION_NAME, analysis_document)
|
| 68 |
-
if result:
|
| 69 |
-
logger.info(f"Análisis del discurso guardado con ID: {result} para el usuario: {username}")
|
| 70 |
-
return True
|
| 71 |
-
|
| 72 |
-
logger.error("No se pudo insertar el documento en MongoDB")
|
| 73 |
-
return False
|
| 74 |
-
|
| 75 |
-
except Exception as e:
|
| 76 |
-
logger.error(f"Error al guardar el análisis del discurso: {str(e)}")
|
| 77 |
-
return False
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
#################################################################################
|
| 82 |
-
|
| 83 |
-
# Corrección 1: Actualizar get_student_discourse_analysis para recuperar todos los campos necesarios
|
| 84 |
-
|
| 85 |
-
def get_student_discourse_analysis(username, limit=10):
|
| 86 |
-
"""
|
| 87 |
-
Recupera los análisis del discurso de un estudiante, incluyendo todos los gráficos y conceptos.
|
| 88 |
-
"""
|
| 89 |
-
try:
|
| 90 |
-
# Obtener la colección
|
| 91 |
-
collection = get_collection(COLLECTION_NAME)
|
| 92 |
-
if collection is None:
|
| 93 |
-
logger.error("No se pudo obtener la colección discourse")
|
| 94 |
-
return []
|
| 95 |
-
|
| 96 |
-
# Consulta
|
| 97 |
-
query = {
|
| 98 |
-
"username": username,
|
| 99 |
-
"analysis_type": "discourse"
|
| 100 |
-
}
|
| 101 |
-
|
| 102 |
-
# Eliminar la proyección para recuperar todos los campos
|
| 103 |
-
# No usar projection para obtener TODOS los campos
|
| 104 |
-
|
| 105 |
-
# Ejecutar consulta
|
| 106 |
-
try:
|
| 107 |
-
cursor = collection.find(query).sort("timestamp", -1)
|
| 108 |
-
if limit:
|
| 109 |
-
cursor = cursor.limit(limit)
|
| 110 |
-
|
| 111 |
-
# Convertir cursor a lista
|
| 112 |
-
results = list(cursor)
|
| 113 |
-
logger.info(f"Recuperados {len(results)} análisis del discurso para {username}")
|
| 114 |
-
|
| 115 |
-
# Verificar qué campos contienen los resultados para depuración
|
| 116 |
-
if results:
|
| 117 |
-
for result in results:
|
| 118 |
-
logger.info(f"Campos disponibles: {list(result.keys())}")
|
| 119 |
-
if 'graph1' in result:
|
| 120 |
-
logger.info(f"Tipo de graph1: {type(result['graph1'])}")
|
| 121 |
-
if 'graph2' in result:
|
| 122 |
-
logger.info(f"Tipo de graph2: {type(result['graph2'])}")
|
| 123 |
-
if 'combined_graph' in result:
|
| 124 |
-
logger.info(f"Tipo de combined_graph: {type(result['combined_graph'])}")
|
| 125 |
-
|
| 126 |
-
return results
|
| 127 |
-
|
| 128 |
-
except Exception as db_error:
|
| 129 |
-
logger.error(f"Error en la consulta a MongoDB: {str(db_error)}")
|
| 130 |
-
return []
|
| 131 |
-
|
| 132 |
-
except Exception as e:
|
| 133 |
-
logger.error(f"Error recuperando análisis del discurso: {str(e)}")
|
| 134 |
-
return []
|
| 135 |
-
|
| 136 |
-
# Corrección 2: Mejorar store_student_discourse_result para manejar varios tipos de gráficos
|
| 137 |
-
|
| 138 |
def store_student_discourse_result(username, text1, text2, analysis_result):
|
| 139 |
"""
|
| 140 |
Guarda el resultado del análisis de discurso comparativo en MongoDB.
|
|
@@ -255,6 +141,61 @@ def store_student_discourse_result(username, text1, text2, analysis_result):
|
|
| 255 |
logger.error(f"Error al guardar el análisis del discurso: {str(e)}")
|
| 256 |
return False
|
| 257 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
#####################################################################################
|
| 259 |
|
| 260 |
def get_student_discourse_data(username):
|
|
|
|
| 21 |
COLLECTION_NAME = 'student_discourse_analysis'
|
| 22 |
|
| 23 |
########################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def store_student_discourse_result(username, text1, text2, analysis_result):
|
| 25 |
"""
|
| 26 |
Guarda el resultado del análisis de discurso comparativo en MongoDB.
|
|
|
|
| 141 |
logger.error(f"Error al guardar el análisis del discurso: {str(e)}")
|
| 142 |
return False
|
| 143 |
|
| 144 |
+
#################################################################################
|
| 145 |
+
|
| 146 |
+
# Corrección 1: Actualizar get_student_discourse_analysis para recuperar todos los campos necesarios
|
| 147 |
+
|
| 148 |
+
def get_student_discourse_analysis(username, limit=10):
|
| 149 |
+
"""
|
| 150 |
+
Recupera los análisis del discurso de un estudiante, incluyendo todos los gráficos y conceptos.
|
| 151 |
+
"""
|
| 152 |
+
try:
|
| 153 |
+
# Obtener la colección
|
| 154 |
+
collection = get_collection(COLLECTION_NAME)
|
| 155 |
+
if collection is None:
|
| 156 |
+
logger.error("No se pudo obtener la colección discourse")
|
| 157 |
+
return []
|
| 158 |
+
|
| 159 |
+
# Consulta
|
| 160 |
+
query = {
|
| 161 |
+
"username": username,
|
| 162 |
+
"analysis_type": "discourse"
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
# Eliminar la proyección para recuperar todos los campos
|
| 166 |
+
# No usar projection para obtener TODOS los campos
|
| 167 |
+
|
| 168 |
+
# Ejecutar consulta
|
| 169 |
+
try:
|
| 170 |
+
cursor = collection.find(query).sort("timestamp", -1)
|
| 171 |
+
if limit:
|
| 172 |
+
cursor = cursor.limit(limit)
|
| 173 |
+
|
| 174 |
+
# Convertir cursor a lista
|
| 175 |
+
results = list(cursor)
|
| 176 |
+
logger.info(f"Recuperados {len(results)} análisis del discurso para {username}")
|
| 177 |
+
|
| 178 |
+
# Verificar qué campos contienen los resultados para depuración
|
| 179 |
+
if results:
|
| 180 |
+
for result in results:
|
| 181 |
+
logger.info(f"Campos disponibles: {list(result.keys())}")
|
| 182 |
+
if 'graph1' in result:
|
| 183 |
+
logger.info(f"Tipo de graph1: {type(result['graph1'])}")
|
| 184 |
+
if 'graph2' in result:
|
| 185 |
+
logger.info(f"Tipo de graph2: {type(result['graph2'])}")
|
| 186 |
+
if 'combined_graph' in result:
|
| 187 |
+
logger.info(f"Tipo de combined_graph: {type(result['combined_graph'])}")
|
| 188 |
+
|
| 189 |
+
return results
|
| 190 |
+
|
| 191 |
+
except Exception as db_error:
|
| 192 |
+
logger.error(f"Error en la consulta a MongoDB: {str(db_error)}")
|
| 193 |
+
return []
|
| 194 |
+
|
| 195 |
+
except Exception as e:
|
| 196 |
+
logger.error(f"Error recuperando análisis del discurso: {str(e)}")
|
| 197 |
+
return []
|
| 198 |
+
|
| 199 |
#####################################################################################
|
| 200 |
|
| 201 |
def get_student_discourse_data(username):
|