drasi-lib 0.6.0

Embedded Drasi for in-process data change processing using continuous queries
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// Copyright 2025 The Drasi Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Component event history tracking with live streaming support.
//!
//! This module provides centralized storage for component lifecycle events,
//! allowing managers to track and query the history of status changes for
//! sources, queries, and reactions. It also supports live streaming of events
//! via broadcast channels.

use std::collections::{HashMap, VecDeque};

use tokio::sync::broadcast;

use crate::channels::{ComponentEvent, ComponentStatus};

/// Default maximum number of events to retain per component.
pub const DEFAULT_MAX_EVENTS_PER_COMPONENT: usize = 100;

/// Default broadcast channel capacity for live event streaming.
pub const DEFAULT_EVENT_CHANNEL_CAPACITY: usize = 256;

/// Per-component event storage and broadcast channel.
struct ComponentEventChannel {
    /// Recent event history
    history: VecDeque<ComponentEvent>,
    /// Maximum history size
    max_history: usize,
    /// Broadcast sender for live streaming
    sender: broadcast::Sender<ComponentEvent>,
}

impl ComponentEventChannel {
    fn new(max_history: usize, channel_capacity: usize) -> Self {
        let (sender, _) = broadcast::channel(channel_capacity);
        Self {
            history: VecDeque::with_capacity(max_history),
            max_history,
            sender,
        }
    }

    fn record(&mut self, event: ComponentEvent) {
        // Add to history
        if self.history.len() >= self.max_history {
            self.history.pop_front();
        }
        self.history.push_back(event.clone());

        // Broadcast to live subscribers (ignore if no subscribers)
        let _ = self.sender.send(event);
    }

    fn get_history(&self) -> Vec<ComponentEvent> {
        self.history.iter().cloned().collect()
    }

    fn subscribe(&self) -> broadcast::Receiver<ComponentEvent> {
        self.sender.subscribe()
    }

    fn get_last_error(&self) -> Option<String> {
        self.history
            .iter()
            .rev()
            .find(|event| event.status == ComponentStatus::Error)
            .and_then(|event| event.message.clone())
    }
}

/// Stores component event history with a bounded size per component and live streaming support.
///
/// Events are stored in a FIFO manner - when the limit is reached for a component,
/// the oldest events are discarded to make room for new ones.
///
/// Subscribers can receive live events as they occur via broadcast channels.
pub struct ComponentEventHistory {
    /// Event channels per component ID
    channels: HashMap<String, ComponentEventChannel>,
    /// Maximum events to retain per component
    max_events_per_component: usize,
    /// Broadcast channel capacity
    channel_capacity: usize,
}

impl std::fmt::Debug for ComponentEventHistory {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ComponentEventHistory")
            .field("max_events_per_component", &self.max_events_per_component)
            .field("channel_capacity", &self.channel_capacity)
            .field("component_count", &self.channels.len())
            .finish()
    }
}

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

impl ComponentEventHistory {
    /// Create a new event history with default capacity (100 events per component).
    pub fn new() -> Self {
        Self {
            channels: HashMap::new(),
            max_events_per_component: DEFAULT_MAX_EVENTS_PER_COMPONENT,
            channel_capacity: DEFAULT_EVENT_CHANNEL_CAPACITY,
        }
    }

    /// Create a new event history with custom capacity per component.
    pub fn with_capacity(max_events_per_component: usize, channel_capacity: usize) -> Self {
        Self {
            channels: HashMap::new(),
            max_events_per_component,
            channel_capacity,
        }
    }

    /// Record a component event in the history and broadcast to subscribers.
    ///
    /// If the component has reached its maximum event count, the oldest
    /// event is removed before adding the new one.
    pub fn record_event(&mut self, event: ComponentEvent) {
        let component_id = event.component_id.clone();
        let channel = self.channels.entry(component_id).or_insert_with(|| {
            ComponentEventChannel::new(self.max_events_per_component, self.channel_capacity)
        });
        channel.record(event);
    }

    /// Get all events for a specific component.
    ///
    /// Returns events in chronological order (oldest first).
    pub fn get_events(&self, component_id: &str) -> Vec<ComponentEvent> {
        self.channels
            .get(component_id)
            .map(|channel| channel.get_history())
            .unwrap_or_default()
    }

    /// Subscribe to live events for a component.
    ///
    /// Returns the current history and a broadcast receiver for new events.
    /// Creates the component's channel if it doesn't exist.
    pub fn subscribe(
        &mut self,
        component_id: &str,
    ) -> (Vec<ComponentEvent>, broadcast::Receiver<ComponentEvent>) {
        let channel = self
            .channels
            .entry(component_id.to_string())
            .or_insert_with(|| {
                ComponentEventChannel::new(self.max_events_per_component, self.channel_capacity)
            });

        let history = channel.get_history();
        let receiver = channel.subscribe();
        (history, receiver)
    }

    /// Subscribe to live events for a component (read-only).
    ///
    /// Returns the current history and a broadcast receiver, or `None` if the
    /// component has no event channel yet. Unlike [`subscribe`](Self::subscribe),
    /// this does not create a channel and therefore only requires `&self`.
    pub fn try_subscribe(
        &self,
        component_id: &str,
    ) -> Option<(Vec<ComponentEvent>, broadcast::Receiver<ComponentEvent>)> {
        let channel = self.channels.get(component_id)?;
        Some((channel.get_history(), channel.subscribe()))
    }

    /// Get the most recent error message for a component.
    ///
    /// Searches backwards through the component's event history to find the
    /// most recent event with `ComponentStatus::Error` and returns its message.
    pub fn get_last_error(&self, component_id: &str) -> Option<String> {
        self.channels
            .get(component_id)
            .and_then(|channel| channel.get_last_error())
    }

    /// Get all events across all components.
    ///
    /// Returns events sorted by timestamp (oldest first).
    pub fn get_all_events(&self) -> Vec<ComponentEvent> {
        let mut all_events: Vec<ComponentEvent> = self
            .channels
            .values()
            .flat_map(|channel| channel.get_history())
            .collect();

        // Sort by timestamp
        all_events.sort_by(|a, b| a.timestamp.cmp(&b.timestamp));
        all_events
    }

    /// Remove all events for a specific component.
    ///
    /// This should be called when a component is deleted to clean up its history.
    pub fn remove_component(&mut self, component_id: &str) {
        self.channels.remove(component_id);
    }

    /// Get the number of events stored for a specific component.
    pub fn event_count(&self, component_id: &str) -> usize {
        self.channels
            .get(component_id)
            .map(|channel| channel.history.len())
            .unwrap_or(0)
    }

    /// Get the total number of events stored across all components.
    pub fn total_event_count(&self) -> usize {
        self.channels
            .values()
            .map(|channel| channel.history.len())
            .sum()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::channels::ComponentType;
    use chrono::Utc;

    fn create_test_event(
        component_id: &str,
        status: ComponentStatus,
        message: Option<&str>,
    ) -> ComponentEvent {
        ComponentEvent {
            component_id: component_id.to_string(),
            component_type: ComponentType::Source,
            status,
            timestamp: Utc::now(),
            message: message.map(|s| s.to_string()),
        }
    }

    #[test]
    fn test_record_and_get_events() {
        let mut history = ComponentEventHistory::new();

        let event1 = create_test_event("source1", ComponentStatus::Starting, None);
        let event2 = create_test_event("source1", ComponentStatus::Running, None);

        history.record_event(event1);
        history.record_event(event2);

        let events = history.get_events("source1");
        assert_eq!(events.len(), 2);
        assert_eq!(events[0].status, ComponentStatus::Starting);
        assert_eq!(events[1].status, ComponentStatus::Running);
    }

    #[test]
    fn test_max_events_per_component() {
        let mut history = ComponentEventHistory::with_capacity(3, 10);

        for i in 0..5 {
            let event = create_test_event(
                "source1",
                ComponentStatus::Running,
                Some(&format!("event {i}")),
            );
            history.record_event(event);
        }

        let events = history.get_events("source1");
        assert_eq!(events.len(), 3);
        // Should have events 2, 3, 4 (oldest removed)
        assert_eq!(events[0].message, Some("event 2".to_string()));
        assert_eq!(events[2].message, Some("event 4".to_string()));
    }

    #[test]
    fn test_get_last_error() {
        let mut history = ComponentEventHistory::new();

        history.record_event(create_test_event(
            "source1",
            ComponentStatus::Starting,
            None,
        ));
        history.record_event(create_test_event(
            "source1",
            ComponentStatus::Error,
            Some("First error"),
        ));
        history.record_event(create_test_event(
            "source1",
            ComponentStatus::Starting,
            None,
        ));
        history.record_event(create_test_event(
            "source1",
            ComponentStatus::Error,
            Some("Second error"),
        ));
        history.record_event(create_test_event("source1", ComponentStatus::Running, None));

        let last_error = history.get_last_error("source1");
        assert_eq!(last_error, Some("Second error".to_string()));
    }

    #[test]
    fn test_get_last_error_none() {
        let mut history = ComponentEventHistory::new();

        history.record_event(create_test_event(
            "source1",
            ComponentStatus::Starting,
            None,
        ));
        history.record_event(create_test_event("source1", ComponentStatus::Running, None));

        let last_error = history.get_last_error("source1");
        assert!(last_error.is_none());
    }

    #[test]
    fn test_get_all_events() {
        let mut history = ComponentEventHistory::new();

        history.record_event(create_test_event(
            "source1",
            ComponentStatus::Starting,
            None,
        ));
        history.record_event(create_test_event("query1", ComponentStatus::Starting, None));
        history.record_event(create_test_event("source1", ComponentStatus::Running, None));

        let all_events = history.get_all_events();
        assert_eq!(all_events.len(), 3);
    }

    #[test]
    fn test_remove_component() {
        let mut history = ComponentEventHistory::new();

        history.record_event(create_test_event(
            "source1",
            ComponentStatus::Starting,
            None,
        ));
        history.record_event(create_test_event(
            "source2",
            ComponentStatus::Starting,
            None,
        ));

        history.remove_component("source1");

        assert_eq!(history.get_events("source1").len(), 0);
        assert_eq!(history.get_events("source2").len(), 1);
    }

    #[test]
    fn test_event_count() {
        let mut history = ComponentEventHistory::new();

        history.record_event(create_test_event(
            "source1",
            ComponentStatus::Starting,
            None,
        ));
        history.record_event(create_test_event("source1", ComponentStatus::Running, None));
        history.record_event(create_test_event(
            "source2",
            ComponentStatus::Starting,
            None,
        ));

        assert_eq!(history.event_count("source1"), 2);
        assert_eq!(history.event_count("source2"), 1);
        assert_eq!(history.event_count("nonexistent"), 0);
        assert_eq!(history.total_event_count(), 3);
    }

    #[test]
    fn test_subscribe_gets_history() {
        let mut history = ComponentEventHistory::new();

        history.record_event(create_test_event(
            "source1",
            ComponentStatus::Starting,
            None,
        ));
        history.record_event(create_test_event("source1", ComponentStatus::Running, None));

        let (events, _receiver) = history.subscribe("source1");
        assert_eq!(events.len(), 2);
        assert_eq!(events[0].status, ComponentStatus::Starting);
        assert_eq!(events[1].status, ComponentStatus::Running);
    }

    #[tokio::test]
    async fn test_subscribe_receives_live_events() {
        let mut history = ComponentEventHistory::new();

        // Subscribe before recording
        let (_history, mut receiver) = history.subscribe("source1");

        // Record a new event
        let event = create_test_event("source1", ComponentStatus::Running, Some("live event"));
        history.record_event(event);

        // Should receive the live event
        let received = receiver.try_recv().unwrap();
        assert_eq!(received.status, ComponentStatus::Running);
        assert_eq!(received.message, Some("live event".to_string()));
    }
}