From 816a6daed1f5146393abe3d336224ac6d73dc196 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Fri, 3 May 2019 00:12:09 -0400 Subject: [PATCH] query results knows how to update itself --- src/handlers.rs | 30 +++++++++++++++++++++++++++++- src/main.rs | 13 +++---------- src/updater.rs | 10 ++-------- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/handlers.rs b/src/handlers.rs index 019cf02..a6438be 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -1,4 +1,4 @@ -use crate::{config::*, State}; +use crate::{config::*, updater::update_status, State}; use actix_web::{ error::ErrorInternalServerError, web::Data, Error as WebError, HttpResponse, Result as WebResult, @@ -63,6 +63,34 @@ pub struct QueryResults { pub groups: Vec, } +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) { + 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) -> String { + timestamp.format("%Y-%m-%d %H:%M:%S").to_string() + } +} + pub fn index(tmpl: Data, state: Data) -> WebResult { let state = state.read().unwrap(); let mut ctx = Context::new(); diff --git a/src/main.rs b/src/main.rs index 4d18311..51bb966 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,6 @@ mod updater; use self::{config::*, handlers::*, updater::*}; use actix::System; use actix_web::{middleware::Logger, web::resource, App, HttpServer}; -use chrono::prelude::*; use ron::de::from_str; use std::{ env::var, @@ -35,24 +34,18 @@ use tokio::{ pub type State = Arc>; fn main() { - System::run(move || { + System::run(|| { let conf_file_loc = var("ENDSTAT_CONF").unwrap_or_else(|_| String::from("./config/endstat_conf.ron")); let config = from_str::( &read_to_string(&conf_file_loc).expect(&format!("finding {}", conf_file_loc)), ) .unwrap(); - let bind_addr = config.bind_address.clone(); + let bind_addr = &config.bind_address; std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init(); - let state: State = Arc::new(RwLock::new(QueryResults { - 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 state = Arc::new(RwLock::new(QueryResults::new(config.clone()))); let clone_state = Arc::clone(&state); diff --git a/src/updater.rs b/src/updater.rs index c77425a..f5524a5 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -13,10 +13,8 @@ use std::time::Duration; pub fn update_state(state: State) { let new_statuses = { Some(update_status(&state.read().unwrap().config)) }; - let mut write_state = state.try_write().expect("Could not unlock"); - write_state.timestamp = Utc::now(); - write_state.timestamp_str = Utc::now().format("%Y-%m-%d %H:%M:%S").to_string(); - write_state.groups = new_statuses.unwrap(); + let mut state = state.try_write().expect("Could not unlock"); + state.update(new_statuses.unwrap()); } pub fn update_status(config: &Config) -> Vec { @@ -123,10 +121,6 @@ fn get_url(base: &Option, path: &String, port: Option) -> Result, to_append: String) -> Option { - // 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(e) => format!("{} {}", e, to_append), None => to_append,