aboutsummaryrefslogtreecommitdiff
path: root/src/webui.rs
blob: 73add5f3e24b5fc6808e510248f1fda923d8c33e (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
use crate::{
    State,
    helper::{Css, Javascript},
};
use axum::extract::State as S;
use axum::response::Html;
use markup::doctype;
use serde_json::{Map, Value};
use std::{collections::HashSet, sync::Arc};
use tokio::{fs::read_to_string, sync::RwLock};

pub(crate) async fn webui_style() -> Css<String> {
    Css(if cfg!(debug_assertions) {
        read_to_string("src/style.css").await.unwrap()
    } else {
        include_str!("style.css").to_string()
    })
}
pub(crate) async fn webui_script() -> Javascript<String> {
    Javascript(if cfg!(debug_assertions) {
        read_to_string("src/webui_live.js").await.unwrap()
    } else {
        include_str!("webui_live.js").to_string()
    })
}

pub(crate) async fn webui(S(state): S<Arc<RwLock<State>>>) -> Html<String> {
    let g = state.read().await;
    let default = &Map::new();
    let g = &g;

    let doc = markup::new! {
        @doctype()
        html {
            head {
                meta[charset="UTF-8"];
                link[rel="stylesheet", href="/style.css"];
                script[src="/webui_live.js", defer] {}
                title { @env!("CARGO_PKG_NAME") }
            }
            body {
                section[id="workers"] {
                    h2 { "Workers"}
                    ul { @for (id, w) in &g.workers {
                        li { @Worker { id: *id, w } }
                    }}
                }
                section.tasks {
                    @Taskbin {title: "Queued", state: "queue", set: &g.queue, default, g }
                    @Taskbin {title: "Loading", state: "loading", set: &g.loading, default, g }
                    @Taskbin {title: "Completed", state: "complete", set: &g.complete, default, g }
                }
            }
        }
    };
    Html(doc.to_string())
}

markup::define!(
    Taskbin<'a>(title: &'a str, state: &'a str, set: &'a HashSet<String>, g: &'a State, default: &'a Map<String, Value>) {
        div[id=format!("bin-{state}")] {
            h2 { @title }
            p.count { @set.len() " tasks" }
            @let class = format!("task {state}");
            ul { @for key in set.iter().take(128) {
                li { @Task { key, data: g.metadata.get(key).unwrap_or(&default), class: &class } }
            }}
        }
    }
    Task<'a>(key: &'a str, data: &'a Map<String, Value>, class: &'a str) {
        div[class=class, id=key, style=task_style(data)] {
            // @if let Some(url) = data.get("thumbnail").and_then(Value::as_str) {
            //     img[src=url, loading="lazy"];
            // }
            h3 { @data.get("title").and_then(Value::as_str).unwrap_or(key) }
            @if let Some(s) = data.get("subtitle").and_then(Value::as_str) {
                span.subtitle { @s } br;
            }
            span.key { @key }
            @if let Some(s) = data.get("status").and_then(Value::as_str) {
                pre.status { @s }
            }
        }
    }
    Worker<'a>(id: u64, w: &'a crate::Worker) {
        div[class=worker_class(w), id=format!("worker-{id}")] {
            h3 { @w.name }
            span { "ID: " @id } ", "
            @if !w.assigned_tasks.is_empty() {
                span { "Busy (" @w.assigned_tasks.len() ")" }
            } else if w.accept > 0 {
                span { "Accepting Tasks (" @w.accept ")" }
            } else {
                span { "Idle" }
            }
        }
    }
);

fn task_style(data: &Map<String, Value>) -> Option<String> {
    data.get("progress")
        .and_then(Value::as_f64)
        .map(|p| format!("background-size: {:.02}%;", p * 100.))
}

fn worker_class(w: &crate::Worker) -> &'static str {
    if w.accept > 0 {
        "worker accepting"
    } else if w.assigned_tasks.is_empty() {
        "worker idle"
    } else {
        "worker busy"
    }
}