From 0e8abe2701a2d1c728c72af2f367f30b72767861 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Wed, 1 May 2019 16:01:27 -0400 Subject: [PATCH] refactor to use chrono --- Cargo.lock | 3 ++- Cargo.toml | 2 +- src/handlers.rs | 5 +++-- src/main.rs | 10 ++++++---- src/updater.rs | 7 ++++--- src/utils.rs | 49 ------------------------------------------------- 6 files changed, 16 insertions(+), 60 deletions(-) delete mode 100644 src/utils.rs diff --git a/Cargo.lock b/Cargo.lock index e552698..8235011 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -426,6 +426,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -653,8 +654,8 @@ version = "0.1.0" dependencies = [ "actix 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "actix-web 1.0.0-beta.2 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.15 (registry+https://github.com/rust-lang/crates.io-index)", "ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index dafc294..b4ad378 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,4 @@ actix = "0.8" tokio = "0.1" tera = "0.11" env_logger = "0.6" -lazy_static = "1.3.0" +chrono = { version = "0.4", features = ["serde"] } diff --git a/src/handlers.rs b/src/handlers.rs index 2a0c71c..09c49fb 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -1,8 +1,9 @@ -use crate::{config::*, utils::EpochTimestamp, State}; +use crate::{config::*, State}; use actix_web::{ error::ErrorInternalServerError, web::Data, Error as WebError, HttpResponse, Result as WebResult, }; +use chrono::prelude::*; use serde::Serialize; use tera::{Context, Tera}; @@ -17,7 +18,7 @@ pub struct Status { #[derive(Serialize, Debug)] pub struct QueryResults { - pub last_update: EpochTimestamp, + pub last_update: DateTime, pub refresh_time: u64, pub config: Config, pub statuses: Vec, diff --git a/src/main.rs b/src/main.rs index c10dcea..d0e86ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,15 +7,16 @@ extern crate serde; extern crate tokio; #[macro_use] extern crate tera; +extern crate chrono; mod config; mod handlers; mod updater; -mod utils; -use self::{config::*, handlers::*, updater::update_state, utils::EpochTimestamp}; +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::{ fs::read_to_string, @@ -40,11 +41,12 @@ fn main() { env_logger::init(); let state: State = Arc::new(RwLock::new(QueryResults { - last_update: EpochTimestamp::now(), + last_update: Utc::now(), refresh_time: config.refresh_time.clone(), config: config.clone(), - statuses: vec![], + statuses: update_status(&config), })); + let clone_state = Arc::clone(&state); HttpServer::new(move || { diff --git a/src/updater.rs b/src/updater.rs index 40ef9cb..58f6157 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -1,14 +1,15 @@ -use crate::{config::*, handlers::Status, utils::EpochTimestamp, State}; +use crate::{config::*, handlers::Status, State}; +use chrono::prelude::*; use reqwest::{Client, Url, UrlError}; 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.last_update = EpochTimestamp::now(); + write_state.last_update = Utc::now(); write_state.statuses = new_statuses.unwrap(); } -fn update_status(config: &Config) -> Vec { +pub fn update_status(config: &Config) -> Vec { let client = Client::new(); let mut results: Vec = vec![]; diff --git a/src/utils.rs b/src/utils.rs deleted file mode 100644 index a020ae8..0000000 --- a/src/utils.rs +++ /dev/null @@ -1,49 +0,0 @@ -use serde::{Serialize, Serializer}; -use std::{ - fmt::{Display, Formatter, Result as FmtResult}, - ops::Sub, - time::{SystemTime, UNIX_EPOCH}, -}; - -#[derive(PartialEq, PartialOrd, Copy, Clone, Debug)] -pub struct EpochTimestamp(SystemTime); - -impl EpochTimestamp { - pub fn now() -> Self { - EpochTimestamp(SystemTime::now()) - } -} - -impl Sub for EpochTimestamp { - type Output = u64; - fn sub(self, other: EpochTimestamp) -> u64 { - self.0.duration_since(other.0).unwrap_or_default().as_secs() - } -} - -impl Display for EpochTimestamp { - fn fmt(&self, f: &mut Formatter) -> FmtResult { - write!( - f, - "{}", - self.0 - .duration_since(UNIX_EPOCH) - .unwrap_or_default() - .as_secs() - ) - } -} - -impl Serialize for EpochTimestamp { - fn serialize(&self, s: S) -> Result - where - S: Serializer, - { - s.serialize_u64( - self.0 - .duration_since(SystemTime::from(UNIX_EPOCH)) - .unwrap_or_default() - .as_secs(), - ) - } -}