diff options
-rw-r--r-- | common/src/lib.rs | 17 | ||||
-rw-r--r-- | import/src/lib.rs | 19 | ||||
-rw-r--r-- | server/src/routes/mod.rs | 17 | ||||
-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 | ||||
-rw-r--r-- | web/style/layout.css | 9 |
8 files changed, 196 insertions, 31 deletions
diff --git a/common/src/lib.rs b/common/src/lib.rs index 3f9cfc3..c496cf2 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -55,6 +55,8 @@ pub struct Node { pub external_ids: BTreeMap<String, String>, #[serde(default)] pub visibility: Visibility, + #[serde(default)] + pub storage_size: u64, } #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Encode, Decode)] @@ -126,7 +128,20 @@ pub enum Visibility { Visible, } -#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Default, Encode, Decode)] +#[derive( + Debug, + Clone, + Copy, + Deserialize, + Serialize, + PartialEq, + Eq, + Default, + Encode, + Decode, + PartialOrd, + Ord, +)] #[serde(rename_all = "snake_case")] pub enum NodeKind { #[default] diff --git a/import/src/lib.rs b/import/src/lib.rs index 32f861e..7d1b635 100644 --- a/import/src/lib.rs +++ b/import/src/lib.rs @@ -335,11 +335,13 @@ fn import_media_file( let node = NodeID::from_slug(&slug); + let meta = path.metadata()?; + db.update_node_init(node, |node| { node.slug = slug; node.title = info.title.or(node.title.clone()); node.visibility = visibility; - node.poster = m.cover.clone(); + node.poster = m.cover.or(node.poster.clone()); node.description = tags .remove("DESCRIPTION") .or(tags.remove("SYNOPSIS")) @@ -438,6 +440,9 @@ fn import_media_file( } } + // TODO merge size + node.storage_size = meta.len(); + // TODO merge tracks node.media = Some(MediaInfo { chapters: m .chapters @@ -462,7 +467,9 @@ fn import_media_file( chaps }) .unwrap_or_default(), - duration: (info.duration.unwrap_or_default() * info.timestamp_scale as f64) * 1e-9, + duration: fix_invalid_runtime( + info.duration.unwrap_or_default() * info.timestamp_scale as f64 * 1e-9, + ), tracks, }); @@ -702,3 +709,11 @@ fn clean_uploader_name(mut s: &str) -> &str { s = s.strip_prefix("Uploads from ").unwrap_or(s); s } + +fn fix_invalid_runtime(d: f64) -> f64 { + match d { + // Broken durations found experimentally + 359999.999 | 359999.000 | 86399.999 | 86399.99900000001 => 0., + x => x, + } +} diff --git a/server/src/routes/mod.rs b/server/src/routes/mod.rs index 6d5dd5c..e909e15 100644 --- a/server/src/routes/mod.rs +++ b/server/src/routes/mod.rs @@ -51,22 +51,12 @@ use ui::{ r_account_login, r_account_login_post, r_account_logout, r_account_logout_post, r_account_register, r_account_register_post, settings::{r_account_settings, r_account_settings_post}, - }, - admin::{ + }, admin::{ log::{r_admin_log, r_admin_log_stream}, r_admin_dashboard, r_admin_delete_cache, r_admin_import, r_admin_invite, r_admin_remove_invite, r_admin_transcode_posters, r_admin_update_search, user::{r_admin_remove_user, r_admin_user, r_admin_user_permission, r_admin_users}, - }, - assets::{r_asset, r_item_backdrop, r_item_poster, r_node_thumbnail, r_person_asset}, - browser::r_all_items_filter, - error::{r_api_catch, r_catch}, - home::r_home, - node::r_library_node_filter, - player::r_player, - r_index, - search::r_search, - style::{r_assets_font, r_assets_js, r_assets_js_map, r_assets_style}, + }, assets::{r_asset, r_item_backdrop, r_item_poster, r_node_thumbnail, r_person_asset}, browser::r_all_items_filter, error::{r_api_catch, r_catch}, home::r_home, node::r_library_node_filter, player::r_player, r_index, search::r_search, stats::r_stats, style::{r_assets_font, r_assets_js, r_assets_js_map, r_assets_style} }; use userdata::{ r_node_userdata, r_node_userdata_progress, r_node_userdata_rating, r_node_userdata_watched, @@ -170,9 +160,10 @@ pub fn build_rocket(database: Database, federation: Federation) -> Rocket<Build> r_node_userdata, r_person_asset, r_player, + r_playersync, r_search, + r_stats, r_stream, - r_playersync, // API r_api_account_login, r_api_asset_token_raw, 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() + }) + }) +} diff --git a/web/style/layout.css b/web/style/layout.css index 9f63c53..29c179d 100644 --- a/web/style/layout.css +++ b/web/style/layout.css @@ -158,7 +158,10 @@ summary h3 { width: max(10em, 40%); } -table.border td, -table.border th { - border: 1px solid gray; +table.striped tr:nth-child(2n) { + background-color: #fff2; +} +table.striped td { + border: none; + padding: 5px; } |