endstat/src/handlers.rs

109 lines
2.8 KiB
Rust
Raw Normal View History

use crate::{config::*, updater::update_status, State};
2019-05-01 05:41:52 +00:00
use actix_web::{
error::ErrorInternalServerError, web::Data, Error as WebError, HttpResponse,
Result as WebResult,
};
2019-05-01 20:01:27 +00:00
use chrono::prelude::*;
2019-05-01 05:41:52 +00:00
use serde::Serialize;
use tera::{Context, Tera};
2019-05-01 16:58:10 +00:00
#[derive(Clone, Serialize, Default, Debug)]
2019-05-01 20:59:55 +00:00
pub struct EndpointStatus {
2019-05-01 18:13:46 +00:00
pub status: u8,
pub location: String,
pub endpoint: String,
2019-05-02 22:47:59 +00:00
pub rtt: Option<String>,
2019-05-01 18:13:46 +00:00
pub error: Option<String>,
2019-05-01 05:41:52 +00:00
}
impl EndpointStatus {
2019-05-02 22:47:59 +00:00
pub fn ok(location: String, endpoint: String, rtt: String) -> Self {
EndpointStatus {
status: 0,
location,
endpoint,
2019-05-02 22:47:59 +00:00
rtt: Some(rtt),
error: None,
}
}
2019-05-02 22:47:59 +00:00
pub fn warn(location: String, endpoint: String, rtt: String, error: Option<String>) -> Self {
EndpointStatus {
status: 1,
location,
endpoint,
2019-05-02 22:47:59 +00:00
rtt: Some(rtt),
error,
}
}
pub fn error(location: String, endpoint: String, error: Option<String>) -> Self {
EndpointStatus {
status: 2,
location,
endpoint,
2019-05-02 22:47:59 +00:00
rtt: None,
error,
}
}
}
2019-05-01 20:59:55 +00:00
#[derive(Serialize, Debug)]
pub struct StatusGroup {
pub label: String,
pub endpoints: Vec<EndpointStatus>,
}
2019-05-01 16:58:10 +00:00
#[derive(Serialize, Debug)]
2019-05-01 06:15:20 +00:00
pub struct QueryResults {
2019-05-01 20:15:18 +00:00
pub timestamp: DateTime<Utc>,
pub timestamp_str: String,
2019-05-01 05:41:52 +00:00
pub refresh_time: u64,
pub config: Config,
2019-05-01 20:59:55 +00:00
pub groups: Vec<StatusGroup>,
2019-05-01 05:41:52 +00:00
}
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()
}
}
2019-05-01 06:15:20 +00:00
pub fn index(tmpl: Data<Tera>, state: Data<State>) -> WebResult<HttpResponse, WebError> {
2019-05-01 16:58:10 +00:00
let state = state.read().unwrap();
2019-05-01 05:41:52 +00:00
let mut ctx = Context::new();
ctx.insert("results", &*state);
let s = tmpl.render("index.html", &ctx).map_err(|e| {
println!("{:?}", e);
ErrorInternalServerError("Template error")
})?;
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
2019-05-01 06:15:20 +00:00
pub fn json_endpoint(state: Data<State>) -> HttpResponse {
2019-05-01 16:58:10 +00:00
let state = state.read().unwrap();
2019-05-01 20:59:55 +00:00
HttpResponse::Ok().json(&*state.groups)
2019-05-01 05:41:52 +00:00
}