Add news data support
- Refactor everything in the process, oops Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
50
src/database/news.rs
Normal file
50
src/database/news.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
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();
|
||||
}
|
Reference in New Issue
Block a user