Skip to content

Commit

Permalink
graphman: Add ability to list removed unused deployment by id
Browse files Browse the repository at this point in the history
  • Loading branch information
incrypto32 committed Feb 8, 2024
1 parent 407ca01 commit 3e88969
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
11 changes: 9 additions & 2 deletions node/src/bin/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,12 @@ pub enum UnusedCommand {
/// List unused deployments
List {
/// Only list unused deployments that still exist
#[clap(short, long)]
#[clap(short, long, conflicts_with = "deployment")]
existing: bool,

/// Deployment
#[clap(short, long)]
deployment: Option<DeploymentSearch>,
},
/// Update and record currently unused deployments
Record,
Expand Down Expand Up @@ -1116,7 +1120,10 @@ async fn main() -> anyhow::Result<()> {
use UnusedCommand::*;

match cmd {
List { existing } => commands::unused_deployments::list(store, existing),
List {
existing,
deployment,
} => commands::unused_deployments::list(store, existing, deployment),
Record => commands::unused_deployments::record(store),
Remove {
count,
Expand Down
18 changes: 12 additions & 6 deletions node/src/manager/commands/unused_deployments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{sync::Arc, time::Instant};
use graph::prelude::{anyhow::Error, chrono};
use graph_store_postgres::{unused, SubgraphStore, UnusedDeployment};

use crate::manager::display::List;
use crate::manager::{deployment::DeploymentSearch, display::List};

fn make_list() -> List {
List::new(vec!["id", "shard", "namespace", "subgraphs", "entities"])
Expand All @@ -29,13 +29,19 @@ fn add_row(list: &mut List, deployment: UnusedDeployment) {
])
}

pub fn list(store: Arc<SubgraphStore>, existing: bool) -> Result<(), Error> {
pub fn list(
store: Arc<SubgraphStore>,
existing: bool,
deployment: Option<DeploymentSearch>,
) -> Result<(), Error> {
let mut list = make_list();

let filter = if existing {
unused::Filter::New
} else {
unused::Filter::All
let filter = match deployment {
Some(deployment) => deployment.to_unused_filter(existing),
None => match existing {
true => unused::Filter::New,
false => unused::Filter::All,
},
};

for deployment in store.list_unused_deployments(filter)? {
Expand Down
16 changes: 16 additions & 0 deletions node/src/manager/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use graph::{
};
use graph_store_postgres::command_support::catalog as store_catalog;
use graph_store_postgres::connection_pool::ConnectionPool;
use graph_store_postgres::unused;

use crate::manager::display::List;

Expand Down Expand Up @@ -70,6 +71,21 @@ impl FromStr for DeploymentSearch {
}

impl DeploymentSearch {
pub fn to_unused_filter(self, existing: bool) -> unused::Filter {
match self {
DeploymentSearch::Name { name } => unused::Filter::Name(name),
DeploymentSearch::Hash { hash, shard: _ } => unused::Filter::Hash(hash),
DeploymentSearch::All => {
if existing {
unused::Filter::New
} else {
unused::Filter::All
}
}
DeploymentSearch::Deployment { namespace } => unused::Filter::Deployment(namespace),
}
}

pub fn lookup(&self, primary: &ConnectionPool) -> Result<Vec<Deployment>, anyhow::Error> {
let conn = primary.get()?;
self.lookup_with_conn(&conn)
Expand Down
16 changes: 16 additions & 0 deletions store/postgres/src/primary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,22 @@ impl<'a> Connection<'a> {
.order_by(u::entity_count)
.load(self.conn.as_ref())?)
}

Name(name) => Ok(u::table
.filter(u::subgraphs.is_not_null())
.filter(sql("ARRAY[").bind::<Text, _>(name).sql("] <@ subgraphs"))
.order_by(u::entity_count)
.load(self.conn.as_ref())?),

Hash(hash) => Ok(u::table
.filter(u::deployment.eq(hash))
.order_by(u::entity_count)
.load(self.conn.as_ref())?),

Deployment(id) => Ok(u::table
.filter(u::namespace.eq(id))
.order_by(u::entity_count)
.load(self.conn.as_ref())?),
}
}

Expand Down
7 changes: 7 additions & 0 deletions store/postgres/src/subgraph_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub trait DeploymentPlacer {
pub mod unused {
use graph::prelude::chrono::Duration;

#[derive(Debug)]
pub enum Filter {
/// List all unused deployments
All,
Expand All @@ -135,6 +136,12 @@ pub mod unused {
/// List only deployments that were recorded as unused at least this
/// long ago but have not been removed at
UnusedLongerThan(Duration),
/// Lists deployments with a specific name
Name(String),
/// Lists deployments with a specific hash
Hash(String),
/// Lists deployments with a specific deployment id
Deployment(String),
}
}

Expand Down

0 comments on commit 3e88969

Please sign in to comment.