Refactor threads to use trait implementations
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
@@ -1,11 +1,26 @@
|
||||
use crate::{threads::data::ThreadType, types::Backfill};
|
||||
use crate::types::Backfill;
|
||||
use clickhouse::Client;
|
||||
use serde::Serialize;
|
||||
use std::fmt::Display;
|
||||
use tokio::join;
|
||||
|
||||
pub enum Table {
|
||||
Bars,
|
||||
News,
|
||||
}
|
||||
|
||||
impl Display for Table {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Bars => write!(f, "backfills_bars"),
|
||||
Self::News => write!(f, "backfills_news"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn select_latest_where_symbol<T>(
|
||||
clickhouse_client: &Client,
|
||||
thread_type: &ThreadType,
|
||||
table: &Table,
|
||||
symbol: &T,
|
||||
) -> Option<Backfill>
|
||||
where
|
||||
@@ -13,11 +28,7 @@ where
|
||||
{
|
||||
clickhouse_client
|
||||
.query(&format!(
|
||||
"SELECT ?fields FROM {} FINAL WHERE symbol = ? ORDER BY time DESC LIMIT 1",
|
||||
match thread_type {
|
||||
ThreadType::Bars(_) => "backfills_bars",
|
||||
ThreadType::News => "backfills_news",
|
||||
}
|
||||
"SELECT ?fields FROM {table} FINAL WHERE symbol = ? ORDER BY time DESC LIMIT 1",
|
||||
))
|
||||
.bind(symbol)
|
||||
.fetch_optional::<Backfill>()
|
||||
@@ -25,32 +36,18 @@ where
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub async fn upsert(clickhouse_client: &Client, thread_type: &ThreadType, backfill: &Backfill) {
|
||||
let mut insert = clickhouse_client
|
||||
.insert(match thread_type {
|
||||
ThreadType::Bars(_) => "backfills_bars",
|
||||
ThreadType::News => "backfills_news",
|
||||
})
|
||||
.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 delete_where_symbols<T>(
|
||||
clickhouse_client: &Client,
|
||||
thread_type: &ThreadType,
|
||||
symbols: &[T],
|
||||
) where
|
||||
pub async fn delete_where_symbols<T>(clickhouse_client: &Client, table: &Table, symbols: &[T])
|
||||
where
|
||||
T: AsRef<str> + Serialize + Send + Sync,
|
||||
{
|
||||
clickhouse_client
|
||||
.query(&format!(
|
||||
"DELETE FROM {} WHERE symbol IN ?",
|
||||
match thread_type {
|
||||
ThreadType::Bars(_) => "backfills_bars",
|
||||
ThreadType::News => "backfills_news",
|
||||
}
|
||||
))
|
||||
.query(&format!("DELETE FROM {table} WHERE symbol IN ?"))
|
||||
.bind(symbols)
|
||||
.execute()
|
||||
.await
|
||||
|
Reference in New Issue
Block a user