aboutsummaryrefslogtreecommitdiff
path: root/server/src/ui/stats.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-04-28 18:27:03 +0200
committermetamuffin <metamuffin@disroot.org>2025-04-28 18:27:03 +0200
commit51761cbdefa39107b9e1f931f1aa8df6aebb2a94 (patch)
tree957ca180786ece777e6e1153ada91da741d845ec /server/src/ui/stats.rs
parent80d28b764c95891551e28c395783f5ff9d065743 (diff)
downloadjellything-51761cbdefa39107b9e1f931f1aa8df6aebb2a94.tar
jellything-51761cbdefa39107b9e1f931f1aa8df6aebb2a94.tar.bz2
jellything-51761cbdefa39107b9e1f931f1aa8df6aebb2a94.tar.zst
many much more generic refactor
Diffstat (limited to 'server/src/ui/stats.rs')
-rw-r--r--server/src/ui/stats.rs68
1 files changed, 10 insertions, 58 deletions
diff --git a/server/src/ui/stats.rs b/server/src/ui/stats.rs
index 345586a..a91e670 100644
--- a/server/src/ui/stats.rs
+++ b/server/src/ui/stats.rs
@@ -8,21 +8,14 @@ use super::{
layout::{DynLayoutPage, LayoutPage},
};
use crate::{
- api::AcceptJson,
- database::Database,
- locale::AcceptLanguage,
- logic::session::Session,
- ui::{
- layout::trs,
- node::{
- format_duration, format_duration_long, format_kind, format_size,
- rocket_uri_macro_r_library_node,
- },
- },
- uri,
+ api::AcceptJson, database::Database, locale::AcceptLanguage, logic::session::Session, uri,
};
use jellybase::locale::tr;
-use jellycommon::{Node, NodeID, NodeKind, Visibility};
+use jellycommon::{
+ api::{ApiStatsResponse, StatsBin},
+ Node, NodeID, NodeKind, Visibility,
+};
+use jellylogic::stats::stats;
use markup::raw;
use rocket::{get, serde::json::Json, Either, State};
use serde::Serialize;
@@ -35,54 +28,13 @@ pub fn r_stats(
db: &State<Database>,
aj: AcceptJson,
lang: AcceptLanguage,
-) -> Result<Either<DynLayoutPage<'_>, Json<Value>>, MyError> {
+) -> Result<Either<DynLayoutPage<'_>, Json<ApiStatsResponse>>, MyError> {
let AcceptLanguage(lang) = lang;
- let mut items = db.list_nodes_with_udata(sess.user.name.as_str())?;
- items.retain(|(n, _)| n.visibility >= Visibility::Reduced);
-
- #[derive(Default, Serialize)]
- struct Bin {
- runtime: f64,
- size: u64,
- count: usize,
- max_runtime: (f64, String),
- max_size: (u64, String),
- }
- impl Bin {
- 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())
- }
- }
- }
- fn average_runtime(&self) -> f64 {
- self.runtime / self.count as f64
- }
- fn average_size(&self) -> f64 {
- self.size as f64 / self.count as f64
- }
- }
-
- let mut all = Bin::default();
- let mut kinds = BTreeMap::<NodeKind, Bin>::new();
- for (i, _) in items {
- all.update(&i);
- kinds.entry(i.kind).or_default().update(&i);
- }
+ let data = stats(db)?;
Ok(if *aj {
- Either::Right(Json(json!({
- "all": all,
- "kinds": kinds,
- })))
+ Either::Right(Json(data))
} else {
- Either::Left()
+ Either::Left(1)
})
}