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 crate::{config::*, updater::update_status};
use chrono::prelude::*; use chrono::prelude::*;
use serde::Serialize; use serde::{Serialize, Serializer};
#[derive(Clone, Serialize, Default, Debug)] #[derive(Clone, Debug)]
pub struct EndpointStatus { enum EndpointStatus {
pub status: u8, OK,
pub location: String, WARN,
pub endpoint: String, ERROR,
pub rtt: Option<String>, MAINTENANCE,
pub errors: Vec<String>, 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 { pub fn ok(location: String, endpoint: String, rtt: String) -> Self {
EndpointStatus { Endpoint {
status: 0, status: EndpointStatus::OK,
location, location,
endpoint, endpoint,
rtt: Some(rtt), rtt: Some(rtt),
@ -23,8 +53,8 @@ impl EndpointStatus {
} }
pub fn warn(location: String, endpoint: String, rtt: String, errors: Vec<String>) -> Self { pub fn warn(location: String, endpoint: String, rtt: String, errors: Vec<String>) -> Self {
EndpointStatus { Endpoint {
status: 1, status: EndpointStatus::WARN,
location, location,
endpoint, endpoint,
rtt: Some(rtt), rtt: Some(rtt),
@ -33,8 +63,28 @@ impl EndpointStatus {
} }
pub fn error(location: String, endpoint: String, errors: Vec<String>) -> Self { pub fn error(location: String, endpoint: String, errors: Vec<String>) -> Self {
EndpointStatus { Endpoint {
status: 2, 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, location,
endpoint, endpoint,
rtt: None, rtt: None,
@ -46,7 +96,7 @@ impl EndpointStatus {
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
pub struct StatusGroup { pub struct StatusGroup {
pub label: String, pub label: String,
pub endpoints: Vec<EndpointStatus>, pub endpoints: Vec<Endpoint>,
} }
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
config::*, config::*,
results::{EndpointStatus, StatusGroup}, results::{Endpoint, StatusGroup},
State, State,
}; };
use chrono::Utc; use chrono::Utc;
@ -46,7 +46,7 @@ fn get_result(
website_conf: &WebsiteConfig, website_conf: &WebsiteConfig,
client: &Client, client: &Client,
endpoint: &EndpointConfig, endpoint: &EndpointConfig,
) -> EndpointStatus { ) -> Endpoint {
let (label, path, port, code, body) = get_endpoint_info(endpoint.clone()); let (label, path, port, code, body) = get_endpoint_info(endpoint.clone());
let url = get_url(&website_conf.base, &path, port).expect("reading config"); let url = get_url(&website_conf.base, &path, port).expect("reading config");
@ -97,16 +97,16 @@ fn get_result(
} }
if !errors.is_empty() { if !errors.is_empty() {
EndpointStatus::warn(url, label, rtt_string, errors) Endpoint::warn(url, label, rtt_string, errors)
} else { } else {
EndpointStatus::ok(url, label, rtt_string) Endpoint::ok(url, label, rtt_string)
} }
} }
Err(e) => { Err(e) => {
if let Some(true) = endpoint.should_err { if let Some(true) = endpoint.should_err {
EndpointStatus::ok(url, label, rtt_string) Endpoint::ok(url, label, rtt_string)
} else { } 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> <h2>{{ group.label }}</h2>
{% for status in group.endpoints -%} {% for status in group.endpoints -%}
<section> <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> <main>
<div class="info"> <div class="info">
<h3>{{ status.endpoint }}</h3> <h3>{{ status.endpoint }}</h3>