Skip to content

Commit ee508cf

Browse files
authored
parse now and duration in start and end time (#494)
Allow user to specify now and duration in `endTime` and `startTime` fields instead of giving concrete timestamps. Note that this only affects the time range for internal file lookups and does not guarantee restriction exact timeframe for p_timestamp for the query. In that case user is expected to pass in where p_timestamp < now() and p_timestamp > (now() - interval '10 minute') Fixes #490
1 parent 44f3ec0 commit ee508cf

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ uptime_lib = "0.2.2"
8888
xxhash-rust = { version = "0.8", features = ["xxh3"] }
8989
xz2 = { version = "*", features = ["static"] }
9090
nom = "7.1.3"
91+
humantime = "2.1.0"
9192

9293
[build-dependencies]
9394
cargo_toml = "0.15"

server/src/validator.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,20 @@ pub fn query(query: &str, start_time: &str, end_time: &str) -> Result<Query, Que
166166
return Err(QueryValidationError::MultipleStreams(query.to_string()));
167167
}
168168

169-
let start: DateTime<Utc> = DateTime::parse_from_rfc3339(start_time)
170-
.map_err(|_| QueryValidationError::StartTimeParse)?
171-
.into();
169+
let start: DateTime<Utc>;
170+
let end: DateTime<Utc>;
172171

173-
let end: DateTime<Utc> = DateTime::parse_from_rfc3339(end_time)
174-
.map_err(|_| QueryValidationError::EndTimeParse)?
175-
.into();
172+
if end_time == "now" {
173+
end = Utc::now();
174+
start = end - chrono::Duration::from_std(humantime::parse_duration(start_time)?)?;
175+
} else {
176+
start = DateTime::parse_from_rfc3339(start_time)
177+
.map_err(|_| QueryValidationError::StartTimeParse)?
178+
.into();
179+
end = DateTime::parse_from_rfc3339(end_time)
180+
.map_err(|_| QueryValidationError::EndTimeParse)?
181+
.into();
182+
};
176183

177184
if start.timestamp() > end.timestamp() {
178185
return Err(QueryValidationError::StartTimeAfterEndTime);
@@ -226,6 +233,10 @@ pub mod error {
226233
StartTimeParse,
227234
#[error("Could not parse end time correctly")]
228235
EndTimeParse,
236+
#[error("While generating times for 'now' failed to parse duration")]
237+
NotValidDuration(#[from] humantime::DurationError),
238+
#[error("Parsed duration out of range")]
239+
OutOfRange(#[from] chrono::OutOfRangeError),
229240
#[error("Start time cannot be greater than the end time")]
230241
StartTimeAfterEndTime,
231242
#[error("Stream is not initialized yet. Post an event first.")]

0 commit comments

Comments
 (0)