59 lines
1.8 KiB
Rust
59 lines
1.8 KiB
Rust
use axum::{
|
|
extract::State,
|
|
response::{IntoResponse, Json},
|
|
};
|
|
use serde_json::json;
|
|
use tower_sessions::Session;
|
|
|
|
use crate::routes::{get_and_clear_flash, render_html, AppState, AuthUser};
|
|
|
|
pub async fn users_get(
|
|
_auth: AuthUser,
|
|
session: Session,
|
|
State(state): State<AppState>,
|
|
) -> impl IntoResponse {
|
|
let flash_messages = get_and_clear_flash(&session).await;
|
|
let mut ctx = tera::Context::new();
|
|
ctx.insert("flash_messages", &flash_messages);
|
|
ctx.insert("is_authenticated", &true);
|
|
ctx.insert("active_page", "users");
|
|
render_html(&state.tera, "users.html", ctx)
|
|
}
|
|
|
|
pub async fn api_users(
|
|
_auth: AuthUser,
|
|
State(state): State<AppState>,
|
|
) -> impl IntoResponse {
|
|
let data = state.user_monitor.data.lock().unwrap().clone();
|
|
if let Some(err) = &data.error {
|
|
return Json(json!({ "error": err }));
|
|
}
|
|
if data.no_files {
|
|
return Json(json!({ "no_files": true }));
|
|
}
|
|
let users: Vec<serde_json::Value> = data
|
|
.users
|
|
.iter()
|
|
.map(|u| {
|
|
json!({
|
|
"login": u.login,
|
|
"status": u.status,
|
|
"last_action_time": u.last_action_time.format("%H:%M:%S").to_string(),
|
|
"last_action_label": u.last_action_label,
|
|
"action_count_24h": u.action_count_24h,
|
|
"connected_since": u.connected_since.map(|t| t.format("%H:%M").to_string()),
|
|
"explicit_logout": u.explicit_logout,
|
|
})
|
|
})
|
|
.collect();
|
|
Json(json!({ "users": users, "hourly": data.hourly }))
|
|
}
|
|
|
|
pub async fn api_users_weekly(
|
|
_auth: AuthUser,
|
|
State(state): State<AppState>,
|
|
) -> impl IntoResponse {
|
|
let weekly = state.user_monitor.get_weekly_activity().await;
|
|
Json(json!({ "weekly": weekly }))
|
|
}
|