endstat/src/results.rs

151 lines
3.9 KiB
Rust
Raw Normal View History

2019-05-03 04:54:43 +00:00
use crate::{config::*, updater::update_status};
use chrono::prelude::*;
2019-05-03 20:30:56 +00:00
use serde::{Serialize, Serializer};
2019-05-03 04:54:43 +00:00
2019-05-03 20:30:56 +00:00
#[derive(Clone, Debug)]
2019-05-04 03:13:31 +00:00
pub enum EndpointStatus {
2019-05-03 20:30:56 +00:00
OK,
WARN,
ERROR,
MAINTENANCE,
UNKNOWN,
2019-05-03 04:54:43 +00:00
}
2019-05-03 20:30:56 +00:00
/// 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",
})
}
}
2019-05-04 03:13:31 +00:00
impl Into<String> for EndpointStatus {
fn into(self) -> String {
match self {
EndpointStatus::OK => String::from("ok"),
EndpointStatus::WARN => String::from("warn"),
EndpointStatus::ERROR => String::from("error"),
EndpointStatus::MAINTENANCE => String::from("maintenance"),
EndpointStatus::UNKNOWN => String::from("unknown"),
}
}
}
2019-05-03 20:30:56 +00:00
/// This holds the results of pinging a single endpoint. RTT exists iff there
/// aren't any errors.
#[derive(Clone, Serialize, Debug)]
pub struct Endpoint {
2019-05-04 03:13:31 +00:00
pub status: EndpointStatus,
pub location: String,
pub label: String,
pub rtt: Option<String>,
pub errors: Vec<String>,
2019-05-03 20:30:56 +00:00
}
/// Various helper functions for generating resulting statuses
impl Endpoint {
2019-05-04 03:13:31 +00:00
pub fn ok(location: String, label: String, rtt: String) -> Self {
2019-05-03 20:30:56 +00:00
Endpoint {
status: EndpointStatus::OK,
2019-05-03 04:54:43 +00:00
location,
2019-05-04 03:13:31 +00:00
label,
2019-05-03 04:54:43 +00:00
rtt: Some(rtt),
errors: vec![],
2019-05-03 04:54:43 +00:00
}
}
2019-05-04 03:13:31 +00:00
pub fn warn(location: String, label: String, rtt: String, errors: Vec<String>) -> Self {
2019-05-03 20:30:56 +00:00
Endpoint {
status: EndpointStatus::WARN,
2019-05-03 04:54:43 +00:00
location,
2019-05-04 03:13:31 +00:00
label,
2019-05-03 04:54:43 +00:00
rtt: Some(rtt),
errors,
2019-05-03 04:54:43 +00:00
}
}
2019-05-04 03:13:31 +00:00
pub fn error(location: String, label: String, errors: Vec<String>) -> Self {
2019-05-03 20:30:56 +00:00
Endpoint {
status: EndpointStatus::ERROR,
location,
2019-05-04 03:13:31 +00:00
label,
2019-05-03 20:30:56 +00:00
rtt: None,
errors,
}
}
2019-05-04 03:13:31 +00:00
pub fn maintenance(location: String, label: String, errors: Vec<String>) -> Self {
2019-05-03 20:30:56 +00:00
Endpoint {
status: EndpointStatus::MAINTENANCE,
location,
2019-05-04 03:13:31 +00:00
label,
2019-05-03 20:30:56 +00:00
rtt: None,
errors,
}
}
2019-05-04 03:13:31 +00:00
pub fn unknown(location: String, label: String, errors: Vec<String>) -> Self {
2019-05-03 20:30:56 +00:00
Endpoint {
status: EndpointStatus::UNKNOWN,
2019-05-03 04:54:43 +00:00
location,
2019-05-04 03:13:31 +00:00
label,
2019-05-03 04:54:43 +00:00
rtt: None,
errors,
2019-05-03 04:54:43 +00:00
}
}
}
#[derive(Serialize, Debug)]
pub struct StatusGroup {
pub label: String,
2019-05-03 20:30:56 +00:00
pub endpoints: Vec<Endpoint>,
2019-05-03 04:54:43 +00:00
}
#[derive(Serialize, Debug)]
pub struct QueryResults {
pub timestamp: DateTime<Utc>,
pub timestamp_str: String,
pub refresh_time: u64,
pub config: Config,
pub groups: Vec<StatusGroup>,
}
impl QueryResults {
pub fn new(config: Config) -> Self {
let time = Utc::now();
QueryResults {
timestamp: time,
timestamp_str: Self::format_timestamp(time),
refresh_time: config.refresh_time,
config: config.clone(),
groups: update_status(&config),
}
}
pub fn update(&mut self, updated_groups: Vec<StatusGroup>) {
self.update_timestamp();
self.groups = updated_groups;
}
2019-05-04 03:13:31 +00:00
#[inline]
2019-05-03 04:54:43 +00:00
fn update_timestamp(&mut self) {
let current_time = Utc::now();
self.timestamp = current_time;
self.timestamp_str = Self::format_timestamp(current_time);
}
fn format_timestamp(timestamp: DateTime<Utc>) -> String {
timestamp.format("%Y-%m-%d %H:%M:%S").to_string()
}
}