Skip to content

Commit

Permalink
refactor: improve time parsing logic in query handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Eshanatnight committed May 7, 2024
1 parent 4e9bd1b commit 8c99690
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions server/src/handlers/http/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,20 +199,7 @@ async fn into_query(
return Err(QueryError::EmptyEndTime);
}

let start: DateTime<Utc>;
let end: DateTime<Utc>;

if query.end_time == "now" {
end = Utc::now();
start = end - chrono::Duration::from_std(humantime::parse_duration(&query.start_time)?)?;
} else {
start = DateTime::parse_from_rfc3339(&query.start_time)
.map_err(|_| QueryError::StartTimeParse)?
.into();
end = DateTime::parse_from_rfc3339(&query.end_time)
.map_err(|_| QueryError::EndTimeParse)?
.into();
};
let (start, end) = parse_human_time(&query.start_time, &query.end_time)?;

if start.timestamp() > end.timestamp() {
return Err(QueryError::StartTimeAfterEndTime);
Expand All @@ -226,6 +213,28 @@ async fn into_query(
})
}

fn parse_human_time(
start_time: &str,
end_time: &str,
) -> Result<(DateTime<Utc>, DateTime<Utc>), QueryError> {
let start: DateTime<Utc>;
let end: DateTime<Utc>;

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(|_| QueryError::StartTimeParse)?
.into();
end = DateTime::parse_from_rfc3339(end_time)
.map_err(|_| QueryError::EndTimeParse)?
.into();
};

Ok((start, end))
}

/// unused for now, might need it in the future
#[allow(unused)]
fn transform_query_for_ingestor(query: &Query) -> Option<Query> {
Expand Down

0 comments on commit 8c99690

Please sign in to comment.