File size: 8,530 Bytes
2c407e7 aeefc06 e9fe616 aeefc06 e9fe616 aeefc06 e9fe616 80a324f aeefc06 80a324f aeefc06 ac4bde9 80a324f ac4bde9 80a324f aeefc06 ac4bde9 80a324f aeefc06 80a324f aeefc06 ecd0216 aeefc06 ecd0216 aeefc06 30ad1ad |
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
#modules/admin/admin_ui.py
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}")
# Crear tres tabs para las diferentes secciones
tab1, tab2, tab3 = st.tabs([
"Gesti贸n de Usuarios",
"B煤squeda de Usuarios",
"Actividad de la Plataforma"
])
new_username = st.text_input("Correo electr贸nico del nuevo usuario", key="admin_new_username")
new_password = st.text_input("Contrase帽a", type="password", key="admin_new_password")
if st.button("Crear Usuario", key="admin_create_user", type="primary"):
try:
# Hashear la contrase帽a antes de llamar a create_student_user
hashed_password = hash_password(new_password)
if create_student_user(new_username, hashed_password):
st.success(f"Usuario estudiante {new_username} creado exitosamente")
else:
st.error("Error al crear el usuario estudiante")
except Exception as e:
logger.error(f"Error en creaci贸n de usuario: {str(e)}")
st.error("Error al crear el usuario estudiante")
############################################################################################
# Tab 1: Gesti贸n de Usuarios
with tab1:
st.header("Crear Nuevo Usuario Estudiante")
# Crear dos columnas para el formulario
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 new_username and new_password: # Verificamos que ambos campos tengan valor
try:
# Ya no necesitamos pasar partitionKey en additional_info
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")
except Exception as e:
st.error(f"Error al crear usuario: {str(e)}")
logger.error(f"Error detallado al crear usuario: {str(e)}")
else:
st.warning("Por favor complete todos los campos")
#####################################################################
# Tab 2: B煤squeda de Usuarios
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:
# Crear tabs para diferentes tipos de informaci贸n
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")
# Tab 3: Actividad de la Plataforma
with tab3:
st.header("Actividad Reciente")
# Obtener sesiones recientes
recent_sessions = get_recent_sessions(10)
if recent_sessions:
# Crear dataframe para mostrar los datos
sessions_data = []
for session in recent_sessions:
# Convertir loginTime a formato legible
login_time = datetime.fromisoformat(
session['loginTime'].rstrip('Z')
).strftime("%Y-%m-%d %H:%M:%S")
# Manejar el caso de logout_time cuando la sesi贸n est谩 activa
if session.get('logoutTime') and session['logoutTime'] != "Activo":
logout_time = datetime.fromisoformat(
session['logoutTime'].rstrip('Z')
).strftime("%Y-%m-%d %H:%M:%S")
else:
logout_time = "Activo"
sessions_data.append({
"Usuario": session['username'],
"Inicio de Sesi贸n": login_time,
"Fin de Sesi贸n": logout_time,
"Duraci贸n": format_duration(session.get('sessionDuration', 0))
})
# Mostrar tabla con estilos
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"
)
}
)
# A帽adir m茅tricas resumen
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")
# Agregar una l铆nea divisoria antes del bot贸n
st.markdown("---")
# Centrar el bot贸n de cierre de sesi贸n
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() |