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),
}))
}

View File

@ -155,6 +155,8 @@ async fn main() -> std::io::Result<()> {
"/api/movement/{movement_id}",
web::delete().to(movement_controller::delete),
)
// Statistics controller
.route("/api/stats/global", web::get().to(stats_controller::global))
// Static assets
.route("/", web::get().to(static_controller::root_index))
.route(

View File

@ -1,12 +1,12 @@
use crate::models::accounts::AccountID;
use crate::models::files::FileID;
use crate::schema::*;
use diesel::{Insertable, Queryable};
use diesel::{Insertable, Queryable, QueryableByName};
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct MovementID(pub i32);
#[derive(Queryable, Debug, Clone, serde::Serialize)]
#[derive(Queryable, QueryableByName, Debug, Clone, serde::Serialize)]
pub struct Movement {
id: i32,
account_id: i32,

View File

@ -112,6 +112,13 @@ pub async fn delete_file_if_unused(id: FileID) -> anyhow::Result<bool> {
}
}
/// Get all the files of a user
pub async fn get_all_files_user(user_id: UserID) -> anyhow::Result<Vec<File>> {
Ok(files::table
.filter(files::dsl::user_id.eq(user_id.0))
.load(&mut db()?)?)
}
/// Get the entire list of file
pub async fn get_entire_list() -> anyhow::Result<Vec<File>> {
Ok(files::table.get_results(&mut db()?)?)

View File

@ -162,6 +162,17 @@ pub async fn get_balances(user_id: UserID) -> anyhow::Result<HashMap<AccountID,
.collect())
}
/// Get all the movements of the user
pub async fn get_all_movements_user(user_id: UserID) -> anyhow::Result<Vec<Movement>> {
let movements = sql_query(format!(
"select m.* from movements m join accounts a on a.id = m.account_id where a.user_id = {}",
user_id.0
))
.load::<Movement>(&mut db()?)?;
Ok(movements)
}
/// Delete a movement
pub async fn delete(id: MovementID) -> anyhow::Result<()> {
diesel::delete(movements::dsl::movements.filter(movements::dsl::id.eq(id.0)))