|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::sync::Arc; |
| 3 | + |
| 4 | +use anyhow::anyhow; |
| 5 | +use graph::blockchain::BlockPtr; |
| 6 | +use graph::components::store::BlockNumber; |
| 7 | +use graph::components::store::DeploymentId; |
| 8 | +use graph::components::store::StatusStore; |
| 9 | +use graph::data::subgraph::schema::SubgraphHealth; |
| 10 | +use graph_store_postgres::connection_pool::ConnectionPool; |
| 11 | +use graph_store_postgres::Store; |
| 12 | +use itertools::Itertools; |
| 13 | + |
| 14 | +use crate::deployment::Deployment; |
| 15 | +use crate::deployment::DeploymentSelector; |
| 16 | +use crate::deployment::DeploymentVersionSelector; |
| 17 | +use crate::GraphmanError; |
| 18 | + |
| 19 | +#[derive(Clone, Debug)] |
| 20 | +pub struct DeploymentStatus { |
| 21 | + pub is_paused: Option<bool>, |
| 22 | + pub is_synced: bool, |
| 23 | + pub health: SubgraphHealth, |
| 24 | + pub earliest_block_number: BlockNumber, |
| 25 | + pub latest_block: Option<BlockPtr>, |
| 26 | + pub chain_head_block: Option<BlockPtr>, |
| 27 | +} |
| 28 | + |
| 29 | +pub fn load_deployments( |
| 30 | + primary_pool: ConnectionPool, |
| 31 | + deployment: &DeploymentSelector, |
| 32 | + version: &DeploymentVersionSelector, |
| 33 | +) -> Result<Vec<Deployment>, GraphmanError> { |
| 34 | + let mut primary_conn = primary_pool.get()?; |
| 35 | + |
| 36 | + crate::deployment::load_deployments(&mut primary_conn, &deployment, &version) |
| 37 | +} |
| 38 | + |
| 39 | +pub fn load_deployment_statuses( |
| 40 | + store: Arc<Store>, |
| 41 | + deployments: &[Deployment], |
| 42 | +) -> Result<HashMap<i32, DeploymentStatus>, GraphmanError> { |
| 43 | + use graph::data::subgraph::status::Filter; |
| 44 | + |
| 45 | + let deployment_ids = deployments |
| 46 | + .iter() |
| 47 | + .map(|deployment| DeploymentId::new(deployment.id)) |
| 48 | + .collect_vec(); |
| 49 | + |
| 50 | + let deployment_statuses = store |
| 51 | + .status(Filter::DeploymentIds(deployment_ids))? |
| 52 | + .into_iter() |
| 53 | + .map(|status| { |
| 54 | + let id = status.id.0; |
| 55 | + |
| 56 | + let chain = status |
| 57 | + .chains |
| 58 | + .get(0) |
| 59 | + .ok_or_else(|| { |
| 60 | + GraphmanError::Store(anyhow!( |
| 61 | + "deployment status has no chains on deployment '{id}'" |
| 62 | + )) |
| 63 | + })? |
| 64 | + .to_owned(); |
| 65 | + |
| 66 | + Ok(( |
| 67 | + id, |
| 68 | + DeploymentStatus { |
| 69 | + is_paused: status.paused, |
| 70 | + is_synced: status.synced, |
| 71 | + health: status.health, |
| 72 | + earliest_block_number: chain.earliest_block_number.to_owned(), |
| 73 | + latest_block: chain.latest_block.map(|x| x.to_ptr()), |
| 74 | + chain_head_block: chain.chain_head_block.map(|x| x.to_ptr()), |
| 75 | + }, |
| 76 | + )) |
| 77 | + }) |
| 78 | + .collect::<Result<_, GraphmanError>>()?; |
| 79 | + |
| 80 | + Ok(deployment_statuses) |
| 81 | +} |
0 commit comments