-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adds a new version to the /vaults endpoint to list cache duration #31
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
mod publications; | ||
|
||
pub use publications::{ | ||
create_job, delete_expired_job, find_job_cache_path_by_cid, get_cache_config, | ||
is_namespace_owner, namespace_create, namespace_exists, pub_cids, | ||
create_job, delete_expired_job, find_cache_config_by_vaults, find_job_cache_path_by_cid, | ||
get_cache_config, is_namespace_owner, namespace_create, namespace_exists, pub_cids, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,17 +70,40 @@ pub async fn is_namespace_owner(pool: &PgPool, ns: &str, owner: Address) -> Resu | |
Ok(!res.is_empty()) | ||
} | ||
|
||
/// Returns cache config | ||
pub async fn get_cache_config(pool: &PgPool, ns: &str, rel: &str) -> Result<Option<i64>> { | ||
/// Returns cache config from a vault | ||
pub async fn get_cache_config(pool: &PgPool, vault: &Vault) -> Result<Option<i64>> { | ||
let (duration, ) : (Option<i64>, ) = sqlx::query_as("SELECT duration FROM cache_config JOIN namespaces ON ns_id = namespaces.id WHERE name = $1 AND relation = $2") | ||
.bind(ns) | ||
.bind(rel) | ||
.bind(vault.namespace()) | ||
.bind(vault.relation()) | ||
.fetch_one(pool) | ||
.await | ||
.unwrap_or((None, )); | ||
Ok(duration) | ||
} | ||
|
||
pub async fn find_cache_config_by_vaults( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new db method that fetches all configs at once for a list of vaults. the query is a bit complicated because of how the schema of the database is structured |
||
pool: &PgPool, | ||
vaults: &Vec<Vault>, | ||
) -> Result<Vec<(String, Option<i64>)>> { | ||
let where_clause = (1..2 * vaults.len() + 1) | ||
.step_by(2) | ||
.map(|i| format!("(name = ${} AND relation = ${})", i, i + 1)) | ||
.collect::<Vec<String>>() | ||
.join(" OR "); | ||
|
||
let sql = format!("SELECT name || '.' || relation as vault, duration FROM namespaces JOIN cache_config ON ns_id = namespaces.id WHERE {}", where_clause); | ||
let mut query = sqlx::query(sql.as_str()); | ||
for vault in vaults { | ||
query = query.bind(vault.namespace()).bind(vault.relation()); | ||
} | ||
|
||
query | ||
.map(|row: PgRow| (row.get("vault"), row.get("duration"))) | ||
.fetch_all(pool) | ||
.await | ||
.map_err(basin_common::errors::Error::from) | ||
} | ||
|
||
// Unsets cache_path and expires_at | ||
pub async fn delete_expired_job(pool: &PgPool) -> Result<()> { | ||
sqlx::query!("UPDATE jobs SET expires_at = null, cache_path = null WHERE now() at time zone 'utc' >= expires_at;") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ use crate::domain::Vault; | |
use crate::gcs::GcsClient; | ||
use crate::web3storage::Web3Storage; | ||
use hex::ToHex; | ||
use std::collections::HashMap; | ||
use std::str::FromStr; | ||
|
||
use basin_evm::EVMClient; | ||
|
@@ -150,6 +151,82 @@ pub async fn find_vaults_by_account<E: EVMClient + 'static + std::marker::Sync>( | |
Ok(with_status(json(&vaults), StatusCode::OK)) | ||
} | ||
|
||
pub async fn find_vaults_by_account_v2<E: EVMClient + 'static + std::marker::Sync>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. handler for v2 |
||
evm_client: E, | ||
pool: PgPool, | ||
params: FindVaultsByAccountParams, | ||
) -> Result<impl warp::Reply, Infallible> { | ||
let account = match Address::from_str(params.account.as_str()) { | ||
Ok(v) => v, | ||
Err(_) => { | ||
return Ok(with_status( | ||
json(&ErrorResponse { | ||
error: "account is invalid".to_string(), | ||
}), | ||
StatusCode::BAD_REQUEST, | ||
)); | ||
} | ||
}; | ||
|
||
let vaults = match evm_client.list_pub(account).await { | ||
Ok(vaults) => vaults | ||
.into_iter() | ||
.map(|s| Vault::from(s).unwrap()) | ||
.collect::<Vec<Vault>>(), | ||
Err(err) => { | ||
return Ok(with_status( | ||
json(&ErrorResponse { | ||
error: err.to_string(), | ||
}), | ||
StatusCode::BAD_REQUEST, | ||
)); | ||
} | ||
}; | ||
|
||
#[derive(Serialize)] | ||
struct ResponseItem { | ||
vault: String, | ||
cache_duration: Option<i64>, | ||
} | ||
|
||
let mut response: Vec<ResponseItem> = Vec::new(); | ||
let rows = match db::find_cache_config_by_vaults(&pool, &vaults).await { | ||
Ok(v) => v, | ||
Err(err) => { | ||
log::error!("{}", err); | ||
return Ok(with_status( | ||
json(&ErrorResponse { | ||
error: "error fetching the cache config".to_string(), | ||
}), | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
)); | ||
} | ||
}; | ||
|
||
let mut vault_duration_map: HashMap<String, Option<i64>> = HashMap::new(); | ||
for row in rows { | ||
vault_duration_map.insert(row.0, row.1); | ||
} | ||
|
||
for vault in vaults { | ||
let vault_name = vault.to_string(); | ||
let item = match vault_duration_map.get(&vault_name) { | ||
Some(&duration) => ResponseItem { | ||
vault: vault_name, | ||
cache_duration: duration, | ||
}, | ||
None => ResponseItem { | ||
vault: vault_name, | ||
cache_duration: None, | ||
}, | ||
}; | ||
|
||
response.push(item); | ||
} | ||
|
||
Ok(with_status(json(&response), StatusCode::OK)) | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct FindVaultsByAccountParams { | ||
account: String, | ||
|
@@ -408,19 +485,18 @@ pub async fn write_event<W: Web3Storage>( | |
} | ||
} | ||
|
||
let cache_duration = | ||
match db::get_cache_config(&pool, &vault.namespace(), &vault.relation()).await { | ||
Ok(v) => v, | ||
Err(err) => { | ||
log::error!("{}", err); | ||
return Ok(with_status( | ||
json(&ErrorResponse { | ||
error: "error fetching the cache config".to_string(), | ||
}), | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
)); | ||
} | ||
}; | ||
let cache_duration = match db::get_cache_config(&pool, &vault).await { | ||
Ok(v) => v, | ||
Err(err) => { | ||
log::error!("{}", err); | ||
return Ok(with_status( | ||
json(&ErrorResponse { | ||
error: "error fetching the cache config".to_string(), | ||
}), | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
)); | ||
} | ||
}; | ||
|
||
let filename = format!( | ||
"{}/{}/{}-{}", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ mod api { | |
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone { | ||
health() | ||
.or(vaults_list(evm_client.clone())) | ||
.or(vaults_list_v2(evm_client.clone(), db.clone())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hooks new route |
||
.or(vaults_create(evm_client, db.clone())) | ||
.or(vaults_events_create( | ||
db.clone(), | ||
|
@@ -67,6 +68,19 @@ mod api { | |
.and_then(routes::find_vaults_by_account) | ||
} | ||
|
||
// GET /v2/vaults | ||
pub fn vaults_list_v2<E: EVMClient + 'static + std::marker::Sync>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new http route |
||
evm_client: E, | ||
db: PgPool, | ||
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone { | ||
warp::path!("v2" / "vaults") | ||
.and(warp::get()) | ||
.and(with_evm_client(evm_client)) | ||
.and(with_db(db)) | ||
.and(warp::query::<FindVaultsByAccountParams>()) | ||
.and_then(routes::find_vaults_by_account_v2) | ||
} | ||
|
||
// POST /vaults/:id | ||
pub fn vaults_create<E: EVMClient + 'static + std::marker::Sync>( | ||
evm_client: E, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,6 +145,18 @@ impl TestApp { | |
.expect("Failed to execute request.") | ||
} | ||
|
||
pub async fn get_vaults_v2(&self) -> Response { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test helper for v2 |
||
self.api_client | ||
.get(&format!( | ||
"{}/v2/vaults?account={:#x}", | ||
&self.address, | ||
self.account.address() | ||
)) | ||
.send() | ||
.await | ||
.expect("Failed to execute request.") | ||
} | ||
|
||
pub async fn get_events_from_vaults(&self, vault: &str) -> Response { | ||
self.api_client | ||
.get(&format!("{}/vaults/{}/events", &self.address, vault)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,37 @@ async fn list_vaults() { | |
assert_eq!(json!(["api.test", "api.test2"]), response); | ||
} | ||
|
||
#[tokio::test] | ||
async fn list_vaults_v2() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new test for v2 |
||
let app = spawn_app().await; | ||
|
||
// setup | ||
app.create_vault("api.test").await; | ||
app.create_vault_with_cache("api.test2", 100).await; | ||
|
||
// make request | ||
let response = app | ||
.get_vaults_v2() | ||
.await | ||
.text() | ||
.await | ||
.unwrap() | ||
.parse::<serde_json::Value>() | ||
.unwrap(); | ||
assert_eq!( | ||
json!([ | ||
{ | ||
"vault": "api.test", | ||
"cache_duration": null, | ||
}, | ||
{ | ||
"vault": "api.test2", | ||
"cache_duration": 100, | ||
}]), | ||
response | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn list_events() { | ||
let app = spawn_app().await; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is just a refactor