Skip to content

Commit

Permalink
refactor: consolidate indexing status requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus committed Dec 6, 2024
1 parent d1a0d1b commit 50c0d2d
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 347 deletions.
1 change: 0 additions & 1 deletion src/indexers.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod indexing_progress;
pub mod public_poi;
201 changes: 0 additions & 201 deletions src/indexers/indexing_progress.rs

This file was deleted.

14 changes: 14 additions & 0 deletions src/network.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub use errors::{DeploymentError, SubgraphError};
pub use internal::{Indexing, IndexingId};
pub use service::{NetworkService, ResolvedSubgraphInfo};
use thegraph_graphql_http::graphql::{IntoDocument as _, IntoDocumentWithVariables};

pub mod cost_model;
mod errors;
Expand All @@ -12,3 +13,16 @@ pub mod internal;
pub mod service;
pub mod subgraph_client;
pub mod version_filter;

pub struct GraphQlRequest {
document: String,
variables: serde_json::Value,
}
impl IntoDocumentWithVariables for GraphQlRequest {
type Variables = serde_json::Value;
fn into_document_with_variables(
self,
) -> (thegraph_graphql_http::graphql::Document, Self::Variables) {
(self.document.into_document(), self.variables)
}
}
27 changes: 8 additions & 19 deletions src/network/cost_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use std::collections::HashMap;

use anyhow::anyhow;
use thegraph_core::DeploymentId;
use thegraph_graphql_http::{
graphql::{Document, IntoDocument, IntoDocumentWithVariables},
http_client::ReqwestExt,
};
use thegraph_graphql_http::http_client::ReqwestExt;
use url::Url;

use crate::network::GraphQlRequest;

pub struct CostModelResolver {
http: reqwest::Client,
cache: parking_lot::Mutex<HashMap<DeploymentId, u128>>,
Expand Down Expand Up @@ -51,15 +50,14 @@ impl CostModelResolver {
) -> anyhow::Result<HashMap<DeploymentId, String>> {
let url = url.join("cost").map_err(|_| anyhow!("invalid URL"))?;

const QUERY: &str = r#"
let query = r#"
query costModels($deployments: [String!]!) {
costModels(deployments: $deployments) {
deployment
model
}
}
"#;
let deployments = deployments.iter().map(|item| item.to_string()).collect();
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct Response {
Expand All @@ -70,22 +68,13 @@ impl CostModelResolver {
pub deployment: DeploymentId,
pub model: String,
}
struct Request {
deployments: Vec<String>,
}
impl IntoDocumentWithVariables for Request {
type Variables = serde_json::Value;
fn into_document_with_variables(self) -> (Document, Self::Variables) {
(
QUERY.into_document(),
serde_json::json!({ "deployments": self.deployments }),
)
}
}
let resp = self
.http
.post(url)
.send_graphql::<Response>(Request { deployments })
.send_graphql::<Response>(GraphQlRequest {
document: query.to_string(),
variables: serde_json::json!({ "deployments": deployments }),
})
.await??;
Ok(resp
.cost_models
Expand Down
Loading

0 comments on commit 50c0d2d

Please sign in to comment.