refactor results into own file
This commit is contained in:
parent
82b944a748
commit
17a0730275
4 changed files with 91 additions and 89 deletions
|
@ -1,96 +1,10 @@
|
||||||
use crate::{config::*, updater::update_status, State};
|
use crate::State;
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
error::ErrorInternalServerError, web::Data, Error as WebError, HttpResponse,
|
error::ErrorInternalServerError, web::Data, Error as WebError, HttpResponse,
|
||||||
Result as WebResult,
|
Result as WebResult,
|
||||||
};
|
};
|
||||||
use chrono::prelude::*;
|
|
||||||
use serde::Serialize;
|
|
||||||
use tera::{Context, Tera};
|
use tera::{Context, Tera};
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Default, Debug)]
|
|
||||||
pub struct EndpointStatus {
|
|
||||||
pub status: u8,
|
|
||||||
pub location: String,
|
|
||||||
pub endpoint: String,
|
|
||||||
pub rtt: Option<String>,
|
|
||||||
pub error: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EndpointStatus {
|
|
||||||
pub fn ok(location: String, endpoint: String, rtt: String) -> Self {
|
|
||||||
EndpointStatus {
|
|
||||||
status: 0,
|
|
||||||
location,
|
|
||||||
endpoint,
|
|
||||||
rtt: Some(rtt),
|
|
||||||
error: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn warn(location: String, endpoint: String, rtt: String, error: Option<String>) -> Self {
|
|
||||||
EndpointStatus {
|
|
||||||
status: 1,
|
|
||||||
location,
|
|
||||||
endpoint,
|
|
||||||
rtt: Some(rtt),
|
|
||||||
error,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn error(location: String, endpoint: String, error: Option<String>) -> Self {
|
|
||||||
EndpointStatus {
|
|
||||||
status: 2,
|
|
||||||
location,
|
|
||||||
endpoint,
|
|
||||||
rtt: None,
|
|
||||||
error,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn index(tmpl: Data<Tera>, state: Data<State>) -> WebResult<HttpResponse, WebError> {
|
pub fn index(tmpl: Data<Tera>, state: Data<State>) -> WebResult<HttpResponse, WebError> {
|
||||||
let state = state.read().unwrap();
|
let state = state.read().unwrap();
|
||||||
let mut ctx = Context::new();
|
let mut ctx = Context::new();
|
||||||
|
|
|
@ -15,8 +15,9 @@ extern crate ring;
|
||||||
mod config;
|
mod config;
|
||||||
mod handlers;
|
mod handlers;
|
||||||
mod updater;
|
mod updater;
|
||||||
|
mod results;
|
||||||
|
|
||||||
use self::{config::*, handlers::*, updater::*};
|
use self::{config::*, handlers::*, updater::*, results::QueryResults};
|
||||||
use actix::System;
|
use actix::System;
|
||||||
use actix_web::{middleware::Logger, web::resource, App, HttpServer};
|
use actix_web::{middleware::Logger, web::resource, App, HttpServer};
|
||||||
use ron::de::from_str;
|
use ron::de::from_str;
|
||||||
|
|
87
src/results.rs
Normal file
87
src/results.rs
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
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 error: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EndpointStatus {
|
||||||
|
pub fn ok(location: String, endpoint: String, rtt: String) -> Self {
|
||||||
|
EndpointStatus {
|
||||||
|
status: 0,
|
||||||
|
location,
|
||||||
|
endpoint,
|
||||||
|
rtt: Some(rtt),
|
||||||
|
error: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn warn(location: String, endpoint: String, rtt: String, error: Option<String>) -> Self {
|
||||||
|
EndpointStatus {
|
||||||
|
status: 1,
|
||||||
|
location,
|
||||||
|
endpoint,
|
||||||
|
rtt: Some(rtt),
|
||||||
|
error,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn error(location: String, endpoint: String, error: Option<String>) -> Self {
|
||||||
|
EndpointStatus {
|
||||||
|
status: 2,
|
||||||
|
location,
|
||||||
|
endpoint,
|
||||||
|
rtt: None,
|
||||||
|
error,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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()
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
config::*,
|
config::*,
|
||||||
handlers::{EndpointStatus, StatusGroup},
|
results::{EndpointStatus, StatusGroup},
|
||||||
State,
|
State,
|
||||||
};
|
};
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
|
|
Loading…
Reference in a new issue