oxirs-gql 0.2.4

GraphQL façade for OxiRS with automatic schema generation from RDF ontologies
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
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
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
//! Canary Release Integration
//!
//! This module provides canary release capabilities for gradually rolling out
//! new versions to a subset of traffic while monitoring for issues.
//!
//! ## Features
//!
//! - **Traffic Segmentation**: Route a percentage of traffic to canary instances
//! - **Automatic Promotion**: Gradually increase canary traffic based on metrics
//! - **Automatic Rollback**: Detect issues and rollback automatically
//! - **Metric-Based Analysis**: Use error rates, latency, and custom metrics
//! - **Feature Flags**: Combine canary releases with feature flag targeting
//! - **A/B Testing Integration**: Support for statistical significance testing
//!
//! ## Usage
//!
//! ```rust,ignore
//! use oxirs_gql::canary_release::{CanaryController, CanaryConfig, PromotionPolicy};
//!
//! let config = CanaryConfig {
//!     initial_percentage: 5,
//!     max_percentage: 50,
//!     ..Default::default()
//! };
//!
//! let controller = CanaryController::new(config);
//! controller.start_canary("v2.0.0").await?;
//! ```

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use tokio::sync::RwLock;

/// Canary release state
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CanaryState {
    /// No canary release active
    Inactive,
    /// Canary release starting
    Starting,
    /// Canary is receiving traffic
    Active,
    /// Canary is being promoted (increasing traffic)
    Promoting,
    /// Canary is paused for analysis
    Paused,
    /// Canary is being rolled back
    RollingBack,
    /// Canary has been fully promoted
    Promoted,
    /// Canary has been rolled back
    RolledBack,
    /// Canary release failed
    Failed,
}

/// Traffic routing strategy
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub enum RoutingStrategy {
    /// Random percentage-based routing
    #[default]
    Random,
    /// Hash-based routing (consistent for same user/session)
    HashBased { hash_key: String },
    /// Header-based routing
    HeaderBased {
        header_name: String,
        header_value: String,
    },
    /// Cookie-based routing
    CookieBased { cookie_name: String },
    /// Geographic routing
    Geographic { regions: Vec<String> },
    /// User segment routing
    UserSegment { segments: Vec<String> },
}

/// Promotion policy for automatic canary promotion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromotionPolicy {
    /// Percentage increments for promotion
    pub percentage_increments: Vec<u8>,
    /// Minimum duration at each stage before promotion
    pub stage_duration: Duration,
    /// Enable automatic promotion
    pub auto_promote: bool,
    /// Maximum error rate allowed (percentage)
    pub max_error_rate: f64,
    /// Maximum p99 latency allowed (milliseconds)
    pub max_latency_p99_ms: u64,
    /// Minimum number of requests before making decisions
    pub min_request_count: u64,
    /// Custom metric thresholds
    pub custom_thresholds: HashMap<String, f64>,
}

impl Default for PromotionPolicy {
    fn default() -> Self {
        Self {
            percentage_increments: vec![5, 10, 25, 50, 75, 100],
            stage_duration: Duration::from_secs(300), // 5 minutes per stage
            auto_promote: true,
            max_error_rate: 1.0,      // 1% error rate
            max_latency_p99_ms: 1000, // 1 second
            min_request_count: 100,
            custom_thresholds: HashMap::new(),
        }
    }
}

/// Rollback policy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RollbackPolicy {
    /// Enable automatic rollback
    pub auto_rollback: bool,
    /// Error rate threshold for immediate rollback
    pub error_rate_threshold: f64,
    /// Latency threshold for rollback (p99 in ms)
    pub latency_threshold_ms: u64,
    /// Number of consecutive failures before rollback
    pub failure_threshold: u32,
    /// Rollback immediately on first failure
    pub fail_fast: bool,
    /// Grace period before rollback decision
    pub grace_period: Duration,
}

impl Default for RollbackPolicy {
    fn default() -> Self {
        Self {
            auto_rollback: true,
            error_rate_threshold: 5.0,  // 5% error rate
            latency_threshold_ms: 5000, // 5 seconds
            failure_threshold: 3,
            fail_fast: false,
            grace_period: Duration::from_secs(60),
        }
    }
}

/// Canary configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CanaryConfig {
    /// Initial traffic percentage for canary
    pub initial_percentage: u8,
    /// Maximum traffic percentage for canary
    pub max_percentage: u8,
    /// Traffic routing strategy
    pub routing_strategy: RoutingStrategy,
    /// Promotion policy
    pub promotion_policy: PromotionPolicy,
    /// Rollback policy
    pub rollback_policy: RollbackPolicy,
    /// Analysis window size
    pub analysis_window: Duration,
    /// Metric collection interval
    pub metric_interval: Duration,
    /// Enable metric comparison with baseline
    pub compare_with_baseline: bool,
    /// Statistical significance level for A/B testing
    pub significance_level: f64,
}

impl Default for CanaryConfig {
    fn default() -> Self {
        Self {
            initial_percentage: 5,
            max_percentage: 100,
            routing_strategy: RoutingStrategy::default(),
            promotion_policy: PromotionPolicy::default(),
            rollback_policy: RollbackPolicy::default(),
            analysis_window: Duration::from_secs(300),
            metric_interval: Duration::from_secs(10),
            compare_with_baseline: true,
            significance_level: 0.95,
        }
    }
}

/// Metric snapshot for canary analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricSnapshot {
    /// Timestamp
    pub timestamp: SystemTime,
    /// Request count
    pub request_count: u64,
    /// Error count
    pub error_count: u64,
    /// Error rate (percentage)
    pub error_rate: f64,
    /// Latency p50 (ms)
    pub latency_p50_ms: u64,
    /// Latency p95 (ms)
    pub latency_p95_ms: u64,
    /// Latency p99 (ms)
    pub latency_p99_ms: u64,
    /// Custom metrics
    pub custom_metrics: HashMap<String, f64>,
}

impl Default for MetricSnapshot {
    fn default() -> Self {
        Self {
            timestamp: SystemTime::now(),
            request_count: 0,
            error_count: 0,
            error_rate: 0.0,
            latency_p50_ms: 0,
            latency_p95_ms: 0,
            latency_p99_ms: 0,
            custom_metrics: HashMap::new(),
        }
    }
}

/// Canary analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnalysisResult {
    /// Overall health score (0-100)
    pub health_score: f64,
    /// Whether canary passed all checks
    pub passed: bool,
    /// Recommendation
    pub recommendation: AnalysisRecommendation,
    /// Individual check results
    pub checks: Vec<AnalysisCheck>,
    /// Comparison with baseline (if available)
    pub baseline_comparison: Option<BaselineComparison>,
    /// Analysis timestamp
    pub timestamp: SystemTime,
}

/// Analysis recommendation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AnalysisRecommendation {
    /// Promote canary to next stage
    Promote,
    /// Keep canary at current stage
    Hold,
    /// Rollback canary
    Rollback,
    /// Need more data
    InsufficientData,
}

/// Individual analysis check
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnalysisCheck {
    /// Check name
    pub name: String,
    /// Check passed
    pub passed: bool,
    /// Actual value
    pub actual_value: f64,
    /// Threshold value
    pub threshold_value: f64,
    /// Description
    pub description: String,
}

/// Baseline comparison result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BaselineComparison {
    /// Error rate delta (canary - baseline)
    pub error_rate_delta: f64,
    /// Latency delta (canary - baseline) in ms
    pub latency_delta_ms: i64,
    /// Statistical significance
    pub statistically_significant: bool,
    /// Confidence level
    pub confidence_level: f64,
    /// Is canary performing better?
    pub is_better: bool,
}

/// Canary release information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CanaryRelease {
    /// Release ID
    pub id: String,
    /// Version being tested
    pub version: String,
    /// Current state
    pub state: CanaryState,
    /// Current traffic percentage
    pub traffic_percentage: u8,
    /// Current promotion stage index
    pub current_stage: usize,
    /// Start time
    pub started_at: SystemTime,
    /// Time at current stage
    pub stage_started_at: SystemTime,
    /// Canary metrics
    pub canary_metrics: MetricSnapshot,
    /// Baseline metrics
    pub baseline_metrics: MetricSnapshot,
    /// Analysis history
    pub analysis_history: Vec<AnalysisResult>,
    /// Metadata
    pub metadata: HashMap<String, String>,
}

impl CanaryRelease {
    fn new(id: String, version: String, initial_percentage: u8) -> Self {
        let now = SystemTime::now();
        Self {
            id,
            version,
            state: CanaryState::Starting,
            traffic_percentage: initial_percentage,
            current_stage: 0,
            started_at: now,
            stage_started_at: now,
            canary_metrics: MetricSnapshot::default(),
            baseline_metrics: MetricSnapshot::default(),
            analysis_history: Vec::new(),
            metadata: HashMap::new(),
        }
    }
}

/// Canary event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CanaryEvent {
    /// Canary started
    Started { release_id: String, version: String },
    /// Traffic percentage changed
    TrafficChanged {
        release_id: String,
        old_percentage: u8,
        new_percentage: u8,
    },
    /// Analysis completed
    AnalysisCompleted {
        release_id: String,
        result: AnalysisResult,
    },
    /// Stage promoted
    StagePromoted {
        release_id: String,
        new_stage: usize,
        new_percentage: u8,
    },
    /// Canary paused
    Paused { release_id: String, reason: String },
    /// Canary resumed
    Resumed { release_id: String },
    /// Rollback initiated
    RollbackInitiated { release_id: String, reason: String },
    /// Canary completed (fully promoted)
    Completed { release_id: String },
    /// Canary rolled back
    RolledBack { release_id: String },
    /// Canary failed
    Failed { release_id: String, error: String },
}

/// Traffic routing decision
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CanaryRoutingDecision {
    /// Route to canary?
    pub is_canary: bool,
    /// Version
    pub version: String,
    /// Weight (for weighted routing)
    pub weight: f64,
    /// Routing reason
    pub reason: String,
}

/// Internal state
struct ControllerState {
    /// Active canary release
    active_release: Option<CanaryRelease>,
    /// Release history
    release_history: Vec<CanaryRelease>,
    /// Event log
    events: Vec<(SystemTime, CanaryEvent)>,
    /// Collected latencies for baseline
    baseline_latencies: Vec<u64>,
    /// Collected latencies for canary
    canary_latencies: Vec<u64>,
    /// Consecutive failure count
    consecutive_failures: u32,
}

impl ControllerState {
    fn new() -> Self {
        Self {
            active_release: None,
            release_history: Vec::new(),
            events: Vec::new(),
            baseline_latencies: Vec::new(),
            canary_latencies: Vec::new(),
            consecutive_failures: 0,
        }
    }
}

/// Canary Release Controller
///
/// Manages canary releases including traffic routing, metric analysis,
/// automatic promotion, and rollback handling.
pub struct CanaryController {
    /// Configuration
    config: CanaryConfig,
    /// Internal state
    state: Arc<RwLock<ControllerState>>,
    /// Event handlers
    event_handlers: Arc<RwLock<Vec<Arc<dyn CanaryEventHandler + Send + Sync>>>>,
    /// Baseline version
    baseline_version: Arc<RwLock<String>>,
}

impl CanaryController {
    /// Create a new canary controller
    pub fn new(config: CanaryConfig) -> Self {
        Self {
            config,
            state: Arc::new(RwLock::new(ControllerState::new())),
            event_handlers: Arc::new(RwLock::new(Vec::new())),
            baseline_version: Arc::new(RwLock::new("baseline".to_string())),
        }
    }

    /// Set baseline version
    pub async fn set_baseline_version(&self, version: &str) {
        let mut baseline = self.baseline_version.write().await;
        *baseline = version.to_string();
    }

    /// Register an event handler
    pub async fn register_event_handler(&self, handler: Arc<dyn CanaryEventHandler + Send + Sync>) {
        let mut handlers = self.event_handlers.write().await;
        handlers.push(handler);
    }

    /// Emit a canary event
    async fn emit_event(&self, event: CanaryEvent) {
        let now = SystemTime::now();

        {
            let mut state = self.state.write().await;
            state.events.push((now, event.clone()));

            // Limit event history
            if state.events.len() > 1000 {
                state.events.drain(0..100);
            }
        }

        let handlers = self.event_handlers.read().await;
        for handler in handlers.iter() {
            handler.on_event(&event).await;
        }
    }

    /// Start a new canary release
    pub async fn start_canary(&self, version: &str) -> Result<String> {
        // Check if there's already an active canary
        {
            let state = self.state.read().await;
            if state.active_release.is_some() {
                return Err(anyhow!("Cannot start canary: another release is active"));
            }
        }

        let release_id = uuid::Uuid::new_v4().to_string();
        let release = CanaryRelease::new(
            release_id.clone(),
            version.to_string(),
            self.config.initial_percentage,
        );

        {
            let mut state = self.state.write().await;
            state.active_release = Some(release);
            state.consecutive_failures = 0;
            state.canary_latencies.clear();
        }

        // Mark as active
        {
            let mut state = self.state.write().await;
            if let Some(ref mut release) = state.active_release {
                release.state = CanaryState::Active;
            }
        }

        self.emit_event(CanaryEvent::Started {
            release_id: release_id.clone(),
            version: version.to_string(),
        })
        .await;

        Ok(release_id)
    }

    /// Get current canary status
    pub async fn get_status(&self) -> Option<CanaryRelease> {
        let state = self.state.read().await;
        state.active_release.clone()
    }

    /// Check if canary is active
    pub async fn is_active(&self) -> bool {
        let state = self.state.read().await;
        state
            .active_release
            .as_ref()
            .map(|r| r.state == CanaryState::Active || r.state == CanaryState::Promoting)
            .unwrap_or(false)
    }

    /// Record a request metric
    pub async fn record_metric(&self, is_canary: bool, latency_ms: u64, is_error: bool) {
        let mut state = self.state.write().await;

        if is_canary {
            state.canary_latencies.push(latency_ms);

            // Limit latency history
            if state.canary_latencies.len() > 10000 {
                state.canary_latencies.drain(0..1000);
            }

            if let Some(ref mut release) = state.active_release {
                release.canary_metrics.request_count += 1;
                if is_error {
                    release.canary_metrics.error_count += 1;
                }
                release.canary_metrics.error_rate = if release.canary_metrics.request_count > 0 {
                    (release.canary_metrics.error_count as f64
                        / release.canary_metrics.request_count as f64)
                        * 100.0
                } else {
                    0.0
                };
            }
        } else {
            state.baseline_latencies.push(latency_ms);

            if state.baseline_latencies.len() > 10000 {
                state.baseline_latencies.drain(0..1000);
            }

            if let Some(ref mut release) = state.active_release {
                release.baseline_metrics.request_count += 1;
                if is_error {
                    release.baseline_metrics.error_count += 1;
                }
                release.baseline_metrics.error_rate = if release.baseline_metrics.request_count > 0
                {
                    (release.baseline_metrics.error_count as f64
                        / release.baseline_metrics.request_count as f64)
                        * 100.0
                } else {
                    0.0
                };
            }
        }
    }

    /// Route a request
    pub async fn route_request(&self, request_id: &str) -> CanaryRoutingDecision {
        let state = self.state.read().await;
        let baseline_version = self.baseline_version.read().await;

        let Some(ref release) = state.active_release else {
            return CanaryRoutingDecision {
                is_canary: false,
                version: baseline_version.clone(),
                weight: 1.0,
                reason: "No active canary".to_string(),
            };
        };

        if release.state != CanaryState::Active && release.state != CanaryState::Promoting {
            return CanaryRoutingDecision {
                is_canary: false,
                version: baseline_version.clone(),
                weight: 1.0,
                reason: format!("Canary not active (state: {:?})", release.state),
            };
        }

        let percentage = release.traffic_percentage;
        let is_canary = match &self.config.routing_strategy {
            RoutingStrategy::Random => {
                let random = fastrand::u8(0..100);
                random < percentage
            }
            RoutingStrategy::HashBased { hash_key: _ } => {
                let hash = request_id
                    .bytes()
                    .fold(0u64, |acc, b| acc.wrapping_add(b as u64));
                ((hash % 100) as u8) < percentage
            }
            RoutingStrategy::HeaderBased { .. }
            | RoutingStrategy::CookieBased { .. }
            | RoutingStrategy::Geographic { .. }
            | RoutingStrategy::UserSegment { .. } => {
                // For these strategies, we'd need more context
                // Fall back to random for now
                let random = fastrand::u8(0..100);
                random < percentage
            }
        };

        CanaryRoutingDecision {
            is_canary,
            version: if is_canary {
                release.version.clone()
            } else {
                baseline_version.clone()
            },
            weight: if is_canary {
                percentage as f64 / 100.0
            } else {
                (100 - percentage) as f64 / 100.0
            },
            reason: if is_canary {
                format!("Routed to canary ({}%)", percentage)
            } else {
                format!("Routed to baseline ({}%)", 100 - percentage)
            },
        }
    }

    /// Analyze canary metrics and return recommendation
    pub async fn analyze(&self) -> Option<AnalysisResult> {
        let state = self.state.read().await;
        let release = state.active_release.as_ref()?;

        let mut checks = Vec::new();
        let mut all_passed = true;

        // Check minimum request count
        let min_requests = self.config.promotion_policy.min_request_count;
        if release.canary_metrics.request_count < min_requests {
            return Some(AnalysisResult {
                health_score: 0.0,
                passed: false,
                recommendation: AnalysisRecommendation::InsufficientData,
                checks: vec![AnalysisCheck {
                    name: "Minimum Requests".to_string(),
                    passed: false,
                    actual_value: release.canary_metrics.request_count as f64,
                    threshold_value: min_requests as f64,
                    description: format!(
                        "Need {} requests, have {}",
                        min_requests, release.canary_metrics.request_count
                    ),
                }],
                baseline_comparison: None,
                timestamp: SystemTime::now(),
            });
        }

        // Error rate check
        let error_rate_ok =
            release.canary_metrics.error_rate <= self.config.promotion_policy.max_error_rate;
        checks.push(AnalysisCheck {
            name: "Error Rate".to_string(),
            passed: error_rate_ok,
            actual_value: release.canary_metrics.error_rate,
            threshold_value: self.config.promotion_policy.max_error_rate,
            description: format!(
                "Error rate: {:.2}% (max: {:.2}%)",
                release.canary_metrics.error_rate, self.config.promotion_policy.max_error_rate
            ),
        });
        if !error_rate_ok {
            all_passed = false;
        }

        // Calculate latency percentiles
        let canary_p99 = self.calculate_percentile(&state.canary_latencies, 0.99);
        let latency_ok = canary_p99 <= self.config.promotion_policy.max_latency_p99_ms;
        checks.push(AnalysisCheck {
            name: "P99 Latency".to_string(),
            passed: latency_ok,
            actual_value: canary_p99 as f64,
            threshold_value: self.config.promotion_policy.max_latency_p99_ms as f64,
            description: format!(
                "P99 latency: {}ms (max: {}ms)",
                canary_p99, self.config.promotion_policy.max_latency_p99_ms
            ),
        });
        if !latency_ok {
            all_passed = false;
        }

        // Baseline comparison
        let baseline_comparison = if self.config.compare_with_baseline
            && release.baseline_metrics.request_count >= min_requests
        {
            let baseline_p99 = self.calculate_percentile(&state.baseline_latencies, 0.99);
            let error_rate_delta =
                release.canary_metrics.error_rate - release.baseline_metrics.error_rate;
            let latency_delta = canary_p99 as i64 - baseline_p99 as i64;

            // Simple statistical comparison
            let is_better = error_rate_delta <= 0.0 && latency_delta <= 0;

            Some(BaselineComparison {
                error_rate_delta,
                latency_delta_ms: latency_delta,
                statistically_significant: release.canary_metrics.request_count >= 1000,
                confidence_level: self.config.significance_level,
                is_better,
            })
        } else {
            None
        };

        // Calculate health score
        let health_score = self.calculate_health_score(&checks);

        // Determine recommendation
        let recommendation = if !all_passed {
            if release.canary_metrics.error_rate > self.config.rollback_policy.error_rate_threshold
            {
                AnalysisRecommendation::Rollback
            } else {
                AnalysisRecommendation::Hold
            }
        } else {
            AnalysisRecommendation::Promote
        };

        Some(AnalysisResult {
            health_score,
            passed: all_passed,
            recommendation,
            checks,
            baseline_comparison,
            timestamp: SystemTime::now(),
        })
    }

    fn calculate_percentile(&self, values: &[u64], percentile: f64) -> u64 {
        if values.is_empty() {
            return 0;
        }

        let mut sorted = values.to_vec();
        sorted.sort();

        let index = ((sorted.len() as f64 - 1.0) * percentile).round() as usize;
        sorted[index.min(sorted.len() - 1)]
    }

    fn calculate_health_score(&self, checks: &[AnalysisCheck]) -> f64 {
        if checks.is_empty() {
            return 0.0;
        }

        let passed_count = checks.iter().filter(|c| c.passed).count();
        (passed_count as f64 / checks.len() as f64) * 100.0
    }

    /// Promote canary to next stage
    pub async fn promote(&self) -> Result<()> {
        let (release_id, current_stage, new_percentage) = {
            let mut state = self.state.write().await;
            let Some(ref mut release) = state.active_release else {
                return Err(anyhow!("No active canary release"));
            };

            if release.state != CanaryState::Active {
                return Err(anyhow!("Canary not in active state"));
            }

            let next_stage = release.current_stage + 1;
            if next_stage >= self.config.promotion_policy.percentage_increments.len() {
                // Fully promoted
                release.state = CanaryState::Promoted;
                release.traffic_percentage = 100;
                return Ok(());
            }

            let new_percentage = self.config.promotion_policy.percentage_increments[next_stage]
                .min(self.config.max_percentage);

            release.current_stage = next_stage;
            release.traffic_percentage = new_percentage;
            release.stage_started_at = SystemTime::now();
            release.state = CanaryState::Active;

            (release.id.clone(), next_stage, new_percentage)
        };

        self.emit_event(CanaryEvent::StagePromoted {
            release_id,
            new_stage: current_stage,
            new_percentage,
        })
        .await;

        Ok(())
    }

    /// Pause canary release
    pub async fn pause(&self, reason: &str) -> Result<()> {
        let release_id = {
            let mut state = self.state.write().await;
            let Some(ref mut release) = state.active_release else {
                return Err(anyhow!("No active canary release"));
            };

            release.state = CanaryState::Paused;
            release.id.clone()
        };

        self.emit_event(CanaryEvent::Paused {
            release_id,
            reason: reason.to_string(),
        })
        .await;

        Ok(())
    }

    /// Resume canary release
    pub async fn resume(&self) -> Result<()> {
        let release_id = {
            let mut state = self.state.write().await;
            let Some(ref mut release) = state.active_release else {
                return Err(anyhow!("No active canary release"));
            };

            if release.state != CanaryState::Paused {
                return Err(anyhow!("Canary is not paused"));
            }

            release.state = CanaryState::Active;
            release.id.clone()
        };

        self.emit_event(CanaryEvent::Resumed { release_id }).await;

        Ok(())
    }

    /// Rollback canary release
    pub async fn rollback(&self, reason: &str) -> Result<()> {
        let release_id = {
            let mut state = self.state.write().await;
            let Some(ref mut release) = state.active_release else {
                return Err(anyhow!("No active canary release"));
            };

            release.state = CanaryState::RollingBack;
            release.id.clone()
        };

        self.emit_event(CanaryEvent::RollbackInitiated {
            release_id: release_id.clone(),
            reason: reason.to_string(),
        })
        .await;

        // Complete rollback
        {
            let mut state = self.state.write().await;
            if let Some(ref mut release) = state.active_release {
                release.state = CanaryState::RolledBack;
                release.traffic_percentage = 0;
            }

            // Move to history
            if let Some(release) = state.active_release.take() {
                state.release_history.push(release);
            }
        }

        self.emit_event(CanaryEvent::RolledBack { release_id })
            .await;

        Ok(())
    }

    /// Complete canary release (full promotion)
    pub async fn complete(&self) -> Result<()> {
        let release_id = {
            let mut state = self.state.write().await;
            let Some(ref mut release) = state.active_release else {
                return Err(anyhow!("No active canary release"));
            };

            release.state = CanaryState::Promoted;
            release.traffic_percentage = 100;
            release.id.clone()
        };

        self.emit_event(CanaryEvent::Completed {
            release_id: release_id.clone(),
        })
        .await;

        // Move to history
        {
            let mut state = self.state.write().await;
            if let Some(release) = state.active_release.take() {
                state.release_history.push(release);
            }
        }

        // Update baseline version
        {
            let state = self.state.read().await;
            if let Some(release) = state.release_history.last() {
                if release.state == CanaryState::Promoted {
                    let mut baseline = self.baseline_version.write().await;
                    *baseline = release.version.clone();
                }
            }
        }

        Ok(())
    }

    /// Run automatic analysis and take action
    pub async fn auto_analyze_and_act(&self) -> Result<Option<AnalysisRecommendation>> {
        let Some(analysis) = self.analyze().await else {
            return Ok(None);
        };

        // Store analysis in history
        {
            let mut state = self.state.write().await;
            if let Some(ref mut release) = state.active_release {
                release.analysis_history.push(analysis.clone());
            }
        }

        match analysis.recommendation {
            AnalysisRecommendation::Promote if self.config.promotion_policy.auto_promote => {
                // Check if stage duration has passed
                let can_promote = {
                    let state = self.state.read().await;
                    state.active_release.as_ref().is_some_and(|r| {
                        r.stage_started_at.elapsed().unwrap_or_default()
                            >= self.config.promotion_policy.stage_duration
                    })
                };

                if can_promote {
                    self.promote().await?;
                }
            }
            AnalysisRecommendation::Rollback if self.config.rollback_policy.auto_rollback => {
                self.rollback("Automatic rollback due to failed checks")
                    .await?;
            }
            _ => {}
        }

        Ok(Some(analysis.recommendation))
    }

    /// Get release history
    pub async fn get_history(&self, limit: Option<usize>) -> Vec<CanaryRelease> {
        let state = self.state.read().await;
        match limit {
            Some(n) => state
                .release_history
                .iter()
                .rev()
                .take(n)
                .cloned()
                .collect(),
            None => state.release_history.clone(),
        }
    }

    /// Get recent events
    pub async fn get_recent_events(&self, limit: usize) -> Vec<(SystemTime, CanaryEvent)> {
        let state = self.state.read().await;
        state.events.iter().rev().take(limit).cloned().collect()
    }
}

/// Trait for handling canary events
#[async_trait::async_trait]
pub trait CanaryEventHandler {
    /// Handle a canary event
    async fn on_event(&self, event: &CanaryEvent);
}

/// Logging event handler
pub struct LoggingCanaryHandler;

#[async_trait::async_trait]
impl CanaryEventHandler for LoggingCanaryHandler {
    async fn on_event(&self, event: &CanaryEvent) {
        match event {
            CanaryEvent::Started {
                release_id,
                version,
            } => {
                tracing::info!("Canary {} started for version {}", release_id, version);
            }
            CanaryEvent::TrafficChanged {
                release_id,
                old_percentage,
                new_percentage,
            } => {
                tracing::info!(
                    "Canary {} traffic changed: {}% -> {}%",
                    release_id,
                    old_percentage,
                    new_percentage
                );
            }
            CanaryEvent::AnalysisCompleted { release_id, result } => {
                tracing::info!(
                    "Canary {} analysis: {:?} (score: {:.1})",
                    release_id,
                    result.recommendation,
                    result.health_score
                );
            }
            CanaryEvent::StagePromoted {
                release_id,
                new_stage,
                new_percentage,
            } => {
                tracing::info!(
                    "Canary {} promoted to stage {} ({}%)",
                    release_id,
                    new_stage,
                    new_percentage
                );
            }
            CanaryEvent::Paused { release_id, reason } => {
                tracing::warn!("Canary {} paused: {}", release_id, reason);
            }
            CanaryEvent::Resumed { release_id } => {
                tracing::info!("Canary {} resumed", release_id);
            }
            CanaryEvent::RollbackInitiated { release_id, reason } => {
                tracing::warn!("Canary {} rollback initiated: {}", release_id, reason);
            }
            CanaryEvent::Completed { release_id } => {
                tracing::info!("Canary {} completed successfully", release_id);
            }
            CanaryEvent::RolledBack { release_id } => {
                tracing::warn!("Canary {} rolled back", release_id);
            }
            CanaryEvent::Failed { release_id, error } => {
                tracing::error!("Canary {} failed: {}", release_id, error);
            }
        }
    }
}

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

    #[tokio::test]
    async fn test_canary_creation() {
        let config = CanaryConfig::default();
        let controller = CanaryController::new(config);

        assert!(!controller.is_active().await);
    }

    #[tokio::test]
    async fn test_start_canary() {
        let config = CanaryConfig::default();
        let controller = CanaryController::new(config);

        let release_id = controller
            .start_canary("v2.0.0")
            .await
            .expect("should succeed");
        assert!(!release_id.is_empty());
        assert!(controller.is_active().await);
    }

    #[tokio::test]
    async fn test_cannot_start_duplicate_canary() {
        let config = CanaryConfig::default();
        let controller = CanaryController::new(config);

        controller
            .start_canary("v2.0.0")
            .await
            .expect("should succeed");
        let result = controller.start_canary("v3.0.0").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_routing() {
        let config = CanaryConfig {
            initial_percentage: 50,
            ..Default::default()
        };
        let controller = CanaryController::new(config);

        controller
            .start_canary("v2.0.0")
            .await
            .expect("should succeed");

        // With 50% canary, roughly half should route to canary
        let mut canary_count = 0;
        for i in 0..100 {
            let decision = controller.route_request(&format!("req-{}", i)).await;
            if decision.is_canary {
                canary_count += 1;
            }
        }

        // Should be roughly 50%, allow some variance
        assert!((30..=70).contains(&canary_count));
    }

    #[tokio::test]
    async fn test_record_metrics() {
        let config = CanaryConfig::default();
        let controller = CanaryController::new(config);

        controller
            .start_canary("v2.0.0")
            .await
            .expect("should succeed");

        // Record some metrics
        for _ in 0..100 {
            controller.record_metric(true, 50, false).await;
            controller.record_metric(false, 45, false).await;
        }

        let status = controller.get_status().await.expect("should succeed");
        assert_eq!(status.canary_metrics.request_count, 100);
        assert_eq!(status.baseline_metrics.request_count, 100);
    }

    #[tokio::test]
    async fn test_analysis_insufficient_data() {
        let config = CanaryConfig {
            promotion_policy: PromotionPolicy {
                min_request_count: 100,
                ..Default::default()
            },
            ..Default::default()
        };
        let controller = CanaryController::new(config);

        controller
            .start_canary("v2.0.0")
            .await
            .expect("should succeed");

        // Record less than min_request_count
        for _ in 0..50 {
            controller.record_metric(true, 50, false).await;
        }

        let analysis = controller.analyze().await.expect("should succeed");
        assert_eq!(
            analysis.recommendation,
            AnalysisRecommendation::InsufficientData
        );
    }

    #[tokio::test]
    async fn test_analysis_passes() {
        let config = CanaryConfig {
            promotion_policy: PromotionPolicy {
                min_request_count: 10,
                max_error_rate: 5.0,
                max_latency_p99_ms: 200,
                ..Default::default()
            },
            ..Default::default()
        };
        let controller = CanaryController::new(config);

        controller
            .start_canary("v2.0.0")
            .await
            .expect("should succeed");

        // Record good metrics
        for _ in 0..100 {
            controller.record_metric(true, 50, false).await;
        }

        let analysis = controller.analyze().await.expect("should succeed");
        assert!(analysis.passed);
        assert_eq!(analysis.recommendation, AnalysisRecommendation::Promote);
    }

    #[tokio::test]
    async fn test_promotion() {
        let config = CanaryConfig {
            initial_percentage: 5,
            promotion_policy: PromotionPolicy {
                percentage_increments: vec![5, 10, 25, 50, 100],
                ..Default::default()
            },
            ..Default::default()
        };
        let controller = CanaryController::new(config);

        controller
            .start_canary("v2.0.0")
            .await
            .expect("should succeed");

        let status = controller.get_status().await.expect("should succeed");
        assert_eq!(status.traffic_percentage, 5);

        controller.promote().await.expect("should succeed");

        let status = controller.get_status().await.expect("should succeed");
        assert_eq!(status.traffic_percentage, 10);
    }

    #[tokio::test]
    async fn test_pause_resume() {
        let config = CanaryConfig::default();
        let controller = CanaryController::new(config);

        controller
            .start_canary("v2.0.0")
            .await
            .expect("should succeed");

        controller.pause("Testing").await.expect("should succeed");
        let status = controller.get_status().await.expect("should succeed");
        assert_eq!(status.state, CanaryState::Paused);

        controller.resume().await.expect("should succeed");
        let status = controller.get_status().await.expect("should succeed");
        assert_eq!(status.state, CanaryState::Active);
    }

    #[tokio::test]
    async fn test_rollback() {
        let config = CanaryConfig::default();
        let controller = CanaryController::new(config);

        controller
            .start_canary("v2.0.0")
            .await
            .expect("should succeed");
        controller
            .rollback("Test rollback")
            .await
            .expect("should succeed");

        assert!(!controller.is_active().await);

        let history = controller.get_history(None).await;
        assert_eq!(history.len(), 1);
        assert_eq!(history[0].state, CanaryState::RolledBack);
    }

    #[tokio::test]
    async fn test_complete() {
        let config = CanaryConfig::default();
        let controller = CanaryController::new(config);

        controller
            .start_canary("v2.0.0")
            .await
            .expect("should succeed");
        controller.complete().await.expect("should succeed");

        assert!(!controller.is_active().await);

        let history = controller.get_history(None).await;
        assert_eq!(history.len(), 1);
        assert_eq!(history[0].state, CanaryState::Promoted);
    }

    #[tokio::test]
    async fn test_baseline_update_on_completion() {
        let config = CanaryConfig::default();
        let controller = CanaryController::new(config);

        controller.set_baseline_version("v1.0.0").await;
        controller
            .start_canary("v2.0.0")
            .await
            .expect("should succeed");
        controller.complete().await.expect("should succeed");

        let baseline = controller.baseline_version.read().await;
        assert_eq!(*baseline, "v2.0.0");
    }

    #[tokio::test]
    async fn test_event_history() {
        let config = CanaryConfig::default();
        let controller = CanaryController::new(config);

        controller
            .start_canary("v2.0.0")
            .await
            .expect("should succeed");
        controller.pause("Testing").await.expect("should succeed");
        controller.resume().await.expect("should succeed");

        let events = controller.get_recent_events(10).await;
        assert!(events.len() >= 3);
    }

    #[tokio::test]
    async fn test_hash_based_routing() {
        let config = CanaryConfig {
            initial_percentage: 50,
            routing_strategy: RoutingStrategy::HashBased {
                hash_key: "user_id".to_string(),
            },
            ..Default::default()
        };
        let controller = CanaryController::new(config);

        controller
            .start_canary("v2.0.0")
            .await
            .expect("should succeed");

        // Same request ID should always route to the same target
        let decision1 = controller.route_request("user-123").await;
        let decision2 = controller.route_request("user-123").await;
        assert_eq!(decision1.is_canary, decision2.is_canary);
    }
}