Update modules/studentact/student_activities.py
Browse files- modules/studentact/student_activities.py +111 -110
modules/studentact/student_activities.py
CHANGED
|
@@ -1,111 +1,112 @@
|
|
| 1 |
-
#modules/studentact/student_activities.py
|
| 2 |
-
|
| 3 |
-
import streamlit as st
|
| 4 |
-
import pandas as pd
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
-
import seaborn as sns
|
| 7 |
-
import base64
|
| 8 |
-
from io import BytesIO
|
| 9 |
-
from reportlab.pdfgen import canvas
|
| 10 |
-
from reportlab.lib.pagesizes import letter
|
| 11 |
-
from docx import Document
|
| 12 |
-
from odf.opendocument import OpenDocumentText
|
| 13 |
-
from odf.text import P
|
| 14 |
-
from datetime import datetime, timedelta
|
| 15 |
-
import pytz
|
| 16 |
-
import logging
|
| 17 |
-
|
| 18 |
-
# Configuración de logging
|
| 19 |
-
logging.basicConfig(level=logging.DEBUG)
|
| 20 |
-
logger = logging.getLogger(__name__)
|
| 21 |
-
|
| 22 |
-
# Importaciones locales
|
| 23 |
-
try:
|
| 24 |
-
from ..database.
|
| 25 |
-
from ..database.
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
st.
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
ax.
|
| 63 |
-
ax.
|
| 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 |
-
st.
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
| 111 |
logger.debug("Finalizando display_student_progress")
|
|
|
|
| 1 |
+
#modules/studentact/student_activities.py
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import seaborn as sns
|
| 7 |
+
import base64
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
from reportlab.pdfgen import canvas
|
| 10 |
+
from reportlab.lib.pagesizes import letter
|
| 11 |
+
from docx import Document
|
| 12 |
+
from odf.opendocument import OpenDocumentText
|
| 13 |
+
from odf.text import P
|
| 14 |
+
from datetime import datetime, timedelta
|
| 15 |
+
import pytz
|
| 16 |
+
import logging
|
| 17 |
+
|
| 18 |
+
# Configuración de logging
|
| 19 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 20 |
+
logger = logging.getLogger(__name__)
|
| 21 |
+
|
| 22 |
+
# Importaciones locales
|
| 23 |
+
try:
|
| 24 |
+
from ..database.semantic_mongo_db import get_student_semantic_data
|
| 25 |
+
from ..database.discourse_mongo_db import get_student_discourse_data
|
| 26 |
+
|
| 27 |
+
from ..database.chat_mongo_db import get_chat_history
|
| 28 |
+
|
| 29 |
+
logger.info("Importaciones locales exitosas")
|
| 30 |
+
except ImportError as e:
|
| 31 |
+
logger.error(f"Error en las importaciones locales: {e}")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
####################################################################
|
| 35 |
+
def display_student_progress(username, lang_code, t):
|
| 36 |
+
logger.debug(f"Iniciando display_student_progress para {username}")
|
| 37 |
+
|
| 38 |
+
st.title(f"{t.get('progress_of', 'Progreso de')} {username}")
|
| 39 |
+
|
| 40 |
+
# Obtener los datos del estudiante
|
| 41 |
+
student_data = get_student_morphosyntax_data(username)
|
| 42 |
+
|
| 43 |
+
if not student_data or len(student_data.get('entries', [])) == 0:
|
| 44 |
+
logger.warning(f"No se encontraron datos para el estudiante {username}")
|
| 45 |
+
st.warning(t.get("no_data_warning", "No se encontraron datos para este estudiante."))
|
| 46 |
+
st.info(t.get("try_analysis", "Intenta realizar algunos análisis de texto primero."))
|
| 47 |
+
return
|
| 48 |
+
|
| 49 |
+
logger.debug(f"Datos del estudiante obtenidos: {len(student_data['entries'])} entradas")
|
| 50 |
+
|
| 51 |
+
# Resumen de actividades
|
| 52 |
+
with st.expander(t.get("activities_summary", "Resumen de Actividades"), expanded=True):
|
| 53 |
+
total_entries = len(student_data['entries'])
|
| 54 |
+
st.write(f"{t.get('total_analyses', 'Total de análisis realizados')}: {total_entries}")
|
| 55 |
+
|
| 56 |
+
# Gráfico de tipos de análisis
|
| 57 |
+
try:
|
| 58 |
+
analysis_types = [entry.get('analysis_type', 'unknown') for entry in student_data['entries']]
|
| 59 |
+
analysis_counts = pd.Series(analysis_types).value_counts()
|
| 60 |
+
fig, ax = plt.subplots()
|
| 61 |
+
sns.barplot(x=analysis_counts.index, y=analysis_counts.values, ax=ax)
|
| 62 |
+
ax.set_title(t.get("analysis_types_chart", "Tipos de análisis realizados"))
|
| 63 |
+
ax.set_xlabel(t.get("analysis_type", "Tipo de análisis"))
|
| 64 |
+
ax.set_ylabel(t.get("count", "Cantidad"))
|
| 65 |
+
st.pyplot(fig)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
logger.error(f"Error al crear el gráfico: {e}")
|
| 68 |
+
st.error("No se pudo crear el gráfico de tipos de análisis.")
|
| 69 |
+
|
| 70 |
+
# Función para generar el contenido del archivo de actividades de las últimas 48 horas
|
| 71 |
+
def generate_activity_content_48h():
|
| 72 |
+
content = f"Actividades de {username} en las últimas 48 horas\n\n"
|
| 73 |
+
|
| 74 |
+
two_days_ago = datetime.now(pytz.utc) - timedelta(days=2)
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
morphosyntax_analyses = get_student_morphosyntax_data(username)
|
| 78 |
+
recent_morphosyntax = [a for a in morphosyntax_analyses if datetime.fromisoformat(a['timestamp']) > two_days_ago]
|
| 79 |
+
|
| 80 |
+
content += f"Análisis morfosintácticos: {len(recent_morphosyntax)}\n"
|
| 81 |
+
for analysis in recent_morphosyntax:
|
| 82 |
+
content += f"- Análisis del {analysis['timestamp']}: {analysis['text'][:50]}...\n"
|
| 83 |
+
|
| 84 |
+
chat_history = get_chat_history(username, None)
|
| 85 |
+
recent_chats = [c for c in chat_history if datetime.fromisoformat(c['timestamp']) > two_days_ago]
|
| 86 |
+
|
| 87 |
+
content += f"\nConversaciones de chat: {len(recent_chats)}\n"
|
| 88 |
+
for chat in recent_chats:
|
| 89 |
+
content += f"- Chat del {chat['timestamp']}: {len(chat['messages'])} mensajes\n"
|
| 90 |
+
except Exception as e:
|
| 91 |
+
logger.error(f"Error al generar el contenido de actividades: {e}")
|
| 92 |
+
content += "Error al recuperar los datos de actividades.\n"
|
| 93 |
+
|
| 94 |
+
return content
|
| 95 |
+
|
| 96 |
+
# Botones para descargar el histórico de actividades de las últimas 48 horas
|
| 97 |
+
st.subheader(t.get("download_history_48h", "Descargar Histórico de Actividades (Últimas 48 horas)"))
|
| 98 |
+
if st.button("Generar reporte de 48 horas"):
|
| 99 |
+
try:
|
| 100 |
+
report_content = generate_activity_content_48h()
|
| 101 |
+
st.text_area("Reporte de 48 horas", report_content, height=300)
|
| 102 |
+
st.download_button(
|
| 103 |
+
label="Descargar TXT (48h)",
|
| 104 |
+
data=report_content,
|
| 105 |
+
file_name="actividades_48h.txt",
|
| 106 |
+
mime="text/plain"
|
| 107 |
+
)
|
| 108 |
+
except Exception as e:
|
| 109 |
+
logger.error(f"Error al generar el reporte: {e}")
|
| 110 |
+
st.error("No se pudo generar el reporte. Por favor, verifica los logs para más detalles.")
|
| 111 |
+
|
| 112 |
logger.debug("Finalizando display_student_progress")
|