Skip to content

Commit

Permalink
Add exact string query
Browse files Browse the repository at this point in the history
  • Loading branch information
darunrs committed May 21, 2024
1 parent b6b01fc commit c17a4a1
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 54 deletions.
55 changes: 21 additions & 34 deletions block-streamer/examples/get_bitmaps.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error>> {
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<get_bitmaps::ResponseData> = res.json().await?;
println!("{:#?}", response_body);
Ok(())
}
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// 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
}
4 changes: 2 additions & 2 deletions block-streamer/graphql/darunrs_near/get_bitmaps.graphql
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
48 changes: 48 additions & 0 deletions block-streamer/graphql/darunrs_near/get_bitmaps.rs
Original file line number Diff line number Diff line change
@@ -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<date>,
pub receiver_ids: Option<Vec<String>>,
pub limit: Option<Int>,
pub offset: Option<Int>,
}
impl Variables {}
#[derive(Deserialize, Debug)]
pub struct ResponseData {
pub darunrs_near_bitmap_v5_actions_index:
Vec<GetBitmapsExactDarunrsNearBitmapV5ActionsIndex>,
}
#[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<Self::Variables> {
graphql_client::QueryBody {
variables,
query: get_bitmaps_exact::QUERY,
operation_name: get_bitmaps_exact::OPERATION_NAME,
}
}
}
66 changes: 48 additions & 18 deletions block-streamer/src/graphql.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -30,17 +20,57 @@ impl GraphqlClient {
}
}

pub async fn get_bitmaps(&self, receiver_ids: String, block_date: String, limit: i64, offset: i64) -> Result<(), Box<dyn Error>> {
let variables = get_bitmaps::Variables {
pub async fn get_bitmaps_exact(
&self,
receiver_ids: Vec<String>,
block_date: String,
limit: i64,
offset: i64,
) -> Result<(), Box<dyn Error>> {
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<get_bitmaps_exact::ResponseData> = 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<dyn Error>> {
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<get_bitmaps::ResponseData> = 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<get_bitmaps_wildcard::ResponseData> = res.json().await?;
println!("{:#?}", response_body);
Ok(())
}
}

// TODO: Add Unit Tests
48 changes: 48 additions & 0 deletions block-streamer/src/graphql_queries/get_bitmaps_exact.rs
Original file line number Diff line number Diff line change
@@ -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<date>,
pub receiver_ids: Option<Vec<String>>,
pub limit: Option<Int>,
pub offset: Option<Int>,
}
impl Variables {}
#[derive(Deserialize, Debug)]
pub struct ResponseData {
pub darunrs_near_bitmap_v5_actions_index:
Vec<GetBitmapsExactDarunrsNearBitmapV5ActionsIndex>,
}
#[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<Self::Variables> {
graphql_client::QueryBody {
variables,
query: get_bitmaps_exact::QUERY,
operation_name: get_bitmaps_exact::OPERATION_NAME,
}
}
}
48 changes: 48 additions & 0 deletions block-streamer/src/graphql_queries/get_bitmaps_wildcard.rs
Original file line number Diff line number Diff line change
@@ -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<date>,
pub receiver_ids: Option<String>,
pub limit: Option<Int>,
pub offset: Option<Int>,
}
impl Variables {}
#[derive(Deserialize, Debug)]
pub struct ResponseData {
pub darunrs_near_bitmap_v5_actions_index:
Vec<GetBitmapsWildcardDarunrsNearBitmapV5ActionsIndex>,
}
#[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<Self::Variables> {
graphql_client::QueryBody {
variables,
query: get_bitmaps_wildcard::QUERY,
operation_name: get_bitmaps_wildcard::OPERATION_NAME,
}
}
}
2 changes: 2 additions & 0 deletions block-streamer/src/graphql_queries/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod get_bitmaps_wildcard;
pub mod get_bitmaps_exact;
1 change: 1 addition & 0 deletions block-streamer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ mod blockstreamer {

pub use blockstreamer::*;
pub mod graphql;
pub mod graphql_queries;
1 change: 1 addition & 0 deletions block-streamer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c17a4a1

Please sign in to comment.