Skip to content

Commit

Permalink
Graphql client now takes DateTime arg
Browse files Browse the repository at this point in the history
  • Loading branch information
darunrs committed Jun 12, 2024
1 parent 8e0ae5a commit 294e172
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
29 changes: 14 additions & 15 deletions block-streamer/src/block_height_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Base64Bitmap> = 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(), &current_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(), &current_date).await.unwrap();
query_result.iter().map(|result_item| Base64Bitmap::try_from(result_item).unwrap()).collect()
},
};
Expand Down Expand Up @@ -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<Utc>| {
date.date_naive() == Utc::now().date_naive()
}),
)
.times(1)
.returning(move |_, _| Ok(mock_query_result.clone()));
Expand Down Expand Up @@ -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<Utc>| {
date.date_naive() == (Utc::now() - Duration::days(2)).date_naive()
}),
)
.times(1)
.returning(move |_, _| {
Expand All @@ -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<Utc>| {
date.date_naive() == (Utc::now() - Duration::days(1)).date_naive()
}),
)
.times(1)
.returning(move |_, _| {
Expand All @@ -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<Utc>| {
date.date_naive() == Utc::now().date_naive()
}),
)
.times(1)
.returning(move |_, _| {
Expand Down
27 changes: 19 additions & 8 deletions block-streamer/src/graphql/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ::reqwest;
use chrono::{DateTime, Utc};
use graphql_client::{GraphQLQuery, Response};

// TODO: Use Dataplatform account
Expand Down Expand Up @@ -64,7 +65,7 @@ impl GraphQLClientImpl {
pub async fn get_bitmaps_exact(
&self,
receiver_ids: Vec<String>,
block_date: String,
block_date: &DateTime<Utc>,
) -> anyhow::Result<Vec<get_bitmaps_exact::GetBitmapsExactDarunrsNearBitmapV5ActionsIndex>>
{
let mut all_query_results: Vec<
Expand All @@ -76,7 +77,7 @@ impl GraphQLClientImpl {
let mut query_result = self
.post_graphql::<GetBitmapsExact>(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),
})
Expand All @@ -99,7 +100,7 @@ impl GraphQLClientImpl {
pub async fn get_bitmaps_wildcard(
&self,
receiver_ids: String,
block_date: String,
block_date: &DateTime<Utc>,
) -> anyhow::Result<Vec<get_bitmaps_wildcard::GetBitmapsWildcardDarunrsNearBitmapV5ActionsIndex>>
{
let mut all_query_results: Vec<
Expand All @@ -111,7 +112,7 @@ impl GraphQLClientImpl {
let mut query_result = self
.post_graphql::<GetBitmapsWildcard>(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),
})
Expand All @@ -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<Utc> {
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> = 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);
Expand All @@ -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> = 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);
Expand Down

0 comments on commit 294e172

Please sign in to comment.