From 51761cbdefa39107b9e1f931f1aa8df6aebb2a94 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 28 Apr 2025 18:27:03 +0200 Subject: many much more generic refactor --- logic/src/stats.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 logic/src/stats.rs (limited to 'logic/src/stats.rs') diff --git a/logic/src/stats.rs b/logic/src/stats.rs new file mode 100644 index 0000000..2569180 --- /dev/null +++ b/logic/src/stats.rs @@ -0,0 +1,48 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2025 metamuffin +*/ + +use crate::session::Session; +use anyhow::Result; +use jellybase::database::Database; +use jellycommon::{ + Node, NodeKind, Visibility, + api::{ApiStatsResponse, StatsBin}, +}; +use std::collections::BTreeMap; + +pub fn stats(db: &Database, session: &Session) -> Result { + let mut items = db.list_nodes_with_udata(session.user.name.as_str())?; + items.retain(|(n, _)| n.visibility >= Visibility::Reduced); + + trait BinExt { + fn update(&mut self, node: &Node); + } + impl BinExt for StatsBin { + fn update(&mut self, node: &Node) { + self.count += 1; + self.size += node.storage_size; + if node.storage_size > self.max_size.0 { + self.max_size = (node.storage_size, node.slug.clone()) + } + if let Some(m) = &node.media { + self.runtime += m.duration; + if m.duration > self.max_runtime.0 { + self.max_runtime = (m.duration, node.slug.clone()) + } + } + } + + } + + let mut total = StatsBin::default(); + let mut kinds = BTreeMap::::new(); + for (i, _) in items { + total.update(&i); + kinds.entry(i.kind).or_default().update(&i); + } + + Ok(ApiStatsResponse { kinds, total }) +} -- cgit v1.2.3-70-g09d2