|
| 1 | +use crate::subgraph_client::Query; |
| 2 | +use axum::{ |
| 3 | + extract::Path, |
| 4 | + response::{IntoResponse, Response as AxumResponse}, |
| 5 | + Extension, Json, |
| 6 | +}; |
| 7 | +use reqwest::StatusCode; |
| 8 | +use serde::Deserialize; |
| 9 | +use serde_json::json; |
| 10 | +use thiserror::Error; |
| 11 | + |
| 12 | +use super::GraphNodeConfig; |
| 13 | + |
| 14 | +#[derive(Deserialize, Debug)] |
| 15 | +struct Response { |
| 16 | + data: SubgraphData, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Deserialize, Debug)] |
| 20 | +#[allow(non_snake_case)] |
| 21 | +struct SubgraphData { |
| 22 | + indexingStatuses: Vec<IndexingStatus>, |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Deserialize, Debug)] |
| 26 | +struct IndexingStatus { |
| 27 | + health: Health, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Deserialize, Debug)] |
| 31 | +#[allow(non_camel_case_types)] |
| 32 | +enum Health { |
| 33 | + healthy, |
| 34 | + unhealthy, |
| 35 | + failed, |
| 36 | +} |
| 37 | + |
| 38 | +impl Health { |
| 39 | + fn as_str(&self) -> &str { |
| 40 | + match self { |
| 41 | + Health::healthy => "healthy", |
| 42 | + Health::unhealthy => "unhealthy", |
| 43 | + Health::failed => "failed", |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Debug, Error)] |
| 49 | +pub enum CheckHealthError { |
| 50 | + #[error("Graph node config not found")] |
| 51 | + GraphNodeConfigNotFound, |
| 52 | + #[error("Deployment not found")] |
| 53 | + DeploymentNotFound, |
| 54 | + #[error("Failed to process query")] |
| 55 | + QueryForwardingError, |
| 56 | +} |
| 57 | + |
| 58 | +impl IntoResponse for CheckHealthError { |
| 59 | + fn into_response(self) -> AxumResponse { |
| 60 | + let (status, error_message) = match &self { |
| 61 | + CheckHealthError::GraphNodeConfigNotFound => { |
| 62 | + (StatusCode::NOT_FOUND, "Graph node config not found") |
| 63 | + } |
| 64 | + CheckHealthError::DeploymentNotFound => (StatusCode::NOT_FOUND, "Deployment not found"), |
| 65 | + CheckHealthError::QueryForwardingError => { |
| 66 | + (StatusCode::INTERNAL_SERVER_ERROR, "Failed to process query") |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + let body = serde_json::json!({ |
| 71 | + "error": error_message, |
| 72 | + }); |
| 73 | + |
| 74 | + (status, Json(body)).into_response() |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +pub async fn health( |
| 79 | + Path(deployment_id): Path<String>, |
| 80 | + Extension(graph_node): Extension<Option<GraphNodeConfig>>, |
| 81 | +) -> Result<impl IntoResponse, CheckHealthError> { |
| 82 | + let url = if let Some(graph_node) = graph_node { |
| 83 | + graph_node.status_url |
| 84 | + } else { |
| 85 | + return Err(CheckHealthError::GraphNodeConfigNotFound); |
| 86 | + }; |
| 87 | + |
| 88 | + let body = Query::new_with_variables( |
| 89 | + r#" |
| 90 | + query indexingStatuses($ids: [String!]!) { |
| 91 | + indexingStatuses(subgraphs: $ids) { |
| 92 | + health |
| 93 | + } |
| 94 | + } |
| 95 | + "#, |
| 96 | + [("ids", json!([deployment_id]))], |
| 97 | + ); |
| 98 | + |
| 99 | + let client = reqwest::Client::new(); |
| 100 | + let response = client.post(url).json(&body).send().await; |
| 101 | + let response = response.expect("Failed to get response"); |
| 102 | + let response_json: Result<Response, reqwest::Error> = response.json().await; |
| 103 | + |
| 104 | + match response_json { |
| 105 | + Ok(res) => { |
| 106 | + if res.data.indexingStatuses.len() == 0 { |
| 107 | + return Err(CheckHealthError::DeploymentNotFound); |
| 108 | + }; |
| 109 | + let health_status = res.data.indexingStatuses[0].health.as_str(); |
| 110 | + Ok(Json(json!({ "health": health_status }))) |
| 111 | + } |
| 112 | + Err(_) => return Err(CheckHealthError::QueryForwardingError), |
| 113 | + } |
| 114 | +} |
0 commit comments