bcms 0.1.7

Bluetooth Client Manager Service for IoT device management
Documentation
use crate::data::IoTData;
use crate::Result;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use chrono::Utc;

/// In-memory data store for IoT data with time-based filtering
/// Equivalent to Python data_store.py
#[derive(Debug)]
pub struct DataStore {
    data: Arc<RwLock<HashMap<String, Vec<IoTData>>>>,
}

impl DataStore {
    pub fn new() -> Self {
        Self {
            data: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Add data to the store
    pub async fn add_data(&self, data: IoTData) -> Result<()> {
        let mut store = self.data.write().await;
        let address = data.address().to_string();
        
        store.entry(address)
            .or_insert_with(Vec::new)
            .push(data);
        
        Ok(())
    }

    /// Add multiple data points to the store
    pub async fn add_data_batch(&self, data_batch: Vec<IoTData>) -> Result<()> {
        let mut store = self.data.write().await;
        
        for data in data_batch {
            let address = data.address().to_string();
            store.entry(address)
                .or_insert_with(Vec::new)
                .push(data);
        }
        
        Ok(())
    }

    /// Get all data for a specific device
    pub async fn get_device_data(&self, address: &str) -> Vec<IoTData> {
        let store = self.data.read().await;
        store.get(address)
            .cloned()
            .unwrap_or_default()
    }

    /// Get data for a device since a specific timestamp
    pub async fn get_device_data_since(&self, address: &str, since_timestamp: i64) -> Vec<IoTData> {
        let store = self.data.read().await;
        if let Some(device_data) = store.get(address) {
            device_data.iter()
                .filter(|data| data.timestamp() > since_timestamp)
                .cloned()
                .collect()
        } else {
            Vec::new()
        }
    }

    /// Get all data from all devices
    pub async fn get_all_data(&self) -> Vec<IoTData> {
        let store = self.data.read().await;
        store.values()
            .flat_map(|device_data| device_data.iter())
            .cloned()
            .collect()
    }

    /// Get all data since a specific timestamp
    pub async fn get_all_data_since(&self, since_timestamp: i64) -> Vec<IoTData> {
        let store = self.data.read().await;
        store.values()
            .flat_map(|device_data| device_data.iter())
            .filter(|data| data.timestamp() > since_timestamp)
            .cloned()
            .collect()
    }

    /// Get data for submission (all unsent data)
    /// Returns data grouped by device address
    pub async fn get_data_for_submission(&self) -> HashMap<String, Vec<IoTData>> {
        let store = self.data.read().await;
        store.clone()
    }

    /// Clear old data older than the specified timestamp
    pub async fn clear_old_data(&self, older_than_timestamp: i64) -> Result<usize> {
        let mut store = self.data.write().await;
        let mut removed_count = 0;
        
        for device_data in store.values_mut() {
            let initial_len = device_data.len();
            device_data.retain(|data| data.timestamp() > older_than_timestamp);
            removed_count += initial_len - device_data.len();
        }
        
        // Remove empty device entries
        store.retain(|_, device_data| !device_data.is_empty());
        
        Ok(removed_count)
    }

    /// Clear all data for a specific device
    pub async fn clear_device_data(&self, address: &str) -> Result<usize> {
        let mut store = self.data.write().await;
        if let Some(device_data) = store.remove(address) {
            Ok(device_data.len())
        } else {
            Ok(0)
        }
    }

    /// Clear all data from the store
    pub async fn clear_all_data(&self) -> Result<usize> {
        let mut store = self.data.write().await;
        let total_count = store.values()
            .map(|device_data| device_data.len())
            .sum();
        
        store.clear();
        Ok(total_count)
    }

    /// Get data count for a specific device
    pub async fn get_device_data_count(&self, address: &str) -> usize {
        let store = self.data.read().await;
        store.get(address)
            .map(|device_data| device_data.len())
            .unwrap_or(0)
    }

    /// Get total data count across all devices
    pub async fn get_total_data_count(&self) -> usize {
        let store = self.data.read().await;
        store.values()
            .map(|device_data| device_data.len())
            .sum()
    }

    /// Get list of devices that have data
    pub async fn get_devices_with_data(&self) -> Vec<String> {
        let store = self.data.read().await;
        store.keys().cloned().collect()
    }

    /// Get the latest data point for a device
    pub async fn get_latest_device_data(&self, address: &str) -> Option<IoTData> {
        let store = self.data.read().await;
        if let Some(device_data) = store.get(address) {
            device_data.iter()
                .max_by_key(|data| data.timestamp())
                .cloned()
        } else {
            None
        }
    }

    /// Mark data as submitted by removing it from the store
    /// This is equivalent to the Python version that removes submitted data
    pub async fn mark_data_submitted(&self, submitted_data: &[IoTData]) -> Result<()> {
        let mut store = self.data.write().await;
        
        for submitted in submitted_data {
            if let Some(device_data) = store.get_mut(submitted.address()) {
                // Remove the specific data point
                device_data.retain(|data| {
                    !(data.address() == submitted.address() && 
                      data.timestamp() == submitted.timestamp() &&
                      data.name() == submitted.name())
                });
            }
        }
        
        // Remove empty device entries
        store.retain(|_, device_data| !device_data.is_empty());
        
        Ok(())
    }

    /// Perform cleanup of old data
    /// This should be called periodically to prevent memory growth
    pub async fn cleanup_old_data(&self, max_age_seconds: i64) -> Result<usize> {
        let now = Utc::now().timestamp();
        let cutoff_timestamp = now - max_age_seconds;
        self.clear_old_data(cutoff_timestamp).await
    }
    
    /// Get data since a specific timestamp (for incremental submission)
    pub async fn get_data_since(&self, since_timestamp: i64) -> Vec<IoTData> {
        let store = self.data.read().await;
        store.values()
            .flat_map(|device_data| device_data.iter())
            .filter(|data| data.timestamp() > since_timestamp)
            .cloned()
            .collect()
    }
    
    /// Limit IoT data sample rate (equivalent to Python's limit_iot_data_sample_rate)
    /// Keeps only 1 sample per (device, data_type) combination every N seconds
    pub fn limit_sample_rate(data: Vec<IoTData>, samples_every_seconds: i64) -> Vec<IoTData> {
        if data.is_empty() {
            return data;
        }
        
        use std::collections::HashMap;
        
        // Group data by device address
        let mut data_by_device: HashMap<String, Vec<IoTData>> = HashMap::new();
        for d in data {
            data_by_device
                .entry(d.address().to_string())
                .or_insert_with(Vec::new)
                .push(d);
        }
        
        // Group by device and data type
        let mut data_by_device_and_type: HashMap<String, HashMap<String, Vec<IoTData>>> = HashMap::new();
        for (address, device_data) in data_by_device {
            let mut data_by_type: HashMap<String, Vec<IoTData>> = HashMap::new();
            for d in device_data {
                data_by_type
                    .entry(d.name().to_string())
                    .or_insert_with(Vec::new)
                    .push(d);
            }
            data_by_device_and_type.insert(address, data_by_type);
        }
        
        // Filter data - keep only 1 sample per (device, type) every N seconds
        let mut filtered_data = Vec::new();
        for (_address, data_by_type) in data_by_device_and_type {
            for (_data_type, mut type_data) in data_by_type {
                // Sort by timestamp to process in chronological order
                type_data.sort_by_key(|d| d.timestamp());
                
                let mut last_timestamp: Option<i64> = None;
                for d in type_data {
                    if last_timestamp.is_none() || d.timestamp() - last_timestamp.unwrap() > samples_every_seconds {
                        filtered_data.push(d.clone());
                        last_timestamp = Some(d.timestamp());
                    }
                }
            }
        }
        
        filtered_data
    }
    
    /// Get data count per device for monitoring
    pub async fn get_data_count_per_device(&self) -> HashMap<String, usize> {
        let store = self.data.read().await;
        store.iter()
            .map(|(address, device_data)| (address.clone(), device_data.len()))
            .collect()
    }
    
    /// Enforce memory limits by removing oldest data when limits are exceeded
    pub async fn enforce_limits(&self, max_data_points_per_device: usize, max_total_data_points: usize) -> Result<(usize, usize)> {
        let mut store = self.data.write().await;
        let mut total_removed = 0;
        let mut total_remaining = 0;
        
        // First pass: enforce per-device limits
        for device_data in store.values_mut() {
            if device_data.len() > max_data_points_per_device {
                // Sort by timestamp and keep only the newest data
                device_data.sort_by_key(|d| d.timestamp());
                let to_remove = device_data.len() - max_data_points_per_device;
                device_data.drain(0..to_remove);
                total_removed += to_remove;
            }
            total_remaining += device_data.len();
        }
        
        // Second pass: enforce total limit if still exceeded
        if total_remaining > max_total_data_points {
            // Collect all data with timestamps, sort globally, and keep newest
            let mut all_data_with_device: Vec<(String, IoTData)> = Vec::new();
            for (address, device_data) in store.iter() {
                for data in device_data {
                    all_data_with_device.push((address.clone(), data.clone()));
                }
            }
            
            // Sort by timestamp (oldest first)
            all_data_with_device.sort_by_key(|(_, data)| data.timestamp());
            
            // Remove oldest data to reach limit
            let to_remove_global = total_remaining - max_total_data_points;
            let data_to_remove = &all_data_with_device[0..to_remove_global];
            
            // Remove the selected data from the store
            for (address, data_to_remove) in data_to_remove {
                if let Some(device_data) = store.get_mut(address) {
                    device_data.retain(|data| {
                        !(data.address() == data_to_remove.address() &&
                          data.timestamp() == data_to_remove.timestamp() &&
                          data.name() == data_to_remove.name())
                    });
                }
            }
            
            total_removed += to_remove_global;
            total_remaining = max_total_data_points;
        }
        
        // Remove empty device entries
        store.retain(|_, device_data| !device_data.is_empty());
        
        Ok((total_removed, total_remaining))
    }
    
    /// Get data summary for a specific device (data type counts and latest timestamps)
    pub async fn get_device_data_summary(&self, address: &str) -> HashMap<String, (u32, i64)> {
        let store = self.data.read().await;
        let mut summary = HashMap::new();
        
        if let Some(device_data) = store.get(address) {
            for data in device_data {
                let data_type = data.name().to_string();
                let timestamp = data.timestamp();
                
                match summary.get_mut(&data_type) {
                    Some((count, last_timestamp)) => {
                        *count += 1;
                        if timestamp > *last_timestamp {
                            *last_timestamp = timestamp;
                        }
                    }
                    None => {
                        summary.insert(data_type, (1, timestamp));
                    }
                }
            }
        }
        
        summary
    }
}

impl Default for DataStore {
    fn default() -> Self {
        Self::new()
    }
}