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
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
// ABOUTME: Real-time ADS-B data ingestion service with PiAware polling
// ABOUTME: Fetches aircraft.json snapshots, normalizes data, and populates database

use crate::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig, CircuitState};
use crate::detect::behavior::BehaviorDetectionService;
use crate::detect::identity::IdentityDetectionService;
use crate::detect::signal::SignalDetectionService;
use crate::detect::temporal::TemporalDetectionService;
use crate::error::Result;
use crate::ingestion::{normalize, piaware};
use crate::store::{observations, sessions};
use sqlx::SqlitePool;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::time::{sleep, Duration, Instant};
use tracing::{debug, error, info, warn, Instrument, Span};

/// Run continuous ingestion loop fetching from PiAware and updating database
#[allow(dead_code)] // Will be used in main.rs
pub async fn run_ingestion(pool: SqlitePool, url: String, interval_ms: u64) -> Result<()> {
    let circuit_config = CircuitBreakerConfig::default();
    run_ingestion_with_circuit_breaker(pool, url, interval_ms, circuit_config).await
}

/// Run continuous ingestion loop with circuit breaker configuration
#[allow(dead_code)] // Will be used in main.rs
pub async fn run_ingestion_with_circuit_breaker(
    pool: SqlitePool,
    url: String,
    interval_ms: u64,
    circuit_config: CircuitBreakerConfig,
) -> Result<()> {
    run_ingestion_with_detection_and_circuit_breaker(
        pool,
        url,
        interval_ms,
        circuit_config,
        None,
        None,
        None,
        None,
    )
    .await
}

/// Run continuous ingestion loop with temporal, signal, identity, and behavioral anomaly detection
#[allow(dead_code)]
pub async fn run_ingestion_with_detection(
    pool: SqlitePool,
    url: String,
    interval_ms: u64,
    temporal_service: Option<TemporalDetectionService>,
    signal_service: Option<SignalDetectionService>,
    identity_service: Option<IdentityDetectionService>,
    behavior_service: Option<BehaviorDetectionService>,
) -> Result<()> {
    let circuit_config = CircuitBreakerConfig::default();
    run_ingestion_with_detection_and_circuit_breaker(
        pool,
        url,
        interval_ms,
        circuit_config,
        temporal_service,
        signal_service,
        identity_service,
        behavior_service,
    )
    .await
}

/// Run continuous ingestion loop with circuit breaker and anomaly detection
#[allow(clippy::too_many_arguments)]
pub async fn run_ingestion_with_detection_and_circuit_breaker(
    pool: SqlitePool,
    url: String,
    interval_ms: u64,
    circuit_config: CircuitBreakerConfig,
    temporal_service: Option<TemporalDetectionService>,
    signal_service: Option<SignalDetectionService>,
    identity_service: Option<IdentityDetectionService>,
    behavior_service: Option<BehaviorDetectionService>,
) -> Result<()> {
    info!("Starting ADS-B ingestion service");
    info!("PiAware URL: {}", url);
    info!("Poll interval: {}ms", interval_ms);
    info!(
        "Circuit breaker config: failure_threshold={}, timeout_ms={}, success_threshold={}",
        circuit_config.failure_threshold,
        circuit_config.timeout_ms,
        circuit_config.success_threshold
    );

    // Create circuit breaker for PiAware requests
    let circuit_breaker = Arc::new(CircuitBreaker::new("PiAware".to_string(), circuit_config));

    let mut backoff_delay = 1000u64; // Start with 1 second backoff
    let max_backoff = 60000u64; // Max 60 second backoff

    loop {
        let loop_start = Instant::now();
        let span = Span::current();

        match run_ingestion_tick_with_circuit_breaker(
            &pool,
            &url,
            circuit_breaker.clone(),
            temporal_service.as_ref(),
            signal_service.as_ref(),
            identity_service.as_ref(),
            behavior_service.as_ref(),
        )
        .instrument(span)
        .await
        {
            Ok(count) => {
                let cycle_time = loop_start.elapsed();
                debug!(
                    "Processed {} aircraft observations in {:?} ({:.2} aircraft/sec)",
                    count,
                    cycle_time,
                    count as f64 / cycle_time.as_secs_f64()
                );
                backoff_delay = 1000; // Reset backoff on success

                // Log circuit breaker state on successful cycles
                let cb_state = circuit_breaker.get_state();
                if cb_state != CircuitState::Closed {
                    info!(
                        "Circuit breaker state: {:?}, failures: {}, successes: {}",
                        cb_state,
                        circuit_breaker.get_failure_count(),
                        circuit_breaker.get_success_count()
                    );
                }

                // Performance warning if cycle exceeds poll interval
                if cycle_time.as_millis() > interval_ms.into() {
                    warn!(
                        "Performance bottleneck: Cycle time {}ms exceeds poll interval {}ms",
                        cycle_time.as_millis(),
                        interval_ms
                    );
                }
            }
            Err(e) => {
                let cb_state = circuit_breaker.get_state();
                let cb_failures = circuit_breaker.get_failure_count();

                match cb_state {
                    CircuitState::Open => {
                        warn!(
                            "Circuit breaker is OPEN (failures: {}). Ingestion paused - error: {}",
                            cb_failures, e
                        );
                        info!("Waiting for circuit breaker timeout before retry");
                    }
                    _ => {
                        warn!(
                            "Ingestion tick failed (CB state: {:?}, failures: {}): {}",
                            cb_state, cb_failures, e
                        );
                        error!("Backing off for {}ms before retry", backoff_delay);
                        backoff_delay = std::cmp::min(backoff_delay * 2, max_backoff);
                    }
                }

                sleep(Duration::from_millis(backoff_delay)).await;
                continue;
            }
        }

        // Sleep for remaining interval time
        let elapsed = loop_start.elapsed();
        let interval_duration = Duration::from_millis(interval_ms);
        if elapsed < interval_duration {
            sleep(interval_duration - elapsed).await;
        }
    }
}

/// Run a single ingestion tick: fetch, normalize, store, and update sessions
#[allow(dead_code)] // Used in tests
async fn run_ingestion_tick(pool: &SqlitePool, url: &str) -> Result<usize> {
    run_ingestion_tick_with_detection(pool, url, None, None, None, None).await
}

/// Run a single ingestion tick with circuit breaker and anomaly detection
#[allow(dead_code)] // Used in tests and integration tests
pub async fn run_ingestion_tick_with_circuit_breaker(
    pool: &SqlitePool,
    url: &str,
    circuit_breaker: Arc<CircuitBreaker>,
    temporal_service: Option<&TemporalDetectionService>,
    signal_service: Option<&SignalDetectionService>,
    identity_service: Option<&IdentityDetectionService>,
    behavior_service: Option<&BehaviorDetectionService>,
) -> Result<usize> {
    // Check circuit breaker before attempting request
    if !circuit_breaker.should_allow_request() {
        return Err(crate::error::Error::Generic(
            "Circuit breaker is open - requests are blocked".to_string(),
        ));
    }

    let snapshot = piaware_fetch_with_circuit_breaker(url, circuit_breaker).await?;

    process_snapshot_data(
        pool,
        snapshot,
        temporal_service,
        signal_service,
        identity_service,
        behavior_service,
    )
    .await
}

/// Run a single ingestion tick with temporal, signal, identity, and behavioral detection
#[allow(dead_code)] // Used in tests and integration tests
pub async fn run_ingestion_tick_with_detection(
    pool: &SqlitePool,
    url: &str,
    temporal_service: Option<&TemporalDetectionService>,
    signal_service: Option<&SignalDetectionService>,
    identity_service: Option<&IdentityDetectionService>,
    behavior_service: Option<&BehaviorDetectionService>,
) -> Result<usize> {
    // Create a default circuit breaker for backwards compatibility
    let circuit_breaker = Arc::new(CircuitBreaker::new(
        "PiAware-Legacy".to_string(),
        CircuitBreakerConfig::default(),
    ));

    run_ingestion_tick_with_circuit_breaker(
        pool,
        url,
        circuit_breaker,
        temporal_service,
        signal_service,
        identity_service,
        behavior_service,
    )
    .await
}

/// Fetch PiAware data with circuit breaker protection
async fn piaware_fetch_with_circuit_breaker(
    url: &str,
    circuit_breaker: Arc<CircuitBreaker>,
) -> Result<piaware::AircraftSnapshot> {
    debug!("Attempting PiAware fetch through circuit breaker");

    // Use a closure that returns the required error type
    let fetch_operation = || async {
        match piaware::fetch_snapshot(url).await {
            Ok(snapshot) => Ok(snapshot),
            Err(e) => Err(Box::new(e) as Box<dyn std::error::Error + Send + Sync>),
        }
    };

    // Execute through circuit breaker
    match circuit_breaker.execute(fetch_operation).await {
        Ok(snapshot) => Ok(snapshot),
        Err(crate::circuit_breaker::CircuitBreakerError::CircuitOpen) => {
            error!("PiAware circuit breaker is open");
            Err(crate::error::Error::Generic(
                "Circuit breaker is open - PiAware requests are blocked".to_string(),
            ))
        }
        Err(crate::circuit_breaker::CircuitBreakerError::OperationFailed(inner)) => {
            error!("PiAware fetch failed: {}", inner);
            Err(crate::error::Error::Generic(format!(
                "PiAware fetch failed: {}",
                inner
            )))
        }
    }
}

/// Process snapshot data through the ingestion pipeline
async fn process_snapshot_data(
    pool: &SqlitePool,
    snapshot: piaware::AircraftSnapshot,
    temporal_service: Option<&TemporalDetectionService>,
    signal_service: Option<&SignalDetectionService>,
    identity_service: Option<&IdentityDetectionService>,
    behavior_service: Option<&BehaviorDetectionService>,
) -> Result<usize> {
    let aircraft_count = snapshot.aircraft.len();
    if aircraft_count == 0 {
        debug!("No aircraft in snapshot");
        return Ok(0);
    }

    debug!("Processing {} aircraft from PiAware", aircraft_count);

    // Get current timestamp
    let now_ms = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_millis() as i64;

    // Normalize entries to observations
    let normalize_span = tracing::span!(tracing::Level::DEBUG, "normalize_data");
    let _guard = normalize_span.enter();

    let mut observations = Vec::with_capacity(aircraft_count);
    for entry in &snapshot.aircraft {
        let obs = normalize::normalize_entry(now_ms, entry);
        observations.push(obs);
    }

    debug!("Normalized {} observations", observations.len());
    drop(_guard);

    // Insert observations using high-performance batch operation
    let insert_span = tracing::span!(tracing::Level::DEBUG, "batch_insert_observations");
    let _guard = insert_span.enter();

    let inserted = observations::insert_observations(pool, &observations).await?;
    debug!("Batch inserted {} observations", inserted);
    drop(_guard);

    // Batch upsert sessions for maximum performance and run anomaly detection
    let session_span = tracing::span!(tracing::Level::DEBUG, "batch_upsert_sessions");
    let _guard = session_span.enter();

    // Use high-performance batch session processing
    let mut obs_clone = observations.to_vec();
    sessions::batch_upsert_sessions_from_observations(pool, &mut obs_clone).await?;
    debug!("Batch processed {} sessions", obs_clone.len());

    // Update original observations with calculated message rates
    for (original, updated) in observations.iter_mut().zip(obs_clone.iter()) {
        original.msg_rate_hz = updated.msg_rate_hz;
    }
    drop(_guard);

    // Run anomaly detection on all processed observations
    let detection_span = tracing::span!(tracing::Level::DEBUG, "anomaly_detection");
    let _guard = detection_span.enter();
    let observation_count = observations.len();

    for obs in observations.iter().cloned() {
        // Run temporal detection if service is available
        if let Some(temporal) = temporal_service {
            temporal.process_observation(obs.clone());
        }

        // Run signal detection if service is available
        if let Some(signal) = signal_service {
            signal.process_observation(obs.clone());
        }

        // Run identity detection if service is available
        if let Some(identity) = identity_service {
            identity.process_observation(obs.clone());
        }

        // Run behavioral detection if service is available
        if let Some(behavior) = behavior_service {
            behavior.process_observation(obs);
        }
    }

    debug!(
        "Completed anomaly detection for {} observations",
        observation_count
    );
    drop(_guard);

    info!(
        "Ingestion tick completed: {} aircraft processed",
        aircraft_count
    );

    Ok(aircraft_count)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::store::connect_and_migrate;
    use axum::{http::StatusCode, response::Json, routing::get, Router};
    use serde_json::json;
    use std::sync::{Arc, Mutex};
    use tempfile::TempDir;
    use tokio::net::TcpListener;
    use tokio::time::{sleep, Duration};

    async fn create_test_pool() -> (SqlitePool, TempDir) {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let pool = connect_and_migrate(db_path.to_str().unwrap(), true)
            .await
            .unwrap();
        (pool, temp_dir)
    }

    #[tokio::test]
    async fn test_run_ingestion_tick_with_data() {
        let (pool, _temp_dir) = create_test_pool().await;

        // Create test HTTP server that serves aircraft JSON
        let _response_data = json!({
            "now": 1641024000.5,
            "messages": 12345,
            "aircraft": [
                {
                    "hex": "abc123",
                    "flight": "UAL456",
                    "lat": 40.7128,
                    "lon": -74.0060,
                    "alt_baro": 35000,
                    "gs": 450.2,
                    "rssi": -45.5,
                    "messages": 1000
                },
                {
                    "hex": "def456",
                    "flight": "DAL789",
                    "lat": 34.0522,
                    "lon": -118.2437,
                    "alt_baro": 28000,
                    "gs": 380.0,
                    "rssi": -52.1,
                    "messages": 750
                }
            ]
        });

        async fn handler() -> std::result::Result<Json<serde_json::Value>, StatusCode> {
            Ok(Json(json!({
                "now": 1641024000.5,
                "messages": 12345,
                "aircraft": [
                    {
                        "hex": "abc123",
                        "flight": "UAL456",
                        "lat": 40.7128,
                        "lon": -74.0060,
                        "alt_baro": 35000,
                        "gs": 450.2,
                        "rssi": -45.5,
                        "messages": 1000
                    },
                    {
                        "hex": "def456",
                        "flight": "DAL789",
                        "lat": 34.0522,
                        "lon": -118.2437,
                        "alt_baro": 28000,
                        "gs": 380.0,
                        "rssi": -52.1,
                        "messages": 750
                    }
                ]
            })))
        }

        let app = Router::new().route("/data/aircraft.json", get(handler));

        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();

        tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });

        // Give server time to start
        sleep(Duration::from_millis(10)).await;

        let url = format!("http://{}/data/aircraft.json", addr);

        // Run ingestion tick
        let count = run_ingestion_tick(&pool, &url).await.unwrap();
        assert_eq!(count, 2);

        // Verify observations were inserted (normalize converts hex to uppercase)
        let observations = observations::list_observations_by_hex(&pool, "ABC123", 10)
            .await
            .unwrap();
        assert_eq!(observations.len(), 1);
        assert_eq!(observations[0].flight, Some("UAL456".to_string()));

        // Verify sessions were created
        let sessions = sessions::list_sessions(&pool, 10).await.unwrap();
        assert_eq!(
            sessions.len(),
            2,
            "Expected 2 sessions, found {}: {:?}",
            sessions.len(),
            sessions.iter().map(|s| &s.hex).collect::<Vec<_>>()
        );

        // Find ABC123 session
        let abc_session = sessions
            .iter()
            .find(|s| s.hex == "ABC123")
            .expect("ABC123 session should exist");
        assert_eq!(abc_session.flight, Some("UAL456".to_string()));
        assert_eq!(abc_session.message_count, 1);
    }

    #[tokio::test]
    async fn test_circuit_breaker_functionality() {
        let (pool, _temp_dir) = create_test_pool().await;

        // Create a circuit breaker that opens quickly
        let circuit_config = CircuitBreakerConfig {
            failure_threshold: 2,
            timeout_ms: 100, // Short timeout for test
            success_threshold: 1,
        };
        let circuit_breaker = Arc::new(CircuitBreaker::new("test".to_string(), circuit_config));

        // Test error server
        async fn error_handler() -> std::result::Result<Json<serde_json::Value>, StatusCode> {
            Err(StatusCode::INTERNAL_SERVER_ERROR)
        }

        let app = Router::new().route("/data/aircraft.json", get(error_handler));
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();

        tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });

        sleep(Duration::from_millis(10)).await;
        let url = format!("http://{}/data/aircraft.json", addr);

        // First failure
        let result = run_ingestion_tick_with_circuit_breaker(
            &pool,
            &url,
            circuit_breaker.clone(),
            None,
            None,
            None,
            None,
        )
        .await;
        assert!(result.is_err());
        assert_eq!(circuit_breaker.get_state(), CircuitState::Closed);

        // Second failure should open circuit
        let result = run_ingestion_tick_with_circuit_breaker(
            &pool,
            &url,
            circuit_breaker.clone(),
            None,
            None,
            None,
            None,
        )
        .await;
        assert!(result.is_err());
        assert_eq!(circuit_breaker.get_state(), CircuitState::Open);

        // Third request should be blocked immediately
        let result = run_ingestion_tick_with_circuit_breaker(
            &pool,
            &url,
            circuit_breaker.clone(),
            None,
            None,
            None,
            None,
        )
        .await;
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Circuit breaker is open"));
    }

    #[tokio::test]
    async fn test_circuit_breaker_recovery() {
        let (pool, _temp_dir) = create_test_pool().await;

        // Create circuit breaker with very short timeout
        let circuit_config = CircuitBreakerConfig {
            failure_threshold: 1,
            timeout_ms: 50,
            success_threshold: 1,
        };
        let circuit_breaker = Arc::new(CircuitBreaker::new(
            "recovery_test".to_string(),
            circuit_config,
        ));

        // Force circuit open
        circuit_breaker.force_open();
        assert_eq!(circuit_breaker.get_state(), CircuitState::Open);

        // Wait for timeout
        sleep(Duration::from_millis(60)).await;

        // Create success server
        async fn success_handler() -> std::result::Result<Json<serde_json::Value>, StatusCode> {
            Ok(Json(json!({
                "now": 1641024000.0,
                "messages": 0,
                "aircraft": []
            })))
        }

        let app = Router::new().route("/data/aircraft.json", get(success_handler));
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();

        tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });

        sleep(Duration::from_millis(10)).await;
        let url = format!("http://{}/data/aircraft.json", addr);

        // Should transition to half-open and succeed
        let result = run_ingestion_tick_with_circuit_breaker(
            &pool,
            &url,
            circuit_breaker.clone(),
            None,
            None,
            None,
            None,
        )
        .await;
        assert!(result.is_ok());
        assert_eq!(circuit_breaker.get_state(), CircuitState::Closed);
    }

    #[tokio::test]
    async fn test_integration_two_ticks() {
        let (pool, _temp_dir) = create_test_pool().await;

        // State to track which response to serve
        let response_index = Arc::new(Mutex::new(0));

        let response_index_clone = response_index.clone();
        let handler = move |(): ()| {
            let response_index = response_index_clone.clone();
            async move {
                let mut idx = response_index.lock().unwrap();
                let current_idx = *idx;
                *idx += 1;

                if current_idx == 0 {
                    // First response
                    Ok::<Json<serde_json::Value>, StatusCode>(Json(json!({
                        "now": 1641024000.0,
                        "messages": 12345,
                        "aircraft": [
                            {
                                "hex": "abc123",
                                "flight": "UAL456",
                                "lat": 40.7128,
                                "lon": -74.0060,
                                "alt_baro": 35000,
                                "gs": 450.0,
                                "rssi": -45.5,
                                "messages": 100
                            }
                        ]
                    })))
                } else {
                    // Second response - same aircraft, more messages
                    Ok(Json(json!({
                        "now": 1641024001.0,
                        "messages": 12355,
                        "aircraft": [
                            {
                                "hex": "abc123",
                                "flight": "UAL456",
                                "lat": 40.7129,
                                "lon": -74.0061,
                                "alt_baro": 35100,
                                "gs": 451.0,
                                "rssi": -45.0,
                                "messages": 110
                            }
                        ]
                    })))
                }
            }
        };

        let app = Router::new().route("/data/aircraft.json", get(handler));

        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();

        tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });

        sleep(Duration::from_millis(10)).await;

        let url = format!("http://{}/data/aircraft.json", addr);

        // First tick
        let count1 = run_ingestion_tick(&pool, &url).await.unwrap();
        assert_eq!(count1, 1);

        // Wait a bit to simulate real polling interval
        sleep(Duration::from_millis(300)).await;

        // Second tick
        let count2 = run_ingestion_tick(&pool, &url).await.unwrap();
        assert_eq!(count2, 1);

        // Verify we have 2 observations for the same aircraft
        let observations = observations::list_observations_by_hex(&pool, "ABC123", 10)
            .await
            .unwrap();
        assert_eq!(observations.len(), 2);

        // Verify session has been updated (should still be only 1 session)
        let sessions = sessions::list_sessions(&pool, 10).await.unwrap();
        assert_eq!(sessions.len(), 1);

        let session = &sessions[0];
        assert_eq!(session.hex, "ABC123");
        // Message count should be 1 + 10 (delta from 100->110)
        assert_eq!(session.message_count, 11);

        // Check that the second observation has msg_rate_hz calculated
        // (This would be populated during session upsert)
        let _latest_obs = &observations[0]; // Should be ordered by ts_ms DESC
                                            // Note: msg_rate_hz would be calculated during upsert but isn't persisted to DB
    }

    #[tokio::test]
    async fn test_run_ingestion_tick_empty_response() {
        let (pool, _temp_dir) = create_test_pool().await;

        async fn empty_handler() -> std::result::Result<Json<serde_json::Value>, StatusCode> {
            Ok(Json(json!({
                "now": 1641024000.0,
                "messages": 0,
                "aircraft": []
            })))
        }

        let app = Router::new().route("/data/aircraft.json", get(empty_handler));

        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();

        tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });

        sleep(Duration::from_millis(10)).await;

        let url = format!("http://{}/data/aircraft.json", addr);

        // Run ingestion tick with empty data
        let count = run_ingestion_tick(&pool, &url).await.unwrap();
        assert_eq!(count, 0);

        // Verify no data was inserted
        let sessions = sessions::list_sessions(&pool, 10).await.unwrap();
        assert_eq!(sessions.len(), 0);
    }

    #[tokio::test]
    async fn test_run_ingestion_tick_server_error() {
        let (pool, _temp_dir) = create_test_pool().await;

        async fn error_handler() -> std::result::Result<Json<serde_json::Value>, StatusCode> {
            Err(StatusCode::INTERNAL_SERVER_ERROR)
        }

        let app = Router::new().route("/data/aircraft.json", get(error_handler));

        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();

        tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });

        sleep(Duration::from_millis(10)).await;

        let url = format!("http://{}/data/aircraft.json", addr);

        // Run ingestion tick - should fail
        let result = run_ingestion_tick(&pool, &url).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_circuit_breaker_stress_test() {
        let (pool, _temp_dir) = create_test_pool().await;

        // Create circuit breaker with aggressive settings for stress testing
        let circuit_config = CircuitBreakerConfig {
            failure_threshold: 3,
            timeout_ms: 100,
            success_threshold: 2,
        };
        let circuit_breaker = Arc::new(CircuitBreaker::new(
            "stress_test".to_string(),
            circuit_config,
        ));

        // Counter to simulate intermittent failures
        let request_count = Arc::new(std::sync::atomic::AtomicU32::new(0));

        let request_count_clone = request_count.clone();
        let handler = move |(): ()| {
            let request_count = request_count_clone.clone();
            async move {
                let count = request_count.fetch_add(1, std::sync::atomic::Ordering::SeqCst);

                // Fail first 3 requests, then succeed twice, then fail again
                if count < 3 || (count >= 5 && count < 8) {
                    Err::<Json<serde_json::Value>, StatusCode>(StatusCode::INTERNAL_SERVER_ERROR)
                } else {
                    Ok(Json(json!({
                        "now": 1641024000.0,
                        "messages": 0,
                        "aircraft": []
                    })))
                }
            }
        };

        let app = Router::new().route("/data/aircraft.json", get(handler));
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();

        tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });

        sleep(Duration::from_millis(10)).await;
        let url = format!("http://{}/data/aircraft.json", addr);

        // First 3 requests should fail and open the circuit
        for i in 1..=3 {
            let result = run_ingestion_tick_with_circuit_breaker(
                &pool,
                &url,
                circuit_breaker.clone(),
                None,
                None,
                None,
                None,
            )
            .await;
            assert!(result.is_err(), "Request {} should fail", i);
        }

        // Circuit should be open now
        assert_eq!(circuit_breaker.get_state(), CircuitState::Open);
        assert_eq!(circuit_breaker.get_failure_count(), 3);

        // Request should be blocked immediately without hitting server
        let result = run_ingestion_tick_with_circuit_breaker(
            &pool,
            &url,
            circuit_breaker.clone(),
            None,
            None,
            None,
            None,
        )
        .await;
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Circuit breaker is open"));

        // Wait for timeout to transition to half-open
        sleep(Duration::from_millis(150)).await;

        // Next requests should succeed and close circuit
        for i in 1..=2 {
            let result = run_ingestion_tick_with_circuit_breaker(
                &pool,
                &url,
                circuit_breaker.clone(),
                None,
                None,
                None,
                None,
            )
            .await;
            assert!(result.is_ok(), "Recovery request {} should succeed", i);
        }

        // Circuit should be closed now
        assert_eq!(circuit_breaker.get_state(), CircuitState::Closed);
        assert_eq!(circuit_breaker.get_failure_count(), 0);

        // Test that it opens again on subsequent failures
        for i in 1..=3 {
            let result = run_ingestion_tick_with_circuit_breaker(
                &pool,
                &url,
                circuit_breaker.clone(),
                None,
                None,
                None,
                None,
            )
            .await;
            assert!(result.is_err(), "Second round failure {} should fail", i);
        }

        // Should be open again
        assert_eq!(circuit_breaker.get_state(), CircuitState::Open);
    }

    #[tokio::test]
    async fn test_circuit_breaker_prevents_resource_exhaustion() {
        let (pool, _temp_dir) = create_test_pool().await;

        // Create circuit breaker with very low threshold
        let circuit_config = CircuitBreakerConfig {
            failure_threshold: 2,
            timeout_ms: 5000, // Long timeout to test blocking
            success_threshold: 1,
        };
        let circuit_breaker = Arc::new(CircuitBreaker::new(
            "exhaustion_test".to_string(),
            circuit_config,
        ));

        // Always failing server
        async fn always_fail() -> std::result::Result<Json<serde_json::Value>, StatusCode> {
            Err(StatusCode::INTERNAL_SERVER_ERROR)
        }

        let app = Router::new().route("/data/aircraft.json", get(always_fail));
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();

        tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });

        sleep(Duration::from_millis(10)).await;
        let url = format!("http://{}/data/aircraft.json", addr);

        // Make requests that will open the circuit
        for _ in 0..2 {
            let result = run_ingestion_tick_with_circuit_breaker(
                &pool,
                &url,
                circuit_breaker.clone(),
                None,
                None,
                None,
                None,
            )
            .await;
            assert!(result.is_err());
        }

        // Circuit should be open
        assert_eq!(circuit_breaker.get_state(), CircuitState::Open);

        // Time many blocked requests to ensure they return immediately
        let start = std::time::Instant::now();
        for _ in 0..10 {
            let result = run_ingestion_tick_with_circuit_breaker(
                &pool,
                &url,
                circuit_breaker.clone(),
                None,
                None,
                None,
                None,
            )
            .await;
            assert!(result.is_err());
            assert!(result
                .unwrap_err()
                .to_string()
                .contains("Circuit breaker is open"));
        }
        let elapsed = start.elapsed();

        // All 10 requests should complete very quickly (much less than timeout period)
        // since they're blocked by the circuit breaker, not waiting for network timeouts
        assert!(
            elapsed < Duration::from_millis(500),
            "Circuit breaker should prevent slow requests, took {:?}",
            elapsed
        );
    }
}