Skip to main content

lfsx_server/
dashboard.rs

1use std::time::Duration;
2
3use crate::locks::{self, Lock};
4use crate::namespace::Namespace;
5
6pub struct Overview {
7    pub namespace: Namespace,
8    pub objects: u64,
9    pub bytes: u64,
10    pub locks: Vec<Lock>,
11    pub lock_max_age: Option<Duration>,
12    pub writable: bool,
13}
14
15pub fn render(overview: &Overview) -> String {
16    let Overview {
17        namespace,
18        objects,
19        bytes,
20        locks,
21        lock_max_age,
22        writable,
23    } = overview;
24
25    format!(
26        r#"<!doctype html>
27<html lang="en">
28<head>
29<meta charset="utf-8">
30<meta name="viewport" content="width=device-width, initial-scale=1">
31<title>{namespace} — LFSX</title>
32<style>{STYLE}</style>
33</head>
34<body>
35<main>
36<h1>{namespace}</h1>
37<dl>
38<div><dt>Objects</dt><dd>{objects}</dd></div>
39<div><dt>On disk</dt><dd>{}</dd></div>
40<div><dt>Your access</dt><dd>{}</dd></div>
41</dl>
42<h2>Locks</h2>
43{}
44<footer>Read only. Objects are reclaimed with <code>lfsx gc</code>, locks are released with
45<code>git lfs unlock</code>.</footer>
46</main>
47</body>
48</html>
49"#,
50        human_bytes(*bytes),
51        if *writable { "read and write" } else { "read" },
52        locks_table(locks, *lock_max_age),
53    )
54}
55
56// This is the only place a person is told a lock has gone stale. `git lfs locks`
57// prints the path, the owner and the id, and has no field for anything else, so
58// a client cannot be made to show it however the server phrases the JSON.
59fn locks_table(locks: &[Lock], max_age: Option<Duration>) -> String {
60    if locks.is_empty() {
61        return "<p class=\"empty\">Nothing is locked.</p>".to_owned();
62    }
63
64    let rows: String = locks
65        .iter()
66        .map(|lock| match locks::stale_for(lock, max_age) {
67            Some(age) => format!(
68                "<tr class=\"stale\"><td>{}</td><td>{}</td><td>{} (untouched for {}, anyone can take it)</td></tr>",
69                escape(&lock.path),
70                escape(&lock.owner.name),
71                escape(&lock.locked_at),
72                human_age(age)
73            ),
74            None => format!(
75                "<tr><td>{}</td><td>{}</td><td>{}</td></tr>",
76                escape(&lock.path),
77                escape(&lock.owner.name),
78                escape(&lock.locked_at)
79            ),
80        })
81        .collect();
82
83    format!(
84        "<table><thead><tr><th>Path</th><th>Held by</th><th>Since</th></tr></thead><tbody>{rows}</tbody></table>"
85    )
86}
87
88fn human_age(age: Duration) -> String {
89    const MINUTE: u64 = 60;
90    const HOUR: u64 = 60 * MINUTE;
91    const DAY: u64 = 24 * HOUR;
92
93    let seconds = age.as_secs();
94    let (count, unit) = match seconds {
95        s if s >= 7 * DAY => (s / (7 * DAY), "week"),
96        s if s >= DAY => (s / DAY, "day"),
97        s if s >= HOUR => (s / HOUR, "hour"),
98        s if s >= MINUTE => (s / MINUTE, "minute"),
99        // A short ceiling is a strange thing to configure, but "untouched for 0
100        // minutes" is a strange thing to print.
101        s => (s, "second"),
102    };
103
104    format!("{count} {unit}{}", if count == 1 { "" } else { "s" })
105}
106
107fn human_bytes(bytes: u64) -> String {
108    const UNITS: [&str; 5] = ["B", "KiB", "MiB", "GiB", "TiB"];
109    let mut size = bytes as f64;
110    let mut unit = 0;
111
112    while size >= 1024.0 && unit < UNITS.len() - 1 {
113        size /= 1024.0;
114        unit += 1;
115    }
116
117    if unit == 0 {
118        format!("{bytes} B")
119    } else {
120        format!("{size:.1} {}", UNITS[unit])
121    }
122}
123
124fn escape(raw: &str) -> String {
125    raw.replace('&', "&amp;")
126        .replace('<', "&lt;")
127        .replace('>', "&gt;")
128        .replace('"', "&quot;")
129}
130
131const STYLE: &str = "\
132:root{color-scheme:light dark}\
133body{font:16px/1.5 system-ui,sans-serif;margin:0;padding:2rem}\
134main{max-width:52rem;margin:0 auto}\
135h1{font-size:1.5rem;margin:0 0 1.5rem}\
136h2{font-size:1.1rem;margin:2rem 0 .75rem}\
137dl{display:grid;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));gap:1rem;margin:0}\
138dt{font-size:.8rem;text-transform:uppercase;letter-spacing:.04em;opacity:.65}\
139dd{margin:.25rem 0 0;font-size:1.5rem;font-variant-numeric:tabular-nums}\
140table{width:100%;border-collapse:collapse}\
141th{text-align:left;font-size:.8rem;text-transform:uppercase;letter-spacing:.04em;opacity:.65;font-weight:400}\
142th,td{padding:.5rem 0;border-bottom:1px solid color-mix(in srgb,currentColor 12%,transparent)}\
143td{font-variant-numeric:tabular-nums}\
144.empty{opacity:.65}\n.stale td{color:#b4690e}\
145footer{margin-top:2.5rem;font-size:.85rem;opacity:.65}\
146code{font-family:ui-monospace,monospace;font-size:.85em}";
147
148#[cfg(test)]
149mod tests;