endstat/src/results.rs

88 lines
2.1 KiB
Rust

use crate::{config::*, updater::update_status};
use chrono::prelude::*;
use serde::Serialize;
#[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>,
}
impl EndpointStatus {
pub fn ok(location: String, endpoint: String, rtt: String) -> Self {
EndpointStatus {
status: 0,
location,
endpoint,
rtt: Some(rtt),
errors: vec![],
}
}
pub fn warn(location: String, endpoint: String, rtt: String, errors: Vec<String>) -> Self {
EndpointStatus {
status: 1,
location,
endpoint,
rtt: Some(rtt),
errors,
}
}
pub fn error(location: String, endpoint: String, errors: Vec<String>) -> Self {
EndpointStatus {
status: 2,
location,
endpoint,
rtt: None,
errors,
}
}
}
#[derive(Serialize, Debug)]
pub struct StatusGroup {
pub label: String,
pub endpoints: Vec<EndpointStatus>,
}
#[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;
}
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()
}
}