1use std::sync::Arc;
9
10use talon_core::types::{ScheduledTask, SessionRow};
11use talon_core::{Error, Result};
12use talon_db::{Db, Investigation};
13
14pub trait HttpStore: Send + Sync {
16 fn get_task_by_id(&self, id: &str) -> Result<Option<ScheduledTask>>;
18 fn create_task(&self, task: &ScheduledTask) -> Result<()>;
20 fn delete_task(&self, id: &str) -> Result<()>;
22 fn get_tasks_for_group(&self, group_folder: &str) -> Result<Vec<ScheduledTask>>;
24 fn get_session_rows(&self) -> Result<Vec<SessionRow>>;
26 fn upsert_investigation(&self, inv: &Investigation) -> Result<()>;
28 fn get_investigation(&self, id: &str) -> Result<Option<Investigation>>;
30 fn list_investigations(&self, limit: usize) -> Result<Vec<Investigation>>;
32 fn latest_investigation_for_alert(&self, alert_id: &str) -> Result<Option<Investigation>>;
34}
35
36impl HttpStore for Db {
37 fn get_task_by_id(&self, id: &str) -> Result<Option<ScheduledTask>> {
38 Db::get_task_by_id(self, id)
39 }
40 fn create_task(&self, task: &ScheduledTask) -> Result<()> {
41 Db::create_task(self, task)
42 }
43 fn delete_task(&self, id: &str) -> Result<()> {
44 Db::delete_task(self, id)
45 }
46 fn get_tasks_for_group(&self, group_folder: &str) -> Result<Vec<ScheduledTask>> {
47 Db::get_tasks_for_group(self, group_folder)
48 }
49 fn get_session_rows(&self) -> Result<Vec<SessionRow>> {
50 Db::get_session_rows(self)
51 }
52 fn upsert_investigation(&self, inv: &Investigation) -> Result<()> {
53 Db::upsert_investigation(self, inv)
54 }
55 fn get_investigation(&self, id: &str) -> Result<Option<Investigation>> {
56 Db::get_investigation(self, id)
57 }
58 fn list_investigations(&self, limit: usize) -> Result<Vec<Investigation>> {
59 Db::list_investigations(self, limit)
60 }
61 fn latest_investigation_for_alert(&self, alert_id: &str) -> Result<Option<Investigation>> {
62 Db::latest_investigation_for_alert(self, alert_id)
63 }
64}
65
66pub(crate) async fn run_blocking<T, F>(store: &Arc<dyn HttpStore>, f: F) -> Result<T>
70where
71 T: Send + 'static,
72 F: FnOnce(&dyn HttpStore) -> Result<T> + Send + 'static,
73{
74 let store = Arc::clone(store);
75 tokio::task::spawn_blocking(move || f(store.as_ref()))
76 .await
77 .map_err(|e| Error::Db(format!("store task failed: {e}")))?
78}