oxigdal-ws 0.1.6

WebSocket streaming support for OxiGDAL - real-time geospatial data delivery
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Event streaming handler.

use crate::error::{Error, Result};
use crate::protocol::{EventType, Message};
use crate::stream::EventData;
use crate::subscription::SubscriptionManager;
use dashmap::DashMap;
use std::sync::Arc;
use tokio::sync::mpsc;
use tracing::{debug, warn};

/// Event streaming handler.
pub struct EventHandler {
    /// Subscription manager
    subscriptions: Arc<SubscriptionManager>,
    /// Client message senders
    client_senders: Arc<DashMap<String, mpsc::UnboundedSender<Message>>>,
    /// Event history for replay
    event_history: Arc<DashMap<EventType, Vec<EventData>>>,
    /// Maximum history size per event type
    max_history_size: usize,
}

impl EventHandler {
    /// Create a new event handler.
    pub fn new(subscriptions: Arc<SubscriptionManager>) -> Self {
        Self {
            subscriptions,
            client_senders: Arc::new(DashMap::new()),
            event_history: Arc::new(DashMap::new()),
            max_history_size: 1000,
        }
    }

    /// Create a new event handler with custom history size.
    pub fn with_history_size(
        subscriptions: Arc<SubscriptionManager>,
        max_history_size: usize,
    ) -> Self {
        Self {
            subscriptions,
            client_senders: Arc::new(DashMap::new()),
            event_history: Arc::new(DashMap::new()),
            max_history_size,
        }
    }

    /// Register a client sender.
    pub fn register_client(&self, client_id: String, sender: mpsc::UnboundedSender<Message>) {
        self.client_senders.insert(client_id, sender);
    }

    /// Unregister a client.
    pub fn unregister_client(&self, client_id: &str) {
        self.client_senders.remove(client_id);
    }

    /// Stream an event to subscribers.
    pub async fn stream_event(&self, event: EventData) -> Result<usize> {
        debug!("Streaming event: type={:?}", event.event_type);

        // Store in history
        self.add_to_history(event.clone());

        // Find matching subscriptions
        let subscriptions = self
            .subscriptions
            .find_event_subscriptions(event.event_type);

        if subscriptions.is_empty() {
            return Ok(0);
        }

        let mut sent_count = 0;

        for subscription in subscriptions {
            let client_id = &subscription.client_id;

            // Send event to client
            if let Some(sender) = self.client_senders.get(client_id) {
                let message = Message::Event {
                    subscription_id: subscription.id.clone(),
                    event_type: event.event_type,
                    payload: event.payload.clone(),
                    timestamp: event.timestamp.to_rfc3339(),
                };

                if sender.send(message).is_ok() {
                    sent_count += 1;
                } else {
                    warn!("Failed to send event to client {}", client_id);
                }
            }
        }

        Ok(sent_count)
    }

    /// Add event to history.
    fn add_to_history(&self, event: EventData) {
        let event_type = event.event_type;

        self.event_history
            .entry(event_type)
            .and_modify(|history| {
                history.push(event.clone());
                // Maintain max size
                if history.len() > self.max_history_size {
                    history.remove(0);
                }
            })
            .or_insert_with(|| vec![event]);
    }

    /// Stream multiple events.
    pub async fn stream_events(&self, events: Vec<EventData>) -> Result<usize> {
        let mut total_sent = 0;

        for event in events {
            total_sent += self.stream_event(event).await?;
        }

        Ok(total_sent)
    }

    /// Notify file change.
    pub async fn notify_file_change(&self, file_path: &str, change_type: &str) -> Result<usize> {
        let payload = serde_json::json!({
            "file_path": file_path,
            "change_type": change_type,
        });

        let event = EventData::new(EventType::FileChange, payload);
        self.stream_event(event).await
    }

    /// Notify processing status.
    pub async fn notify_processing_status(
        &self,
        task_id: &str,
        status: &str,
        progress: Option<f64>,
    ) -> Result<usize> {
        let mut payload = serde_json::json!({
            "task_id": task_id,
            "status": status,
        });

        if let Some(progress_val) = progress {
            payload["progress"] = serde_json::json!(progress_val);
        }

        let event = EventData::new(EventType::ProcessingStatus, payload);
        self.stream_event(event).await
    }

    /// Notify error.
    pub async fn notify_error(
        &self,
        error_message: &str,
        context: Option<serde_json::Value>,
    ) -> Result<usize> {
        let mut payload = serde_json::json!({
            "error": error_message,
        });

        if let Some(ctx) = context {
            payload["context"] = ctx;
        }

        let event = EventData::new(EventType::Error, payload);
        self.stream_event(event).await
    }

    /// Stream progress updates.
    pub async fn stream_progress(
        &self,
        task_id: &str,
        current: usize,
        total: usize,
        message: Option<&str>,
    ) -> Result<usize> {
        let progress = if total > 0 {
            (current as f64 / total as f64) * 100.0
        } else {
            0.0
        };

        let mut payload = serde_json::json!({
            "task_id": task_id,
            "current": current,
            "total": total,
            "progress_percent": progress,
        });

        if let Some(msg) = message {
            payload["message"] = serde_json::json!(msg);
        }

        let event = EventData::new(EventType::Progress, payload);
        self.stream_event(event).await
    }

    /// Stream custom event.
    pub async fn stream_custom_event(&self, payload: serde_json::Value) -> Result<usize> {
        let event = EventData::new(EventType::Custom, payload);
        self.stream_event(event).await
    }

    /// Get event history for a specific type.
    pub fn get_history(&self, event_type: EventType) -> Vec<EventData> {
        self.event_history
            .get(&event_type)
            .map(|h| h.clone())
            .unwrap_or_default()
    }

    /// Get recent events of a specific type.
    pub fn get_recent_events(&self, event_type: EventType, count: usize) -> Vec<EventData> {
        if let Some(history) = self.event_history.get(&event_type) {
            let start = if history.len() > count {
                history.len() - count
            } else {
                0
            };
            history[start..].to_vec()
        } else {
            Vec::new()
        }
    }

    /// Replay events to a new subscriber.
    pub async fn replay_events(
        &self,
        client_id: &str,
        event_type: EventType,
        count: Option<usize>,
    ) -> Result<usize> {
        let events = if let Some(n) = count {
            self.get_recent_events(event_type, n)
        } else {
            self.get_history(event_type)
        };

        if events.is_empty() {
            return Ok(0);
        }

        let sender = self
            .client_senders
            .get(client_id)
            .ok_or_else(|| Error::NotFound(format!("Client not found: {}", client_id)))?;

        let mut sent_count = 0;

        for event in events {
            // Find subscription for this client and event type
            let subscriptions = self.subscriptions.get_client_subscriptions(client_id);

            for subscription in subscriptions {
                if subscription.matches_event(event_type) {
                    let message = Message::Event {
                        subscription_id: subscription.id.clone(),
                        event_type: event.event_type,
                        payload: event.payload.clone(),
                        timestamp: event.timestamp.to_rfc3339(),
                    };

                    if sender.send(message).is_ok() {
                        sent_count += 1;
                    }
                    break;
                }
            }
        }

        Ok(sent_count)
    }

    /// Clear event history.
    pub fn clear_history(&self) {
        self.event_history.clear();
    }

    /// Clear history for a specific event type.
    pub fn clear_history_for_type(&self, event_type: EventType) {
        self.event_history.remove(&event_type);
    }

    /// Get total event count across all types.
    pub fn total_event_count(&self) -> usize {
        self.event_history
            .iter()
            .map(|entry| entry.value().len())
            .sum()
    }

    /// Get event count for a specific type.
    pub fn event_count_for_type(&self, event_type: EventType) -> usize {
        self.event_history
            .get(&event_type)
            .map(|h| h.len())
            .unwrap_or(0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_event_handler_creation() {
        let subscriptions = Arc::new(SubscriptionManager::new());
        let handler = EventHandler::new(subscriptions);

        assert_eq!(handler.total_event_count(), 0);
        assert_eq!(handler.max_history_size, 1000);
    }

    #[tokio::test]
    async fn test_stream_event() {
        let subscriptions = Arc::new(SubscriptionManager::new());
        let handler = EventHandler::new(subscriptions);

        let payload = serde_json::json!({"test": "data"});
        let event = EventData::new(EventType::Custom, payload);

        // Stream event (won't send to anyone as no subscriptions)
        let result = handler.stream_event(event).await;
        assert!(result.is_ok());

        // Check it was added to history
        assert_eq!(handler.event_count_for_type(EventType::Custom), 1);
    }

    #[tokio::test]
    async fn test_notify_file_change() {
        let subscriptions = Arc::new(SubscriptionManager::new());
        let handler = EventHandler::new(subscriptions);

        let result = handler
            .notify_file_change("/path/to/file.tif", "modified")
            .await;
        assert!(result.is_ok());

        assert_eq!(handler.event_count_for_type(EventType::FileChange), 1);
    }

    #[tokio::test]
    async fn test_notify_processing_status() {
        let subscriptions = Arc::new(SubscriptionManager::new());
        let handler = EventHandler::new(subscriptions);

        let result = handler
            .notify_processing_status("task-123", "running", Some(50.0))
            .await;
        assert!(result.is_ok());

        assert_eq!(handler.event_count_for_type(EventType::ProcessingStatus), 1);
    }

    #[tokio::test]
    async fn test_stream_progress() {
        let subscriptions = Arc::new(SubscriptionManager::new());
        let handler = EventHandler::new(subscriptions);

        let result = handler
            .stream_progress("task-123", 50, 100, Some("Processing tiles"))
            .await;
        assert!(result.is_ok());

        assert_eq!(handler.event_count_for_type(EventType::Progress), 1);
    }

    #[tokio::test]
    async fn test_event_history() {
        let subscriptions = Arc::new(SubscriptionManager::new());
        let handler = EventHandler::new(subscriptions);

        // Add multiple events
        for i in 0..5 {
            let payload = serde_json::json!({"index": i});
            let event = EventData::new(EventType::Custom, payload);
            let result = handler.stream_event(event).await;
            assert!(result.is_ok());
        }

        assert_eq!(handler.event_count_for_type(EventType::Custom), 5);

        let recent = handler.get_recent_events(EventType::Custom, 3);
        assert_eq!(recent.len(), 3);

        handler.clear_history_for_type(EventType::Custom);
        assert_eq!(handler.event_count_for_type(EventType::Custom), 0);
    }

    #[tokio::test]
    async fn test_history_size_limit() {
        let subscriptions = Arc::new(SubscriptionManager::new());
        let handler = EventHandler::with_history_size(subscriptions, 10);

        // Add more events than max size
        for i in 0..20 {
            let payload = serde_json::json!({"index": i});
            let event = EventData::new(EventType::Custom, payload);
            let result = handler.stream_event(event).await;
            assert!(result.is_ok());
        }

        // Should only keep last 10
        assert_eq!(handler.event_count_for_type(EventType::Custom), 10);
    }
}