Add local market calendar storage
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
38
src/database/calendar.rs
Normal file
38
src/database/calendar.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use crate::{optimize, types::Calendar};
|
||||
use clickhouse::error::Error;
|
||||
use tokio::try_join;
|
||||
|
||||
optimize!("calendar");
|
||||
|
||||
pub async fn upsert_batch_and_delete<'a, T>(
|
||||
client: &clickhouse::Client,
|
||||
records: T,
|
||||
) -> Result<(), Error>
|
||||
where
|
||||
T: IntoIterator<Item = &'a Calendar> + Send + Sync + Clone,
|
||||
T::IntoIter: Send,
|
||||
{
|
||||
let upsert_future = async {
|
||||
let mut insert = client.insert("calendar")?;
|
||||
for record in records.clone() {
|
||||
insert.write(record).await?;
|
||||
}
|
||||
insert.end().await
|
||||
};
|
||||
|
||||
let delete_future = async {
|
||||
let dates = records
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|r| r.date)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
client
|
||||
.query("DELETE FROM calendar WHERE date NOT IN ?")
|
||||
.bind(dates)
|
||||
.execute()
|
||||
.await
|
||||
};
|
||||
|
||||
try_join!(upsert_future, delete_future).map(|_| ())
|
||||
}
|
@@ -2,6 +2,7 @@ pub mod assets;
|
||||
pub mod backfills_bars;
|
||||
pub mod backfills_news;
|
||||
pub mod bars;
|
||||
pub mod calendar;
|
||||
pub mod news;
|
||||
pub mod orders;
|
||||
|
||||
@@ -144,7 +145,8 @@ pub async fn optimize_all(clickhouse_client: &Client) -> Result<(), Error> {
|
||||
news::optimize(clickhouse_client),
|
||||
backfills_bars::optimize(clickhouse_client),
|
||||
backfills_news::optimize(clickhouse_client),
|
||||
orders::optimize(clickhouse_client)
|
||||
orders::optimize(clickhouse_client),
|
||||
calendar::optimize(clickhouse_client)
|
||||
)
|
||||
.map(|_| ())
|
||||
}
|
||||
|
Reference in New Issue
Block a user