use std::sync::Arc;
use talon_core::types::{ScheduledTask, SessionRow};
use talon_core::{Error, Result};
use talon_db::{Db, Investigation};
pub trait HttpStore: Send + Sync {
fn get_task_by_id(&self, id: &str) -> Result<Option<ScheduledTask>>;
fn create_task(&self, task: &ScheduledTask) -> Result<()>;
fn delete_task(&self, id: &str) -> Result<()>;
fn get_tasks_for_group(&self, group_folder: &str) -> Result<Vec<ScheduledTask>>;
fn get_session_rows(&self) -> Result<Vec<SessionRow>>;
fn upsert_investigation(&self, inv: &Investigation) -> Result<()>;
fn get_investigation(&self, id: &str) -> Result<Option<Investigation>>;
fn list_investigations(&self, limit: usize) -> Result<Vec<Investigation>>;
fn latest_investigation_for_alert(&self, alert_id: &str) -> Result<Option<Investigation>>;
}
impl HttpStore for Db {
fn get_task_by_id(&self, id: &str) -> Result<Option<ScheduledTask>> {
Db::get_task_by_id(self, id)
}
fn create_task(&self, task: &ScheduledTask) -> Result<()> {
Db::create_task(self, task)
}
fn delete_task(&self, id: &str) -> Result<()> {
Db::delete_task(self, id)
}
fn get_tasks_for_group(&self, group_folder: &str) -> Result<Vec<ScheduledTask>> {
Db::get_tasks_for_group(self, group_folder)
}
fn get_session_rows(&self) -> Result<Vec<SessionRow>> {
Db::get_session_rows(self)
}
fn upsert_investigation(&self, inv: &Investigation) -> Result<()> {
Db::upsert_investigation(self, inv)
}
fn get_investigation(&self, id: &str) -> Result<Option<Investigation>> {
Db::get_investigation(self, id)
}
fn list_investigations(&self, limit: usize) -> Result<Vec<Investigation>> {
Db::list_investigations(self, limit)
}
fn latest_investigation_for_alert(&self, alert_id: &str) -> Result<Option<Investigation>> {
Db::latest_investigation_for_alert(self, alert_id)
}
}
pub(crate) async fn run_blocking<T, F>(store: &Arc<dyn HttpStore>, f: F) -> Result<T>
where
T: Send + 'static,
F: FnOnce(&dyn HttpStore) -> Result<T> + Send + 'static,
{
let store = Arc::clone(store);
tokio::task::spawn_blocking(move || f(store.as_ref()))
.await
.map_err(|e| Error::Db(format!("store task failed: {e}")))?
}