Skip to content

Commit

Permalink
cleanup get slot info db functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Apr 9, 2024
1 parent 747b182 commit d63158d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 59 deletions.
74 changes: 20 additions & 54 deletions catalyst-gateway/bin/src/event_db/follower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ const BLOCK_TIME_COLUMN: &str = "block_time";
/// `ended` column name
const ENDED_COLUMN: &str = "ended";

/// Query type
pub(crate) enum SlotInfoQueryType {
Previous,
Current,
Next,
}

impl EventDB {
/// Index follower block stream
pub(crate) async fn index_follower_data(
Expand All @@ -48,70 +55,29 @@ impl EventDB {
Ok(())
}

/// Get current slot info for the provided date-time and network
pub(crate) async fn current_slot_info(
&self, date_time: DateTime, network: Network,
/// Get slot info for the provided date-time and network and query type
pub(crate) async fn get_slot_info(
&self, date_time: DateTime, network: Network, query_type: SlotInfoQueryType,
) -> Result<(SlotNumber, BlockHash, DateTime), Error> {
let conn = self.pool.get().await?;

let rows = conn
.query(
include_str!(
"../../../event-db/queries/follower/select_slot_index_by_datetime/current_slot_info.sql"
),
&[&network.to_string(), &date_time],
)
.await?;

let Some(row) = rows.first() else {
return Err(Error::NotFound);
};

let slot_number: SlotNumber = row.try_get(SLOT_NO_COLUMN)?;
let block_hash = hex::encode(row.try_get::<_, Vec<u8>>(BLOCK_HASH_COLUMN)?);
let block_time = row.try_get(BLOCK_TIME_COLUMN)?;
Ok((slot_number, block_hash, block_time))
}

/// Get previous slot info for the provided date-time and network
pub(crate) async fn previous_slot_info(
&self, date_time: DateTime, network: Network,
) -> Result<(SlotNumber, BlockHash, DateTime), Error> {
let conn = self.pool.get().await?;

let rows = conn
.query(
include_str!(
match query_type {
SlotInfoQueryType::Previous => include_str!(
"../../../event-db/queries/follower/select_slot_index_by_datetime/previous_slot_info.sql"
),
&[&network.to_string(), &date_time],
)
.await?;

let Some(row) = rows.first() else {
return Err(Error::NotFound);
};

let slot_number: SlotNumber = row.try_get(SLOT_NO_COLUMN)?;
let block_hash = hex::encode(row.try_get::<_, Vec<u8>>(BLOCK_HASH_COLUMN)?);
let block_time = row.try_get(BLOCK_TIME_COLUMN)?;
Ok((slot_number, block_hash, block_time))
}

/// Get next slot info for the provided date-time and network
pub(crate) async fn next_slot_info(
&self, date_time: DateTime, network: Network,
) -> Result<(SlotNumber, BlockHash, DateTime), Error> {
let conn = self.pool.get().await?;

let rows = conn
.query(
include_str!(
"../../../event-db/queries/follower/select_slot_index_by_datetime/next_slot_info.sql"
),
SlotInfoQueryType::Current => include_str!(
"../../../event-db/queries/follower/select_slot_index_by_datetime/current_slot_info.sql"
),
SlotInfoQueryType::Next => include_str!(
"../../../event-db/queries/follower/select_slot_index_by_datetime/next_slot_info.sql"
),
},
&[&network.to_string(), &date_time],
)
.await?;

let Some(row) = rows.first() else {
return Err(Error::NotFound);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use poem_openapi::payload::Json;

use crate::{
cli::Error,
event_db::{error::Error as DBError, follower::DateTime},
event_db::{
error::Error as DBError,
follower::{DateTime, SlotInfoQueryType},
},
service::common::{
objects::cardano::{
network::Network,
Expand Down Expand Up @@ -53,10 +56,18 @@ pub(crate) async fn endpoint(
let date_time = date_time.unwrap_or_else(chrono::Utc::now);
let network = network.unwrap_or(Network::Mainnet);

let (current, previous, next) = tokio::join!(
event_db.current_slot_info(date_time, network.clone().into()),
event_db.previous_slot_info(date_time, network.clone().into(),),
event_db.next_slot_info(date_time, network.into())
let (previous, current, next) = tokio::join!(
event_db.get_slot_info(
date_time,
network.clone().into(),
SlotInfoQueryType::Previous
),
event_db.get_slot_info(
date_time,
network.clone().into(),
SlotInfoQueryType::Current
),
event_db.get_slot_info(date_time, network.into(), SlotInfoQueryType::Next)
);

let process_slot_info_result = |slot_info_result| {
Expand Down

0 comments on commit d63158d

Please sign in to comment.