introduce sbot status data

This commit is contained in:
glyph 2022-02-01 10:06:45 +02:00
parent 3397e5eb75
commit d801c957bd
7 changed files with 58 additions and 41 deletions

View File

@ -1,4 +1,4 @@
use peach_stats::sbot; use peach_lib::sbot::SbotStatus;
use rocket::{get, request::FlashMessage, State}; use rocket::{get, request::FlashMessage, State};
use rocket_dyn_templates::{tera::Context, Template}; use rocket_dyn_templates::{tera::Context, Template};
@ -9,11 +9,11 @@ use crate::RocketConfig;
#[get("/")] #[get("/")]
pub fn home(_auth: Authenticated, config: &State<RocketConfig>) -> Template { pub fn home(_auth: Authenticated, config: &State<RocketConfig>) -> Template {
// retrieve go-sbot systemd process stats // retrieve go-sbot systemd process status
let sbot_stats = sbot::sbot_stats().ok(); let sbot_status = SbotStatus::read().ok();
let mut context = Context::new(); let mut context = Context::new();
context.insert("sbot_stats", &sbot_stats); context.insert("sbot_status", &sbot_status);
context.insert("flash_name", &None::<()>); context.insert("flash_name", &None::<()>);
context.insert("flash_msg", &None::<()>); context.insert("flash_msg", &None::<()>);
context.insert("title", &None::<()>); context.insert("title", &None::<()>);

View File

@ -12,7 +12,7 @@ use std::{
}; };
use peach_lib::{ use peach_lib::{
config_manager::load_peach_config, dyndns_client, network_client, oled_client, sbot_client, config_manager::load_peach_config, dyndns_client, network_client, oled_client, sbot::SbotStatus,
}; };
use peach_stats::{ use peach_stats::{
stats, stats,
@ -117,10 +117,11 @@ impl StatusContext {
} }
// test if go-sbot is running // test if go-sbot is running
let sbot_is_online_result = sbot_client::is_sbot_online(); let sbot_status = SbotStatus::read();
let sbot_is_online: bool = match sbot_is_online_result { let sbot_is_online: bool = match sbot_status {
Ok(val) => val, // return true if state is active
Err(_err) => false, Ok(status) => matches!(status.state == Some("active".to_string()), true),
_ => false,
}; };
StatusContext { StatusContext {

View File

@ -1,4 +1,4 @@
use peach_stats::sbot; use peach_lib::sbot::{SbotConfig, SbotStatus};
use rocket::{get, State}; use rocket::{get, State};
use rocket_dyn_templates::{tera::Context, Template}; use rocket_dyn_templates::{tera::Context, Template};
@ -9,10 +9,14 @@ use crate::RocketConfig;
#[get("/scuttlebutt")] #[get("/scuttlebutt")]
pub fn scuttlebutt_status(_auth: Authenticated, config: &State<RocketConfig>) -> Template { pub fn scuttlebutt_status(_auth: Authenticated, config: &State<RocketConfig>) -> Template {
// retrieve go-sbot systemd process status
let sbot_status = SbotStatus::read().ok();
// retrieve go-sbot configuration parameters
let sbot_config = SbotConfig::read().ok();
let mut context = Context::new(); let mut context = Context::new();
// retrieve go-sbot systemd process stats context.insert("sbot_status", &sbot_status);
let sbot_stats = sbot::sbot_stats().ok(); context.insert("sbot_config", &sbot_config);
context.insert("sbot_stats", &sbot_stats);
context.insert("flash_name", &None::<()>); context.insert("flash_name", &None::<()>);
context.insert("flash_msg", &None::<()>); context.insert("flash_msg", &None::<()>);
context.insert("title", &Some("Scuttlebutt Status")); context.insert("title", &Some("Scuttlebutt Status"));

View File

@ -25,7 +25,7 @@
</a> </a>
<!-- middle --> <!-- middle -->
<a class="middle"> <a class="middle">
<div class="circle circle-large {% if sbot_stats.state == "active" %}circle-success{% else %}circle-error{% endif %}"></div> <div class="circle circle-large {% if sbot_status.state == "active" %}circle-success{% else %}circle-error{% endif %}"></div>
</a> </a>
<!-- bottom-left --> <!-- bottom-left -->
<!-- SYSTEM STATUS LINK AND ICON --> <!-- SYSTEM STATUS LINK AND ICON -->

View File

@ -1,20 +1,32 @@
{%- extends "nav" -%} {%- extends "nav" -%}
{%- block card %} {%- block card %}
{# ASSIGN VARIABLES #}
{# ---------------- #}
{%- if sbot_config.hops -%}
{% set hops = sbot_config.hops -%}
{%- else -%}
{% set hops = "X" -%}
{%- endif -%}
{%- if sbot_config.lis -%}
{%- set listen_addr = sbot_config.lis | split(pat=":") -%}
{%- else -%}
{%- set listen_addr = ["", ""] -%}
{%- endif -%}
<!-- SBOT CONFIGURATION FORM --> <!-- SBOT CONFIGURATION FORM -->
<div class="card center"> <div class="card center">
<form> <form>
<div class="center" style="display: flex; flex-direction: column; width: 80%;" title="Number of hops to replicate"> <div class="center" style="display: flex; flex-direction: column; width: 80%;" title="Number of hops to replicate">
<label for="hops" class="label-small">HOPS</label> <label for="hops" class="label-small">HOPS</label>
<div id="hops" style="display: flex; justify-content: space-evenly;"> <div id="hops" style="display: flex; justify-content: space-evenly;">
<input type="radio" id="hops_0" name="hops" value="0"> <input type="radio" id="hops_0" name="hops" value="0"{% if hops == 0 %} checked{% endif %}>
<label for="hops_0">0</label> <label for="hops_0">0</label>
<input type="radio" id="hops_1" name="hops" value="1"> <input type="radio" id="hops_1" name="hops" value="1"{% if hops == 1 %} checked{% endif %}>
<label for="hops_1">1</label> <label for="hops_1">1</label>
<input type="radio" id="hops_2" name="hops" value="2"> <input type="radio" id="hops_2" name="hops" value="2"{% if hops == 2 %} checked{% endif %}>
<label for="hops_2">2</label> <label for="hops_2">2</label>
<input type="radio" id="hops_3" name="hops" value="3"> <input type="radio" id="hops_3" name="hops" value="3"{% if hops == 3 %} checked{% endif %}>
<label for="hops_3">3</label> <label for="hops_3">3</label>
<input type="radio" id="hops_4" name="hops" value="4"> <input type="radio" id="hops_4" name="hops" value="4"{% if hops == 4 %} checked{% endif %}>
<label for="hops_4">4</label><br> <label for="hops_4">4</label><br>
</div> </div>
</div> </div>
@ -22,32 +34,32 @@
<div class="center" style="display: flex; justify-content: space-between; width: 80%;"> <div class="center" style="display: flex; justify-content: space-between; width: 80%;">
<div style="display: flex; flex-direction: column; width: 60%;" title="IP address on which the sbot runs"> <div style="display: flex; flex-direction: column; width: 60%;" title="IP address on which the sbot runs">
<label for="ip" class="label-small">IP ADDRESS</label> <label for="ip" class="label-small">IP ADDRESS</label>
<input type="text" id="ip" name="ip"> <input type="text" id="ip" name="ip" value="{{ listen_addr.0 }}">
</div> </div>
<div style="display: flex; flex-direction: column; width: 20%;" title="Port on which the sbot runs"> <div style="display: flex; flex-direction: column; width: 20%;" title="Port on which the sbot runs">
<label for="port" class="label-small">PORT</label> <label for="port" class="label-small">PORT</label>
<input type="text" id="port" name="port"> <input type="text" id="port" name="port" value="{{ listen_addr.1 }}">
</div> </div>
</div> </div>
<br> <br>
<div class="center" style="display: flex; flex-direction: column; width: 80%;" title="Network key (aka 'caps key') to define the Scuttleverse in which the sbot operates in"> <div class="center" style="display: flex; flex-direction: column; width: 80%;" title="Network key (aka 'caps key') to define the Scuttleverse in which the sbot operates in">
<label for="network_key" class="label-small">NETWORK KEY</label> <label for="network_key" class="label-small">NETWORK KEY</label>
<input type="text" id="network_key" name="network_key"> <input type="text" id="network_key" name="network_key" value="{{ sbot_config.shscap }}">
</div> </div>
<br> <br>
<div class="center" style="display: flex; flex-direction: column; width: 80%;" title="Directory in which the sbot database is saved"> <div class="center" style="display: flex; flex-direction: column; width: 80%;" title="Directory in which the sbot database is saved">
<label for="database_dir" class="label-small">DATABASE DIRECTORY</label> <label for="database_dir" class="label-small">DATABASE DIRECTORY</label>
<input type="text" id="database_dir" name="database_dir"> <input type="text" id="database_dir" name="database_dir" value="{{ sbot_config.repo }}">
</div> </div>
<br> <br>
<div class="center" style="width: 80%;" title="Broadcast the IP and port of this sbot instance so that local peers can discovery it and attempt to connect"> <div class="center" style="width: 80%;" title="Broadcast the IP and port of this sbot instance so that local peers can discovery it and attempt to connect">
<input type="checkbox" id="lanBroadcast" name="lan_broadcast"> <input type="checkbox" id="lanBroadcast" name="lan_broadcast"{% if sbot_config.localadv == true %} checked{% endif %}>
<label for="lanBroadcast">Enable LAN Broadcasting</label><br> <label for="lanBroadcast">Enable LAN Broadcasting</label><br>
<br> <br>
<input type="checkbox" id="lanDiscovery" name="lan_discovery" title="Listen for the presence of local peers and attempt to connect if found"> <input type="checkbox" id="lanDiscovery" name="lan_discovery" title="Listen for the presence of local peers and attempt to connect if found"{% if sbot_config.localdiscov == true %} checked{% endif %}>
<label for="lanDiscovery">Enable LAN Discovery</label><br> <label for="lanDiscovery">Enable LAN Discovery</label><br>
<br> <br>
<input type="checkbox" id="startup" name="startup" title="Define whether the pub runs automatically on system startup"> <input type="checkbox" id="startup" name="startup" title="Define whether the pub runs automatically on system startup"{% if run_on_startup == "enabled" %}checked{% endif %}>
<label for="startup">Run pub on system startup</label><br> <label for="startup">Run pub on system startup</label><br>
</div> </div>
<br> <br>

View File

@ -5,7 +5,7 @@
<!-- BUTTONS --> <!-- BUTTONS -->
<div id="settingsButtons"> <div id="settingsButtons">
<a id="configureSbot" class="button button-primary center" href="/settings/scuttlebutt/configure" title="Configure Sbot">Configure Sbot</a> <a id="configureSbot" class="button button-primary center" href="/settings/scuttlebutt/configure" title="Configure Sbot">Configure Sbot</a>
{% if sbot_stats.state == "active" %} {% if sbot_status.state == "active" %}
<a id="stop" class="button button-primary center" href="/settings/scuttlebutt/stop" title="Stop Sbot">Stop Sbot</a> <a id="stop" class="button button-primary center" href="/settings/scuttlebutt/stop" title="Stop Sbot">Stop Sbot</a>
<a id="restart" class="button button-primary center" href="/settings/scuttlebutt/restart" title="Restart Sbot">Restart Sbot</a> <a id="restart" class="button button-primary center" href="/settings/scuttlebutt/restart" title="Restart Sbot">Restart Sbot</a>
{% else %} {% else %}

View File

@ -2,15 +2,15 @@
{%- block card %} {%- block card %}
{# ASSIGN VARIABLES #} {# ASSIGN VARIABLES #}
{# ---------------- #} {# ---------------- #}
{%- if sbot_stats.memory -%} {%- if sbot_status.memory -%}
{% set mem = sbot_stats.memory / 1024 / 1024 | round -%} {% set mem = sbot_status.memory / 1024 / 1024 | round -%}
{%- else -%} {%- else -%}
{% set mem = "X" -%} {% set mem = "X" -%}
{%- endif -%} {%- endif -%}
<!-- SCUTTLEBUTT STATUS --> <!-- SCUTTLEBUTT STATUS -->
<div class="card center"> <div class="card center">
<!-- SBOT INFO BOX --> <!-- SBOT INFO BOX -->
<div class="capsule capsule-container {% if sbot_stats.state == "active" %}success-border{% else %}warning-border{% endif %}"> <div class="capsule capsule-container {% if sbot_status.state == "active" %}success-border{% else %}warning-border{% endif %}">
<!-- SBOT STATUS GRID --> <!-- SBOT STATUS GRID -->
<div class="two-grid" title="go-sbot process state"> <div class="two-grid" title="go-sbot process state">
<!-- top-right config icon --> <!-- top-right config icon -->
@ -20,25 +20,25 @@
<!-- left column --> <!-- left column -->
<!-- go-sbot state icon with label --> <!-- go-sbot state icon with label -->
<div class="grid-column-1"> <div class="grid-column-1">
<img id="sbotStateIcon" class="center icon {% if sbot_stats.state == "inactive" %}icon-inactive{% endif %}" src="/icons/hermies.svg" alt="Hermies"> <img id="sbotStateIcon" class="center icon {% if sbot_status.state == "inactive" %}icon-inactive{% endif %}" src="/icons/hermies.svg" alt="Hermies">
<label id="sbotStateLabel" for="sbotStateIcon" class="center label-small font-gray" style="margin-top: 0.5rem;" title="Sbot state">{{ sbot_stats.state | upper }}</label> <label id="sbotStateLabel" for="sbotStateIcon" class="center label-small font-gray" style="margin-top: 0.5rem;" title="Sbot state">{{ sbot_status.state | upper }}</label>
</div> </div>
<!-- right column --> <!-- right column -->
<!-- go-sbot version and uptime / downtime with labels --> <!-- go-sbot version and uptime / downtime with labels -->
<div class="grid-column-2"> <div class="grid-column-2">
<label class="label-small font-gray" for="sbotVersion" title="go-sbot version">VERSION</label> <label class="label-small font-gray" for="sbotVersion" title="go-sbot version">VERSION</label>
<p id="sbotVersion" class="card-text" title="Version">1.1.0-alpha</p> <p id="sbotVersion" class="card-text" title="Version">1.1.0-alpha</p>
{% if sbot_stats.state == "active" %} {% if sbot_status.state == "active" %}
<label class="label-small font-gray" for="sbotUptime" title="go-sbot uptime" style="margin-top: 0.5rem;">UPTIME</label> <label class="label-small font-gray" for="sbotUptime" title="go-sbot uptime" style="margin-top: 0.5rem;">UPTIME</label>
<p id="sbotUptime" class="card-text" title="Uptime">{{ sbot_stats.uptime }}</p> <p id="sbotUptime" class="card-text" title="Uptime">{{ sbot_status.uptime }}</p>
{# render downtime element if downtime is `Some(time)` #} {# render downtime element if downtime is `Some(time)` #}
{# downtime will be `None` if service is stopped and disabled #} {# downtime will be `None` if service is stopped and disabled #}
{%- elif sbot_stats.downtime -%} {%- elif sbot_status.downtime -%}
<label class="label-small font-gray" for="sbotDowntime" title="go-sbot downtime" style="margin-top: 0.5rem;">DOWNTIME</label> <label class="label-small font-gray" for="sbotDowntime" title="go-sbot downtime" style="margin-top: 0.5rem;">DOWNTIME</label>
<p id="sbotDowntime" class="card-text" title="Downtime">{{ sbot_stats.downtime }}</p> <p id="sbotDowntime" class="card-text" title="Downtime">{{ sbot_status.downtime }}</p>
{%- endif -%} {%- endif -%}
<label class="label-small font-gray" for="sbotBootState" title="go-sbot boot state" style="margin-top: 0.5rem;">RUN ON STARTUP</label> <label class="label-small font-gray" for="sbotBootState" title="go-sbot boot state" style="margin-top: 0.5rem;">RUN ON STARTUP</label>
{% if sbot_stats.boot_state == "enabled" %} {% if sbot_status.boot_state == "enabled" %}
<p id="runOnStartup" class="card-text" title="Enabled">Enabled</p> <p id="runOnStartup" class="card-text" title="Enabled">Enabled</p>
{% else %} {% else %}
<p id="runOnStartup" class="card-text" title="Disabled">Disabled</p> <p id="runOnStartup" class="card-text" title="Disabled">Disabled</p>
@ -80,7 +80,7 @@
<div class="stack"> <div class="stack">
<img class="icon" title="Hops" src="/icons/orbits.png"> <img class="icon" title="Hops" src="/icons/orbits.png">
<div class="flex-grid" style="padding-top: 0.5rem;"> <div class="flex-grid" style="padding-top: 0.5rem;">
<label class="label-medium" style="padding-right: 3px;" title="Replication hops">2</label> <label class="label-medium" style="padding-right: 3px;" title="Replication hops">{{ sbot_config.hops }}</label>
</div> </div>
<label class="label-small font-gray">HOPS</label> <label class="label-small font-gray">HOPS</label>
</div> </div>
@ -93,10 +93,10 @@
<label class="label-small font-gray">BLOBSTORE</label> <label class="label-small font-gray">BLOBSTORE</label>
</div> </div>
<div class="stack"> <div class="stack">
<img class="icon{% if not sbot_stats.memory %} icon-inactive{% endif %}" title="Memory" src="/icons/ram.png"> <img class="icon{% if not sbot_status.memory %} icon-inactive{% endif %}" title="Memory" src="/icons/ram.png">
<div class="flex-grid" style="padding-top: 0.5rem;"> <div class="flex-grid" style="padding-top: 0.5rem;">
<label class="label-medium{% if sbot_stats.state == "inactive" %} font-gray{% endif %}" style="padding-right: 3px;" title="Memory usage of the go-sbot process in MB">{{ mem }}</label> <label class="label-medium{% if sbot_status.state == "inactive" %} font-gray{% endif %}" style="padding-right: 3px;" title="Memory usage of the go-sbot process in MB">{{ mem }}</label>
<label class="label-small {% if sbot_stats.state == "inactive" %}font-gray{% else %}font-near-black{% endif %}">MB</label> <label class="label-small {% if sbot_status.state == "inactive" %}font-gray{% else %}font-near-black{% endif %}">MB</label>
</div> </div>
<label class="label-small font-gray">MEMORY</label> <label class="label-small font-gray">MEMORY</label>
</div> </div>