|
|
|
|
|
import streamlit as st |
|
from datetime import datetime |
|
from ..database.sql_db import ( |
|
get_user, |
|
get_student_user, |
|
get_admin_user, |
|
get_teacher_user, |
|
create_student_user, |
|
update_student_user, |
|
delete_student_user, |
|
record_login, |
|
record_logout, |
|
get_recent_sessions, |
|
get_user_total_time |
|
) |
|
from ..database.morphosintax_mongo_db import get_student_morphosyntax_analysis |
|
|
|
def format_duration(seconds): |
|
"""Convierte segundos a formato legible""" |
|
if not seconds: |
|
return "0h 0m" |
|
hours = seconds // 3600 |
|
minutes = (seconds % 3600) // 60 |
|
return f"{hours}h {minutes}m" |
|
|
|
def admin_page(): |
|
st.title("Panel de Administraci贸n") |
|
st.write(f"Bienvenido, {st.session_state.username}") |
|
|
|
|
|
tab1, tab2, tab3 = st.tabs([ |
|
"Gesti贸n de Usuarios", |
|
"B煤squeda de Usuarios", |
|
"Actividad de la Plataforma" |
|
]) |
|
|
|
|
|
with tab1: |
|
st.header("Crear Nuevo Usuario Estudiante") |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
|
with col1: |
|
new_username = st.text_input( |
|
"Correo electr贸nico del nuevo usuario", |
|
key="admin_new_username" |
|
) |
|
|
|
with col2: |
|
new_password = st.text_input( |
|
"Contrase帽a", |
|
type="password", |
|
key="admin_new_password" |
|
) |
|
|
|
if st.button("Crear Usuario", key="admin_create_user", type="primary"): |
|
if create_student_user(new_username, new_password): |
|
st.success(f"Usuario estudiante {new_username} creado exitosamente") |
|
else: |
|
st.error("Error al crear el usuario estudiante") |
|
|
|
|
|
with tab2: |
|
st.header("B煤squeda de Usuarios") |
|
|
|
search_col1, search_col2 = st.columns([2,1]) |
|
|
|
with search_col1: |
|
student_username = st.text_input( |
|
"Nombre de usuario del estudiante", |
|
key="admin_view_student" |
|
) |
|
|
|
with search_col2: |
|
search_button = st.button( |
|
"Buscar", |
|
key="admin_view_student_data", |
|
type="primary" |
|
) |
|
|
|
if search_button: |
|
student = get_student_user(student_username) |
|
if student: |
|
|
|
info_tab1, info_tab2, info_tab3 = st.tabs([ |
|
"Informaci贸n B谩sica", |
|
"An谩lisis Realizados", |
|
"Tiempo en Plataforma" |
|
]) |
|
|
|
with info_tab1: |
|
st.subheader("Informaci贸n del Usuario") |
|
st.json(student) |
|
|
|
with info_tab2: |
|
st.subheader("An谩lisis Realizados") |
|
student_data = get_student_morphosyntax_analysis(student_username) |
|
if student_data: |
|
st.json(student_data) |
|
else: |
|
st.info("No hay datos de an谩lisis para este estudiante.") |
|
|
|
with info_tab3: |
|
st.subheader("Tiempo en Plataforma") |
|
total_time = get_user_total_time(student_username) |
|
if total_time: |
|
st.metric( |
|
"Tiempo Total", |
|
format_duration(total_time) |
|
) |
|
else: |
|
st.info("No hay registros de tiempo para este usuario") |
|
else: |
|
st.error("Estudiante no encontrado") |
|
|
|
|
|
with tab3: |
|
st.header("Actividad Reciente") |
|
|
|
|
|
recent_sessions = get_recent_sessions(10) |
|
|
|
if recent_sessions: |
|
|
|
sessions_data = [] |
|
for session in recent_sessions: |
|
sessions_data.append({ |
|
"Usuario": session['username'], |
|
"Inicio de Sesi贸n": datetime.fromisoformat( |
|
session['loginTime'].rstrip('Z') |
|
).strftime("%Y-%m-%d %H:%M:%S"), |
|
"Fin de Sesi贸n": datetime.fromisoformat( |
|
session['logoutTime'].rstrip('Z') |
|
).strftime("%Y-%m-%d %H:%M:%S") if session.get('logoutTime') else "Activo", |
|
"Duraci贸n": format_duration(session.get('sessionDuration', 0)) |
|
}) |
|
|
|
|
|
st.dataframe( |
|
sessions_data, |
|
hide_index=True, |
|
column_config={ |
|
"Usuario": st.column_config.TextColumn( |
|
"Usuario", |
|
width="medium" |
|
), |
|
"Inicio de Sesi贸n": st.column_config.TextColumn( |
|
"Inicio de Sesi贸n", |
|
width="medium" |
|
), |
|
"Fin de Sesi贸n": st.column_config.TextColumn( |
|
"Fin de Sesi贸n", |
|
width="medium" |
|
), |
|
"Duraci贸n": st.column_config.TextColumn( |
|
"Duraci贸n", |
|
width="small" |
|
) |
|
} |
|
) |
|
|
|
|
|
total_sessions = len(sessions_data) |
|
total_users = len(set(session['Usuario'] for session in sessions_data)) |
|
|
|
metric_col1, metric_col2 = st.columns(2) |
|
with metric_col1: |
|
st.metric("Total de Sesiones", total_sessions) |
|
with metric_col2: |
|
st.metric("Usuarios 脷nicos", total_users) |
|
|
|
else: |
|
st.info("No hay registros de sesiones recientes") |
|
|
|
|
|
st.markdown("---") |
|
|
|
|
|
col1, col2, col3 = st.columns([2,1,2]) |
|
with col2: |
|
if st.button("Cerrar Sesi贸n", key="admin_logout", type="primary", use_container_width=True): |
|
from ..auth.auth import logout |
|
logout() |
|
st.rerun() |