From a9ec46e7213371cf7d507ca2e496e84b74681aa6 Mon Sep 17 00:00:00 2001 From: Simon Gellis Date: Thu, 31 Oct 2024 17:02:58 -0400 Subject: [PATCH] fix: replace API string durations with milliseconds --- firefly-cardanoconnect/src/routes/streams.rs | 31 +++++++++++--- firefly-server/src/apitypes.rs | 2 - firefly-server/src/apitypes/duration.rs | 45 -------------------- 3 files changed, 24 insertions(+), 54 deletions(-) delete mode 100644 firefly-server/src/apitypes/duration.rs diff --git a/firefly-cardanoconnect/src/routes/streams.rs b/firefly-cardanoconnect/src/routes/streams.rs index c861067..ec265fc 100644 --- a/firefly-cardanoconnect/src/routes/streams.rs +++ b/firefly-cardanoconnect/src/routes/streams.rs @@ -1,6 +1,8 @@ +use std::time::Duration; + use axum::extract::{Path, Query}; use axum::{extract::State, Json}; -use firefly_server::apitypes::{ApiDuration, ApiError, ApiResult, NoContent}; +use firefly_server::apitypes::{ApiError, ApiResult, NoContent}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; @@ -15,6 +17,14 @@ fn example_opt_batch_size() -> Option { Some(example_batch_size()) } +fn example_batch_timeout_ms() -> u64 { + 500 +} + +fn example_opt_batch_timeout_ms() -> Option { + Some(example_batch_timeout_ms()) +} + fn example_from_block() -> Option { Some("earliest".into()) } @@ -25,7 +35,8 @@ pub struct CreateStreamRequest { pub name: String, #[schemars(example = "example_batch_size")] pub batch_size: usize, - pub batch_timeout: ApiDuration, + #[schemars(example = "example_batch_timeout_ms")] + pub batch_timeout_ms: u64, } #[derive(Deserialize, JsonSchema)] @@ -39,7 +50,8 @@ pub struct StreamPathParameters { pub struct UpdateStreamRequest { #[schemars(example = "example_opt_batch_size")] pub batch_size: Option, - pub batch_timeout: Option, + #[schemars(example = "example_opt_batch_timeout_ms")] + pub batch_timeout_ms: Option, } #[derive(Serialize, JsonSchema)] @@ -49,7 +61,8 @@ pub struct EventStream { pub name: String, #[schemars(example = "example_batch_size")] pub batch_size: usize, - pub batch_timeout: ApiDuration, + #[schemars(example = "example_batch_timeout_ms")] + pub batch_timeout_ms: u64, } impl From for EventStream { fn from(value: Stream) -> Self { @@ -57,7 +70,7 @@ impl From for EventStream { id: value.id.into(), name: value.name, batch_size: value.batch_size, - batch_timeout: value.batch_timeout.into(), + batch_timeout_ms: value.batch_timeout.as_millis() as u64, } } } @@ -111,7 +124,11 @@ pub async fn create_stream( Json(req): Json, ) -> ApiResult> { let stream = stream_manager - .create_stream(&req.name, req.batch_size, req.batch_timeout.into()) + .create_stream( + &req.name, + req.batch_size, + Duration::from_millis(req.batch_timeout_ms), + ) .await?; Ok(Json(stream.into())) } @@ -151,7 +168,7 @@ pub async fn update_stream( ) -> ApiResult> { let id = stream_id.into(); let batch_size = req.batch_size; - let batch_timeout = req.batch_timeout.map(|d| d.into()); + let batch_timeout = req.batch_timeout_ms.map(Duration::from_millis); let stream = stream_manager .update_stream(&id, batch_size, batch_timeout) .await?; diff --git a/firefly-server/src/apitypes.rs b/firefly-server/src/apitypes.rs index 33dbb66..faf3fc8 100644 --- a/firefly-server/src/apitypes.rs +++ b/firefly-server/src/apitypes.rs @@ -1,7 +1,5 @@ -mod duration; mod error; mod no_content; -pub use duration::ApiDuration; pub use error::{ApiError, ApiResult, Context, ToAnyhow}; pub use no_content::NoContent; diff --git a/firefly-server/src/apitypes/duration.rs b/firefly-server/src/apitypes/duration.rs deleted file mode 100644 index 4a9358b..0000000 --- a/firefly-server/src/apitypes/duration.rs +++ /dev/null @@ -1,45 +0,0 @@ -use std::time::Duration; - -use schemars::JsonSchema; -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Clone, Copy)] -pub struct ApiDuration(Duration); -impl From for ApiDuration { - fn from(value: Duration) -> Self { - Self(value) - } -} -impl From for Duration { - fn from(value: ApiDuration) -> Self { - value.0 - } -} -impl Serialize for ApiDuration { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - format!("{}ms", self.0.as_millis()).serialize(serializer) - } -} -impl<'de> Deserialize<'de> for ApiDuration { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - let value = duration_str::deserialize_duration(deserializer)?; - Ok(Self(value)) - } -} -impl JsonSchema for ApiDuration { - fn schema_name() -> String { - "Duration".into() - } - - fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { - let mut schema = gen.subschema_for::().into_object(); - schema.metadata().default = Some("500ms".into()); - schema.into() - } -}