Skip to content

Commit 533890a

Browse files
use web query for date stats
1 parent 7f3e752 commit 533890a

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/handlers/http/logstream.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,8 @@ pub mod error {
524524
HotTierValidation(#[from] HotTierValidationError),
525525
#[error("{0}")]
526526
HotTierError(#[from] HotTierError),
527+
#[error("Invalid query parameter: {0}")]
528+
InvalidQueryParameter(String),
527529
}
528530

529531
impl actix_web::ResponseError for StreamError {
@@ -559,6 +561,7 @@ pub mod error {
559561
StreamError::HotTierNotEnabled(_) => StatusCode::FORBIDDEN,
560562
StreamError::HotTierValidation(_) => StatusCode::BAD_REQUEST,
561563
StreamError::HotTierError(_) => StatusCode::INTERNAL_SERVER_ERROR,
564+
StreamError::InvalidQueryParameter(_) => StatusCode::BAD_REQUEST,
562565
}
563566
}
564567

src/handlers/http/modal/query/querier_logstream.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
use core::str;
20-
use std::fs;
20+
use std::{collections::HashMap, fs};
2121

2222
use actix_web::{
2323
web::{self, Path},
@@ -47,6 +47,7 @@ use crate::{
4747
stats,
4848
storage::{ObjectStoreFormat, StreamType, STREAM_ROOT_DIRECTORY},
4949
};
50+
const STATS_DATE_QUERY_PARAM: &str = "date";
5051

5152
pub async fn delete(stream_name: Path<String>) -> Result<impl Responder, StreamError> {
5253
let stream_name = stream_name.into_inner();
@@ -141,16 +142,13 @@ pub async fn get_stats(
141142
return Err(StreamNotFound(stream_name.clone()).into());
142143
}
143144

144-
let query_string = req.query_string();
145-
if !query_string.is_empty() {
146-
let date_key = query_string.split('=').collect::<Vec<&str>>()[0];
147-
let date_value = query_string.split('=').collect::<Vec<&str>>()[1];
148-
if date_key != "date" {
149-
return Err(StreamError::Custom {
150-
msg: "Invalid query parameter".to_string(),
151-
status: StatusCode::BAD_REQUEST,
152-
});
153-
}
145+
let query_map = web::Query::<HashMap<String, String>>::from_query(req.query_string())
146+
.map_err(|_| StreamError::InvalidQueryParameter(STATS_DATE_QUERY_PARAM.to_string()))?;
147+
148+
if !query_map.is_empty() {
149+
let date_value = query_map.get(STATS_DATE_QUERY_PARAM).ok_or_else(|| {
150+
StreamError::InvalidQueryParameter(STATS_DATE_QUERY_PARAM.to_string())
151+
})?;
154152

155153
if !date_value.is_empty() {
156154
// this function requires all the ingestor stream jsons
@@ -180,7 +178,7 @@ pub async fn get_stats(
180178

181179
let stats = serde_json::to_value(stats)?;
182180

183-
return Ok((web::Json(stats), StatusCode::OK));
181+
return Ok(web::Json(stats));
184182
}
185183
}
186184

@@ -227,5 +225,5 @@ pub async fn get_stats(
227225

228226
let stats = serde_json::to_value(stats)?;
229227

230-
Ok((web::Json(stats), StatusCode::OK))
228+
Ok(web::Json(stats))
231229
}

0 commit comments

Comments
 (0)