Skip to content

Commit ede0f45

Browse files
author
Devdutt Shenoi
committed
fix: failure to deserialize StatsParams
1 parent 4eee17a commit ede0f45

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/handlers/http/logstream.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ pub async fn put_retention(
241241

242242
pub async fn get_stats(
243243
stream_name: Path<String>,
244-
params: Option<Query<StatsParams>>,
244+
Query(params): Query<StatsParams>,
245245
) -> Result<HttpResponse, StreamError> {
246246
let stream_name = stream_name.into_inner();
247247

@@ -259,8 +259,7 @@ pub async fn get_stats(
259259
}
260260
}
261261

262-
if let Some(Query(params)) = params {
263-
let stats = params.get_stats(&stream_name);
262+
if let Some(stats) = params.get_stats(&stream_name) {
264263
return Ok(HttpResponse::build(StatusCode::OK).json(stats));
265264
}
266265

@@ -733,13 +732,13 @@ pub mod error {
733732

734733
#[cfg(test)]
735734
mod tests {
736-
use crate::handlers::http::logstream::error::StreamError;
737-
use crate::handlers::http::logstream::get_stats;
738-
use crate::handlers::http::modal::utils::logstream_utils::PutStreamHeaders;
739-
use actix_web::test::TestRequest;
740-
use actix_web::web;
735+
use actix_web::{test::TestRequest, web};
741736
use anyhow::bail;
742737

738+
use crate::handlers::http::modal::utils::logstream_utils::PutStreamHeaders;
739+
740+
use super::*;
741+
743742
// TODO: Fix this test with routes
744743
// #[actix_web::test]
745744
// #[should_panic]
@@ -750,7 +749,12 @@ mod tests {
750749

751750
#[actix_web::test]
752751
async fn get_stats_stream_not_found_error_for_unknown_logstream() -> anyhow::Result<()> {
753-
match get_stats(web::Path::from("test".to_string()), None).await {
752+
match get_stats(
753+
web::Path::from("test".to_string()),
754+
Query(StatsParams { date: None }),
755+
)
756+
.await
757+
{
754758
Err(StreamError::StreamNotFound(_)) => Ok(()),
755759
_ => bail!("expected StreamNotFound error"),
756760
}

src/stats.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,14 @@ pub fn storage_size_labels_date<'a>(stream_name: &'a str, date: &'a str) -> [&'a
210210

211211
#[derive(Debug, Deserialize)]
212212
pub struct StatsParams {
213-
date: String,
213+
pub date: Option<String>,
214214
}
215215

216216
impl StatsParams {
217-
pub fn get_stats(&self, stream_name: &str) -> Stats {
218-
let event_labels = event_labels_date(stream_name, "json", &self.date);
219-
let storage_size_labels = storage_size_labels_date(stream_name, &self.date);
217+
pub fn get_stats(self, stream_name: &str) -> Option<Stats> {
218+
let date = self.date?;
219+
let event_labels = event_labels_date(stream_name, "json", &date);
220+
let storage_size_labels = storage_size_labels_date(stream_name, &date);
220221
let events_ingested = EVENTS_INGESTED_DATE
221222
.get_metric_with_label_values(&event_labels)
222223
.unwrap()
@@ -230,10 +231,10 @@ impl StatsParams {
230231
.unwrap()
231232
.get() as u64;
232233

233-
Stats {
234+
Some(Stats {
234235
events: events_ingested,
235236
ingestion: ingestion_size,
236237
storage: storage_size,
237-
}
238+
})
238239
}
239240
}

0 commit comments

Comments
 (0)