aboutsummaryrefslogtreecommitdiff
path: root/ui/src/stats.rs
blob: 6be30bdefdda98298c29d69bca48451874cb4e3d (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
/*
    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 crate::{
    Page,
    format::{format_duration, format_duration_long, format_kind, format_size},
    locale::{Language, tr, trs},
};
use jellycommon::{
    api::{ApiStatsResponse, StatsBin},
    routes::u_node_slug,
};
use markup::raw;

impl Page for StatsPage<'_> {
    fn title(&self) -> String {
        tr(*self.lang, "stats.title").to_string()
    }
    fn to_render(&self) -> markup::DynRender<'_> {
        markup::new!(@self)
    }
}

markup::define! {
    StatsPage<'a>(lang: &'a Language, r: ApiStatsResponse) {
        .page.stats {
            h1 { @trs(&lang, "stats.title") }
            p { @raw(tr(**lang, "stats.count")
                .replace("{count}", &format!("<b>{}</b>", r.total.count))
            )}
            p { @raw(tr(**lang, "stats.runtime")
                .replace("{dur}", &format!("<b>{}</b>", format_duration_long(r.total.runtime, **lang)))
                .replace("{size}", &format!("<b>{}</b>", format_size(r.total.size)))
            )}
            p { @raw(tr(**lang, "stats.average")
                .replace("{dur}", &format!("<b>{}</b>",  format_duration(r.total.average_runtime())))
                .replace("{size}", &format!("<b>{}</b>", format_size(r.total.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 &r.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=u_node_slug(&b.max_size.1)]{ @format_size(b.max_size.0) }}}
                    td { @if b.max_runtime.0 > 0. { a[href=u_node_slug(&b.max_runtime.1)]{ @format_duration(b.max_runtime.0) }}}
                }}
            }
        }
    }
}

trait BinExt {
    fn average_runtime(&self) -> f64;
    fn average_size(&self) -> f64;
}
impl BinExt for StatsBin {
    fn average_runtime(&self) -> f64 {
        self.runtime / self.count as f64
    }
    fn average_size(&self) -> f64 {
        self.size as f64 / self.count as f64
    }
}