refactor to use chrono

This commit is contained in:
Edward Shen 2019-05-01 16:01:27 -04:00
parent fef8fd66c7
commit 0e8abe2701
Signed by: edward
GPG key ID: F350507060ED6C90
6 changed files with 16 additions and 60 deletions

3
Cargo.lock generated
View file

@ -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)",

View file

@ -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"] }

View file

@ -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<Utc>,
pub refresh_time: u64,
pub config: Config,
pub statuses: Vec<Status>,

View file

@ -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 || {

View file

@ -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<Status> {
pub fn update_status(config: &Config) -> Vec<Status> {
let client = Client::new();
let mut results: Vec<Status> = vec![];

View file

@ -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<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_u64(
self.0
.duration_since(SystemTime::from(UNIX_EPOCH))
.unwrap_or_default()
.as_secs(),
)
}
}