From 8cc9810bd1526d5f82f1398ab6d56b2b7c46f9cd Mon Sep 17 00:00:00 2001 From: Darun Seethammagari Date: Mon, 20 May 2024 18:25:14 -0700 Subject: [PATCH] Prepare for PR --- block-streamer/examples/get_bitmaps.rs | 32 ----- block-streamer/src/graphql.rs | 76 ----------- block-streamer/src/graphql/client.rs | 126 ++++++++++++++++++ block-streamer/src/graphql/mod.rs | 2 + .../queries}/get_bitmaps_exact.rs | 0 .../queries}/get_bitmaps_wildcard.rs | 0 .../queries}/mod.rs | 0 block-streamer/src/lib.rs | 2 - block-streamer/src/main.rs | 1 - 9 files changed, 128 insertions(+), 111 deletions(-) delete mode 100644 block-streamer/examples/get_bitmaps.rs delete mode 100644 block-streamer/src/graphql.rs create mode 100644 block-streamer/src/graphql/client.rs create mode 100644 block-streamer/src/graphql/mod.rs rename block-streamer/src/{graphql_queries => graphql/queries}/get_bitmaps_exact.rs (100%) rename block-streamer/src/{graphql_queries => graphql/queries}/get_bitmaps_wildcard.rs (100%) rename block-streamer/src/{graphql_queries => graphql/queries}/mod.rs (100%) diff --git a/block-streamer/examples/get_bitmaps.rs b/block-streamer/examples/get_bitmaps.rs deleted file mode 100644 index 7d2dd10fb..000000000 --- a/block-streamer/examples/get_bitmaps.rs +++ /dev/null @@ -1,32 +0,0 @@ -// TODO: Remove this file when working query to production bitmap indexer is ready -use block_streamer::graphql; - -use std::error::Error; - -#[tokio::main] -async fn main() -> Result<(), Box> { - let client = graphql::GraphqlClient::new( - "https://queryapi-hasura-graphql-mainnet-vcqilefdcq-ew.a.run.app/v1/graphql".to_string(), - ); - - let exact_query = client - .get_bitmaps_exact( - vec!["app.nearcrowd.near".to_owned()], - "2024-03-21".to_string(), - 100, - 0, - ) - .await; - println!("exact query: {:#?}", exact_query); - - let wildcard_query = client - .get_bitmaps_wildcard( - "app.nearcrowd.near".to_string(), - "2024-03-21".to_string(), - 100, - 0, - ) - .await; - println!("wildcard query: {:#?}", wildcard_query); - Ok(()) -} diff --git a/block-streamer/src/graphql.rs b/block-streamer/src/graphql.rs deleted file mode 100644 index 36981a48b..000000000 --- a/block-streamer/src/graphql.rs +++ /dev/null @@ -1,76 +0,0 @@ -use crate::graphql_queries::get_bitmaps_exact::{get_bitmaps_exact, GetBitmapsExact}; -use crate::graphql_queries::get_bitmaps_wildcard::{get_bitmaps_wildcard, GetBitmapsWildcard}; -use ::reqwest; -use graphql_client::{GraphQLQuery, Response}; -use std::error::Error; - -const HASURA_ACCOUNT: &str = "darunrs_near"; - -pub struct GraphqlClient { - client: reqwest::Client, - graphql_endpoint: String, -} - -#[cfg_attr(test, mockall::automock)] -impl GraphqlClient { - pub fn new(graphql_endpoint: String) -> Self { - Self { - client: reqwest::Client::new(), - graphql_endpoint, - } - } - - pub async fn get_bitmaps_exact( - &self, - receiver_ids: Vec, - block_date: String, - limit: i64, - offset: i64, - ) -> Result, Box> { - let variables = get_bitmaps_exact::Variables { - receiver_ids: Some(receiver_ids), - block_date: Some(block_date), - limit: Some(limit), - offset: Some(offset), - }; - let request_body = GetBitmapsExact::build_query(variables); - let res = self - .client - .post(&self.graphql_endpoint) - .header("x-hasura-role", HASURA_ACCOUNT) - .json(&request_body) - .send() - .await - .expect("Failed to query bitmaps for list of exact receivers"); - let response_body: Response = res.json().await?; - Ok(response_body) - } - - pub async fn get_bitmaps_wildcard( - &self, - receiver_ids: String, - block_date: String, - limit: i64, - offset: i64, - ) -> Result, Box> { - let variables = get_bitmaps_wildcard::Variables { - receiver_ids: Some(receiver_ids), - block_date: Some(block_date), - limit: Some(limit), - offset: Some(offset), - }; - let request_body = GetBitmapsWildcard::build_query(variables); - let res = self - .client - .post(&self.graphql_endpoint) - .header("x-hasura-role", HASURA_ACCOUNT) - .json(&request_body) - .send() - .await - .expect("Failed to query bitmaps for wildcard receivers"); - let response_body: Response = res.json().await?; - Ok(response_body) - } -} - -// TODO: Add Unit Tests diff --git a/block-streamer/src/graphql/client.rs b/block-streamer/src/graphql/client.rs new file mode 100644 index 000000000..efe798317 --- /dev/null +++ b/block-streamer/src/graphql/client.rs @@ -0,0 +1,126 @@ +use crate::graphql::queries::get_bitmaps_exact::{get_bitmaps_exact, GetBitmapsExact}; +use crate::graphql::queries::get_bitmaps_wildcard::{get_bitmaps_wildcard, GetBitmapsWildcard}; +use ::reqwest; +use graphql_client::{GraphQLQuery, Response}; +use std::error::Error; + +// TODO: Use Dataplatform account +const HASURA_ACCOUNT: &str = "darunrs_near"; + +pub struct GraphqlClient { + client: reqwest::Client, + graphql_endpoint: String, +} + +#[cfg_attr(test, mockall::automock)] +impl GraphqlClient { + pub fn new(graphql_endpoint: String) -> Self { + Self { + client: reqwest::Client::new(), + graphql_endpoint, + } + } + + pub async fn get_bitmaps_exact( + &self, + receiver_ids: Vec, + block_date: String, + limit: i64, + offset: i64, + ) -> Result< + Vec, + Box, + > { + let variables = get_bitmaps_exact::Variables { + receiver_ids: Some(receiver_ids), + block_date: Some(block_date), + limit: Some(limit), + offset: Some(offset), + }; + let request_body = GetBitmapsExact::build_query(variables); + let res = self + .client + .post(&self.graphql_endpoint) + .header("x-hasura-role", HASURA_ACCOUNT) + .json(&request_body) + .send() + .await + .expect("Failed to query bitmaps for list of exact receivers"); + let response_body: Response = res.json().await?; + match response_body.data { + Some(data) => Ok(data.darunrs_near_bitmap_v5_actions_index), + None => Ok([].into()), + } + } + + pub async fn get_bitmaps_wildcard( + &self, + receiver_ids: String, + block_date: String, + limit: i64, + offset: i64, + ) -> Result< + Vec, + Box, + > { + let variables = get_bitmaps_wildcard::Variables { + receiver_ids: Some(receiver_ids), + block_date: Some(block_date), + limit: Some(limit), + offset: Some(offset), + }; + let request_body = GetBitmapsWildcard::build_query(variables); + let res = self + .client + .post(&self.graphql_endpoint) + .header("x-hasura-role", HASURA_ACCOUNT) + .json(&request_body) + .send() + .await + .expect("Failed to query bitmaps for wildcard receivers"); + let response_body: Response = res.json().await?; + match response_body.data { + Some(data) => Ok(data.darunrs_near_bitmap_v5_actions_index), + None => Ok([].into()), + } + } +} + +// TODO: Remove Unit tests after bitmap query is integrated into the main application +#[cfg(test)] +mod tests { + use super::*; + + const HASURA_ENDPOINT: &str = + "https://queryapi-hasura-graphql-mainnet-vcqilefdcq-ew.a.run.app/v1/graphql"; + + #[tokio::test] + async fn test_get_bitmaps_exact() { + let client = GraphqlClient::new(HASURA_ENDPOINT.to_string()); + let receiver_ids = vec!["app.nearcrowd.near".to_string()]; + let block_date = "2024-03-21".to_string(); + let limit = 10; + let offset = 0; + let response = client + .get_bitmaps_exact(receiver_ids, block_date, limit, offset) + .await + .unwrap(); + assert_eq!(response[0].first_block_height, 115130287); + } + + // This query takes several seconds + #[ignore] + #[tokio::test] + async fn test_get_bitmaps_wildcard() { + let client = GraphqlClient::new(HASURA_ENDPOINT.to_string()); + let receiver_ids = "app.nearcrowd.near".to_string(); + let block_date = "2024-03-21".to_string(); + let limit = 10; + let offset = 0; + let response = client + .get_bitmaps_wildcard(receiver_ids, block_date, limit, offset) + .await + .unwrap(); + assert_eq!(response[0].first_block_height, 115130287); + } +} diff --git a/block-streamer/src/graphql/mod.rs b/block-streamer/src/graphql/mod.rs new file mode 100644 index 000000000..a718684ac --- /dev/null +++ b/block-streamer/src/graphql/mod.rs @@ -0,0 +1,2 @@ +pub mod client; +pub mod queries; diff --git a/block-streamer/src/graphql_queries/get_bitmaps_exact.rs b/block-streamer/src/graphql/queries/get_bitmaps_exact.rs similarity index 100% rename from block-streamer/src/graphql_queries/get_bitmaps_exact.rs rename to block-streamer/src/graphql/queries/get_bitmaps_exact.rs diff --git a/block-streamer/src/graphql_queries/get_bitmaps_wildcard.rs b/block-streamer/src/graphql/queries/get_bitmaps_wildcard.rs similarity index 100% rename from block-streamer/src/graphql_queries/get_bitmaps_wildcard.rs rename to block-streamer/src/graphql/queries/get_bitmaps_wildcard.rs diff --git a/block-streamer/src/graphql_queries/mod.rs b/block-streamer/src/graphql/queries/mod.rs similarity index 100% rename from block-streamer/src/graphql_queries/mod.rs rename to block-streamer/src/graphql/queries/mod.rs diff --git a/block-streamer/src/lib.rs b/block-streamer/src/lib.rs index 9b6201984..a91817f41 100644 --- a/block-streamer/src/lib.rs +++ b/block-streamer/src/lib.rs @@ -3,5 +3,3 @@ mod blockstreamer { } pub use blockstreamer::*; -pub mod graphql; -pub mod graphql_queries; diff --git a/block-streamer/src/main.rs b/block-streamer/src/main.rs index 561aca132..29e915823 100644 --- a/block-streamer/src/main.rs +++ b/block-streamer/src/main.rs @@ -3,7 +3,6 @@ use tracing_subscriber::prelude::*; mod block_stream; mod delta_lake_client; mod graphql; -mod graphql_queries; mod indexer_config; mod lake_s3_client; mod metrics;