Skip to content

Commit

Permalink
test(costs): get latest costs from api
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Dec 17, 2024
1 parent 16cf625 commit 3764648
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 4 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions committer/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,16 @@ async fn metrics(registry: web::Data<Arc<Registry>>) -> impl Responder {
std::result::Result::<_, InternalError<_>>::Ok(text)
}

#[derive(Deserialize)]
#[serde(tag = "variant", content = "value")]
enum HeightVariant {
Latest,
Height(u32),
}

#[derive(Deserialize)]
struct CostQueryParams {
from_height: u32,
from_height: HeightVariant,
limit: Option<usize>,
}

Expand All @@ -103,8 +110,13 @@ async fn costs(
) -> impl Responder {
let limit = query.limit.unwrap_or(100);

match data.get_costs(query.from_height, limit).await {
Ok(bundle_costs) => HttpResponse::Ok().json(bundle_costs),
let response = match query.from_height {
HeightVariant::Latest => data.get_latest_costs(limit).await,
HeightVariant::Height(from_height) => data.get_costs(from_height, limit).await,
};

match response {
Ok(costs) => HttpResponse::Ok().json(costs),
Err(services::Error::Other(e)) => {
HttpResponse::from_error(InternalError::new(e, StatusCode::BAD_REQUEST))
}
Expand Down
4 changes: 4 additions & 0 deletions packages/adapters/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ impl services::cost_reporter::port::Storage for Postgres {
.await
.map_err(Into::into)
}

async fn get_latest_costs(&self, limit: usize) -> Result<Vec<BundleCost>> {
self._get_latest_costs(limit).await.map_err(Into::into)
}
}

impl services::status_reporter::port::Storage for Postgres {
Expand Down
28 changes: 28 additions & 0 deletions packages/adapters/storage/src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,34 @@ impl Postgres {
.collect::<Result<Vec<_>>>()
}

pub(crate) async fn _get_latest_costs(&self, limit: usize) -> Result<Vec<BundleCost>> {
sqlx::query_as!(
tables::BundleCost,
r#"
SELECT
bc.bundle_id,
bc.cost,
bc.size,
bc.da_block_height,
bc.is_finalized,
b.start_height,
b.end_height
FROM
bundle_cost bc
JOIN bundles b ON bc.bundle_id = b.id
ORDER BY
b.start_height DESC
LIMIT $1
"#,
limit as i64
)
.fetch_all(&self.connection_pool)
.await?
.into_iter()
.map(BundleCost::try_from)
.collect::<Result<Vec<_>>>()
}

pub(crate) async fn _next_bundle_id(&self) -> Result<NonNegative<i32>> {
let next_id = sqlx::query!("SELECT nextval(pg_get_serial_sequence('bundles', 'id'))")
.fetch_one(&self.connection_pool)
Expand Down
6 changes: 5 additions & 1 deletion packages/adapters/storage/src/test_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
ops::RangeInclusive,
sync::{Arc, Weak},
};

use std::future::Future;
use delegate::delegate;
use services::{
block_bundler, block_committer, block_importer,
Expand Down Expand Up @@ -351,4 +351,8 @@ impl services::cost_reporter::port::Storage for DbWithProcess {
.await
.map_err(Into::into)
}

async fn get_latest_costs(&self, limit: usize) -> services::Result<Vec<BundleCost>> {
self.db._get_latest_costs(limit).await.map_err(Into::into)
}
}
13 changes: 13 additions & 0 deletions packages/services/src/cost_reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ pub mod service {
.get_finalized_costs(from_block_height, limit)
.await
}

pub async fn get_latest_costs(&self, limit: usize) -> Result<Vec<BundleCost>> {
if limit > self.request_limit {
return Err(Error::Other(format!(
"requested: {} items, but limit is: {}",
limit, self.request_limit
)));
}

self.storage.get_latest_costs(limit).await
}
}
}

Expand All @@ -50,5 +61,7 @@ pub mod port {
from_block_height: u32,
limit: usize,
) -> Result<Vec<BundleCost>>;

async fn get_latest_costs(&self, limit: usize) -> Result<Vec<BundleCost>>;
}
}

0 comments on commit 3764648

Please sign in to comment.