diff options
author | metamuffin <metamuffin@disroot.org> | 2025-04-28 18:27:03 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-04-28 18:27:03 +0200 |
commit | 51761cbdefa39107b9e1f931f1aa8df6aebb2a94 (patch) | |
tree | 957ca180786ece777e6e1153ada91da741d845ec /logic/src/stats.rs | |
parent | 80d28b764c95891551e28c395783f5ff9d065743 (diff) | |
download | jellything-51761cbdefa39107b9e1f931f1aa8df6aebb2a94.tar jellything-51761cbdefa39107b9e1f931f1aa8df6aebb2a94.tar.bz2 jellything-51761cbdefa39107b9e1f931f1aa8df6aebb2a94.tar.zst |
many much more generic refactor
Diffstat (limited to 'logic/src/stats.rs')
-rw-r--r-- | logic/src/stats.rs | 48 |
1 files changed, 48 insertions, 0 deletions
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 <metamuffin.org> +*/ + +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<ApiStatsResponse> { + 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::<NodeKind, StatsBin>::new(); + for (i, _) in items { + total.update(&i); + kinds.entry(i.kind).or_default().update(&i); + } + + Ok(ApiStatsResponse { kinds, total }) +} |