aboutsummaryrefslogtreecommitdiff
path: root/src/webui.rs
blob: 5a66ffc79cef2133774a0875b48631bb56af94d2 (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
use crate::{State, helper::Css};
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(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"];
                title { "Queue-Server" }
            }
            body {
                section {
                    h2 { "Workers"}
                    ul { @for (id, w) in &g.workers {
                        li { @Worker { id: *id, w } }
                    }}
                }
                section.tasks {
                    @Taskbin {title: "Queued", class: "task queue", set: &g.queue, default, g }
                    @Taskbin {title: "Loading", class: "task loading", set: &g.loading, default, g }
                    @Taskbin {title: "Completed", class: "task complete", set: &g.complete, default, g }
                }
            }
        }
    };
    Html(doc.to_string())
}

markup::define!(
    Taskbin<'a>(title: &'a str, class: &'a str, set: &'a HashSet<String>, g: &'a State, default: &'a Map<String, Value>) {
        div {
            h2 { @title }
            p.count { @set.len() " tasks" }
            ul { @for key in set.iter().take(128) {
                li { @Task { key, data: g.metadata.get(key).unwrap_or(&default), class } }
            }}
        }
    }
    Task<'a>(key: &'a str, data: &'a Map<String, Value>, class: &'a str) {
        div[class=class] {
            // @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 }
        }
    }
    Worker<'a>(id: u64, w: &'a crate::Worker) {
        div[class=worker_class(w)] {
            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 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"
    }
}