query results knows how to update itself
This commit is contained in:
parent
ddab660945
commit
816a6daed1
3 changed files with 34 additions and 19 deletions
|
@ -1,4 +1,4 @@
|
||||||
use crate::{config::*, State};
|
use crate::{config::*, updater::update_status, 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,
|
||||||
|
@ -63,6 +63,34 @@ pub struct QueryResults {
|
||||||
pub groups: Vec<StatusGroup>,
|
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();
|
||||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -19,7 +19,6 @@ mod updater;
|
||||||
use self::{config::*, handlers::*, updater::*};
|
use self::{config::*, handlers::*, updater::*};
|
||||||
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 chrono::prelude::*;
|
|
||||||
use ron::de::from_str;
|
use ron::de::from_str;
|
||||||
use std::{
|
use std::{
|
||||||
env::var,
|
env::var,
|
||||||
|
@ -35,24 +34,18 @@ use tokio::{
|
||||||
pub type State = Arc<RwLock<QueryResults>>;
|
pub type State = Arc<RwLock<QueryResults>>;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
System::run(move || {
|
System::run(|| {
|
||||||
let conf_file_loc =
|
let conf_file_loc =
|
||||||
var("ENDSTAT_CONF").unwrap_or_else(|_| String::from("./config/endstat_conf.ron"));
|
var("ENDSTAT_CONF").unwrap_or_else(|_| String::from("./config/endstat_conf.ron"));
|
||||||
let config = from_str::<Config>(
|
let config = from_str::<Config>(
|
||||||
&read_to_string(&conf_file_loc).expect(&format!("finding {}", conf_file_loc)),
|
&read_to_string(&conf_file_loc).expect(&format!("finding {}", conf_file_loc)),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let bind_addr = config.bind_address.clone();
|
let bind_addr = &config.bind_address;
|
||||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let state: State = Arc::new(RwLock::new(QueryResults {
|
let state = Arc::new(RwLock::new(QueryResults::new(config.clone())));
|
||||||
timestamp: Utc::now(),
|
|
||||||
timestamp_str: Utc::now().format("%Y-%m-%d %H:%M:%S").to_string(),
|
|
||||||
refresh_time: config.refresh_time.clone(),
|
|
||||||
config: config.clone(),
|
|
||||||
groups: update_status(&config),
|
|
||||||
}));
|
|
||||||
|
|
||||||
let clone_state = Arc::clone(&state);
|
let clone_state = Arc::clone(&state);
|
||||||
|
|
||||||
|
|
|
@ -13,10 +13,8 @@ use std::time::Duration;
|
||||||
|
|
||||||
pub fn update_state(state: State) {
|
pub fn update_state(state: State) {
|
||||||
let new_statuses = { Some(update_status(&state.read().unwrap().config)) };
|
let new_statuses = { Some(update_status(&state.read().unwrap().config)) };
|
||||||
let mut write_state = state.try_write().expect("Could not unlock");
|
let mut state = state.try_write().expect("Could not unlock");
|
||||||
write_state.timestamp = Utc::now();
|
state.update(new_statuses.unwrap());
|
||||||
write_state.timestamp_str = Utc::now().format("%Y-%m-%d %H:%M:%S").to_string();
|
|
||||||
write_state.groups = new_statuses.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_status(config: &Config) -> Vec<StatusGroup> {
|
pub fn update_status(config: &Config) -> Vec<StatusGroup> {
|
||||||
|
@ -123,10 +121,6 @@ fn get_url(base: &Option<String>, path: &String, port: Option<u16>) -> Result<St
|
||||||
}
|
}
|
||||||
|
|
||||||
fn append_err_msg(optional: Option<String>, to_append: String) -> Option<String> {
|
fn append_err_msg(optional: Option<String>, to_append: String) -> Option<String> {
|
||||||
// Turns out Rust doesn't recognize that only one of the FnOnce occurs, so
|
|
||||||
// as a result both closures want ownership of to_append
|
|
||||||
// Some(optional.map_or_else(|| to_append, |msg| format!("{} {}", msg, to_append)))
|
|
||||||
|
|
||||||
Some(match optional {
|
Some(match optional {
|
||||||
Some(e) => format!("{} {}", e, to_append),
|
Some(e) => format!("{} {}", e, to_append),
|
||||||
None => to_append,
|
None => to_append,
|
||||||
|
|
Loading…
Reference in a new issue