- Refactor everything in the process, oops Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
51 lines
1.3 KiB
Rust
51 lines
1.3 KiB
Rust
use super::assets;
|
|
use crate::types::News;
|
|
use clickhouse::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_batch<T>(clickhouse_client: &Client, news: T)
|
|
where
|
|
T: IntoIterator<Item = News> + Send + Sync,
|
|
T::IntoIter: Send,
|
|
{
|
|
let mut insert = clickhouse_client.insert("news").unwrap();
|
|
for news in news {
|
|
insert.write(&news).await.unwrap();
|
|
}
|
|
insert.end().await.unwrap();
|
|
}
|
|
|
|
pub async fn delete_where_symbols<T>(clickhouse_client: &Client, symbols: &[T])
|
|
where
|
|
T: AsRef<str> + Serialize + Send + Sync,
|
|
{
|
|
clickhouse_client
|
|
.query("DELETE FROM news WHERE hasAny(symbols, ?)")
|
|
.bind(symbols)
|
|
.execute()
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
pub async fn cleanup(clickhouse_client: &Client) {
|
|
let assets = assets::select(clickhouse_client).await;
|
|
|
|
let symbols = assets
|
|
.into_iter()
|
|
.map(|asset| asset.abbreviation)
|
|
.collect::<Vec<_>>();
|
|
|
|
clickhouse_client
|
|
.query("DELETE FROM news WHERE NOT hasAny(symbols, ?)")
|
|
.bind(symbols)
|
|
.execute()
|
|
.await
|
|
.unwrap();
|
|
}
|