From c17a4a1b523f62ed586ffcf0a7a2f6039d95f2ae Mon Sep 17 00:00:00 2001 From: Darun Seethammagari Date: Mon, 20 May 2024 17:04:04 -0700 Subject: [PATCH] Add exact string query --- block-streamer/examples/get_bitmaps.rs | 55 ++++++---------- .../graphql/darunrs_near/get_bitmaps.graphql | 4 +- .../graphql/darunrs_near/get_bitmaps.rs | 48 ++++++++++++++ block-streamer/src/graphql.rs | 66 ++++++++++++++----- .../src/graphql_queries/get_bitmaps_exact.rs | 48 ++++++++++++++ .../graphql_queries/get_bitmaps_wildcard.rs | 48 ++++++++++++++ block-streamer/src/graphql_queries/mod.rs | 2 + block-streamer/src/lib.rs | 1 + block-streamer/src/main.rs | 1 + 9 files changed, 219 insertions(+), 54 deletions(-) create mode 100644 block-streamer/graphql/darunrs_near/get_bitmaps.rs create mode 100644 block-streamer/src/graphql_queries/get_bitmaps_exact.rs create mode 100644 block-streamer/src/graphql_queries/get_bitmaps_wildcard.rs create mode 100644 block-streamer/src/graphql_queries/mod.rs diff --git a/block-streamer/examples/get_bitmaps.rs b/block-streamer/examples/get_bitmaps.rs index 0e8b2aeb4..1a9ba0302 100644 --- a/block-streamer/examples/get_bitmaps.rs +++ b/block-streamer/examples/get_bitmaps.rs @@ -1,40 +1,27 @@ -use graphql_client::{GraphQLQuery, Response}; -use std::error::Error; -use ::reqwest::Client; +// TODO: Remove this file when working query to production bitmap indexer is ready use block_streamer::graphql; -#[allow(clippy::upper_case_acronyms)] -type URI = String; -type Date = String; - -#[derive(GraphQLQuery)] -#[graphql( - schema_path = "graphql/darunrs_near/schema.json", - query_path = "graphql/darunrs_near/get_bitmaps.graphql", - response_derives = "Debug", - normalization = "rust" -)] -struct GetBitmaps; - -async fn perform_my_query(variables: get_bitmaps::Variables) -> Result<(), Box> { - let request_body = GetBitmaps::build_query(variables); - - let client = reqwest::Client::new(); - let res = client.post("https://queryapi-hasura-graphql-mainnet-vcqilefdcq-ew.a.run.app/v1/graphql").header("x-hasura-role", "darunrs_near").json(&request_body).send().await?; - let response_body: Response = res.json().await?; - println!("{:#?}", response_body); - Ok(()) -} +use std::error::Error; #[tokio::main] async fn main() -> Result<(), Box> { - // let variables = get_bitmaps::Variables { - // receiver_ids: Some(".*".to_string()), - // block_date: Some("2024-03-21".to_string()), - // limit: Some(100), - // offset: Some(0), - // }; - // perform_my_query(variables).await - let client = graphql::GraphqlClient::new("https://queryapi-hasura-graphql-mainnet-vcqilefdcq-ew.a.run.app/v1/graphql".to_string()); - client.get_bitmaps("app.nearcrowd.near".to_string(), "2024-03-21".to_string(), 100, 0).await + let client = graphql::GraphqlClient::new( + "https://queryapi-hasura-graphql-mainnet-vcqilefdcq-ew.a.run.app/v1/graphql".to_string(), + ); + let _ = client + .get_bitmaps_wildcard( + "app.nearcrowd.near".to_string(), + "2024-03-21".to_string(), + 100, + 0, + ) + .await; + client + .get_bitmaps_exact( + vec!["app.nearcrowd.near".to_owned()], + "2024-03-21".to_string(), + 100, + 0, + ) + .await } diff --git a/block-streamer/graphql/darunrs_near/get_bitmaps.graphql b/block-streamer/graphql/darunrs_near/get_bitmaps.graphql index 5b804e8a6..3709b6bf9 100644 --- a/block-streamer/graphql/darunrs_near/get_bitmaps.graphql +++ b/block-streamer/graphql/darunrs_near/get_bitmaps.graphql @@ -1,5 +1,5 @@ -query GetBitmaps($block_date: date, $receiver_ids: String, $limit: Int, $offset: Int) { - darunrs_near_bitmap_v5_actions_index(limit: $limit, offset: $offset, where: {block_date: {_eq: $block_date}, receiver: {receiver: {_regex: $receiver_ids}}}) { +query GetBitmapsExact($block_date: date, $receiver_ids: [String!], $limit: Int, $offset: Int) { + darunrs_near_bitmap_v5_actions_index(limit: $limit, offset: $offset, where: {block_date: {_eq: $block_date}, receiver: {receiver: {_in: $receiver_ids}}}) { bitmap first_block_height } diff --git a/block-streamer/graphql/darunrs_near/get_bitmaps.rs b/block-streamer/graphql/darunrs_near/get_bitmaps.rs new file mode 100644 index 000000000..c16b502ae --- /dev/null +++ b/block-streamer/graphql/darunrs_near/get_bitmaps.rs @@ -0,0 +1,48 @@ +#![allow(clippy::all, warnings)] +pub struct GetBitmapsExact; +pub mod get_bitmaps_exact { + #![allow(dead_code)] + use std::result::Result; + pub const OPERATION_NAME: &str = "GetBitmapsExact"; + pub const QUERY : & str = "query GetBitmapsExact($block_date: date, $receiver_ids: [String!], $limit: Int, $offset: Int) {\n darunrs_near_bitmap_v5_actions_index(limit: $limit, offset: $offset, where: {block_date: {_eq: $block_date}, receiver: {receiver: {_in: $receiver_ids}}}) {\n bitmap\n first_block_height\n }\n}" ; + use super::*; + use serde::{Deserialize, Serialize}; + #[allow(dead_code)] + type Boolean = bool; + #[allow(dead_code)] + type Float = f64; + #[allow(dead_code)] + type Int = i64; + #[allow(dead_code)] + type ID = String; + type date = super::date; + #[derive(Serialize)] + pub struct Variables { + pub block_date: Option, + pub receiver_ids: Option>, + pub limit: Option, + pub offset: Option, + } + impl Variables {} + #[derive(Deserialize, Debug)] + pub struct ResponseData { + pub darunrs_near_bitmap_v5_actions_index: + Vec, + } + #[derive(Deserialize, Debug)] + pub struct GetBitmapsExactDarunrsNearBitmapV5ActionsIndex { + pub bitmap: String, + pub first_block_height: Int, + } +} +impl graphql_client::GraphQLQuery for GetBitmapsExact { + type Variables = get_bitmaps_exact::Variables; + type ResponseData = get_bitmaps_exact::ResponseData; + fn build_query(variables: Self::Variables) -> ::graphql_client::QueryBody { + graphql_client::QueryBody { + variables, + query: get_bitmaps_exact::QUERY, + operation_name: get_bitmaps_exact::OPERATION_NAME, + } + } +} diff --git a/block-streamer/src/graphql.rs b/block-streamer/src/graphql.rs index be46612de..c6657d7aa 100644 --- a/block-streamer/src/graphql.rs +++ b/block-streamer/src/graphql.rs @@ -1,21 +1,11 @@ +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; -use ::reqwest; const HASURA_ACCOUNT: &str = "darunrs_near"; -#[allow(clippy::upper_case_acronyms)] -type Date = String; - -#[derive(GraphQLQuery)] -#[graphql( - schema_path = "graphql/darunrs_near/schema.json", - query_path = "graphql/darunrs_near/get_bitmaps.graphql", - response_derives = "Debug", - normalization = "rust" -)] -struct GetBitmaps; - pub struct GraphqlClient { client: reqwest::Client, graphql_endpoint: String, @@ -30,17 +20,57 @@ impl GraphqlClient { } } - pub async fn get_bitmaps(&self, receiver_ids: String, block_date: String, limit: i64, offset: i64) -> Result<(), Box> { - let variables = get_bitmaps::Variables { + 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?; + let response_body: Response = res.json().await?; + println!("{:#?}", response_body); + Ok(()) + } + + 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 = GetBitmaps::build_query(variables); - let res = self.client.post(&self.graphql_endpoint).header("x-hasura-role", HASURA_ACCOUNT).json(&request_body).send().await?; - let response_body: Response = res.json().await?; + 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?; + let response_body: Response = res.json().await?; println!("{:#?}", response_body); Ok(()) } } + +// TODO: Add Unit Tests diff --git a/block-streamer/src/graphql_queries/get_bitmaps_exact.rs b/block-streamer/src/graphql_queries/get_bitmaps_exact.rs new file mode 100644 index 000000000..f1443c3bf --- /dev/null +++ b/block-streamer/src/graphql_queries/get_bitmaps_exact.rs @@ -0,0 +1,48 @@ +#![allow(clippy::all, warnings)] +pub struct GetBitmapsExact; +pub mod get_bitmaps_exact { + #![allow(dead_code)] + use std::result::Result; + pub const OPERATION_NAME: &str = "GetBitmapsExact"; + pub const QUERY : & str = "query GetBitmapsExact($block_date: date, $receiver_ids: [String!], $limit: Int, $offset: Int) {\n darunrs_near_bitmap_v5_actions_index(limit: $limit, offset: $offset, where: {block_date: {_eq: $block_date}, receiver: {receiver: {_in: $receiver_ids}}}) {\n bitmap\n first_block_height\n }\n}" ; + use super::*; + use serde::{Deserialize, Serialize}; + #[allow(dead_code)] + type Boolean = bool; + #[allow(dead_code)] + type Float = f64; + #[allow(dead_code)] + type Int = i64; + #[allow(dead_code)] + type ID = String; + type date = String; + #[derive(Serialize)] + pub struct Variables { + pub block_date: Option, + pub receiver_ids: Option>, + pub limit: Option, + pub offset: Option, + } + impl Variables {} + #[derive(Deserialize, Debug)] + pub struct ResponseData { + pub darunrs_near_bitmap_v5_actions_index: + Vec, + } + #[derive(Deserialize, Debug)] + pub struct GetBitmapsExactDarunrsNearBitmapV5ActionsIndex { + pub bitmap: String, + pub first_block_height: Int, + } +} +impl graphql_client::GraphQLQuery for GetBitmapsExact { + type Variables = get_bitmaps_exact::Variables; + type ResponseData = get_bitmaps_exact::ResponseData; + fn build_query(variables: Self::Variables) -> ::graphql_client::QueryBody { + graphql_client::QueryBody { + variables, + query: get_bitmaps_exact::QUERY, + operation_name: get_bitmaps_exact::OPERATION_NAME, + } + } +} diff --git a/block-streamer/src/graphql_queries/get_bitmaps_wildcard.rs b/block-streamer/src/graphql_queries/get_bitmaps_wildcard.rs new file mode 100644 index 000000000..0d57d5da9 --- /dev/null +++ b/block-streamer/src/graphql_queries/get_bitmaps_wildcard.rs @@ -0,0 +1,48 @@ +#![allow(clippy::all, warnings)] +pub struct GetBitmapsWildcard; +pub mod get_bitmaps_wildcard { + #![allow(dead_code)] + use std::result::Result; + pub const OPERATION_NAME: &str = "GetBitmapsWildcard"; + pub const QUERY : & str = "query GetBitmapsWildcard($block_date: date, $receiver_ids: String, $limit: Int, $offset: Int) {\n darunrs_near_bitmap_v5_actions_index(limit: $limit, offset: $offset, where: {block_date: {_eq: $block_date}, receiver: {receiver: {_regex: $receiver_ids}}}) {\n bitmap\n first_block_height\n }\n}" ; + use super::*; + use serde::{Deserialize, Serialize}; + #[allow(dead_code)] + type Boolean = bool; + #[allow(dead_code)] + type Float = f64; + #[allow(dead_code)] + type Int = i64; + #[allow(dead_code)] + type ID = String; + type date = String; + #[derive(Serialize)] + pub struct Variables { + pub block_date: Option, + pub receiver_ids: Option, + pub limit: Option, + pub offset: Option, + } + impl Variables {} + #[derive(Deserialize, Debug)] + pub struct ResponseData { + pub darunrs_near_bitmap_v5_actions_index: + Vec, + } + #[derive(Deserialize, Debug)] + pub struct GetBitmapsWildcardDarunrsNearBitmapV5ActionsIndex { + pub bitmap: String, + pub first_block_height: Int, + } +} +impl graphql_client::GraphQLQuery for GetBitmapsWildcard { + type Variables = get_bitmaps_wildcard::Variables; + type ResponseData = get_bitmaps_wildcard::ResponseData; + fn build_query(variables: Self::Variables) -> ::graphql_client::QueryBody { + graphql_client::QueryBody { + variables, + query: get_bitmaps_wildcard::QUERY, + operation_name: get_bitmaps_wildcard::OPERATION_NAME, + } + } +} diff --git a/block-streamer/src/graphql_queries/mod.rs b/block-streamer/src/graphql_queries/mod.rs new file mode 100644 index 000000000..3c653c616 --- /dev/null +++ b/block-streamer/src/graphql_queries/mod.rs @@ -0,0 +1,2 @@ +pub mod get_bitmaps_wildcard; +pub mod get_bitmaps_exact; diff --git a/block-streamer/src/lib.rs b/block-streamer/src/lib.rs index 594491014..9b6201984 100644 --- a/block-streamer/src/lib.rs +++ b/block-streamer/src/lib.rs @@ -4,3 +4,4 @@ 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 29e915823..561aca132 100644 --- a/block-streamer/src/main.rs +++ b/block-streamer/src/main.rs @@ -3,6 +3,7 @@ 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;