Update modules/database/database.py
Browse files- modules/database/database.py +22 -54
modules/database/database.py
CHANGED
@@ -288,73 +288,41 @@ def store_semantic_result(username, text, analysis_result):
|
|
288 |
###############################################################################################################
|
289 |
|
290 |
def store_discourse_analysis_result(username, text1, text2, analysis_result):
|
|
|
|
|
|
|
|
|
291 |
try:
|
292 |
-
#
|
293 |
-
base_document = {
|
294 |
-
'username': username,
|
295 |
-
'timestamp': datetime.utcnow(),
|
296 |
-
'text1': text1,
|
297 |
-
'text2': text2,
|
298 |
-
'analysis_type': 'discourse'
|
299 |
-
}
|
300 |
-
|
301 |
-
result = analysis_collection.insert_one(base_document)
|
302 |
-
document_id = result.inserted_id
|
303 |
-
logger.info(f"Documento base guardado con ID: {document_id}")
|
304 |
-
|
305 |
-
# Guardar los conceptos clave
|
306 |
-
key_concepts1 = [(concept, float(frequency)) for concept, frequency in analysis_result['key_concepts1']]
|
307 |
-
key_concepts2 = [(concept, float(frequency)) for concept, frequency in analysis_result['key_concepts2']]
|
308 |
-
|
309 |
-
analysis_collection.update_one(
|
310 |
-
{'_id': document_id},
|
311 |
-
{'$set': {'key_concepts1': key_concepts1, 'key_concepts2': key_concepts2}}
|
312 |
-
)
|
313 |
-
logger.info("Conceptos clave guardados exitosamente")
|
314 |
-
|
315 |
-
# Guardar el primer grafo
|
316 |
buf1 = io.BytesIO()
|
317 |
analysis_result['graph1'].savefig(buf1, format='png')
|
318 |
buf1.seek(0)
|
319 |
img_str1 = base64.b64encode(buf1.getvalue()).decode('utf-8')
|
320 |
-
|
321 |
-
analysis_collection.update_one(
|
322 |
-
{'_id': document_id},
|
323 |
-
{'$set': {'graph1': img_str1}}
|
324 |
-
)
|
325 |
-
logger.info("Grafo 1 guardado exitosamente")
|
326 |
|
327 |
-
# Guardar el segundo grafo
|
328 |
buf2 = io.BytesIO()
|
329 |
analysis_result['graph2'].savefig(buf2, format='png')
|
330 |
buf2.seek(0)
|
331 |
img_str2 = base64.b64encode(buf2.getvalue()).decode('utf-8')
|
332 |
-
|
333 |
-
analysis_collection.update_one(
|
334 |
-
{'_id': document_id},
|
335 |
-
{'$set': {'graph2': img_str2}}
|
336 |
-
)
|
337 |
-
logger.info("Grafo 2 guardado exitosamente")
|
338 |
-
|
339 |
-
# Guardar el diagrama de Sankey
|
340 |
-
buf_sankey = io.BytesIO()
|
341 |
-
analysis_result['sankey_diagram'].write_image(buf_sankey, format='png')
|
342 |
-
buf_sankey.seek(0)
|
343 |
-
img_str_sankey = base64.b64encode(buf_sankey.getvalue()).decode('utf-8')
|
344 |
-
|
345 |
-
analysis_collection.update_one(
|
346 |
-
{'_id': document_id},
|
347 |
-
{'$set': {'sankey_diagram': img_str_sankey}}
|
348 |
-
)
|
349 |
-
logger.info("Diagrama Sankey guardado exitosamente")
|
350 |
|
351 |
-
|
352 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
353 |
|
|
|
|
|
|
|
|
|
354 |
except Exception as e:
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
return False
|
359 |
|
360 |
###############################################################################################################
|
|
|
288 |
###############################################################################################################
|
289 |
|
290 |
def store_discourse_analysis_result(username, text1, text2, analysis_result):
|
291 |
+
if analysis_collection is None:
|
292 |
+
print("La conexi贸n a MongoDB no est谩 inicializada")
|
293 |
+
return False
|
294 |
+
|
295 |
try:
|
296 |
+
# Convertir los grafos individuales a im谩genes base64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
297 |
buf1 = io.BytesIO()
|
298 |
analysis_result['graph1'].savefig(buf1, format='png')
|
299 |
buf1.seek(0)
|
300 |
img_str1 = base64.b64encode(buf1.getvalue()).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
301 |
|
|
|
302 |
buf2 = io.BytesIO()
|
303 |
analysis_result['graph2'].savefig(buf2, format='png')
|
304 |
buf2.seek(0)
|
305 |
img_str2 = base64.b64encode(buf2.getvalue()).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
306 |
|
307 |
+
# Crear el documento para guardar
|
308 |
+
analysis_document = {
|
309 |
+
'username': username,
|
310 |
+
'timestamp': datetime.utcnow(),
|
311 |
+
'text1': text1,
|
312 |
+
'text2': text2,
|
313 |
+
'graph1': img_str1,
|
314 |
+
'graph2': img_str2,
|
315 |
+
'analysis_type': 'discourse'
|
316 |
+
}
|
317 |
|
318 |
+
# Insertar el documento en la base de datos
|
319 |
+
result = analysis_collection.insert_one(analysis_document)
|
320 |
+
print(f"An谩lisis discursivo guardado con ID: {result.inserted_id} para el usuario: {username}")
|
321 |
+
return True
|
322 |
except Exception as e:
|
323 |
+
print(f"Error al guardar el an谩lisis discursivo para el usuario {username}: {str(e)}")
|
324 |
+
print(f"Tipo de excepci贸n: {type(e).__name__}")
|
325 |
+
print(f"Detalles de la excepci贸n: {e.args}")
|
326 |
return False
|
327 |
|
328 |
###############################################################################################################
|