Display global statistics

This commit is contained in:
2025-05-02 10:01:22 +02:00
parent 16ef1147fe
commit 272c8ab312
8 changed files with 170 additions and 6 deletions

View File

@ -8,6 +8,7 @@ pub mod files_controller;
pub mod movement_controller;
pub mod server_controller;
pub mod static_controller;
pub mod stats_controller;
pub mod tokens_controller;
#[derive(thiserror::Error, Debug)]

View File

@ -0,0 +1,25 @@
use crate::controllers::HttpResult;
use crate::extractors::auth_extractor::AuthExtractor;
use crate::services::{files_service, movements_service};
use actix_web::HttpResponse;
#[derive(serde::Serialize)]
struct GlobalStats {
global_balance: f32,
number_movements: usize,
number_files: usize,
total_files_size: u64,
}
/// Get global statistics
pub async fn global(auth: AuthExtractor) -> HttpResult {
let movements = movements_service::get_all_movements_user(auth.user.id()).await?;
let files = files_service::get_all_files_user(auth.user.id()).await?;
Ok(HttpResponse::Ok().json(GlobalStats {
global_balance: movements.iter().fold(0.0, |sum, m| sum + m.amount),
number_movements: movements.len(),
number_files: files.len(),
total_files_size: files.iter().fold(0, |sum, m| sum + m.file_size as u64),
}))
}