reinhardt-core 0.1.0

Core components for Reinhardt framework
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
//! Signal history tracking system for monitoring signal emission patterns
//!
//! This module provides functionality to track signal emission history with timestamps,
//! enabling analysis, debugging, and monitoring of signal patterns.
//!
//! # Examples
//!
//! ```
//! use reinhardt_core::signals::history::{SignalHistory, HistoryConfig};
//! use reinhardt_core::signals::{Signal, SignalName};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Clone, Serialize, Deserialize)]
//! struct Event {
//!     id: i32,
//! }
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let signal = Signal::<Event>::new(SignalName::custom("events"));
//!
//! let config = HistoryConfig::new().with_max_entries(1000);
//! let history = SignalHistory::new(signal.clone(), config);
//!
//! history.send(Event { id: 42 }).await?;
//!
//! // Query history
//! let entries = history.get_all();
//! println!("Recorded {} events", entries.len());
//! # Ok(())
//! # }
//! ```

use super::error::SignalError;
use super::signal::Signal;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::sync::Arc;
use std::time::{Duration, SystemTime};

/// A single history entry recording a signal emission
///
/// # Examples
///
/// ```
/// use reinhardt_core::signals::history::HistoryEntry;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Clone, Serialize, Deserialize)]
/// struct Event {
///     id: i32,
/// }
///
/// let entry = HistoryEntry::new(Event { id: 42 }, true);
/// assert!(entry.success());
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryEntry<T> {
	/// Timestamp when the signal was emitted
	pub timestamp: SystemTime,
	/// The signal payload
	pub payload: T,
	/// Whether the signal was successfully processed
	pub success: bool,
	/// Error message if the signal failed
	pub error_message: Option<String>,
	/// Number of receivers that processed this signal
	pub receiver_count: usize,
}

impl<T> HistoryEntry<T> {
	/// Create a new history entry
	pub fn new(payload: T, success: bool) -> Self {
		Self {
			timestamp: SystemTime::now(),
			payload,
			success,
			error_message: None,
			receiver_count: 0,
		}
	}

	/// Create a new history entry with error
	pub fn with_error(payload: T, error: String) -> Self {
		Self {
			timestamp: SystemTime::now(),
			payload,
			success: false,
			error_message: Some(error),
			receiver_count: 0,
		}
	}

	/// Create a new history entry with full details
	pub fn with_details(
		payload: T,
		success: bool,
		error_message: Option<String>,
		receiver_count: usize,
	) -> Self {
		Self {
			timestamp: SystemTime::now(),
			payload,
			success,
			error_message,
			receiver_count,
		}
	}

	/// Check if the signal was successful
	pub fn success(&self) -> bool {
		self.success
	}

	/// Get the error message if any
	pub fn error(&self) -> Option<&str> {
		self.error_message.as_deref()
	}

	/// Get the age of this entry
	pub fn age(&self) -> Duration {
		SystemTime::now()
			.duration_since(self.timestamp)
			.unwrap_or(Duration::from_secs(0))
	}
}

/// Configuration for signal history tracking
///
/// # Examples
///
/// ```
/// use reinhardt_core::signals::history::HistoryConfig;
/// use std::time::Duration;
///
/// let config = HistoryConfig::new()
///     .with_max_entries(500)
///     .with_ttl(Duration::from_secs(3600));
///
/// assert_eq!(config.max_entries(), 500);
/// assert_eq!(config.ttl(), Some(Duration::from_secs(3600)));
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HistoryConfig {
	/// Maximum number of history entries to keep
	max_entries: usize,
	/// Time-to-live for history entries (None = unlimited)
	ttl: Option<Duration>,
	/// Whether to track errors only
	errors_only: bool,
}

impl HistoryConfig {
	/// Create a new history configuration with default values
	///
	/// Defaults:
	/// - `max_entries`: 1000
	/// - `ttl`: None (unlimited)
	/// - `errors_only`: false
	pub fn new() -> Self {
		Self {
			max_entries: 1000,
			ttl: None,
			errors_only: false,
		}
	}

	/// Set the maximum number of entries to keep
	pub fn with_max_entries(mut self, max: usize) -> Self {
		self.max_entries = max;
		self
	}

	/// Set the time-to-live for entries
	pub fn with_ttl(mut self, ttl: Duration) -> Self {
		self.ttl = Some(ttl);
		self
	}

	/// Track only errors
	pub fn with_errors_only(mut self, errors_only: bool) -> Self {
		self.errors_only = errors_only;
		self
	}

	/// Get the maximum entries
	pub fn max_entries(&self) -> usize {
		self.max_entries
	}

	/// Get the TTL
	pub fn ttl(&self) -> Option<Duration> {
		self.ttl
	}

	/// Check if tracking errors only
	pub fn errors_only(&self) -> bool {
		self.errors_only
	}
}

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

/// Statistics about signal history
///
/// # Examples
///
/// ```
/// use reinhardt_core::signals::history::HistoryStats;
///
/// let stats = HistoryStats::new();
/// assert_eq!(stats.total_count(), 0);
/// assert_eq!(stats.success_rate(), 100.0);
/// ```
#[derive(Debug, Clone, Default)]
pub struct HistoryStats {
	/// Total number of signals in history
	total_count: usize,
	/// Number of successful signals
	success_count: usize,
	/// Number of failed signals
	error_count: usize,
	/// Oldest entry timestamp
	oldest_timestamp: Option<SystemTime>,
	/// Newest entry timestamp
	newest_timestamp: Option<SystemTime>,
}

impl HistoryStats {
	/// Create new history statistics
	pub fn new() -> Self {
		Self::default()
	}

	/// Get the total count
	pub fn total_count(&self) -> usize {
		self.total_count
	}

	/// Get the success count
	pub fn success_count(&self) -> usize {
		self.success_count
	}

	/// Get the error count
	pub fn error_count(&self) -> usize {
		self.error_count
	}

	/// Get the success rate as a percentage
	pub fn success_rate(&self) -> f64 {
		if self.total_count == 0 {
			return 100.0;
		}
		(self.success_count as f64 / self.total_count as f64) * 100.0
	}

	/// Get the timespan of the history
	pub fn timespan(&self) -> Option<Duration> {
		match (self.oldest_timestamp, self.newest_timestamp) {
			(Some(oldest), Some(newest)) => newest.duration_since(oldest).ok(),
			_ => None,
		}
	}
}

/// Signal history tracker
///
/// Wraps a signal and tracks all emissions with timestamps and results.
///
/// # Examples
///
/// ```
/// use reinhardt_core::signals::history::{SignalHistory, HistoryConfig};
/// use reinhardt_core::signals::{Signal, SignalName};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Clone, Serialize, Deserialize)]
/// struct Event {
///     id: i32,
/// }
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let signal = Signal::<Event>::new(SignalName::custom("events"));
/// let config = HistoryConfig::new();
/// let history = SignalHistory::new(signal, config);
///
/// history.send(Event { id: 1 }).await?;
/// history.send(Event { id: 2 }).await?;
///
/// let stats = history.stats();
/// assert_eq!(stats.total_count(), 2);
/// # Ok(())
/// # }
/// ```
pub struct SignalHistory<T: Send + Sync + 'static> {
	signal: Signal<T>,
	config: HistoryConfig,
	entries: Arc<RwLock<VecDeque<HistoryEntry<T>>>>,
}

impl<T: Send + Sync + Clone + 'static> SignalHistory<T> {
	/// Create a new signal history tracker
	///
	/// # Arguments
	///
	/// * `signal` - The signal to track
	/// * `config` - History configuration
	pub fn new(signal: Signal<T>, config: HistoryConfig) -> Self {
		Self {
			signal,
			config,
			entries: Arc::new(RwLock::new(VecDeque::new())),
		}
	}

	/// Send a signal and record it in history
	///
	/// # Examples
	///
	/// ```
	/// # use reinhardt_core::signals::history::{SignalHistory, HistoryConfig};
	/// # use reinhardt_core::signals::{Signal, SignalName};
	/// # use serde::{Deserialize, Serialize};
	/// # #[derive(Clone, Serialize, Deserialize)]
	/// # struct Event { id: i32 }
	/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
	/// # let signal = Signal::<Event>::new(SignalName::custom("events"));
	/// # let history = SignalHistory::new(signal, HistoryConfig::new());
	/// history.send(Event { id: 42 }).await?;
	/// # Ok(())
	/// # }
	/// ```
	pub async fn send(&self, instance: T) -> Result<(), SignalError> {
		let payload = instance.clone();
		let receiver_count = self.signal.receiver_count();

		// Send the signal
		let result = self.signal.send(instance).await;

		// Record in history
		let success = result.is_ok();
		let error_message = result.as_ref().err().map(|e| e.message.clone());

		// Only record if not errors_only or if it's an error
		if !self.config.errors_only || !success {
			let entry = HistoryEntry::with_details(payload, success, error_message, receiver_count);
			self.add_entry(entry);
		}

		result
	}

	/// Add an entry to the history
	fn add_entry(&self, entry: HistoryEntry<T>) {
		let mut entries = self.entries.write();

		// Remove expired entries
		if let Some(ttl) = self.config.ttl {
			let cutoff = SystemTime::now() - ttl;
			entries.retain(|e| e.timestamp >= cutoff);
		}

		// Evict oldest if at capacity
		if entries.len() >= self.config.max_entries {
			entries.pop_front();
		}

		entries.push_back(entry);
	}

	/// Get all history entries
	///
	/// # Examples
	///
	/// ```
	/// # use reinhardt_core::signals::history::{SignalHistory, HistoryConfig};
	/// # use reinhardt_core::signals::{Signal, SignalName};
	/// # use serde::{Deserialize, Serialize};
	/// # #[derive(Clone, Serialize, Deserialize)]
	/// # struct Event { id: i32 }
	/// # let signal = Signal::<Event>::new(SignalName::custom("events"));
	/// # let history = SignalHistory::new(signal, HistoryConfig::new());
	/// let entries = history.get_all();
	/// println!("History contains {} entries", entries.len());
	/// ```
	pub fn get_all(&self) -> Vec<HistoryEntry<T>> {
		self.entries.read().iter().cloned().collect()
	}

	/// Get recent history entries
	///
	/// # Examples
	///
	/// ```
	/// # use reinhardt_core::signals::history::{SignalHistory, HistoryConfig};
	/// # use reinhardt_core::signals::{Signal, SignalName};
	/// # use serde::{Deserialize, Serialize};
	/// # #[derive(Clone, Serialize, Deserialize)]
	/// # struct Event { id: i32 }
	/// # let signal = Signal::<Event>::new(SignalName::custom("events"));
	/// # let history = SignalHistory::new(signal, HistoryConfig::new());
	/// let recent = history.get_recent(10);
	/// println!("Last 10 entries: {:?}", recent.len());
	/// ```
	pub fn get_recent(&self, count: usize) -> Vec<HistoryEntry<T>> {
		let entries = self.entries.read();
		entries.iter().rev().take(count).cloned().collect()
	}

	/// Get error entries only
	///
	/// # Examples
	///
	/// ```
	/// # use reinhardt_core::signals::history::{SignalHistory, HistoryConfig};
	/// # use reinhardt_core::signals::{Signal, SignalName};
	/// # use serde::{Deserialize, Serialize};
	/// # #[derive(Clone, Serialize, Deserialize)]
	/// # struct Event { id: i32 }
	/// # let signal = Signal::<Event>::new(SignalName::custom("events"));
	/// # let history = SignalHistory::new(signal, HistoryConfig::new());
	/// let errors = history.get_errors();
	/// println!("Found {} errors", errors.len());
	/// ```
	pub fn get_errors(&self) -> Vec<HistoryEntry<T>> {
		self.entries
			.read()
			.iter()
			.filter(|e| !e.success)
			.cloned()
			.collect()
	}

	/// Get entries within a time range
	///
	/// # Examples
	///
	/// ```
	/// # use reinhardt_core::signals::history::{SignalHistory, HistoryConfig};
	/// # use reinhardt_core::signals::{Signal, SignalName};
	/// # use serde::{Deserialize, Serialize};
	/// # use std::time::{SystemTime, Duration};
	/// # #[derive(Clone, Serialize, Deserialize)]
	/// # struct Event { id: i32 }
	/// # let signal = Signal::<Event>::new(SignalName::custom("events"));
	/// # let history = SignalHistory::new(signal, HistoryConfig::new());
	/// let start = SystemTime::now() - Duration::from_secs(3600);
	/// let end = SystemTime::now();
	/// let entries = history.get_range(start, end);
	/// ```
	pub fn get_range(&self, start: SystemTime, end: SystemTime) -> Vec<HistoryEntry<T>> {
		self.entries
			.read()
			.iter()
			.filter(|e| e.timestamp >= start && e.timestamp <= end)
			.cloned()
			.collect()
	}

	/// Get history statistics
	///
	/// # Examples
	///
	/// ```
	/// # use reinhardt_core::signals::history::{SignalHistory, HistoryConfig};
	/// # use reinhardt_core::signals::{Signal, SignalName};
	/// # use serde::{Deserialize, Serialize};
	/// # #[derive(Clone, Serialize, Deserialize)]
	/// # struct Event { id: i32 }
	/// # let signal = Signal::<Event>::new(SignalName::custom("events"));
	/// # let history = SignalHistory::new(signal, HistoryConfig::new());
	/// let stats = history.stats();
	/// println!("Success rate: {:.2}%", stats.success_rate());
	/// ```
	pub fn stats(&self) -> HistoryStats {
		let entries = self.entries.read();

		let total_count = entries.len();
		let success_count = entries.iter().filter(|e| e.success).count();
		let error_count = total_count - success_count;

		let oldest_timestamp = entries.front().map(|e| e.timestamp);
		let newest_timestamp = entries.back().map(|e| e.timestamp);

		HistoryStats {
			total_count,
			success_count,
			error_count,
			oldest_timestamp,
			newest_timestamp,
		}
	}

	/// Clear all history
	///
	/// # Examples
	///
	/// ```
	/// # use reinhardt_core::signals::history::{SignalHistory, HistoryConfig};
	/// # use reinhardt_core::signals::{Signal, SignalName};
	/// # use serde::{Deserialize, Serialize};
	/// # #[derive(Clone, Serialize, Deserialize)]
	/// # struct Event { id: i32 }
	/// # let signal = Signal::<Event>::new(SignalName::custom("events"));
	/// # let history = SignalHistory::new(signal, HistoryConfig::new());
	/// history.clear();
	/// assert_eq!(history.stats().total_count(), 0);
	/// ```
	pub fn clear(&self) {
		self.entries.write().clear();
	}

	/// Get access to the underlying signal
	pub fn signal(&self) -> &Signal<T> {
		&self.signal
	}
}

impl<T: Send + Sync + Clone + 'static> Clone for SignalHistory<T> {
	fn clone(&self) -> Self {
		Self {
			signal: self.signal.clone(),
			config: self.config.clone(),
			entries: Arc::clone(&self.entries),
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::signals::SignalName;
	use std::sync::atomic::{AtomicUsize, Ordering};

	#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
	struct TestEvent {
		id: i32,
		message: String,
	}

	#[test]
	fn test_history_config() {
		let config = HistoryConfig::new()
			.with_max_entries(500)
			.with_ttl(Duration::from_secs(3600))
			.with_errors_only(true);

		assert_eq!(config.max_entries(), 500);
		assert_eq!(config.ttl(), Some(Duration::from_secs(3600)));
		assert!(config.errors_only());
	}

	#[test]
	fn test_history_entry() {
		let entry = HistoryEntry::new(
			TestEvent {
				id: 1,
				message: "test".to_string(),
			},
			true,
		);

		assert!(entry.success());
		assert!(entry.error().is_none());
		assert!(entry.age() < Duration::from_secs(1));
	}

	#[tokio::test]
	async fn test_signal_history_basic() {
		let signal = Signal::<TestEvent>::new(SignalName::custom("test_history"));
		let config = HistoryConfig::new();
		let history = SignalHistory::new(signal.clone(), config);

		let counter = Arc::new(AtomicUsize::new(0));
		let counter_clone = Arc::clone(&counter);

		signal.connect(move |_event| {
			let counter = Arc::clone(&counter_clone);
			async move {
				counter.fetch_add(1, Ordering::SeqCst);
				Ok(())
			}
		});

		// Send some events
		for i in 0..5 {
			history
				.send(TestEvent {
					id: i,
					message: format!("Event {}", i),
				})
				.await
				.unwrap();
		}

		// Check history
		let stats = history.stats();
		assert_eq!(stats.total_count(), 5);
		assert_eq!(stats.success_count(), 5);
		assert_eq!(stats.error_count(), 0);
		assert_eq!(stats.success_rate(), 100.0);

		// Check counter
		assert_eq!(counter.load(Ordering::SeqCst), 5);
	}

	#[tokio::test]
	async fn test_history_max_entries() {
		let signal = Signal::<TestEvent>::new(SignalName::custom("test_max"));
		let config = HistoryConfig::new().with_max_entries(3);
		let history = SignalHistory::new(signal, config);

		// Send 5 events
		for i in 0..5 {
			history
				.send(TestEvent {
					id: i,
					message: "test".to_string(),
				})
				.await
				.unwrap();
		}

		// Should only keep last 3
		let all = history.get_all();
		assert_eq!(all.len(), 3);
		assert_eq!(all[0].payload.id, 2);
		assert_eq!(all[1].payload.id, 3);
		assert_eq!(all[2].payload.id, 4);
	}

	#[tokio::test]
	async fn test_history_errors_only() {
		let signal = Signal::<TestEvent>::new(SignalName::custom("test_errors"));
		let config = HistoryConfig::new().with_errors_only(true);
		let history = SignalHistory::new(signal.clone(), config);

		// Connect a receiver that always fails
		signal.connect(|_event| async move { Err(SignalError::new("Test error")) });

		// Send some events (all will fail)
		for i in 0..3 {
			let _ = history
				.send(TestEvent {
					id: i,
					message: "test".to_string(),
				})
				.await;
		}

		// All should be recorded (errors_only mode)
		let stats = history.stats();
		assert_eq!(stats.total_count(), 3);
		assert_eq!(stats.error_count(), 3);
	}

	#[tokio::test]
	async fn test_history_get_recent() {
		let signal = Signal::<TestEvent>::new(SignalName::custom("test_recent"));
		let history = SignalHistory::new(signal, HistoryConfig::new());

		for i in 0..10 {
			history
				.send(TestEvent {
					id: i,
					message: "test".to_string(),
				})
				.await
				.unwrap();
		}

		let recent = history.get_recent(3);
		assert_eq!(recent.len(), 3);

		// Most recent first
		assert_eq!(recent[0].payload.id, 9);
		assert_eq!(recent[1].payload.id, 8);
		assert_eq!(recent[2].payload.id, 7);
	}

	#[tokio::test]
	async fn test_history_get_errors() {
		let signal = Signal::<TestEvent>::new(SignalName::custom("test_get_errors"));
		let history = SignalHistory::new(signal.clone(), HistoryConfig::new());

		let fail_on_odd = Arc::new(AtomicUsize::new(0));
		let fail_clone = Arc::clone(&fail_on_odd);

		signal.connect(move |event| {
			let fail = Arc::clone(&fail_clone);
			async move {
				if event.id % 2 == 1 {
					fail.fetch_add(1, Ordering::SeqCst);
					Err(SignalError::new("Odd number"))
				} else {
					Ok(())
				}
			}
		});

		// Send 10 events (odd ones will fail)
		for i in 0..10 {
			let _ = history
				.send(TestEvent {
					id: i,
					message: "test".to_string(),
				})
				.await;
		}

		let errors = history.get_errors();
		assert_eq!(errors.len(), 5); // 1, 3, 5, 7, 9
		assert!(errors.iter().all(|e| !e.success));
	}

	#[tokio::test]
	async fn test_history_clear() {
		let signal = Signal::<TestEvent>::new(SignalName::custom("test_clear"));
		let history = SignalHistory::new(signal, HistoryConfig::new());

		for i in 0..5 {
			history
				.send(TestEvent {
					id: i,
					message: "test".to_string(),
				})
				.await
				.unwrap();
		}

		assert_eq!(history.stats().total_count(), 5);

		history.clear();
		assert_eq!(history.stats().total_count(), 0);
	}
}