From 7cdf8c553db4c781815cde1be708f5d26f939a3c Mon Sep 17 00:00:00 2001 From: glyph Date: Thu, 17 Mar 2022 16:30:26 +0200 Subject: [PATCH] complete scuttlebutt status route --- peach-web/src/routes/status/scuttlebutt.rs | 147 ++++++++++++++------- 1 file changed, 101 insertions(+), 46 deletions(-) diff --git a/peach-web/src/routes/status/scuttlebutt.rs b/peach-web/src/routes/status/scuttlebutt.rs index f2eb965..9e1bde6 100644 --- a/peach-web/src/routes/status/scuttlebutt.rs +++ b/peach-web/src/routes/status/scuttlebutt.rs @@ -8,21 +8,6 @@ use peach_lib::sbot::{SbotConfig, SbotStatus}; use crate::{error::PeachWebError, templates}; -/* -{# ASSIGN VARIABLES #} -{# ---------------- #} -{%- if sbot_status.memory -%} -{% set mem = sbot_status.memory / 1024 / 1024 | round | int -%} -{%- else -%} -{% set mem = "0" -%} -{%- endif -%} -{%- if sbot_status.blobstore -%} -{% set blobs = sbot_status.blobstore / 1024 / 1024 | round | int -%} -{%- else -%} -{% set blobs = "0" -%} -{%- endif -%} -*/ - // HELPER FUNCTIONS pub async fn init_sbot_with_config( @@ -116,9 +101,89 @@ fn database_element(state: &str) -> Markup { } } +fn memory_element(memory: Option) -> Markup { + let (memory, img_class, medium_label_class, small_label_class) = match memory { + Some(mem) => { + // convert memory to mb representation + let memory_rounded = mem / 1024 / 1024; + ( + memory_rounded.to_string(), + "icon icon-active", + "label-medium font-normal", + "label-small font-normal", + ) + } + _ => ( + 0.to_string(), + "icon icon-inactive", + "label-medium font-gray", + "label-small font-gray", + ), + }; + + html! { + div class="stack" { + img class=(img_class) title="Memory" src="/icons/ram.png"; + div class="flex-grid" style="padding-top: 0.5rem;" { + label class=(medium_label_class) style="padding-right: 3px;" title="Memory usage of the go-sbot process in MB" { (memory) } + label class=(small_label_class) { "MB" } + } + label class=(small_label_class) { "MEMORY" } + } + } +} + +fn hops_element() -> Markup { + // retrieve go-sbot systemd process status + let hops = match SbotConfig::read() { + Ok(conf) => conf.hops, + _ => 0, + }; + + html! { + div class="stack" { + img class="icon icon-active" title="Hops" src="/icons/orbits.png"; + div class="flex-grid" style="padding-top: 0.5rem;" { + label class="label-medium font-normal" style="padding-right: 3px;" title="Replication hops" { + (hops) + } + } + label class="label-small font-gray" { "HOPS" } + } + } +} + +fn blobs_element(blobstore: Option) -> Markup { + let blobstore_size = match blobstore { + // convert blobstore size to mb representation + Some(blobs) => blobs / 1024 / 1024, + None => 0, + }; + + html! { + div class="stack" { + img class="icon icon-active" title="Blobs" src="/icons/image-file.png"; + div class="flex-grid" style="padding-top: 0.5rem;" { + label class="label-medium font-normal" style="padding-right: 3px;" title="Blobstore size in MB" { (blobstore_size) } + label class="label-small font-normal" { "MB" } + } + label class="label-small font-gray" { "BLOBSTORE" } + } + } +} + /// Read the state of the go-sbot process and define status-related /// elements accordingly. -fn render_status_elements<'a>() -> (String, &'a str, &'a str, Markup, Markup, Markup) { +fn render_status_elements<'a>() -> ( + String, + &'a str, + &'a str, + Markup, + Markup, + Markup, + Markup, + Markup, +) { // retrieve go-sbot systemd process status let sbot_status = SbotStatus::read(); @@ -134,6 +199,8 @@ fn render_status_elements<'a>() -> (String, &'a str, &'a str, Markup, Markup, Ma uptime_element(&status.uptime), run_on_startup_element(&status.boot_state), database_element("active"), + memory_element(status.memory), + blobs_element(status.blobstore), ), Some(state) if state == "inactive" => ( "INACTIVE".to_string(), @@ -142,6 +209,8 @@ fn render_status_elements<'a>() -> (String, &'a str, &'a str, Markup, Markup, Ma downtime_element(&status.downtime), run_on_startup_element(&status.boot_state), database_element("inactive"), + memory_element(None), + blobs_element(status.blobstore), ), // state is neither active nor inactive (might be failed) Some(state) => ( @@ -151,6 +220,8 @@ fn render_status_elements<'a>() -> (String, &'a str, &'a str, Markup, Markup, Ma downtime_element(&None), run_on_startup_element(&status.boot_state), database_element("failed"), + memory_element(None), + blobs_element(status.blobstore), ), None => ( "UNAVAILABLE".to_string(), @@ -159,6 +230,8 @@ fn render_status_elements<'a>() -> (String, &'a str, &'a str, Markup, Markup, Ma downtime_element(&None), run_on_startup_element(&status.boot_state), database_element("unavailable"), + memory_element(None), + blobs_element(status.blobstore), ), } // show an error state if the attempt to read the go-sbot process @@ -171,6 +244,8 @@ fn render_status_elements<'a>() -> (String, &'a str, &'a str, Markup, Markup, Ma downtime_element(&None), run_on_startup_element(&None), database_element("error"), + memory_element(None), + blobs_element(None), ) } } @@ -184,8 +259,12 @@ pub fn build_template() -> PreEscaped { uptime_downtime_element, run_on_startup_element, database_element, + memory_element, + blobs_element, ) = render_status_elements(); + let hops_element = hops_element(); + let status_template = html! { (PreEscaped("")) div class="card center" { @@ -228,36 +307,12 @@ pub fn build_template() -> PreEscaped { } } hr style="color: var(--light-gray);"; - /* - (PreEscaped(" -
-
- -
- -
- -
-
- -
- - -
- -
-
- -
- - -
- -
-
- - - */ + (PreEscaped("")) + div class="three-grid card-container" style="margin-top: 1rem;" { + (hops_element) + (blobs_element) + (memory_element) + } } } };