File size: 9,711 Bytes
2c407e7 aeefc06 eb0ecb9 aeefc06 eb0ecb9 aeefc06 eb0ecb9 aeefc06 eb0ecb9 aeefc06 eb0ecb9 aeefc06 8007480 aeefc06 ac4bde9 eb0ecb9 ac4bde9 aeefc06 ac4bde9 aeefc06 8007480 aeefc06 dfb74b5 6674bfa 3ec6163 6674bfa 3ec6163 6674bfa 3ec6163 6674bfa 3ec6163 ecd0216 3ec6163 aeefc06 3ec6163 aeefc06 6674bfa 30ad1ad 6674bfa 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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
#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
from ..auth.auth import hash_password # Agregar esta importación al inicio
########################################################
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"
])
########################################################
# 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:
# Hashear la contraseña antes de crear el usuario
hashed_password = hash_password(new_password)
if create_student_user(new_username, hashed_password, {'partitionKey': new_username}):
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)}")
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")
# Agregar botón de actualización
if st.button("Actualizar datos", key="refresh_sessions", type="primary"):
st.rerun()
# Mostrar spinner mientras carga
with st.spinner("Cargando datos de sesiones..."):
# Obtener sesiones recientes
recent_sessions = get_recent_sessions(20) # Aumentado a 20 para más datos
if recent_sessions:
# Crear dataframe para mostrar los datos
sessions_data = []
for session in recent_sessions:
try:
# Manejar el formato de fecha con manejo de excepciones
try:
login_time = datetime.fromisoformat(
session['loginTime'].replace('Z', '+00:00')
).strftime("%Y-%m-%d %H:%M:%S")
except Exception as e:
login_time = session['loginTime']
# Manejar el caso de logout_time cuando la sesión está activa
if session.get('logoutTime') and session['logoutTime'] != "Activo":
try:
logout_time = datetime.fromisoformat(
session['logoutTime'].replace('Z', '+00:00')
).strftime("%Y-%m-%d %H:%M:%S")
except Exception as e:
logout_time = session['logoutTime']
else:
logout_time = "Activo"
# Agregar datos a la lista
sessions_data.append({
"Usuario": session.get('username', 'Desconocido'),
"Inicio de Sesión": login_time,
"Fin de Sesión": logout_time,
"Duración": format_duration(session.get('sessionDuration', 0))
})
except Exception as e:
st.error(f"Error procesando sesión: {str(e)}")
continue
# Mostrar información de depuración si hay problemas
with st.expander("Información de depuración", expanded=False):
st.write("Datos crudos recuperados:")
st.json(recent_sessions)
st.write("Datos procesados para mostrar:")
st.json(sessions_data)
# 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 o hubo un problema al recuperarlos.")
# Ayuda de depuración
if st.button("Mostrar diagnóstico"):
st.write("Verificando la función get_recent_sessions:")
container = get_container("users_sessions")
if container:
st.success("✅ Conectado al contenedor users_sessions")
else:
st.error("❌ No se pudo conectar al contenedor users_sessions")
##################################################################
# 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() |