diff options
author | metamuffin <metamuffin@disroot.org> | 2025-02-16 13:25:02 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-02-16 13:25:02 +0100 |
commit | abe663807337faa717f9485b047c8f0e808f2a09 (patch) | |
tree | db969df5bf119debc9e2ba6450e3fd05c2f39b0d /server/src/routes/ui | |
parent | 079fec9f206751047248c8c7733d7eccbd89d94b (diff) | |
download | jellything-abe663807337faa717f9485b047c8f0e808f2a09.tar jellything-abe663807337faa717f9485b047c8f0e808f2a09.tar.bz2 jellything-abe663807337faa717f9485b047c8f0e808f2a09.tar.zst |
stats page
Diffstat (limited to 'server/src/routes/ui')
-rw-r--r-- | server/src/routes/ui/layout.rs | 2 | ||||
-rw-r--r-- | server/src/routes/ui/mod.rs | 1 | ||||
-rw-r--r-- | server/src/routes/ui/node.rs | 46 | ||||
-rw-r--r-- | server/src/routes/ui/stats.rs | 116 |
4 files changed, 153 insertions, 12 deletions
diff --git a/server/src/routes/ui/layout.rs b/server/src/routes/ui/layout.rs index 3b2db28..b94a873 100644 --- a/server/src/routes/ui/layout.rs +++ b/server/src/routes/ui/layout.rs @@ -14,6 +14,7 @@ use crate::{ browser::rocket_uri_macro_r_all_items, node::rocket_uri_macro_r_library_node, search::rocket_uri_macro_r_search, + stats::rocket_uri_macro_r_stats, }, uri, }; @@ -49,6 +50,7 @@ markup::define! { a.library[href=uri!(r_library_node("library"))] { "My Library" } " " a.library[href=uri!(r_all_items())] { "All Items" } " " a.library[href=uri!(r_search(None::<&'static str>, None::<usize>))] { "Search" } " " + a.library[href=uri!(r_stats())] { "Stats" } " " } @if is_importing() { span.warn { "Library database is updating..." } } div.account { diff --git a/server/src/routes/ui/mod.rs b/server/src/routes/ui/mod.rs index 9a3e61b..d56285c 100644 --- a/server/src/routes/ui/mod.rs +++ b/server/src/routes/ui/mod.rs @@ -43,6 +43,7 @@ pub mod player; pub mod search; pub mod sort; pub mod style; +pub mod stats; #[get("/")] pub async fn r_index(sess: Option<Session>) -> MyResult<Either<Redirect, DynLayoutPage<'static>>> { diff --git a/server/src/routes/ui/node.rs b/server/src/routes/ui/node.rs index ebd21db..3b30c57 100644 --- a/server/src/routes/ui/node.rs +++ b/server/src/routes/ui/node.rs @@ -36,7 +36,7 @@ use jellycommon::{ Chapter, MediaInfo, Node, NodeID, NodeKind, PeopleGroup, Rating, SourceTrackKind, Visibility, }; use rocket::{get, serde::json::Json, Either, State}; -use std::sync::Arc; +use std::{fmt::Write, sync::Arc}; /// This function is a stub and only useful for use in the uri! macro. #[get("/n/<id>")] @@ -325,22 +325,44 @@ pub fn aspect_class(kind: NodeKind) -> &'static str { } } -pub fn format_duration(mut d: f64) -> String { +pub fn format_duration(d: f64) -> String { + format_duration_mode(d, false) +} +pub fn format_duration_long(d: f64) -> String { + format_duration_mode(d, true) +} +fn format_duration_mode(mut d: f64, long_units: bool) -> String { let mut s = String::new(); let sign = if d > 0. { "" } else { "-" }; d = d.abs(); - for (unit, k) in [("h", 60. * 60.), ("m", 60.), ("s", 1.)] { - let mut h = 0; - // TODO dont iterate like that. can be a simple rem and div - while d > k { - d -= k; - h += 1; - } - if h > 0 { - s += &format!("{h}{unit}") + for (short, long, k) in [ + ("d", "day", 60. * 60. * 24.), + ("h", "hour", 60. * 60.), + ("m", "minute", 60.), + ("s", "second", 1.), + ] { + let h = (d / k).floor(); + d -= h * k; + if h > 0. { + if long_units { + // TODO breaks if seconds is zero + write!( + s, + "{}{h} {long}{}{}", + if k != 1. { "" } else { " and " }, + if h != 1. { "s" } else { "" }, + if k > 60. { ", " } else { "" }, + ) + .unwrap(); + } else { + write!(s, "{h}{short} ").unwrap(); + } } } - format!("{sign}{s}") + format!("{sign}{}", s.trim()) +} +pub fn format_size(size: u64) -> String { + humansize::format_size(size, humansize::DECIMAL) } pub trait DatabaseNodeUserDataExt { diff --git a/server/src/routes/ui/stats.rs b/server/src/routes/ui/stats.rs new file mode 100644 index 0000000..927d85c --- /dev/null +++ b/server/src/routes/ui/stats.rs @@ -0,0 +1,116 @@ +/* + 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 super::{ + account::session::Session, + error::MyError, + layout::{DynLayoutPage, LayoutPage}, +}; +use crate::{ + database::Database, + routes::{ + api::AcceptJson, + ui::node::{ + format_duration, format_duration_long, format_size, rocket_uri_macro_r_library_node, + }, + }, + uri, +}; +use jellycommon::{Node, NodeID, NodeKind, Visibility}; +use rocket::{get, serde::json::Json, Either, State}; +use serde::Serialize; +use serde_json::{json, Value}; +use std::collections::BTreeMap; + +#[get("/stats")] +pub fn r_stats( + sess: Session, + db: &State<Database>, + aj: AcceptJson, +) -> Result<Either<DynLayoutPage<'_>, Json<Value>>, MyError> { + let mut items = db.list_nodes_with_udata(sess.user.name.as_str())?; + items.retain(|(n, _)| matches!(n.visibility, Visibility::Visible)); + + #[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); + } + + Ok(if *aj { + Either::Right(Json(json!({ + "all": all, + "kinds": kinds, + }))) + } else { + Either::Left(LayoutPage { + title: "Library Statistics".to_owned(), + content: markup::new! { + .page.stats { + h1 { "Library Statistics" } + p { "There is a total of " b{@all.count} " nodes in the library." } + p { "The total runtime of the library is " b{@format_duration_long(all.runtime)} ", taking up " b{@format_size(all.size)} " of disk space." } + p { "An average node has a runtime of " b{@format_duration(all.average_runtime())} " and file size of " b{@format_size(all.average_size() as u64)} "." } + + h2 { "Grouped by Kind" } + table.striped { + tr { + th { "Kind" } + th { "Count" } + th { "Storage Size" } + th { "Media Runtime" } + th { "Average Size" } + th { "Average Runtime" } + th { "Largest File" } + th { "Longest Runtime" } + } + @for (k,b) in &kinds { tr { + td { @format!("{k:?}") } + td { @b.count } + td { @format_size(b.size) } + td { @format_duration(b.runtime) } + td { @format_size(b.average_size() as u64) } + td { @format_duration(b.average_runtime()) } + td { @if b.max_size.0 > 0 { a[href=uri!(r_library_node(&b.max_size.1))]{ @format_size(b.max_size.0) }}} + td { @if b.max_runtime.0 > 0. { a[href=uri!(r_library_node(&b.max_runtime.1))]{ @format_duration(b.max_runtime.0) }}} + }} + } + } + }, + ..Default::default() + }) + }) +} |