Separate clock handler

Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2024-01-20 18:22:36 +00:00
parent 2d14fe35c8
commit 178a062c25
15 changed files with 262 additions and 221 deletions

View File

@@ -1,5 +1,6 @@
use crate::types::{Asset, Class};
use clickhouse::Client;
use serde::Serialize;
pub async fn select(clickhouse_client: &Client) -> Vec<Asset> {
clickhouse_client
@@ -18,7 +19,10 @@ pub async fn select_where_class(clickhouse_client: &Client, class: &Class) -> Ve
.unwrap()
}
pub async fn select_where_symbol(clickhouse_client: &Client, symbol: &str) -> Option<Asset> {
pub async fn select_where_symbol<T>(clickhouse_client: &Client, symbol: &T) -> Option<Asset>
where
T: AsRef<str> + Serialize + Send + Sync,
{
clickhouse_client
.query("SELECT ?fields FROM assets FINAL WHERE symbol = ?")
.bind(symbol)
@@ -27,15 +31,22 @@ pub async fn select_where_symbol(clickhouse_client: &Client, symbol: &str) -> Op
.unwrap()
}
pub async fn upsert_batch(clickhouse_client: &Client, assets: &Vec<Asset>) {
pub async fn upsert_batch<T>(clickhouse_client: &Client, assets: T)
where
T: IntoIterator<Item = Asset> + Send + Sync,
T::IntoIter: Send,
{
let mut insert = clickhouse_client.insert("assets").unwrap();
for asset in assets {
insert.write(asset).await.unwrap();
insert.write(&asset).await.unwrap();
}
insert.end().await.unwrap();
}
pub async fn delete_where_symbols(clickhouse_client: &Client, symbols: &Vec<String>) {
pub async fn delete_where_symbols<T>(clickhouse_client: &Client, symbols: &[T])
where
T: AsRef<str> + Serialize + Send + Sync,
{
clickhouse_client
.query("DELETE FROM assets WHERE symbol IN ?")
.bind(symbols)

View File

@@ -1,10 +1,14 @@
use crate::types::Backfill;
use clickhouse::Client;
use serde::Serialize;
pub async fn select_latest_where_symbol(
pub async fn select_latest_where_symbol<T>(
clickhouse_client: &Client,
symbol: &str,
) -> Option<Backfill> {
symbol: &T,
) -> Option<Backfill>
where
T: AsRef<str> + Serialize + Send + Sync,
{
clickhouse_client
.query("SELECT ?fields FROM backfills FINAL WHERE symbol = ? ORDER BY time DESC LIMIT 1")
.bind(symbol)
@@ -19,7 +23,10 @@ pub async fn upsert(clickhouse_client: &Client, backfill: &Backfill) {
insert.end().await.unwrap();
}
pub async fn delete_where_symbols(clickhouse_client: &Client, symbols: &Vec<String>) {
pub async fn delete_where_symbols<T>(clickhouse_client: &Client, symbols: &[T])
where
T: AsRef<str> + Serialize + Send + Sync,
{
clickhouse_client
.query("DELETE FROM backfills WHERE symbol IN ?")
.bind(symbols)
@@ -28,7 +35,10 @@ pub async fn delete_where_symbols(clickhouse_client: &Client, symbols: &Vec<Stri
.unwrap();
}
pub async fn delete_where_not_symbols(clickhouse_client: &Client, symbols: &Vec<String>) {
pub async fn delete_where_not_symbols<T>(clickhouse_client: &Client, symbols: &[T])
where
T: AsRef<str> + Serialize + Send + Sync,
{
clickhouse_client
.query("DELETE FROM backfills WHERE symbol NOT IN ?")
.bind(symbols)

View File

@@ -1,5 +1,6 @@
use crate::types::Bar;
use clickhouse::Client;
use serde::Serialize;
pub async fn upsert(clickhouse_client: &Client, bar: &Bar) {
let mut insert = clickhouse_client.insert("bars").unwrap();
@@ -7,15 +8,22 @@ pub async fn upsert(clickhouse_client: &Client, bar: &Bar) {
insert.end().await.unwrap();
}
pub async fn upsert_batch(clickhouse_client: &Client, bars: &[Bar]) {
pub async fn upsert_batch<T>(clickhouse_client: &Client, bars: T)
where
T: IntoIterator<Item = Bar> + Send + Sync,
T::IntoIter: Send,
{
let mut insert = clickhouse_client.insert("bars").unwrap();
for bar in bars {
insert.write(bar).await.unwrap();
insert.write(&bar).await.unwrap();
}
insert.end().await.unwrap();
}
pub async fn delete_where_symbols(clickhouse_client: &Client, symbols: &Vec<String>) {
pub async fn delete_where_symbols<T>(clickhouse_client: &Client, symbols: &[T])
where
T: AsRef<str> + Serialize + Send + Sync,
{
clickhouse_client
.query("DELETE FROM bars WHERE symbol IN ?")
.bind(symbols)
@@ -24,7 +32,10 @@ pub async fn delete_where_symbols(clickhouse_client: &Client, symbols: &Vec<Stri
.unwrap();
}
pub async fn delete_where_not_symbols(clickhouse_client: &Client, symbols: &Vec<String>) {
pub async fn delete_where_not_symbols<T>(clickhouse_client: &Client, symbols: &[T])
where
T: AsRef<str> + Serialize + Send + Sync,
{
clickhouse_client
.query("DELETE FROM bars WHERE symbol NOT IN ?")
.bind(symbols)