Skip to content

Commit

Permalink
fix: Add missing /cost GraphQL API back
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannis committed Sep 19, 2023
1 parent f752f80 commit cc960b5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions service/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ pub async fn create_server(options: ServerOptions) -> Router {
get(routes::deployment::deployment_health
.layer(AddExtensionLayer::new(slow_ratelimiter()))),
)
.route(
"/cost",
post(routes::cost::graphql_handler)
.get(routes::cost::graphql_handler)
.layer(AddExtensionLayer::new(slow_ratelimiter())),
)
.nest(
"/operator",
routes::basic::create_operator_server(options.clone())
Expand Down
51 changes: 51 additions & 0 deletions service/src/server/routes/cost.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2023-, GraphOps and Semiotic Labs.
// SPDX-License-Identifier: Apache-2.0

use async_graphql::{Context, EmptyMutation, EmptySubscription, Object, Schema};
use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
use axum::extract::Extension;

use crate::{
common::{
indexer_error::IndexerError,
indexer_management::{self, CostModel},
},
server::ServerOptions,
};

pub type CostSchema = Schema<QueryRoot, EmptyMutation, EmptySubscription>;

#[derive(Default)]
pub struct QueryRoot;

#[Object]
impl QueryRoot {
async fn cost_models(
&self,
ctx: &Context<'_>,
deployments: Vec<String>,
) -> Result<Vec<CostModel>, IndexerError> {
let pool = &ctx.data_unchecked::<ServerOptions>().indexer_management_db;
indexer_management::cost_models(pool, &deployments).await
}

async fn cost_model(
&self,
ctx: &Context<'_>,
deployment: String,
) -> Result<Option<CostModel>, IndexerError> {
let pool = &ctx.data_unchecked::<ServerOptions>().indexer_management_db;
indexer_management::cost_model(pool, &deployment).await
}
}

pub(crate) async fn graphql_handler(
req: GraphQLRequest,
Extension(schema): Extension<CostSchema>,
Extension(server_options): Extension<ServerOptions>,
) -> GraphQLResponse {
schema
.execute(req.into_inner().data(server_options))
.await
.into()
}
1 change: 1 addition & 0 deletions service/src/server/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use hyper::http::HeaderName;
use tower::limit::RateLimitLayer;

pub mod basic;
pub mod cost;
pub mod deployment;
pub mod network;
pub mod status;
Expand Down

0 comments on commit cc960b5

Please sign in to comment.