aboutsummaryrefslogtreecommitdiff
path: root/server/src/ui/stats.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/ui/stats.rs')
-rw-r--r--server/src/ui/stats.rs131
1 files changed, 131 insertions, 0 deletions
diff --git a/server/src/ui/stats.rs b/server/src/ui/stats.rs
new file mode 100644
index 0000000..4c5bed8
--- /dev/null
+++ b/server/src/ui/stats.rs
@@ -0,0 +1,131 @@
+/*
+ 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::{
+ error::MyError,
+ 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,
+};
+use jellybase::locale::tr;
+use jellycommon::{Node, NodeID, NodeKind, Visibility};
+use markup::raw;
+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,
+ lang: AcceptLanguage,
+) -> Result<Either<DynLayoutPage<'_>, Json<Value>>, 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);
+ }
+
+ Ok(if *aj {
+ Either::Right(Json(json!({
+ "all": all,
+ "kinds": kinds,
+ })))
+ } else {
+ Either::Left(LayoutPage {
+ title: tr(lang, "stats.title").to_string(),
+ content: markup::new! {
+ .page.stats {
+ h1 { @trs(&lang, "stats.title") }
+ p { @raw(tr(lang, "stats.count")
+ .replace("{count}", &format!("<b>{}</b>", all.count))
+ )}
+ p { @raw(tr(lang, "stats.runtime")
+ .replace("{dur}", &format!("<b>{}</b>", format_duration_long(all.runtime, lang)))
+ .replace("{size}", &format!("<b>{}</b>", format_size(all.size)))
+ )}
+ p { @raw(tr(lang, "stats.average")
+ .replace("{dur}", &format!("<b>{}</b>", format_duration(all.average_runtime())))
+ .replace("{size}", &format!("<b>{}</b>", format_size(all.average_size() as u64)))
+ )}
+
+ h2 { @trs(&lang, "stats.by_kind.title") }
+ table.striped {
+ tr {
+ th { @trs(&lang, "stats.by_kind.kind") }
+ th { @trs(&lang, "stats.by_kind.count") }
+ th { @trs(&lang, "stats.by_kind.total_size") }
+ th { @trs(&lang, "stats.by_kind.total_runtime") }
+ th { @trs(&lang, "stats.by_kind.average_size") }
+ th { @trs(&lang, "stats.by_kind.average_runtime") }
+ th { @trs(&lang, "stats.by_kind.max_size") }
+ th { @trs(&lang, "stats.by_kind.max_runtime") }
+ }
+ @for (k,b) in &kinds { tr {
+ td { @format_kind(*k, lang) }
+ 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()
+ })
+ })
+}