server stats component

This commit is contained in:
2026-04-16 12:26:43 +02:00
parent cb9414bc43
commit ad29538422
9 changed files with 136 additions and 10 deletions

View File

@ -153,7 +153,7 @@ pub fn UploadFile() -> Element {
let mut selected : Signal<Vec<(String, String, Option<Result<String, HttpError>>)>> = use_signal(|| vec![]);
rsx! {
div { class : "p-2 h-full w-full",
div { class : "p-2",
p { class: "text-4xl font-bold p-1", "Upload a file" }
form {

View File

@ -51,7 +51,7 @@ async fn download_file(id: String) -> Result<FileStream, HttpError> {
let db_file = result.unwrap();
if db_file.len() == 0 {
return HttpError::internal_server_error("The requested file does not exist");
return HttpError::not_found("The requested file does not exist");
}
let file_path = s_config.upload_folder + &id;

View File

@ -6,3 +6,6 @@ pub use info::FileInfo;
mod get;
mod stats;
pub use stats::UploadStats;

View File

@ -0,0 +1,85 @@
use dioxus::prelude::*;
#[cfg(feature = "server")]
use httpserver::models::GetFile;
#[cfg(feature = "server")]
use diesel::prelude::*;
#[cfg(feature = "server")]
use httpserver::DB;
#[cfg(feature = "server")]
use diesel::dsl;
#[cfg(feature = "server")]
use httpserver::schema;
#[cfg(feature = "server")]
use crate::config::ServerConfig;
use serde::{Deserialize, Serialize};
use bigdecimal::{BigDecimal, ToPrimitive};
use httpserver::utils::byte_to_human_size;
#[derive(Deserialize, Serialize)]
struct UploadServerStats {
total_limit_soft: u64,
total_limit_hard : u64,
used_space: u64,
nb_active_files: u64,
nb_total_files: u64
}
#[get("/upload/stats")]
async fn get_upload_stats() -> Result<UploadServerStats, HttpError> {
use httpserver::schema::file;
let s_config = ServerConfig::load();
let active_files = match DB.with(|pool| file::table.select((dsl::sum(file::file_size), dsl::count(file::id)))
.filter(file::deleted.eq(false))
.first::<(Option<BigDecimal>, i64)>(&mut pool.get().unwrap())) {
Ok((Some(val1),val2)) => (val1, val2),
_ => return HttpError::internal_server_error("wth ?")
};
let total_files = match DB.with(|pool| file::table.select((dsl::count(file::id)))
.first::<(i64)>(&mut pool.get().unwrap())) {
Ok(val) => val,
_ => return HttpError::internal_server_error("wth ?")
};
Ok(UploadServerStats {
total_limit_soft: s_config.upload_storage_limit_soft,
total_limit_hard: s_config.upload_storage_limit_hard,
used_space: active_files.0.to_u64().unwrap(),
nb_active_files: active_files.1.to_u64().unwrap(),
nb_total_files: total_files.to_u64().unwrap()
})
}
fn render_stats(stats : &UploadServerStats) -> Element {
let soft_percentage = stats.used_space as f32 / stats.total_limit_soft as f32;
let hard_percentage = stats.used_space as f32 / stats.total_limit_hard as f32;
rsx! {
h1 {class: "text-4xl font-bold", "Server info"}
p { "percentage of the soft limit used : {soft_percentage}%"}
p { "percentage of the hard limit used : {hard_percentage}%"}
p { "number of currenlty active files : {stats.nb_active_files}"}
p { "number of files uploaded : {stats.nb_total_files}" }
}
}
#[component]
pub fn UploadStats() -> Element {
let r_stats = use_resource(move || get_upload_stats());
match &*r_stats.read_unchecked() {
Some(Ok(stats)) => { render_stats(stats) },
Some(Err(err)) => { rsx! { p {"server retrned an error : {err}"} } },
_ => { rsx! { p {"Loading ..."} } }
}
}

View File

@ -2,6 +2,9 @@
pub struct ServerConfig {
pub upload_folder: String,
pub db_url: String,
pub upload_storage_limit_soft: u64,
pub upload_storage_limit_hard: u64,
}
#[cfg(feature = "server")]
@ -10,6 +13,10 @@ impl ServerConfig {
Self {
upload_folder: "./test/".to_string(),
db_url: std::env::var("DATABASE_URL").expect("missing DATABASE_URL"),
upload_storage_limit_soft: 1024 * 1024 * 1024 * 200,
upload_storage_limit_hard: 1024 * 1024 * 1024 * 300,
}
}
}

View File

@ -1,6 +1,6 @@
use dioxus::prelude::*;
use crate::components::upload::{FileInfo, UploadFile};
use crate::components::upload::{FileInfo, UploadFile, UploadStats};
use tracing::Level;
pub mod config;
@ -34,7 +34,9 @@ fn App() -> Element {
div {
class: "h-screen w-screen bg-zinc-900 text-white",
ToastProvider {
UploadFile { }
FileInfo {file: "ULI0GZWJQH9BGEZR1VZ1"}
UploadStats { }
// Router::<Route> {}
}
}