use crate::error::EdgeComputingError;
use crate::models::{SyncOperation, SyncStatus};
use chrono::Utc;
use std::sync::Arc;
use tokio::sync::RwLock;
use uuid::Uuid;
pub struct EdgeSyncService {
sync_operations: Arc<RwLock<std::collections::HashMap<Uuid, SyncOperation>>>,
_cloud_endpoint: String,
}
impl EdgeSyncService {
pub fn new(cloud_endpoint: String) -> Self {
Self {
sync_operations: Arc::new(RwLock::new(std::collections::HashMap::new())),
_cloud_endpoint: cloud_endpoint,
}
}
pub async fn create_sync_operation(
&self,
source_node: Uuid,
target_node: Uuid,
data_type: String,
data: serde_json::Value,
) -> Uuid {
let id = Uuid::new_v4();
let operation = SyncOperation {
id,
source_node,
target_node,
data_type,
data,
status: SyncStatus::Pending,
created_at: Utc::now(),
completed_at: None,
};
self.sync_operations.write().await.insert(id, operation);
id
}
pub async fn sync_to_cloud(&self, sync_id: Uuid) -> Result<(), EdgeComputingError> {
let mut operations = self.sync_operations.write().await;
let operation = operations.get_mut(&sync_id).ok_or_else(|| {
EdgeComputingError::SynchronizationFailed("Sync operation not found".to_string())
})?;
operation.status = SyncStatus::InProgress;
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
operation.status = SyncStatus::Completed;
operation.completed_at = Some(Utc::now());
Ok(())
}
pub async fn get_sync_status(&self, sync_id: Uuid) -> Option<SyncStatus> {
self.sync_operations
.read()
.await
.get(&sync_id)
.map(|op| op.status)
}
pub async fn get_pending_syncs(&self) -> Vec<SyncOperation> {
self.sync_operations
.read()
.await
.values()
.filter(|op| op.status == SyncStatus::Pending)
.cloned()
.collect()
}
}