Update modules/database/discourse_mongo_db.py
Browse files
modules/database/discourse_mongo_db.py
CHANGED
@@ -19,46 +19,33 @@ logger = logging.getLogger(__name__)
|
|
19 |
|
20 |
COLLECTION_NAME = 'student_discourse_analysis'
|
21 |
|
22 |
-
def fig_to_bytes(fig):
|
23 |
-
"""Convierte una figura de matplotlib a bytes."""
|
24 |
-
try:
|
25 |
-
buf = io.BytesIO()
|
26 |
-
fig.savefig(buf, format='png', dpi=300, bbox_inches='tight')
|
27 |
-
buf.seek(0)
|
28 |
-
return buf.getvalue()
|
29 |
-
except Exception as e:
|
30 |
-
logger.error(f"Error en fig_to_bytes: {str(e)}")
|
31 |
-
return None
|
32 |
-
|
33 |
-
|
34 |
-
#################################################################################
|
35 |
def store_student_discourse_result(username, text1, text2, analysis_result):
|
36 |
"""
|
37 |
Guarda el resultado del análisis de discurso comparativo en MongoDB.
|
38 |
"""
|
39 |
try:
|
40 |
-
#
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
try:
|
43 |
-
|
44 |
-
graph1_bytes = fig_to_bytes(analysis_result['graph1'])
|
45 |
-
if graph1_bytes:
|
46 |
-
analysis_result['graph1_bytes'] = graph1_bytes
|
47 |
-
analysis_result['graph1_base64'] = base64.b64encode(graph1_bytes).decode('utf-8')
|
48 |
-
plt.close(analysis_result['graph1']) # Cerrar la figura
|
49 |
except Exception as e:
|
50 |
-
logger.error(f"Error al
|
51 |
|
52 |
-
if '
|
53 |
try:
|
54 |
-
|
55 |
-
graph2_bytes = fig_to_bytes(analysis_result['graph2'])
|
56 |
-
if graph2_bytes:
|
57 |
-
analysis_result['graph2_bytes'] = graph2_bytes
|
58 |
-
analysis_result['graph2_base64'] = base64.b64encode(graph2_bytes).decode('utf-8')
|
59 |
-
plt.close(analysis_result['graph2']) # Cerrar la figura
|
60 |
except Exception as e:
|
61 |
-
logger.error(f"Error al
|
62 |
|
63 |
# Crear documento para MongoDB
|
64 |
analysis_document = {
|
@@ -69,23 +56,26 @@ def store_student_discourse_result(username, text1, text2, analysis_result):
|
|
69 |
'analysis_type': 'discourse',
|
70 |
'key_concepts1': analysis_result.get('key_concepts1', []),
|
71 |
'key_concepts2': analysis_result.get('key_concepts2', []),
|
72 |
-
'
|
73 |
-
'
|
|
|
74 |
}
|
75 |
|
76 |
# Insertar en MongoDB
|
77 |
result = insert_document(COLLECTION_NAME, analysis_document)
|
78 |
-
if
|
79 |
-
logger.
|
80 |
-
return
|
81 |
|
82 |
-
logger.
|
83 |
-
return
|
84 |
|
85 |
except Exception as e:
|
86 |
logger.error(f"Error al guardar el análisis del discurso: {str(e)}")
|
87 |
return False
|
88 |
|
|
|
|
|
89 |
#################################################################################
|
90 |
def get_student_discourse_analysis(username, limit=10):
|
91 |
"""
|
@@ -130,6 +120,10 @@ def get_student_discourse_analysis(username, limit=10):
|
|
130 |
logger.error(f"Error recuperando análisis del discurso: {str(e)}")
|
131 |
return []
|
132 |
#####################################################################################
|
|
|
|
|
|
|
|
|
133 |
|
134 |
def get_student_discourse_data(username):
|
135 |
"""
|
|
|
19 |
|
20 |
COLLECTION_NAME = 'student_discourse_analysis'
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def store_student_discourse_result(username, text1, text2, analysis_result):
|
23 |
"""
|
24 |
Guarda el resultado del análisis de discurso comparativo en MongoDB.
|
25 |
"""
|
26 |
try:
|
27 |
+
# Los gráficos ya vienen en bytes, solo necesitamos codificar a base64
|
28 |
+
graph1_data = None
|
29 |
+
graph2_data = None
|
30 |
+
combined_graph_data = None
|
31 |
+
|
32 |
+
if 'graph1' in analysis_result and analysis_result['graph1'] is not None:
|
33 |
+
try:
|
34 |
+
graph1_data = base64.b64encode(analysis_result['graph1']).decode('utf-8')
|
35 |
+
except Exception as e:
|
36 |
+
logger.error(f"Error al codificar gráfico 1: {str(e)}")
|
37 |
+
|
38 |
+
if 'graph2' in analysis_result and analysis_result['graph2'] is not None:
|
39 |
try:
|
40 |
+
graph2_data = base64.b64encode(analysis_result['graph2']).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
41 |
except Exception as e:
|
42 |
+
logger.error(f"Error al codificar gráfico 2: {str(e)}")
|
43 |
|
44 |
+
if 'combined_graph' in analysis_result and analysis_result['combined_graph'] is not None:
|
45 |
try:
|
46 |
+
combined_graph_data = base64.b64encode(analysis_result['combined_graph']).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
47 |
except Exception as e:
|
48 |
+
logger.error(f"Error al codificar gráfico combinado: {str(e)}")
|
49 |
|
50 |
# Crear documento para MongoDB
|
51 |
analysis_document = {
|
|
|
56 |
'analysis_type': 'discourse',
|
57 |
'key_concepts1': analysis_result.get('key_concepts1', []),
|
58 |
'key_concepts2': analysis_result.get('key_concepts2', []),
|
59 |
+
'graph1': graph1_data,
|
60 |
+
'graph2': graph2_data,
|
61 |
+
'combined_graph': combined_graph_data
|
62 |
}
|
63 |
|
64 |
# Insertar en MongoDB
|
65 |
result = insert_document(COLLECTION_NAME, analysis_document)
|
66 |
+
if result:
|
67 |
+
logger.info(f"Análisis del discurso guardado con ID: {result} para el usuario: {username}")
|
68 |
+
return True
|
69 |
|
70 |
+
logger.error("No se pudo insertar el documento en MongoDB")
|
71 |
+
return False
|
72 |
|
73 |
except Exception as e:
|
74 |
logger.error(f"Error al guardar el análisis del discurso: {str(e)}")
|
75 |
return False
|
76 |
|
77 |
+
|
78 |
+
|
79 |
#################################################################################
|
80 |
def get_student_discourse_analysis(username, limit=10):
|
81 |
"""
|
|
|
120 |
logger.error(f"Error recuperando análisis del discurso: {str(e)}")
|
121 |
return []
|
122 |
#####################################################################################
|
123 |
+
|
124 |
+
|
125 |
+
|
126 |
+
|
127 |
|
128 |
def get_student_discourse_data(username):
|
129 |
"""
|