diff --git a/block-streamer/src/block_height_stream.rs b/block-streamer/src/block_height_stream.rs index 4e8562ebe..e7c9ccaed 100644 --- a/block-streamer/src/block_height_stream.rs +++ b/block-streamer/src/block_height_stream.rs @@ -141,14 +141,13 @@ impl BlockHeightStreamImpl { Box::pin(try_stream! { let mut current_date = start_date; while current_date <= Utc::now() { - let current_date_string = current_date.format("%Y-%m-%d").to_string(); let bitmaps_from_query: Vec = match contract_pattern_type { ContractPatternType::Exact(ref pattern) => { - let query_result: Vec<_> = self.graphql_client.get_bitmaps_exact(pattern.clone(), current_date_string.clone()).await.unwrap(); + let query_result: Vec<_> = self.graphql_client.get_bitmaps_exact(pattern.clone(), ¤t_date).await.unwrap(); query_result.iter().map(|result_item| Base64Bitmap::try_from(result_item).unwrap()).collect() }, ContractPatternType::Wildcard(ref pattern) => { - let query_result: Vec<_> = self.graphql_client.get_bitmaps_wildcard(pattern.clone(), current_date_string.clone()).await.unwrap(); + let query_result: Vec<_> = self.graphql_client.get_bitmaps_wildcard(pattern.clone(), ¤t_date).await.unwrap(); query_result.iter().map(|result_item| Base64Bitmap::try_from(result_item).unwrap()).collect() }, }; @@ -349,7 +348,9 @@ mod tests { .expect_get_bitmaps_exact() .with( predicate::eq(vec!["someone.near".to_string()]), - predicate::eq(Utc::now().format("%Y-%m-%d").to_string()), + predicate::function(|date: &DateTime| { + date.date_naive() == Utc::now().date_naive() + }), ) .times(1) .returning(move |_, _| Ok(mock_query_result.clone())); @@ -389,11 +390,9 @@ mod tests { .expect_get_bitmaps_wildcard() .with( predicate::eq(".*\\.someone\\.near".to_string()), - predicate::eq( - (Utc::now() - Duration::days(2)) - .format("%Y-%m-%d") - .to_string(), - ), + predicate::function(|date: &DateTime| { + date.date_naive() == (Utc::now() - Duration::days(2)).date_naive() + }), ) .times(1) .returning(move |_, _| { @@ -406,11 +405,9 @@ mod tests { .expect_get_bitmaps_wildcard() .with( predicate::eq(".*\\.someone\\.near".to_string()), - predicate::eq( - (Utc::now() - Duration::days(1)) - .format("%Y-%m-%d") - .to_string(), - ), + predicate::function(|date: &DateTime| { + date.date_naive() == (Utc::now() - Duration::days(1)).date_naive() + }), ) .times(1) .returning(move |_, _| { @@ -423,7 +420,9 @@ mod tests { .expect_get_bitmaps_wildcard() .with( predicate::eq(".*\\.someone\\.near".to_string()), - predicate::eq(Utc::now().format("%Y-%m-%d").to_string()), + predicate::function(|date: &DateTime| { + date.date_naive() == Utc::now().date_naive() + }), ) .times(1) .returning(move |_, _| { diff --git a/block-streamer/src/graphql/client.rs b/block-streamer/src/graphql/client.rs index 6b56f3975..6f109603f 100644 --- a/block-streamer/src/graphql/client.rs +++ b/block-streamer/src/graphql/client.rs @@ -1,4 +1,5 @@ use ::reqwest; +use chrono::{DateTime, Utc}; use graphql_client::{GraphQLQuery, Response}; // TODO: Use Dataplatform account @@ -64,7 +65,7 @@ impl GraphQLClientImpl { pub async fn get_bitmaps_exact( &self, receiver_ids: Vec, - block_date: String, + block_date: &DateTime, ) -> anyhow::Result> { let mut all_query_results: Vec< @@ -76,7 +77,7 @@ impl GraphQLClientImpl { let mut query_result = self .post_graphql::(get_bitmaps_exact::Variables { receiver_ids: Some(receiver_ids.clone()), - block_date: Some(block_date.clone()), + block_date: Some(block_date.format("%Y-%m-%d").to_string()), limit: Some(QUERY_LIMIT), offset: Some(offset), }) @@ -99,7 +100,7 @@ impl GraphQLClientImpl { pub async fn get_bitmaps_wildcard( &self, receiver_ids: String, - block_date: String, + block_date: &DateTime, ) -> anyhow::Result> { let mut all_query_results: Vec< @@ -111,7 +112,7 @@ impl GraphQLClientImpl { let mut query_result = self .post_graphql::(get_bitmaps_wildcard::Variables { receiver_ids: Some(receiver_ids.clone()), - block_date: Some(block_date.clone()), + block_date: Some(block_date.format("%Y-%m-%d").to_string()), limit: Some(QUERY_LIMIT), offset: Some(offset), }) @@ -135,18 +136,28 @@ impl GraphQLClientImpl { // TODO: Remove Unit tests after bitmap query is integrated into the main application #[cfg(test)] mod tests { + use chrono::{NaiveDateTime, TimeZone}; + use super::*; const HASURA_ENDPOINT: &str = "https://queryapi-hasura-graphql-mainnet-vcqilefdcq-ew.a.run.app/v1/graphql"; + fn utc_date_time_from_date_string(date: &str) -> DateTime { + let naive_date_time: NaiveDateTime = chrono::NaiveDate::parse_from_str(date, "%Y-%m-%d") + .unwrap() + .and_hms_opt(0, 0, 0) + .unwrap(); + TimeZone::from_utc_datetime(&chrono::Utc, &naive_date_time) + } + #[tokio::test] async fn test_get_bitmaps_exact() { let client = GraphQLClientImpl::new(HASURA_ENDPOINT.to_string()); let receiver_ids = vec!["app.nearcrowd.near".to_string()]; - let block_date = "2024-03-21".to_string(); + let block_date: DateTime = utc_date_time_from_date_string("2024-03-21"); let response = client - .get_bitmaps_exact(receiver_ids, block_date) + .get_bitmaps_exact(receiver_ids, &block_date) .await .unwrap(); assert_eq!(response[0].first_block_height, 115130287); @@ -158,9 +169,9 @@ mod tests { async fn test_get_bitmaps_wildcard() { let client = GraphQLClientImpl::new(HASURA_ENDPOINT.to_string()); let receiver_ids = "app.nearcrowd.near".to_string(); - let block_date = "2024-03-21".to_string(); + let block_date: DateTime = utc_date_time_from_date_string("2024-03-21"); let response = client - .get_bitmaps_wildcard(receiver_ids, block_date) + .get_bitmaps_wildcard(receiver_ids, &block_date) .await .unwrap(); assert_eq!(response[0].first_block_height, 115130287);