Clean up error propagation

Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2024-02-08 18:13:52 +00:00
parent 52e88f4bc9
commit 76bf2fddcb
24 changed files with 465 additions and 325 deletions

View File

@@ -1,16 +1,18 @@
use crate::types::Asset;
use clickhouse::Client;
use clickhouse::{error::Error, Client};
use serde::Serialize;
pub async fn select(clickhouse_client: &Client) -> Vec<Asset> {
pub async fn select(clickhouse_client: &Client) -> Result<Vec<Asset>, Error> {
clickhouse_client
.query("SELECT ?fields FROM assets FINAL")
.fetch_all::<Asset>()
.await
.unwrap()
}
pub async fn select_where_symbol<T>(clickhouse_client: &Client, symbol: &T) -> Option<Asset>
pub async fn select_where_symbol<T>(
clickhouse_client: &Client,
symbol: &T,
) -> Result<Option<Asset>, Error>
where
T: AsRef<str> + Serialize + Send + Sync,
{
@@ -19,22 +21,21 @@ where
.bind(symbol)
.fetch_optional::<Asset>()
.await
.unwrap()
}
pub async fn upsert_batch<T>(clickhouse_client: &Client, assets: T)
pub async fn upsert_batch<T>(clickhouse_client: &Client, assets: T) -> Result<(), Error>
where
T: IntoIterator<Item = Asset> + Send + Sync,
T::IntoIter: Send,
{
let mut insert = clickhouse_client.insert("assets").unwrap();
let mut insert = clickhouse_client.insert("assets")?;
for asset in assets {
insert.write(&asset).await.unwrap();
insert.write(&asset).await?;
}
insert.end().await.unwrap();
insert.end().await
}
pub async fn delete_where_symbols<T>(clickhouse_client: &Client, symbols: &[T])
pub async fn delete_where_symbols<T>(clickhouse_client: &Client, symbols: &[T]) -> Result<(), Error>
where
T: AsRef<str> + Serialize + Send + Sync,
{
@@ -43,5 +44,4 @@ where
.bind(symbols)
.execute()
.await
.unwrap();
}

View File

@@ -1,8 +1,8 @@
use crate::types::Backfill;
use clickhouse::Client;
use clickhouse::{error::Error, Client};
use serde::Serialize;
use std::fmt::Display;
use tokio::join;
use std::fmt::{Display, Formatter};
use tokio::try_join;
pub enum Table {
Bars,
@@ -10,7 +10,7 @@ pub enum Table {
}
impl Display for Table {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Bars => write!(f, "backfills_bars"),
Self::News => write!(f, "backfills_news"),
@@ -22,7 +22,7 @@ pub async fn select_latest_where_symbol<T>(
clickhouse_client: &Client,
table: &Table,
symbol: &T,
) -> Option<Backfill>
) -> Result<Option<Backfill>, Error>
where
T: AsRef<str> + Serialize + Send + Sync,
{
@@ -33,16 +33,23 @@ where
.bind(symbol)
.fetch_optional::<Backfill>()
.await
.unwrap()
}
pub async fn upsert(clickhouse_client: &Client, table: &Table, backfill: &Backfill) {
let mut insert = clickhouse_client.insert(&table.to_string()).unwrap();
insert.write(backfill).await.unwrap();
insert.end().await.unwrap();
pub async fn upsert(
clickhouse_client: &Client,
table: &Table,
backfill: &Backfill,
) -> Result<(), Error> {
let mut insert = clickhouse_client.insert(&table.to_string())?;
insert.write(backfill).await?;
insert.end().await
}
pub async fn delete_where_symbols<T>(clickhouse_client: &Client, table: &Table, symbols: &[T])
pub async fn delete_where_symbols<T>(
clickhouse_client: &Client,
table: &Table,
symbols: &[T],
) -> Result<(), Error>
where
T: AsRef<str> + Serialize + Send + Sync,
{
@@ -51,16 +58,14 @@ where
.bind(symbols)
.execute()
.await
.unwrap();
}
pub async fn cleanup(clickhouse_client: &Client) {
pub async fn cleanup(clickhouse_client: &Client) -> Result<(), Error> {
let delete_bars_future = async {
clickhouse_client
.query("DELETE FROM backfills_bars WHERE symbol NOT IN (SELECT symbol FROM assets)")
.execute()
.await
.unwrap();
};
let delete_news_future = async {
@@ -68,8 +73,7 @@ pub async fn cleanup(clickhouse_client: &Client) {
.query("DELETE FROM backfills_news WHERE symbol NOT IN (SELECT symbol FROM assets)")
.execute()
.await
.unwrap();
};
join!(delete_bars_future, delete_news_future);
try_join!(delete_bars_future, delete_news_future).map(|_| ())
}

View File

@@ -1,26 +1,26 @@
use crate::types::Bar;
use clickhouse::Client;
use clickhouse::{error::Error, Client};
use serde::Serialize;
pub async fn upsert(clickhouse_client: &Client, bar: &Bar) {
let mut insert = clickhouse_client.insert("bars").unwrap();
insert.write(bar).await.unwrap();
insert.end().await.unwrap();
pub async fn upsert(clickhouse_client: &Client, bar: &Bar) -> Result<(), Error> {
let mut insert = clickhouse_client.insert("bars")?;
insert.write(bar).await?;
insert.end().await
}
pub async fn upsert_batch<T>(clickhouse_client: &Client, bars: T)
pub async fn upsert_batch<T>(clickhouse_client: &Client, bars: T) -> Result<(), Error>
where
T: IntoIterator<Item = Bar> + Send + Sync,
T::IntoIter: Send,
{
let mut insert = clickhouse_client.insert("bars").unwrap();
let mut insert = clickhouse_client.insert("bars")?;
for bar in bars {
insert.write(&bar).await.unwrap();
insert.write(&bar).await?;
}
insert.end().await.unwrap();
insert.end().await
}
pub async fn delete_where_symbols<T>(clickhouse_client: &Client, symbols: &[T])
pub async fn delete_where_symbols<T>(clickhouse_client: &Client, symbols: &[T]) -> Result<(), Error>
where
T: AsRef<str> + Serialize + Send + Sync,
{
@@ -29,13 +29,11 @@ where
.bind(symbols)
.execute()
.await
.unwrap();
}
pub async fn cleanup(clickhouse_client: &Client) {
pub async fn cleanup(clickhouse_client: &Client) -> Result<(), Error> {
clickhouse_client
.query("DELETE FROM bars WHERE symbol NOT IN (SELECT symbol FROM assets)")
.execute()
.await
.unwrap();
}

View File

@@ -1,26 +1,26 @@
use crate::types::News;
use clickhouse::Client;
use clickhouse::{error::Error, Client};
use serde::Serialize;
pub async fn upsert(clickhouse_client: &Client, news: &News) {
let mut insert = clickhouse_client.insert("news").unwrap();
insert.write(news).await.unwrap();
insert.end().await.unwrap();
pub async fn upsert(clickhouse_client: &Client, news: &News) -> Result<(), Error> {
let mut insert = clickhouse_client.insert("news")?;
insert.write(news).await?;
insert.end().await
}
pub async fn upsert_batch<T>(clickhouse_client: &Client, news: T)
pub async fn upsert_batch<T>(clickhouse_client: &Client, news: T) -> Result<(), Error>
where
T: IntoIterator<Item = News> + Send + Sync,
T::IntoIter: Send,
{
let mut insert = clickhouse_client.insert("news").unwrap();
let mut insert = clickhouse_client.insert("news")?;
for news in news {
insert.write(&news).await.unwrap();
insert.write(&news).await?;
}
insert.end().await.unwrap();
insert.end().await
}
pub async fn delete_where_symbols<T>(clickhouse_client: &Client, symbols: &[T])
pub async fn delete_where_symbols<T>(clickhouse_client: &Client, symbols: &[T]) -> Result<(), Error>
where
T: AsRef<str> + Serialize + Send + Sync,
{
@@ -29,15 +29,13 @@ where
.bind(symbols)
.execute()
.await
.unwrap();
}
pub async fn cleanup(clickhouse_client: &Client) {
pub async fn cleanup(clickhouse_client: &Client) -> Result<(), Error> {
clickhouse_client
.query(
"DELETE FROM news WHERE NOT hasAny(symbols, (SELECT groupArray(symbol) FROM assets))",
)
.execute()
.await
.unwrap();
}