Skip to main content

oversync_api/
state.rs

1use std::sync::Arc;
2
3use tokio::sync::RwLock;
4
5use crate::types::PipePresetInfo;
6use crate::types::SinkInfo;
7
8pub struct ApiState {
9	pub sinks: Arc<RwLock<Vec<SinkConfig>>>,
10	pub pipes: Arc<RwLock<Vec<PipeConfigCache>>>,
11	pub pipe_presets: Arc<RwLock<Vec<PipePresetCache>>>,
12	pub db_client: Option<Arc<surrealdb::Surreal<surrealdb::engine::any::Any>>>,
13	pub lifecycle: Option<Arc<dyn LifecycleControl>>,
14	pub api_key: Option<String>,
15}
16
17/// Trait for lifecycle operations so the API crate doesn't depend on the root crate.
18#[async_trait::async_trait]
19pub trait LifecycleControl: Send + Sync {
20	async fn restart_with_config_json(
21		&self,
22		db: &surrealdb::Surreal<surrealdb::engine::any::Any>,
23	) -> Result<(), oversync_core::error::OversyncError>;
24	async fn runtime_cache_snapshot(
25		&self,
26	) -> Result<RuntimeCacheSnapshot, oversync_core::error::OversyncError>;
27	async fn export_config(
28		&self,
29		db: &surrealdb::Surreal<surrealdb::engine::any::Any>,
30		format: crate::types::ExportConfigFormat,
31	) -> Result<String, oversync_core::error::OversyncError>;
32	async fn import_config(
33		&self,
34		db: &surrealdb::Surreal<surrealdb::engine::any::Any>,
35		format: crate::types::ExportConfigFormat,
36		content: &str,
37	) -> Result<Vec<String>, oversync_core::error::OversyncError>;
38	async fn run_pipe_once(
39		&self,
40		pipe_name: &str,
41	) -> Result<Vec<crate::types::PipeRunQueryResult>, oversync_core::error::OversyncError>;
42	async fn pause(&self);
43	async fn resume(&self) -> Result<(), oversync_core::error::OversyncError>;
44	async fn is_running(&self) -> bool;
45	async fn is_paused(&self) -> bool;
46}
47
48#[derive(Clone)]
49pub struct SinkConfig {
50	pub name: String,
51	pub sink_type: String,
52	pub config: Option<serde_json::Value>,
53}
54
55#[derive(Clone)]
56pub struct PipeConfigCache {
57	pub name: String,
58	pub origin_connector: String,
59	pub origin_dsn: String,
60	pub targets: Vec<String>,
61	pub interval_secs: u64,
62	pub query_count: usize,
63	pub recipe: Option<serde_json::Value>,
64	pub enabled: bool,
65}
66
67pub struct PipePresetCache {
68	pub name: String,
69	pub description: Option<String>,
70	pub spec: serde_json::Value,
71}
72
73#[derive(Default)]
74pub struct RuntimeCacheSnapshot {
75	pub sinks: Vec<SinkConfig>,
76	pub pipes: Vec<PipeConfigCache>,
77}
78
79impl ApiState {
80	pub fn sinks_info(&self) -> Vec<SinkInfo> {
81		let sinks = match self.sinks.try_read() {
82			Ok(s) => s,
83			Err(_) => return vec![],
84		};
85		sinks
86			.iter()
87			.map(|s| SinkInfo {
88				name: s.name.clone(),
89				sink_type: s.sink_type.clone(),
90				config: s.config.clone(),
91			})
92			.collect()
93	}
94
95	pub fn pipes_info(&self) -> Vec<crate::types::PipeInfo> {
96		let pipes = match self.pipes.try_read() {
97			Ok(p) => p,
98			Err(_) => return vec![],
99		};
100		pipes
101			.iter()
102			.map(|p| crate::types::PipeInfo {
103				name: p.name.clone(),
104				origin_connector: p.origin_connector.clone(),
105				origin_dsn: p.origin_dsn.clone(),
106				targets: p.targets.clone(),
107				interval_secs: p.interval_secs,
108				query_count: p.query_count,
109				recipe: p.recipe.clone(),
110				enabled: p.enabled,
111			})
112			.collect()
113	}
114
115	pub fn pipe_presets_info(&self) -> Vec<PipePresetInfo> {
116		let presets = match self.pipe_presets.try_read() {
117			Ok(p) => p,
118			Err(_) => return vec![],
119		};
120		presets
121			.iter()
122			.map(|preset| PipePresetInfo {
123				name: preset.name.clone(),
124				description: preset.description.clone(),
125				spec: preset.spec.clone(),
126			})
127			.collect()
128	}
129}