From ecb0b8ca6ae8426d56c14d8f1a29810818a6a0b7 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Fri, 3 May 2019 16:30:56 -0400 Subject: [PATCH] made endpoint status into an enum --- src/results.rs | 82 +++++++++++++++++++++++++++++++++++--------- src/updater.rs | 12 +++---- templates/index.html | 2 +- 3 files changed, 73 insertions(+), 23 deletions(-) diff --git a/src/results.rs b/src/results.rs index 7ca5581..ba3ed86 100644 --- a/src/results.rs +++ b/src/results.rs @@ -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, - pub errors: Vec, +#[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(&self, s: S) -> Result + 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, + errors: Vec, +} + +/// 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) -> 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) -> Self { - EndpointStatus { - status: 2, + Endpoint { + status: EndpointStatus::ERROR, + location, + endpoint, + rtt: None, + errors, + } + } + + pub fn maintenance(location: String, endpoint: String, errors: Vec) -> Self { + Endpoint { + status: EndpointStatus::MAINTENANCE, + location, + endpoint, + rtt: None, + errors, + } + } + + pub fn unknown(location: String, endpoint: String, errors: Vec) -> 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, + pub endpoints: Vec, } #[derive(Serialize, Debug)] diff --git a/src/updater.rs b/src/updater.rs index 4afca52..f25383d 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -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)]) } } } diff --git a/templates/index.html b/templates/index.html index b21c4e1..24b9f1a 100644 --- a/templates/index.html +++ b/templates/index.html @@ -63,7 +63,7 @@

{{ group.label }}

{% for status in group.endpoints -%}
- +

{{ status.endpoint }}