feat(admin): overview dashboard sections + system temperature/load sensors (0.85.0) (#11)
This commit was merged in pull request #11.
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
pub mod analysis;
|
||||
pub mod crawler;
|
||||
pub mod mangas;
|
||||
pub mod overview;
|
||||
pub mod resync;
|
||||
pub mod settings;
|
||||
pub mod storage;
|
||||
@@ -24,6 +25,7 @@ pub fn routes() -> Router<AppState> {
|
||||
.merge(resync::routes())
|
||||
.merge(storage::routes())
|
||||
.merge(system::routes())
|
||||
.merge(overview::routes())
|
||||
.merge(crawler::routes())
|
||||
.merge(analysis::routes())
|
||||
.merge(settings::routes())
|
||||
|
||||
81
backend/src/api/admin/overview.rs
Normal file
81
backend/src/api/admin/overview.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
//! Admin overview dashboard aggregates.
|
||||
//!
|
||||
//! One composed endpoint feeding the Overview tab's per-section summary
|
||||
//! cards (Users, Mangas, Analysis). Pure DB aggregates — no system
|
||||
//! sampling — so it's cheap enough to poll. The System and Crawler cards
|
||||
//! reuse their own existing endpoints (`/admin/system`, `/admin/crawler`),
|
||||
//! and the Settings strip reuses the settings endpoints; only these three
|
||||
//! sections needed new aggregation, so they live together here.
|
||||
//!
|
||||
//! Admin-only (`RequireAdmin`, cookie-only).
|
||||
|
||||
use axum::extract::State;
|
||||
use axum::routing::get;
|
||||
use axum::{Json, Router};
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::app::AppState;
|
||||
use crate::auth::extractor::RequireAdmin;
|
||||
use crate::error::AppResult;
|
||||
use crate::repo;
|
||||
|
||||
pub fn routes() -> Router<AppState> {
|
||||
Router::new().route("/admin/overview", get(overview))
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct OverviewStats {
|
||||
pub users: UsersOverview,
|
||||
pub mangas: repo::admin_view::MangaStats,
|
||||
pub analysis: AnalysisOverview,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct UsersOverview {
|
||||
pub total: i64,
|
||||
pub admins: i64,
|
||||
pub newest_username: Option<String>,
|
||||
pub newest_created_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct AnalysisOverview {
|
||||
pub analyzed_pages: i64,
|
||||
pub total_pages: i64,
|
||||
}
|
||||
|
||||
async fn overview(
|
||||
State(state): State<AppState>,
|
||||
_admin: RequireAdmin,
|
||||
) -> AppResult<Json<OverviewStats>> {
|
||||
// Independent reads — run concurrently so latency is the slowest
|
||||
// query, not their sum (mirrors the storage handler).
|
||||
let (user_counts, user_newest, mangas, coverage) = tokio::try_join!(
|
||||
repo::user::counts(&state.db),
|
||||
repo::user::newest(&state.db),
|
||||
repo::admin_view::manga_stats(&state.db),
|
||||
repo::page_analysis::library_coverage(&state.db),
|
||||
)?;
|
||||
|
||||
let (total, admins) = user_counts;
|
||||
let (newest_username, newest_created_at) = match user_newest {
|
||||
Some((username, created_at)) => (Some(username), Some(created_at)),
|
||||
None => (None, None),
|
||||
};
|
||||
let (analyzed_pages, total_pages) = coverage;
|
||||
|
||||
Ok(Json(OverviewStats {
|
||||
users: UsersOverview {
|
||||
total,
|
||||
admins,
|
||||
newest_username,
|
||||
newest_created_at,
|
||||
},
|
||||
mangas,
|
||||
analysis: AnalysisOverview {
|
||||
analyzed_pages,
|
||||
total_pages,
|
||||
},
|
||||
}))
|
||||
}
|
||||
@@ -18,7 +18,7 @@ use axum::extract::State;
|
||||
use axum::routing::get;
|
||||
use axum::{Json, Router};
|
||||
use serde::Serialize;
|
||||
use sysinfo::{CpuRefreshKind, MemoryRefreshKind, RefreshKind, System};
|
||||
use sysinfo::{Components, CpuRefreshKind, MemoryRefreshKind, RefreshKind, System};
|
||||
|
||||
use crate::app::AppState;
|
||||
use crate::auth::extractor::RequireAdmin;
|
||||
@@ -35,6 +35,10 @@ pub struct SystemStats {
|
||||
pub disk: Option<DiskStats>,
|
||||
pub memory: MemoryStats,
|
||||
pub cpu: CpuStats,
|
||||
/// Hardware temperature sensors. Empty when the platform doesn't
|
||||
/// expose any (common inside containers without `/sys` access) —
|
||||
/// the frontend renders an "unavailable" state, not an error.
|
||||
pub temperatures: Vec<TempStat>,
|
||||
pub alerts: Vec<Alert>,
|
||||
}
|
||||
|
||||
@@ -56,6 +60,26 @@ pub struct MemoryStats {
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct CpuStats {
|
||||
pub percent_used: f64,
|
||||
pub load_avg: LoadAvg,
|
||||
/// Per-core usage percentages, one entry per logical core.
|
||||
pub per_core: Vec<f64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct LoadAvg {
|
||||
pub one: f64,
|
||||
pub five: f64,
|
||||
pub fifteen: f64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct TempStat {
|
||||
pub label: String,
|
||||
pub celsius: f64,
|
||||
/// Highest reading seen since boot, when the kernel exposes it.
|
||||
pub max_celsius: Option<f64>,
|
||||
/// Vendor critical/shutdown threshold, when available.
|
||||
pub critical_celsius: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
@@ -76,6 +100,7 @@ async fn system(
|
||||
) -> AppResult<Json<SystemStats>> {
|
||||
let disk = state.storage.local_root().and_then(disk_stats_for);
|
||||
let (memory, cpu) = memory_and_cpu().await;
|
||||
let temperatures = temperatures();
|
||||
let mut alerts = Vec::new();
|
||||
if let Some(d) = &disk {
|
||||
if d.percent_used >= ALERT_THRESHOLD_PERCENT {
|
||||
@@ -97,14 +122,53 @@ async fn system(
|
||||
),
|
||||
});
|
||||
}
|
||||
for t in &temperatures {
|
||||
if let Some(crit) = t.critical_celsius {
|
||||
if t.celsius >= crit {
|
||||
alerts.push(Alert {
|
||||
level: AlertLevel::Warning,
|
||||
message: format!(
|
||||
"{} temperature critical ({:.0}°C / {:.0}°C)",
|
||||
t.label, t.celsius, crit
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Json(SystemStats {
|
||||
disk,
|
||||
memory,
|
||||
cpu,
|
||||
temperatures,
|
||||
alerts,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Hardware temperature sensors via `sysinfo`'s `Components`. Components
|
||||
/// whose temperature can't be read report `f32::NAN` on Linux — we skip
|
||||
/// those so the dashboard never shows a bogus "NaN°C" row. Returns an
|
||||
/// empty Vec when no sensors are exposed at all.
|
||||
fn temperatures() -> Vec<TempStat> {
|
||||
let components = Components::new_with_refreshed_list();
|
||||
components
|
||||
.list()
|
||||
.iter()
|
||||
.filter_map(|c| {
|
||||
let celsius = c.temperature();
|
||||
if celsius.is_nan() {
|
||||
return None;
|
||||
}
|
||||
let max = c.max();
|
||||
Some(TempStat {
|
||||
label: c.label().to_string(),
|
||||
celsius: celsius as f64,
|
||||
max_celsius: (!max.is_nan()).then_some(max as f64),
|
||||
critical_celsius: c.critical().map(|v| v as f64),
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(crate) fn disk_stats_for(root: &Path) -> Option<DiskStats> {
|
||||
let s = nix::sys::statvfs::statvfs(root).ok()?;
|
||||
// statvfs reports `f_frsize * f_blocks` for total bytes. `f_bavail`
|
||||
@@ -156,8 +220,15 @@ async fn memory_and_cpu() -> (MemoryStats, CpuStats) {
|
||||
percent_used: mem_pct,
|
||||
};
|
||||
|
||||
let load = System::load_average();
|
||||
let cpu = CpuStats {
|
||||
percent_used: sys.global_cpu_usage() as f64,
|
||||
load_avg: LoadAvg {
|
||||
one: load.one,
|
||||
five: load.five,
|
||||
fifteen: load.fifteen,
|
||||
},
|
||||
per_core: sys.cpus().iter().map(|c| c.cpu_usage() as f64).collect(),
|
||||
};
|
||||
(memory, cpu)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user