made endpoint status into an enum

This commit is contained in:
Edward Shen 2019-05-03 16:30:56 -04:00
parent 443dca2841
commit ecb0b8ca6a
Signed by: edward
GPG key ID: F350507060ED6C90
3 changed files with 73 additions and 23 deletions

View file

@ -1,20 +1,50 @@
use crate::{config::*, updater::update_status};
use chrono::prelude::*;
use serde::Serialize;
use serde::{Serialize, Serializer};
#[derive(Clone, Serialize, Default, Debug)]
pub struct EndpointStatus {
pub status: u8,
pub location: String,
pub endpoint: String,
pub rtt: Option<String>,
pub errors: Vec<String>,
#[derive(Clone, Debug)]
enum EndpointStatus {
OK,
WARN,
ERROR,
MAINTENANCE,
UNKNOWN,
}
impl EndpointStatus {
/// Custom serialization implementation, since its rendered form will be as a
/// CSS class. The default serialization keeps things uppercase, which is
/// discouraged as CSS class names.
impl Serialize for EndpointStatus {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_str(match *self {
EndpointStatus::OK => "ok",
EndpointStatus::WARN => "warn",
EndpointStatus::ERROR => "error",
EndpointStatus::MAINTENANCE => "maintenance",
EndpointStatus::UNKNOWN => "unknown",
})
}
}
/// This holds the results of pinging a single endpoint. RTT exists iff there
/// aren't any errors.
#[derive(Clone, Serialize, Debug)]
pub struct Endpoint {
status: EndpointStatus,
location: String,
endpoint: String,
rtt: Option<String>,
errors: Vec<String>,
}
/// Various helper functions for generating resulting statuses
impl Endpoint {
pub fn ok(location: String, endpoint: String, rtt: String) -> Self {
EndpointStatus {
status: 0,
Endpoint {
status: EndpointStatus::OK,
location,
endpoint,
rtt: Some(rtt),
@ -23,8 +53,8 @@ impl EndpointStatus {
}
pub fn warn(location: String, endpoint: String, rtt: String, errors: Vec<String>) -> Self {
EndpointStatus {
status: 1,
Endpoint {
status: EndpointStatus::WARN,
location,
endpoint,
rtt: Some(rtt),
@ -33,8 +63,28 @@ impl EndpointStatus {
}
pub fn error(location: String, endpoint: String, errors: Vec<String>) -> Self {
EndpointStatus {
status: 2,
Endpoint {
status: EndpointStatus::ERROR,
location,
endpoint,
rtt: None,
errors,
}
}
pub fn maintenance(location: String, endpoint: String, errors: Vec<String>) -> Self {
Endpoint {
status: EndpointStatus::MAINTENANCE,
location,
endpoint,
rtt: None,
errors,
}
}
pub fn unknown(location: String, endpoint: String, errors: Vec<String>) -> Self {
Endpoint {
status: EndpointStatus::UNKNOWN,
location,
endpoint,
rtt: None,
@ -46,7 +96,7 @@ impl EndpointStatus {
#[derive(Serialize, Debug)]
pub struct StatusGroup {
pub label: String,
pub endpoints: Vec<EndpointStatus>,
pub endpoints: Vec<Endpoint>,
}
#[derive(Serialize, Debug)]

View file

@ -1,6 +1,6 @@
use crate::{
config::*,
results::{EndpointStatus, StatusGroup},
results::{Endpoint, StatusGroup},
State,
};
use chrono::Utc;
@ -46,7 +46,7 @@ fn get_result(
website_conf: &WebsiteConfig,
client: &Client,
endpoint: &EndpointConfig,
) -> EndpointStatus {
) -> Endpoint {
let (label, path, port, code, body) = get_endpoint_info(endpoint.clone());
let url = get_url(&website_conf.base, &path, port).expect("reading config");
@ -97,16 +97,16 @@ fn get_result(
}
if !errors.is_empty() {
EndpointStatus::warn(url, label, rtt_string, errors)
Endpoint::warn(url, label, rtt_string, errors)
} else {
EndpointStatus::ok(url, label, rtt_string)
Endpoint::ok(url, label, rtt_string)
}
}
Err(e) => {
if let Some(true) = endpoint.should_err {
EndpointStatus::ok(url, label, rtt_string)
Endpoint::ok(url, label, rtt_string)
} else {
EndpointStatus::error(url, label, vec![format!("{}", e)])
Endpoint::error(url, label, vec![format!("{}", e)])
}
}
}

View file

@ -63,7 +63,7 @@
<h2>{{ group.label }}</h2>
{% for status in group.endpoints -%}
<section>
<aside class="indicator {% if status.status == 0 %}ok{% elif status.status == 1 %}warn{% else %}error{% endif %}"></aside>
<aside class="indicator {{ status.status }}"></aside>
<main>
<div class="info">
<h3>{{ status.endpoint }}</h3>