File size: 6,125 Bytes
d07932c 64f3506 a8643a1 64f3506 d5968bb 64f3506 5778534 64f3506 6d577d6 5778534 6d577d6 5778534 858fd40 b94c8a0 858fd40 b94c8a0 858fd40 b94c8a0 858fd40 b94c8a0 858fd40 b94c8a0 858fd40 b94c8a0 d07932c 5778534 6d577d6 d07932c 858fd40 5778534 d07932c 5778534 858fd40 3022cb9 858fd40 3022cb9 5778534 6d577d6 5778534 858fd40 97ad90b 28f3afb 5778534 d07932c 5778534 d07932c 97ad90b f534eef 97ad90b 7d3bc93 f534eef 97ad90b d07932c f534eef 97ad90b f534eef 97ad90b 7d3bc93 97ad90b f534eef d07932c f534eef d07932c 97ad90b 7d3bc93 d07932c 5778534 d07932c 5778534 d07932c 5778534 64f3506 d07932c 5778534 d07932c 5778534 d07932c 5778534 64f3506 d07932c 5778534 d07932c 5778534 d07932c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# modules/database/discourse_mongo_db.py
# Importaciones estándar
import io
import base64
from datetime import datetime, timezone
import logging
# Importaciones de terceros
import matplotlib.pyplot as plt
from .mongo_db import (
get_collection,
insert_document,
find_documents,
update_document,
delete_document
)
# Configuración del logger
logger = logging.getLogger(__name__)
COLLECTION_NAME = 'student_discourse_analysis'
########################################################################
def store_student_discourse_result(username, text1, text2, analysis_result):
"""
Guarda el resultado del análisis de discurso comparativo en MongoDB.
"""
try:
# Los gráficos ya vienen en bytes, solo necesitamos codificar a base64
graph1_data = None
graph2_data = None
combined_graph_data = None
if 'graph1' in analysis_result and analysis_result['graph1'] is not None:
try:
graph1_data = base64.b64encode(analysis_result['graph1']).decode('utf-8')
except Exception as e:
logger.error(f"Error al codificar gráfico 1: {str(e)}")
if 'graph2' in analysis_result and analysis_result['graph2'] is not None:
try:
graph2_data = base64.b64encode(analysis_result['graph2']).decode('utf-8')
except Exception as e:
logger.error(f"Error al codificar gráfico 2: {str(e)}")
if 'combined_graph' in analysis_result and analysis_result['combined_graph'] is not None:
try:
combined_graph_data = base64.b64encode(analysis_result['combined_graph']).decode('utf-8')
except Exception as e:
logger.error(f"Error al codificar gráfico combinado: {str(e)}")
# Crear documento para MongoDB
analysis_document = {
'username': username,
'timestamp': datetime.now(timezone.utc).isoformat(),
'text1': text1,
'text2': text2,
'analysis_type': 'discourse',
'key_concepts1': analysis_result.get('key_concepts1', []),
'key_concepts2': analysis_result.get('key_concepts2', []),
'graph1': graph1_data,
'graph2': graph2_data,
'combined_graph': combined_graph_data
}
# Insertar en MongoDB
result = insert_document(COLLECTION_NAME, analysis_document)
if result:
logger.info(f"Análisis del discurso guardado con ID: {result} para el usuario: {username}")
return True
logger.error("No se pudo insertar el documento en MongoDB")
return False
except Exception as e:
logger.error(f"Error al guardar el análisis del discurso: {str(e)}")
return False
#################################################################################
def get_student_discourse_analysis(username, limit=10):
"""
Recupera los análisis del discurso de un estudiante.
"""
try:
# Obtener la colección
collection = get_collection(COLLECTION_NAME)
if collection is None: # Cambiado de if not collection a if collection is None
logger.error("No se pudo obtener la colección discourse")
return []
# Consulta
query = {
"username": username,
"analysis_type": "discourse"
}
# Campos a recuperar
projection = {
"timestamp": 1,
"combined_graph": 1,
"_id": 1
}
# Ejecutar consulta
try:
cursor = collection.find(query, projection).sort("timestamp", -1)
if limit:
cursor = cursor.limit(limit)
# Convertir cursor a lista
results = list(cursor)
logger.info(f"Recuperados {len(results)} análisis del discurso para {username}")
return results
except Exception as db_error:
logger.error(f"Error en la consulta a MongoDB: {str(db_error)}")
return []
except Exception as e:
logger.error(f"Error recuperando análisis del discurso: {str(e)}")
return []
#####################################################################################
def get_student_discourse_data(username):
"""
Obtiene un resumen de los análisis del discurso de un estudiante.
"""
try:
analyses = get_student_discourse_analysis(username, limit=None)
formatted_analyses = []
for analysis in analyses:
formatted_analysis = {
'timestamp': analysis['timestamp'],
'text1': analysis.get('text1', ''),
'text2': analysis.get('text2', ''),
'key_concepts1': analysis.get('key_concepts1', []),
'key_concepts2': analysis.get('key_concepts2', [])
}
formatted_analyses.append(formatted_analysis)
return {'entries': formatted_analyses}
except Exception as e:
logger.error(f"Error al obtener datos del discurso: {str(e)}")
return {'entries': []}
###########################################################################
def update_student_discourse_analysis(analysis_id, update_data):
"""
Actualiza un análisis del discurso existente.
"""
try:
query = {"_id": analysis_id}
update = {"$set": update_data}
return update_document(COLLECTION_NAME, query, update)
except Exception as e:
logger.error(f"Error al actualizar análisis del discurso: {str(e)}")
return False
###########################################################################
def delete_student_discourse_analysis(analysis_id):
"""
Elimina un análisis del discurso.
"""
try:
query = {"_id": analysis_id}
return delete_document(COLLECTION_NAME, query)
except Exception as e:
logger.error(f"Error al eliminar análisis del discurso: {str(e)}")
return False |