aboutsummaryrefslogtreecommitdiff
path: root/server/src/routes/ui/stats.rs
blob: 927d85c74d2074b1b28d3c2f5763de2bf9c65c6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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()
        })
    })
}