refine sbot status pattern matching

This commit is contained in:
glyph 2022-03-18 11:25:53 +02:00
parent 7cdf8c553d
commit 7fe919d9a1
1 changed files with 47 additions and 55 deletions

View File

@ -8,7 +8,7 @@ use peach_lib::sbot::{SbotConfig, SbotStatus};
use crate::{error::PeachWebError, templates};
// HELPER FUNCTIONS
// SBOT HELPER FUNCTIONS
pub async fn init_sbot_with_config(
sbot_config: &Option<SbotConfig>,
@ -47,6 +47,8 @@ fn latest_sequence_number() -> Result<u64, Box<dyn Error>> {
})
}
// HTML RENDERING FOR ELEMENTS
fn downtime_element(downtime: &Option<String>) -> Markup {
match downtime {
Some(time) => {
@ -185,59 +187,49 @@ fn render_status_elements<'a>() -> (
Markup,
) {
// retrieve go-sbot systemd process status
let sbot_status = SbotStatus::read();
// conditionally render the following elements:
// state, capsule border class, sbot icon class, uptime or downtime element,
// run on startup element and database (sequence number) element
if let Ok(status) = sbot_status {
match status.state {
Some(state) if state == "active" => (
"ACTIVE".to_string(),
"capsule capsule-container border-success",
"center icon icon-active",
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(),
"capsule capsule-container border-warning",
"center icon icon-inactive",
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) => (
state.to_string(),
"capsule capsule-container border-danger",
"center icon icon-inactive",
downtime_element(&None),
run_on_startup_element(&status.boot_state),
database_element("failed"),
memory_element(None),
blobs_element(status.blobstore),
),
None => (
"UNAVAILABLE".to_string(),
"capsule capsule-container border-danger",
"center icon icon-inactive",
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
// status fails
} else {
(
match SbotStatus::read() {
Ok(status) if status.state == Some("active".to_string()) => (
"ACTIVE".to_string(),
"capsule capsule-container border-success",
"center icon icon-active",
uptime_element(&status.uptime),
run_on_startup_element(&status.boot_state),
database_element("active"),
memory_element(status.memory),
blobs_element(status.blobstore),
),
Ok(status) if status.state == Some("inactive".to_string()) => (
"INACTIVE".to_string(),
"capsule capsule-container border-warning",
"center icon icon-inactive",
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")
Ok(status) if status.state.is_some() => (
status.state.unwrap(),
"capsule capsule-container border-danger",
"center icon icon-inactive",
downtime_element(&None),
run_on_startup_element(&status.boot_state),
database_element("failed"),
memory_element(None),
blobs_element(status.blobstore),
),
Ok(status) if status.state.is_none() => (
"UNAVAILABLE".to_string(),
"capsule capsule-container border-danger",
"center icon icon-inactive",
downtime_element(&None),
run_on_startup_element(&status.boot_state),
database_element("unavailable"),
memory_element(None),
blobs_element(status.blobstore),
),
_ => (
"PROCESS QUERY FAILED".to_string(),
"capsule capsule-container border-danger",
"center icon icon-inactive",
@ -246,7 +238,7 @@ fn render_status_elements<'a>() -> (
database_element("error"),
memory_element(None),
blobs_element(None),
)
),
}
}