From eb3e472167d56b930e2bb63b12a4c9cfd73e71eb Mon Sep 17 00:00:00 2001 From: Satyam Singh Date: Tue, 12 Sep 2023 16:18:14 +0530 Subject: [PATCH] Parse now and duration in start and end time --- Cargo.lock | 1 + server/Cargo.toml | 1 + server/src/validator.rs | 23 +++++++++++++++++------ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 617ed32d9..1d26e0d3f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2674,6 +2674,7 @@ dependencies = [ "hex", "hostname", "http", + "humantime", "humantime-serde", "itertools 0.10.5", "log", diff --git a/server/Cargo.toml b/server/Cargo.toml index 99016b000..cb6d4e933 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -88,6 +88,7 @@ uptime_lib = "0.2.2" xxhash-rust = { version = "0.8", features = ["xxh3"] } xz2 = { version = "*", features = ["static"] } nom = "7.1.3" +humantime = "2.1.0" [build-dependencies] cargo_toml = "0.15" diff --git a/server/src/validator.rs b/server/src/validator.rs index 1a44a16c2..260c7e475 100644 --- a/server/src/validator.rs +++ b/server/src/validator.rs @@ -166,13 +166,20 @@ pub fn query(query: &str, start_time: &str, end_time: &str) -> Result = DateTime::parse_from_rfc3339(start_time) - .map_err(|_| QueryValidationError::StartTimeParse)? - .into(); + let start: DateTime; + let end: DateTime; - let end: DateTime = DateTime::parse_from_rfc3339(end_time) - .map_err(|_| QueryValidationError::EndTimeParse)? - .into(); + if end_time == "now" { + end = Utc::now(); + start = end - chrono::Duration::from_std(humantime::parse_duration(start_time)?)?; + } else { + start = DateTime::parse_from_rfc3339(start_time) + .map_err(|_| QueryValidationError::StartTimeParse)? + .into(); + end = DateTime::parse_from_rfc3339(end_time) + .map_err(|_| QueryValidationError::EndTimeParse)? + .into(); + }; if start.timestamp() > end.timestamp() { return Err(QueryValidationError::StartTimeAfterEndTime); @@ -226,6 +233,10 @@ pub mod error { StartTimeParse, #[error("Could not parse end time correctly")] EndTimeParse, + #[error("While generating times for 'now' failed to parse duration")] + NotValidDuration(#[from] humantime::DurationError), + #[error("Parsed duration out of range")] + OutOfRange(#[from] chrono::OutOfRangeError), #[error("Start time cannot be greater than the end time")] StartTimeAfterEndTime, #[error("Stream is not initialized yet. Post an event first.")]