Add node status http api
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
use err::Error;
|
||||
use netpod::log::*;
|
||||
use netpod::{Channel, NodeConfigCached};
|
||||
use tokio_postgres::NoTls;
|
||||
use tokio_postgres::{Client, NoTls};
|
||||
|
||||
pub async fn channel_exists(channel: &Channel, node_config: &NodeConfigCached) -> Result<bool, Error> {
|
||||
pub async fn create_connection(node_config: &NodeConfigCached) -> Result<Client, Error> {
|
||||
let d = &node_config.node_config.cluster.database;
|
||||
let uri = format!("postgresql://{}:{}@{}:{}/{}", d.user, d.pass, d.host, 5432, d.name);
|
||||
let (cl, conn) = tokio_postgres::connect(&uri, NoTls).await?;
|
||||
@@ -14,6 +14,11 @@ pub async fn channel_exists(channel: &Channel, node_config: &NodeConfigCached) -
|
||||
}
|
||||
Ok::<_, Error>(())
|
||||
});
|
||||
Ok(cl)
|
||||
}
|
||||
|
||||
pub async fn channel_exists(channel: &Channel, node_config: &NodeConfigCached) -> Result<bool, Error> {
|
||||
let cl = create_connection(node_config).await?;
|
||||
let rows = cl
|
||||
.query("select rowid from channels where name = $1::text", &[&channel.name])
|
||||
.await?;
|
||||
@@ -28,3 +33,19 @@ pub async fn channel_exists(channel: &Channel, node_config: &NodeConfigCached) -
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub async fn database_size(node_config: &NodeConfigCached) -> Result<u64, Error> {
|
||||
let cl = create_connection(node_config).await?;
|
||||
let rows = cl
|
||||
.query(
|
||||
"select pg_database_size($1::text)",
|
||||
&[&node_config.node_config.cluster.database.name],
|
||||
)
|
||||
.await?;
|
||||
if rows.len() == 0 {
|
||||
Err(Error::with_msg("could not get database size"))?;
|
||||
}
|
||||
let size: i64 = rows[0].get(0);
|
||||
let size = size as u64;
|
||||
Ok(size)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user