Update modules/database/discourse_mongo_db.py
Browse files
modules/database/discourse_mongo_db.py
CHANGED
@@ -19,22 +19,48 @@ def store_student_discourse_result(username, text1, text2, analysis_result):
|
|
19 |
analysis_result: Resultado del análisis
|
20 |
"""
|
21 |
try:
|
22 |
-
# Convertir gráficos a
|
23 |
graph1_data = None
|
24 |
graph2_data = None
|
25 |
-
|
|
|
|
|
26 |
if 'graph1' in analysis_result:
|
27 |
buf = io.BytesIO()
|
28 |
analysis_result['graph1'].savefig(buf, format='png')
|
29 |
buf.seek(0)
|
30 |
graph1_data = base64.b64encode(buf.getvalue()).decode('utf-8')
|
31 |
-
|
|
|
32 |
if 'graph2' in analysis_result:
|
33 |
buf = io.BytesIO()
|
34 |
analysis_result['graph2'].savefig(buf, format='png')
|
35 |
buf.seek(0)
|
36 |
graph2_data = base64.b64encode(buf.getvalue()).decode('utf-8')
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
# Crear documento para MongoDB
|
39 |
analysis_document = {
|
40 |
'username': username,
|
@@ -45,19 +71,19 @@ def store_student_discourse_result(username, text1, text2, analysis_result):
|
|
45 |
'key_concepts1': analysis_result.get('key_concepts1', []),
|
46 |
'key_concepts2': analysis_result.get('key_concepts2', []),
|
47 |
'graph1': graph1_data,
|
48 |
-
'graph2': graph2_data
|
|
|
49 |
}
|
50 |
|
51 |
# Insertar en MongoDB
|
52 |
result = insert_document(COLLECTION_NAME, analysis_document)
|
53 |
-
|
54 |
if result:
|
55 |
logger.info(f"Análisis del discurso guardado con ID: {result} para el usuario: {username}")
|
56 |
return True
|
57 |
-
|
58 |
logger.error("No se pudo insertar el documento en MongoDB")
|
59 |
return False
|
60 |
-
|
61 |
except Exception as e:
|
62 |
logger.error(f"Error al guardar el análisis del discurso: {str(e)}")
|
63 |
return False
|
|
|
19 |
analysis_result: Resultado del análisis
|
20 |
"""
|
21 |
try:
|
22 |
+
# Convertir gráficos individuales a base64
|
23 |
graph1_data = None
|
24 |
graph2_data = None
|
25 |
+
combined_graph_data = None
|
26 |
+
|
27 |
+
# Convertir primer gráfico
|
28 |
if 'graph1' in analysis_result:
|
29 |
buf = io.BytesIO()
|
30 |
analysis_result['graph1'].savefig(buf, format='png')
|
31 |
buf.seek(0)
|
32 |
graph1_data = base64.b64encode(buf.getvalue()).decode('utf-8')
|
33 |
+
|
34 |
+
# Convertir segundo gráfico
|
35 |
if 'graph2' in analysis_result:
|
36 |
buf = io.BytesIO()
|
37 |
analysis_result['graph2'].savefig(buf, format='png')
|
38 |
buf.seek(0)
|
39 |
graph2_data = base64.b64encode(buf.getvalue()).decode('utf-8')
|
40 |
|
41 |
+
# Crear gráfico combinado
|
42 |
+
if graph1_data and graph2_data:
|
43 |
+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 10))
|
44 |
+
|
45 |
+
# Convertir base64 a imagen para el gráfico combinado
|
46 |
+
img1 = plt.imread(io.BytesIO(base64.b64decode(graph1_data)))
|
47 |
+
img2 = plt.imread(io.BytesIO(base64.b64decode(graph2_data)))
|
48 |
+
|
49 |
+
ax1.imshow(img1)
|
50 |
+
ax1.axis('off')
|
51 |
+
ax1.set_title("Documento 1: Relaciones Conceptuales")
|
52 |
+
|
53 |
+
ax2.imshow(img2)
|
54 |
+
ax2.axis('off')
|
55 |
+
ax2.set_title("Documento 2: Relaciones Conceptuales")
|
56 |
+
|
57 |
+
# Guardar gráfico combinado en base64
|
58 |
+
buf = io.BytesIO()
|
59 |
+
fig.savefig(buf, format='png')
|
60 |
+
buf.seek(0)
|
61 |
+
combined_graph_data = base64.b64encode(buf.getvalue()).decode('utf-8')
|
62 |
+
plt.close(fig)
|
63 |
+
|
64 |
# Crear documento para MongoDB
|
65 |
analysis_document = {
|
66 |
'username': username,
|
|
|
71 |
'key_concepts1': analysis_result.get('key_concepts1', []),
|
72 |
'key_concepts2': analysis_result.get('key_concepts2', []),
|
73 |
'graph1': graph1_data,
|
74 |
+
'graph2': graph2_data,
|
75 |
+
'combined_graph': combined_graph_data
|
76 |
}
|
77 |
|
78 |
# Insertar en MongoDB
|
79 |
result = insert_document(COLLECTION_NAME, analysis_document)
|
|
|
80 |
if result:
|
81 |
logger.info(f"Análisis del discurso guardado con ID: {result} para el usuario: {username}")
|
82 |
return True
|
83 |
+
|
84 |
logger.error("No se pudo insertar el documento en MongoDB")
|
85 |
return False
|
86 |
+
|
87 |
except Exception as e:
|
88 |
logger.error(f"Error al guardar el análisis del discurso: {str(e)}")
|
89 |
return False
|