adsb-anomaly 0.2.2

A sophisticated real-time anomaly detection system for ADS-B aircraft data with multi-tier detection algorithms, real-time web dashboard, and production-grade architecture built in Rust
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
// ABOUTME: Temporal anomaly detection for message timing and frequency patterns
// ABOUTME: Detects rapid transmission rates and burst-after-silence patterns

use crate::config::AnalysisConfig;
use crate::model::{AircraftObservation, AnomalyCandidate, AnomalyType};
use dashmap::DashMap;
use serde_json::json;
use std::collections::VecDeque;
use std::sync::Arc;
use tokio::sync::mpsc;
use tracing::{debug, warn};

/// Ring buffer for storing recent observations per aircraft
#[derive(Debug, Clone)]
struct ObservationRingBuffer {
    observations: VecDeque<AircraftObservation>,
    max_size: usize,
    last_seen_ms: i64,
}

impl ObservationRingBuffer {
    fn new(max_size: usize) -> Self {
        Self {
            observations: VecDeque::with_capacity(max_size),
            max_size,
            last_seen_ms: 0,
        }
    }

    fn push(&mut self, obs: AircraftObservation) {
        self.last_seen_ms = obs.ts_ms;

        // Add to front of deque (most recent first)
        self.observations.push_front(obs);

        // Remove oldest if we exceed max size
        if self.observations.len() > self.max_size {
            self.observations.pop_back();
        }
    }

    fn get_recent_observations(&self) -> Vec<AircraftObservation> {
        self.observations.iter().cloned().collect()
    }

    fn get_last_gap_ms(&self, current_ts_ms: i64) -> Option<u64> {
        if self.last_seen_ms > 0 && current_ts_ms > self.last_seen_ms {
            Some((current_ts_ms - self.last_seen_ms) as u64)
        } else {
            None
        }
    }
}

/// Temporal detection service with ring buffers per aircraft
#[derive(Clone)]
pub struct TemporalDetectionService {
    buffers: Arc<DashMap<String, ObservationRingBuffer>>,
    config: Arc<AnalysisConfig>,
    alert_sender: mpsc::UnboundedSender<AnomalyCandidate>,
    window_size: usize,
    max_aircraft: usize,     // Maximum number of aircraft to track
    stale_threshold_ms: i64, // Time threshold for removing stale aircraft
}

impl TemporalDetectionService {
    /// Create new temporal detection service with memory management
    pub fn new(
        config: Arc<AnalysisConfig>,
        alert_sender: mpsc::UnboundedSender<AnomalyCandidate>,
        window_size: Option<usize>,
    ) -> Self {
        Self::new_with_limits(config, alert_sender, window_size, None, None)
    }

    /// Create new temporal detection service with memory limits
    pub fn new_with_limits(
        config: Arc<AnalysisConfig>,
        alert_sender: mpsc::UnboundedSender<AnomalyCandidate>,
        window_size: Option<usize>,
        max_aircraft: Option<usize>,
        stale_threshold_minutes: Option<u32>,
    ) -> Self {
        Self {
            buffers: Arc::new(DashMap::new()),
            config,
            alert_sender,
            window_size: window_size.unwrap_or(10), // Default 10 observations per aircraft
            max_aircraft: max_aircraft.unwrap_or(5000), // Default max 5000 aircraft
            stale_threshold_ms: stale_threshold_minutes.unwrap_or(30) as i64 * 60 * 1000, // Default 30 minutes
        }
    }

    /// Process a new observation and check for temporal anomalies
    pub fn process_observation(&self, obs: AircraftObservation) {
        let hex = obs.hex.clone();

        // Get or create ring buffer for this aircraft
        let mut entry = self
            .buffers
            .entry(hex.clone())
            .or_insert_with(|| ObservationRingBuffer::new(self.window_size));

        // Calculate gap before adding new observation
        let last_gap_ms = entry.get_last_gap_ms(obs.ts_ms);

        // Add observation to ring buffer
        entry.push(obs.clone());

        // Get current window for analysis
        let obs_window = entry.get_recent_observations();

        // Drop the entry to release the lock
        drop(entry);

        // Run temporal detection
        if let Some(anomaly) = detect_temporal(&obs_window, last_gap_ms, &self.config) {
            debug!(
                "Temporal anomaly detected for {}: {} (confidence: {:.2})",
                anomaly.hex, anomaly.subtype, anomaly.confidence
            );

            // Send anomaly to alert channel
            if let Err(e) = self.alert_sender.send(anomaly) {
                warn!("Failed to send temporal anomaly alert: {}", e);
            }
        }
    }

    /// Clean up stale aircraft and enforce memory limits
    #[allow(dead_code)] // Used in tests, will be used for production monitoring
    pub fn cleanup_and_manage_memory(&self, current_time_ms: i64) -> (usize, usize) {
        let initial_count = self.buffers.len();

        // First, remove stale aircraft
        let cutoff_time = current_time_ms - self.stale_threshold_ms;
        let mut stale_aircraft = Vec::new();

        for entry in self.buffers.iter() {
            let (hex, buffer) = entry.pair();
            if buffer.last_seen_ms < cutoff_time {
                stale_aircraft.push(hex.clone());
            }
        }

        for hex in &stale_aircraft {
            self.buffers.remove(hex);
        }

        let after_stale_cleanup = self.buffers.len();
        let stale_removed = initial_count - after_stale_cleanup;

        // If still over limit, remove oldest aircraft (LRU eviction)
        let mut lru_removed = 0;
        if after_stale_cleanup > self.max_aircraft {
            let excess_count = after_stale_cleanup - self.max_aircraft;

            // Collect aircraft with their last_seen times for LRU sorting
            let mut aircraft_times: Vec<(String, i64)> = self
                .buffers
                .iter()
                .map(|entry| {
                    let (hex, buffer) = entry.pair();
                    (hex.clone(), buffer.last_seen_ms)
                })
                .collect();

            // Sort by last_seen_ms ascending (oldest first)
            aircraft_times.sort_by_key(|(_, time)| *time);

            // Remove oldest aircraft
            for (hex, _) in aircraft_times.into_iter().take(excess_count) {
                self.buffers.remove(&hex);
                lru_removed += 1;
            }
        }

        if stale_removed > 0 || lru_removed > 0 {
            debug!(
                "Temporal memory cleanup: removed {} stale aircraft, {} by LRU eviction. Buffer count: {} -> {}",
                stale_removed, lru_removed, initial_count, self.buffers.len()
            );
        }

        (stale_removed, lru_removed)
    }

    /// Get current buffer stats for debugging and monitoring
    #[allow(dead_code)] // Will be used for monitoring/debugging
    pub fn get_buffer_stats(&self) -> Vec<(String, usize, i64)> {
        self.buffers
            .iter()
            .map(|entry| {
                let (hex, buffer) = entry.pair();
                (hex.clone(), buffer.observations.len(), buffer.last_seen_ms)
            })
            .collect()
    }

    /// Get memory usage statistics
    #[allow(dead_code)] // Used in tests, will be used for production monitoring
    pub fn get_memory_stats(&self) -> (usize, usize, usize) {
        let aircraft_count = self.buffers.len();
        let total_observations: usize = self
            .buffers
            .iter()
            .map(|entry| entry.value().observations.len())
            .sum();

        // Estimate memory usage (rough calculation)
        // Each observation ~200 bytes, each buffer ~100 bytes overhead
        let estimated_bytes = (total_observations * 200) + (aircraft_count * 100);

        (aircraft_count, total_observations, estimated_bytes)
    }

    /// Check if memory pressure cleanup is needed
    #[allow(dead_code)] // Used in tests, will be used for production monitoring
    pub fn needs_cleanup(&self, current_time_ms: i64) -> bool {
        let aircraft_count = self.buffers.len();

        // Always cleanup if over the limit
        if aircraft_count > self.max_aircraft {
            return true;
        }

        // Check for stale aircraft periodically
        let cutoff_time = current_time_ms - self.stale_threshold_ms;
        self.buffers
            .iter()
            .any(|entry| entry.value().last_seen_ms < cutoff_time)
    }
}

/// Detect temporal anomalies in aircraft message patterns
pub fn detect_temporal(
    obs_window: &[AircraftObservation],
    last_gap_ms: Option<u64>,
    config: &AnalysisConfig,
) -> Option<AnomalyCandidate> {
    if obs_window.is_empty() {
        return None;
    }

    let latest_obs = &obs_window[0]; // Assume sorted by timestamp descending
    let hex = latest_obs.hex.clone();

    // Check for burst after silence pattern first (higher priority)
    if let Some(gap_ms) = last_gap_ms {
        let max_gap_ms = config.max_session_gap_seconds * 1000;
        if gap_ms > max_gap_ms {
            // We had a long silence, now check if current rate is elevated
            if let Some(msg_rate_hz) = latest_obs.msg_rate_hz {
                // Consider it a burst if rate is above 50% of normal threshold
                let burst_threshold = config.max_messages_per_second * 0.5;
                if msg_rate_hz > burst_threshold {
                    return Some(
                        AnomalyCandidate::new(
                            hex,
                            AnomalyType::Temporal,
                            "burst_after_silence".to_string(),
                            0.8, // Slightly lower confidence as pattern is more complex
                        )
                        .with_details(json!({
                            "silence_duration_ms": gap_ms,
                            "max_gap_ms": max_gap_ms,
                            "burst_rate_hz": msg_rate_hz,
                            "burst_threshold": burst_threshold
                        }))
                        .with_trigger_observation(latest_obs.clone()),
                    );
                }
            }
        }
    }

    // Check for rapid transmission rate (only if no burst after silence detected)
    if let Some(msg_rate_hz) = latest_obs.msg_rate_hz {
        if msg_rate_hz > config.max_messages_per_second {
            return Some(
                AnomalyCandidate::new(
                    hex,
                    AnomalyType::Temporal,
                    "rapid_transmission".to_string(),
                    0.9, // High confidence for clear threshold violation
                )
                .with_details(json!({
                    "msg_rate_hz": msg_rate_hz,
                    "threshold": config.max_messages_per_second,
                    "violation_factor": if config.max_messages_per_second > 0.0 {
                        msg_rate_hz / config.max_messages_per_second
                    } else {
                        f64::INFINITY
                    }
                }))
                .with_trigger_observation(latest_obs.clone()),
            );
        }
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::AircraftObservation;
    use tokio::sync::mpsc;

    fn create_test_config() -> AnalysisConfig {
        AnalysisConfig {
            max_messages_per_second: 10.0,
            min_message_interval_ms: 50,
            max_session_gap_seconds: 600, // 10 minutes
            min_rssi_units: -120.0,
            max_rssi_units: -10.0,
            suspicious_rssi_units: -20.0,
            suspicious_callsigns: vec![],
            invalid_hex_patterns: vec![],
        }
    }

    fn create_test_observation(
        hex: &str,
        ts_ms: i64,
        msg_rate_hz: Option<f64>,
    ) -> AircraftObservation {
        AircraftObservation {
            id: None,
            ts_ms,
            hex: hex.to_string(),
            flight: Some("TEST123".to_string()),
            lat: Some(40.0),
            lon: Some(-74.0),
            altitude: Some(35000),
            gs: Some(450.0),
            rssi: Some(-45.0),
            msg_count_total: Some(1000),
            raw_json: "{}".to_string(),
            msg_rate_hz,
        }
    }

    #[test]
    fn test_detect_rapid_transmission() {
        let config = create_test_config();
        let obs = create_test_observation("ABC123", 1641024000000, Some(15.0)); // Above 10.0 threshold
        let obs_window = vec![obs];

        let result = detect_temporal(&obs_window, None, &config);

        assert!(result.is_some());
        let anomaly = result.unwrap();
        assert_eq!(anomaly.anomaly_type, AnomalyType::Temporal);
        assert_eq!(anomaly.subtype, "rapid_transmission");
        assert_eq!(anomaly.confidence, 0.9);
        assert_eq!(anomaly.hex, "ABC123");

        // Check details
        let details = anomaly.details.unwrap();
        assert_eq!(details["msg_rate_hz"], 15.0);
        assert_eq!(details["threshold"], 10.0);
        assert_eq!(details["violation_factor"], 1.5);
    }

    #[test]
    fn test_detect_burst_after_silence() {
        let config = create_test_config();
        let obs = create_test_observation("ABC123", 1641024000000, Some(6.0)); // Above 5.0 burst threshold
        let obs_window = vec![obs];

        // Simulate a gap of 15 minutes (900 seconds = 900000 ms)
        let gap_ms = 900000u64; // Exceeds 600 second threshold

        let result = detect_temporal(&obs_window, Some(gap_ms), &config);

        assert!(result.is_some());
        let anomaly = result.unwrap();
        assert_eq!(anomaly.anomaly_type, AnomalyType::Temporal);
        assert_eq!(anomaly.subtype, "burst_after_silence");
        assert_eq!(anomaly.confidence, 0.8);
        assert_eq!(anomaly.hex, "ABC123");

        // Check details
        let details = anomaly.details.unwrap();
        assert_eq!(details["silence_duration_ms"], 900000);
        assert_eq!(details["max_gap_ms"], 600000);
        assert_eq!(details["burst_rate_hz"], 6.0);
        assert_eq!(details["burst_threshold"], 5.0);
    }

    #[test]
    fn test_no_anomaly_normal_rate() {
        let config = create_test_config();
        let obs = create_test_observation("ABC123", 1641024000000, Some(2.0)); // Well below threshold
        let obs_window = vec![obs];

        let result = detect_temporal(&obs_window, None, &config);
        assert!(result.is_none());
    }

    #[test]
    fn test_no_anomaly_steady_rate() {
        let config = create_test_config();
        let obs = create_test_observation("ABC123", 1641024000000, Some(1.5)); // Steady low rate
        let obs_window = vec![obs];

        let result = detect_temporal(&obs_window, None, &config);
        assert!(result.is_none());
    }

    #[test]
    fn test_no_anomaly_short_gap_high_rate() {
        let config = create_test_config();
        let obs = create_test_observation("ABC123", 1641024000000, Some(6.0)); // Above burst threshold
        let obs_window = vec![obs];

        // Short gap (5 minutes = 300000 ms) - within acceptable range
        let gap_ms = 300000u64;

        let result = detect_temporal(&obs_window, Some(gap_ms), &config);
        assert!(result.is_none());
    }

    #[test]
    fn test_no_anomaly_long_gap_low_rate() {
        let config = create_test_config();
        let obs = create_test_observation("ABC123", 1641024000000, Some(2.0)); // Below burst threshold
        let obs_window = vec![obs];

        // Long gap but low rate after - not a burst
        let gap_ms = 900000u64;

        let result = detect_temporal(&obs_window, Some(gap_ms), &config);
        assert!(result.is_none());
    }

    #[test]
    fn test_no_anomaly_missing_msg_rate() {
        let config = create_test_config();
        let obs = create_test_observation("ABC123", 1641024000000, None); // No message rate available
        let obs_window = vec![obs];

        let result = detect_temporal(&obs_window, None, &config);
        assert!(result.is_none());
    }

    #[test]
    fn test_empty_window() {
        let config = create_test_config();
        let obs_window = vec![];

        let result = detect_temporal(&obs_window, None, &config);
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_temporal_detection_service() {
        let config = Arc::new(create_test_config());
        let (alert_sender, mut alert_receiver) = mpsc::unbounded_channel();
        let service = TemporalDetectionService::new(config, alert_sender, Some(5));

        // Send normal observation - should not trigger anomaly
        let obs1 = create_test_observation("ABC123", 1641024000000, Some(2.0));
        service.process_observation(obs1);

        // Give a moment for processing
        tokio::task::yield_now().await;

        // Should be no alerts
        assert!(alert_receiver.try_recv().is_err());

        // Send high rate observation - should trigger anomaly
        let obs2 = create_test_observation("ABC123", 1641024001000, Some(15.0));
        service.process_observation(obs2);

        // Give a moment for processing
        tokio::task::yield_now().await;

        // Should have an alert
        let alert = alert_receiver
            .try_recv()
            .expect("Should have received an alert");
        assert_eq!(alert.anomaly_type, AnomalyType::Temporal);
        assert_eq!(alert.subtype, "rapid_transmission");
        assert_eq!(alert.hex, "ABC123");
    }

    #[tokio::test]
    async fn test_burst_after_silence_detection() {
        let config = Arc::new(create_test_config());
        let (alert_sender, mut alert_receiver) = mpsc::unbounded_channel();
        let service = TemporalDetectionService::new(config, alert_sender, Some(5));

        // Send initial observation
        let obs1 = create_test_observation("DEF456", 1641024000000, Some(2.0));
        service.process_observation(obs1);

        // Wait a moment
        tokio::task::yield_now().await;
        assert!(alert_receiver.try_recv().is_err());

        // Send observation after long silence with elevated rate
        let obs2 = create_test_observation("DEF456", 1641024000000 + 900000, Some(6.0)); // 15 min gap
        service.process_observation(obs2);

        // Should have a burst after silence alert
        let alert = alert_receiver
            .try_recv()
            .expect("Should have received an alert");
        assert_eq!(alert.anomaly_type, AnomalyType::Temporal);
        assert_eq!(alert.subtype, "burst_after_silence");
        assert_eq!(alert.hex, "DEF456");
    }

    #[test]
    fn test_ring_buffer() {
        let mut buffer = ObservationRingBuffer::new(3);

        let obs1 = create_test_observation("ABC123", 1000, Some(2.0));
        let obs2 = create_test_observation("ABC123", 2000, Some(3.0));
        let obs3 = create_test_observation("ABC123", 3000, Some(4.0));
        let obs4 = create_test_observation("ABC123", 4000, Some(5.0));

        buffer.push(obs1);
        buffer.push(obs2);
        buffer.push(obs3);
        assert_eq!(buffer.observations.len(), 3);

        // Adding 4th should remove oldest
        buffer.push(obs4);
        assert_eq!(buffer.observations.len(), 3);

        // Most recent should be first
        let recent = buffer.get_recent_observations();
        assert_eq!(recent[0].ts_ms, 4000);
        assert_eq!(recent[1].ts_ms, 3000);
        assert_eq!(recent[2].ts_ms, 2000);
    }

    #[test]
    fn test_ring_buffer_gap_calculation() {
        let mut buffer = ObservationRingBuffer::new(5);

        let obs1 = create_test_observation("ABC123", 1000, Some(2.0));
        buffer.push(obs1);

        // Gap should be 500ms
        let gap = buffer.get_last_gap_ms(1500);
        assert_eq!(gap, Some(500));

        // No gap for earlier timestamp
        let gap2 = buffer.get_last_gap_ms(800);
        assert_eq!(gap2, None);
    }

    #[tokio::test]
    async fn test_temporal_service_memory_management() {
        let config = Arc::new(create_test_config());
        let (alert_sender, _alert_receiver) = mpsc::unbounded_channel();

        // Create service with small limits for testing
        let service = TemporalDetectionService::new_with_limits(
            config,
            alert_sender,
            Some(5),
            Some(3), // Max 3 aircraft
            Some(1), // 1 minute timeout
        );

        let base_time = 1641024000000;

        // Add observations for 5 aircraft (exceeds limit of 3)
        for i in 0..5 {
            let hex = format!("AIRCRAFT{:02}", i);
            let obs = create_test_observation(&hex, base_time + (i * 1000), Some(2.0));
            service.process_observation(obs);
        }

        let (aircraft_count, _total_obs, _estimated_bytes) = service.get_memory_stats();
        assert_eq!(aircraft_count, 5); // All should be present initially

        // Trigger memory cleanup - should remove 2 oldest aircraft (LRU)
        let (stale_removed, lru_removed) = service.cleanup_and_manage_memory(base_time + 5000);
        assert_eq!(stale_removed, 0); // None are stale yet
        assert_eq!(lru_removed, 2); // Should remove 2 to get to limit of 3

        let (aircraft_count_after, _, _) = service.get_memory_stats();
        assert_eq!(aircraft_count_after, 3); // Should be at limit

        // Verify the oldest aircraft were removed (AIRCRAFT00 and AIRCRAFT01)
        let stats = service.get_buffer_stats();
        let remaining_aircraft: Vec<String> = stats.into_iter().map(|(hex, _, _)| hex).collect();
        assert!(!remaining_aircraft.contains(&"AIRCRAFT00".to_string()));
        assert!(!remaining_aircraft.contains(&"AIRCRAFT01".to_string()));
        assert!(remaining_aircraft.contains(&"AIRCRAFT02".to_string()));
        assert!(remaining_aircraft.contains(&"AIRCRAFT03".to_string()));
        assert!(remaining_aircraft.contains(&"AIRCRAFT04".to_string()));
    }

    #[tokio::test]
    async fn test_temporal_service_stale_cleanup() {
        let config = Arc::new(create_test_config());
        let (alert_sender, _alert_receiver) = mpsc::unbounded_channel();

        // Create service with 1 minute timeout
        let service = TemporalDetectionService::new_with_limits(
            config,
            alert_sender,
            Some(5),
            Some(10), // High limit
            Some(1),  // 1 minute timeout
        );

        let base_time = 1641024000000;

        // Add old observations (2 minutes ago)
        let old_obs = create_test_observation("OLD_AIRCRAFT", base_time - 120000, Some(2.0));
        service.process_observation(old_obs);

        // Add recent observation
        let recent_obs = create_test_observation("NEW_AIRCRAFT", base_time, Some(2.0));
        service.process_observation(recent_obs);

        let (aircraft_count, _, _) = service.get_memory_stats();
        assert_eq!(aircraft_count, 2);

        // Trigger cleanup - should remove stale aircraft
        let (stale_removed, lru_removed) = service.cleanup_and_manage_memory(base_time);
        assert_eq!(stale_removed, 1); // Should remove 1 stale aircraft
        assert_eq!(lru_removed, 0);

        let (aircraft_count_after, _, _) = service.get_memory_stats();
        assert_eq!(aircraft_count_after, 1);

        // Verify only the recent aircraft remains
        let stats = service.get_buffer_stats();
        assert_eq!(stats.len(), 1);
        assert_eq!(stats[0].0, "NEW_AIRCRAFT");
    }

    #[tokio::test]
    async fn test_temporal_service_needs_cleanup() {
        let config = Arc::new(create_test_config());
        let (alert_sender, _alert_receiver) = mpsc::unbounded_channel();

        let service = TemporalDetectionService::new_with_limits(
            config,
            alert_sender,
            Some(5),
            Some(2), // Max 2 aircraft
            Some(1), // 1 minute timeout
        );

        let base_time = 1641024000000;

        // Should not need cleanup initially
        assert!(!service.needs_cleanup(base_time));

        // Add aircraft beyond limit
        for i in 0..3 {
            let hex = format!("AIRCRAFT{:02}", i);
            let obs = create_test_observation(&hex, base_time + (i * 1000), Some(2.0));
            service.process_observation(obs);
        }

        // Should need cleanup due to exceeding limit
        assert!(service.needs_cleanup(base_time));

        // Add stale aircraft
        let stale_obs = create_test_observation("STALE", base_time - 120000, Some(2.0));
        service.process_observation(stale_obs);

        // Should need cleanup due to stale aircraft
        assert!(service.needs_cleanup(base_time));
    }

    #[tokio::test]
    async fn test_memory_stats_calculation() {
        let config = Arc::new(create_test_config());
        let (alert_sender, _alert_receiver) = mpsc::unbounded_channel();

        let service = TemporalDetectionService::new_with_limits(
            config,
            alert_sender,
            Some(3), // 3 observations per aircraft
            Some(10),
            Some(30),
        );

        let base_time = 1641024000000;

        // Add multiple observations for multiple aircraft
        for aircraft in 0..2 {
            for obs in 0..3 {
                let hex = format!("PLANE{}", aircraft);
                let observation =
                    create_test_observation(&hex, base_time + (obs * 1000), Some(2.0));
                service.process_observation(observation);
            }
        }

        let (aircraft_count, total_observations, estimated_bytes) = service.get_memory_stats();
        assert_eq!(aircraft_count, 2);
        assert_eq!(total_observations, 6); // 2 aircraft * 3 observations each
        assert!(estimated_bytes > 0);

        // Memory should scale with observations
        assert!(estimated_bytes >= total_observations * 200); // Rough minimum
    }
}