litellm-rs 0.4.16

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
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
//! Aggregated metrics models

use super::super::Metadata;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

/// Provider metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderMetrics {
    /// Metrics metadata
    #[serde(flatten)]
    pub metadata: Metadata,
    /// Provider name
    pub provider: String,
    /// Time period start
    pub period_start: chrono::DateTime<chrono::Utc>,
    /// Time period end
    pub period_end: chrono::DateTime<chrono::Utc>,
    /// Total requests
    pub total_requests: u64,
    /// Successful requests
    pub successful_requests: u64,
    /// Failed requests
    pub failed_requests: u64,
    /// Success rate (0.0 to 1.0)
    pub success_rate: f64,
    /// Average response time in milliseconds
    pub avg_response_time_ms: f64,
    /// P50 response time
    pub p50_response_time_ms: f64,
    /// P95 response time
    pub p95_response_time_ms: f64,
    /// P99 response time
    pub p99_response_time_ms: f64,
    /// Total tokens processed
    pub total_tokens: u64,
    /// Total cost
    pub total_cost: f64,
    /// Error breakdown
    pub error_breakdown: HashMap<String, u64>,
    /// Model breakdown
    pub model_breakdown: HashMap<String, ModelMetrics>,
}

/// Model-specific metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelMetrics {
    /// Model name
    pub model: String,
    /// Request count
    pub requests: u64,
    /// Success count
    pub successes: u64,
    /// Total tokens
    pub tokens: u64,
    /// Total cost
    pub cost: f64,
    /// Average response time
    pub avg_response_time_ms: f64,
}

/// System metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemMetrics {
    /// Metrics metadata
    #[serde(flatten)]
    pub metadata: Metadata,
    /// Timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,
    /// CPU usage percentage
    pub cpu_usage: f64,
    /// Memory usage in bytes
    pub memory_usage: u64,
    /// Memory usage percentage
    pub memory_usage_percent: f64,
    /// Disk usage in bytes
    pub disk_usage: u64,
    /// Disk usage percentage
    pub disk_usage_percent: f64,
    /// Network I/O
    pub network_io: NetworkIO,
    /// Active connections
    pub active_connections: u32,
    /// Queue sizes
    pub queue_sizes: HashMap<String, u32>,
    /// Thread pool stats
    pub thread_pool: ThreadPoolStats,
}

/// Network I/O metrics
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct NetworkIO {
    /// Bytes received
    pub bytes_received: u64,
    /// Bytes sent
    pub bytes_sent: u64,
    /// Packets received
    pub packets_received: u64,
    /// Packets sent
    pub packets_sent: u64,
}

/// Thread pool statistics
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ThreadPoolStats {
    /// Active threads
    pub active_threads: u32,
    /// Total threads
    pub total_threads: u32,
    /// Queued tasks
    pub queued_tasks: u32,
    /// Completed tasks
    pub completed_tasks: u64,
}

/// Usage analytics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UsageAnalytics {
    /// Analytics metadata
    #[serde(flatten)]
    pub metadata: Metadata,
    /// Time period
    pub period: TimePeriod,
    /// User ID (if user-specific)
    pub user_id: Option<Uuid>,
    /// Team ID (if team-specific)
    pub team_id: Option<Uuid>,
    /// Total requests
    pub total_requests: u64,
    /// Total tokens
    pub total_tokens: u64,
    /// Total cost
    pub total_cost: f64,
    /// Model usage breakdown
    pub model_usage: HashMap<String, ModelUsage>,
    /// Provider usage breakdown
    pub provider_usage: HashMap<String, ProviderUsage>,
    /// Daily breakdown
    pub daily_breakdown: Vec<DailyUsage>,
    /// Top endpoints
    pub top_endpoints: Vec<EndpointUsage>,
}

/// Time period
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimePeriod {
    /// Period start
    pub start: chrono::DateTime<chrono::Utc>,
    /// Period end
    pub end: chrono::DateTime<chrono::Utc>,
    /// Period type
    pub period_type: PeriodType,
}

/// Period type
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PeriodType {
    /// Hourly period
    Hour,
    /// Daily period
    Day,
    /// Weekly period
    Week,
    /// Monthly period
    Month,
    /// Yearly period
    Year,
    /// Custom period
    Custom,
}

/// Model usage statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelUsage {
    /// Model name
    pub model: String,
    /// Request count
    pub requests: u64,
    /// Token count
    pub tokens: u64,
    /// Cost
    pub cost: f64,
    /// Success rate
    pub success_rate: f64,
    /// Average response time
    pub avg_response_time_ms: f64,
}

/// Provider usage statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderUsage {
    /// Provider name
    pub provider: String,
    /// Request count
    pub requests: u64,
    /// Token count
    pub tokens: u64,
    /// Cost
    pub cost: f64,
    /// Success rate
    pub success_rate: f64,
    /// Average response time
    pub avg_response_time_ms: f64,
}

/// Daily usage statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DailyUsage {
    /// Date
    pub date: chrono::NaiveDate,
    /// Request count
    pub requests: u64,
    /// Token count
    pub tokens: u64,
    /// Cost
    pub cost: f64,
    /// Unique users
    pub unique_users: u32,
}

/// Endpoint usage statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EndpointUsage {
    /// Endpoint path
    pub endpoint: String,
    /// Request count
    pub requests: u64,
    /// Success rate
    pub success_rate: f64,
    /// Average response time
    pub avg_response_time_ms: f64,
}

/// Alert configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertConfig {
    /// Alert metadata
    #[serde(flatten)]
    pub metadata: Metadata,
    /// Alert name
    pub name: String,
    /// Alert description
    pub description: Option<String>,
    /// Alert condition
    pub condition: AlertCondition,
    /// Alert threshold
    pub threshold: f64,
    /// Alert severity
    pub severity: AlertSeverity,
    /// Alert channels
    pub channels: Vec<String>,
    /// Alert enabled
    pub enabled: bool,
    /// Cooldown period in seconds
    pub cooldown_seconds: u64,
}

/// Alert condition
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AlertCondition {
    /// High error rate condition
    ErrorRateHigh,
    /// Slow response time condition
    ResponseTimeSlow,
    /// High request volume condition
    RequestVolumeHigh,
    /// High cost condition
    CostHigh,
    /// Provider down condition
    ProviderDown,
    /// Quota exceeded condition
    QuotaExceeded,
    /// Custom alert condition
    Custom(String),
}

/// Alert severity
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AlertSeverity {
    /// Informational alert
    Info,
    /// Warning alert
    Warning,
    /// Error alert
    Error,
    /// Critical alert
    Critical,
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::{NaiveDate, Utc};

    fn create_test_metadata() -> Metadata {
        Metadata {
            id: Uuid::new_v4(),
            created_at: Utc::now(),
            updated_at: Utc::now(),
            version: 1,
            extra: HashMap::new(),
        }
    }

    // ==================== ModelMetrics Tests ====================

    #[test]
    fn test_model_metrics_structure() {
        let metrics = ModelMetrics {
            model: "gpt-4".to_string(),
            requests: 100,
            successes: 95,
            tokens: 50000,
            cost: 5.0,
            avg_response_time_ms: 150.0,
        };

        assert_eq!(metrics.model, "gpt-4");
        assert_eq!(metrics.requests, 100);
        assert_eq!(metrics.successes, 95);
    }

    #[test]
    fn test_model_metrics_serialization() {
        let metrics = ModelMetrics {
            model: "claude-3".to_string(),
            requests: 50,
            successes: 48,
            tokens: 25000,
            cost: 2.5,
            avg_response_time_ms: 200.0,
        };

        let json = serde_json::to_value(&metrics).unwrap();
        assert_eq!(json["model"], "claude-3");
        assert_eq!(json["requests"], 50);
        assert_eq!(json["cost"], 2.5);
    }

    #[test]
    fn test_model_metrics_clone() {
        let metrics = ModelMetrics {
            model: "test".to_string(),
            requests: 10,
            successes: 10,
            tokens: 1000,
            cost: 0.5,
            avg_response_time_ms: 100.0,
        };

        let cloned = metrics.clone();
        assert_eq!(metrics.model, cloned.model);
        assert_eq!(metrics.requests, cloned.requests);
    }

    // ==================== NetworkIO Tests ====================

    #[test]
    fn test_network_io_default() {
        let io = NetworkIO::default();
        assert_eq!(io.bytes_received, 0);
        assert_eq!(io.bytes_sent, 0);
        assert_eq!(io.packets_received, 0);
        assert_eq!(io.packets_sent, 0);
    }

    #[test]
    fn test_network_io_serialization() {
        let io = NetworkIO {
            bytes_received: 1024,
            bytes_sent: 2048,
            packets_received: 100,
            packets_sent: 200,
        };

        let json = serde_json::to_value(&io).unwrap();
        assert_eq!(json["bytes_received"], 1024);
        assert_eq!(json["bytes_sent"], 2048);
    }

    // ==================== ThreadPoolStats Tests ====================

    #[test]
    fn test_thread_pool_stats_default() {
        let stats = ThreadPoolStats::default();
        assert_eq!(stats.active_threads, 0);
        assert_eq!(stats.total_threads, 0);
        assert_eq!(stats.queued_tasks, 0);
        assert_eq!(stats.completed_tasks, 0);
    }

    #[test]
    fn test_thread_pool_stats_serialization() {
        let stats = ThreadPoolStats {
            active_threads: 4,
            total_threads: 8,
            queued_tasks: 10,
            completed_tasks: 1000,
        };

        let json = serde_json::to_value(&stats).unwrap();
        assert_eq!(json["active_threads"], 4);
        assert_eq!(json["total_threads"], 8);
    }

    // ==================== PeriodType Tests ====================

    #[test]
    fn test_period_type_variants() {
        let types = vec![
            PeriodType::Hour,
            PeriodType::Day,
            PeriodType::Week,
            PeriodType::Month,
            PeriodType::Year,
            PeriodType::Custom,
        ];

        for period_type in types {
            let json = serde_json::to_string(&period_type).unwrap();
            assert!(!json.is_empty());
        }
    }

    #[test]
    fn test_period_type_serialization() {
        assert!(
            serde_json::to_string(&PeriodType::Hour)
                .unwrap()
                .contains("hour")
        );
        assert!(
            serde_json::to_string(&PeriodType::Day)
                .unwrap()
                .contains("day")
        );
        assert!(
            serde_json::to_string(&PeriodType::Week)
                .unwrap()
                .contains("week")
        );
        assert!(
            serde_json::to_string(&PeriodType::Month)
                .unwrap()
                .contains("month")
        );
    }

    // ==================== TimePeriod Tests ====================

    #[test]
    fn test_time_period_structure() {
        let period = TimePeriod {
            start: Utc::now(),
            end: Utc::now(),
            period_type: PeriodType::Day,
        };

        assert!(matches!(period.period_type, PeriodType::Day));
    }

    #[test]
    fn test_time_period_serialization() {
        let period = TimePeriod {
            start: Utc::now(),
            end: Utc::now(),
            period_type: PeriodType::Week,
        };

        let json = serde_json::to_value(&period).unwrap();
        assert!(json["start"].is_string());
        assert!(json["end"].is_string());
        assert!(json["period_type"].is_string());
    }

    // ==================== ModelUsage Tests ====================

    #[test]
    fn test_model_usage_structure() {
        let usage = ModelUsage {
            model: "gpt-4".to_string(),
            requests: 100,
            tokens: 50000,
            cost: 5.0,
            success_rate: 0.95,
            avg_response_time_ms: 150.0,
        };

        assert_eq!(usage.model, "gpt-4");
        assert!((usage.success_rate - 0.95).abs() < f64::EPSILON);
    }

    // ==================== ProviderUsage Tests ====================

    #[test]
    fn test_provider_usage_structure() {
        let usage = ProviderUsage {
            provider: "openai".to_string(),
            requests: 500,
            tokens: 100000,
            cost: 10.0,
            success_rate: 0.98,
            avg_response_time_ms: 120.0,
        };

        assert_eq!(usage.provider, "openai");
        assert_eq!(usage.requests, 500);
    }

    // ==================== DailyUsage Tests ====================

    #[test]
    fn test_daily_usage_structure() {
        let usage = DailyUsage {
            date: NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            requests: 1000,
            tokens: 500000,
            cost: 50.0,
            unique_users: 25,
        };

        assert_eq!(usage.requests, 1000);
        assert_eq!(usage.unique_users, 25);
    }

    #[test]
    fn test_daily_usage_serialization() {
        let usage = DailyUsage {
            date: NaiveDate::from_ymd_opt(2024, 6, 15).unwrap(),
            requests: 100,
            tokens: 10000,
            cost: 1.0,
            unique_users: 5,
        };

        let json = serde_json::to_value(&usage).unwrap();
        assert!(json["date"].is_string());
        assert_eq!(json["requests"], 100);
    }

    // ==================== EndpointUsage Tests ====================

    #[test]
    fn test_endpoint_usage_structure() {
        let usage = EndpointUsage {
            endpoint: "/v1/chat/completions".to_string(),
            requests: 5000,
            success_rate: 0.99,
            avg_response_time_ms: 100.0,
        };

        assert_eq!(usage.endpoint, "/v1/chat/completions");
        assert_eq!(usage.requests, 5000);
    }

    // ==================== AlertCondition Tests ====================

    #[test]
    fn test_alert_condition_variants() {
        let conditions = vec![
            AlertCondition::ErrorRateHigh,
            AlertCondition::ResponseTimeSlow,
            AlertCondition::RequestVolumeHigh,
            AlertCondition::CostHigh,
            AlertCondition::ProviderDown,
            AlertCondition::QuotaExceeded,
            AlertCondition::Custom("custom_condition".to_string()),
        ];

        for condition in conditions {
            let json = serde_json::to_string(&condition).unwrap();
            assert!(!json.is_empty());
        }
    }

    #[test]
    fn test_alert_condition_serialization() {
        let condition = AlertCondition::ErrorRateHigh;
        let json = serde_json::to_string(&condition).unwrap();
        assert!(json.contains("error_rate_high"));
    }

    #[test]
    fn test_alert_condition_custom() {
        let condition = AlertCondition::Custom("my_custom_alert".to_string());
        let json = serde_json::to_string(&condition).unwrap();
        assert!(json.contains("my_custom_alert"));
    }

    // ==================== AlertSeverity Tests ====================

    #[test]
    fn test_alert_severity_variants() {
        let severities = vec![
            AlertSeverity::Info,
            AlertSeverity::Warning,
            AlertSeverity::Error,
            AlertSeverity::Critical,
        ];

        for severity in severities {
            let json = serde_json::to_string(&severity).unwrap();
            assert!(!json.is_empty());
        }
    }

    #[test]
    fn test_alert_severity_serialization() {
        assert!(
            serde_json::to_string(&AlertSeverity::Info)
                .unwrap()
                .contains("info")
        );
        assert!(
            serde_json::to_string(&AlertSeverity::Warning)
                .unwrap()
                .contains("warning")
        );
        assert!(
            serde_json::to_string(&AlertSeverity::Error)
                .unwrap()
                .contains("error")
        );
        assert!(
            serde_json::to_string(&AlertSeverity::Critical)
                .unwrap()
                .contains("critical")
        );
    }

    // ==================== AlertConfig Tests ====================

    #[test]
    fn test_alert_config_structure() {
        let config = AlertConfig {
            metadata: create_test_metadata(),
            name: "High Error Rate".to_string(),
            description: Some("Alert when error rate exceeds threshold".to_string()),
            condition: AlertCondition::ErrorRateHigh,
            threshold: 0.05,
            severity: AlertSeverity::Warning,
            channels: vec!["slack".to_string(), "email".to_string()],
            enabled: true,
            cooldown_seconds: 300,
        };

        assert_eq!(config.name, "High Error Rate");
        assert!(config.enabled);
        assert_eq!(config.channels.len(), 2);
    }

    #[test]
    fn test_alert_config_no_description() {
        let config = AlertConfig {
            metadata: create_test_metadata(),
            name: "Simple Alert".to_string(),
            description: None,
            condition: AlertCondition::CostHigh,
            threshold: 100.0,
            severity: AlertSeverity::Info,
            channels: vec![],
            enabled: false,
            cooldown_seconds: 0,
        };

        assert!(config.description.is_none());
        assert!(!config.enabled);
    }

    // ==================== ProviderMetrics Tests ====================

    #[test]
    fn test_provider_metrics_structure() {
        let mut error_breakdown = HashMap::new();
        error_breakdown.insert("rate_limit".to_string(), 10u64);
        error_breakdown.insert("timeout".to_string(), 5u64);

        let metrics = ProviderMetrics {
            metadata: create_test_metadata(),
            provider: "openai".to_string(),
            period_start: Utc::now(),
            period_end: Utc::now(),
            total_requests: 1000,
            successful_requests: 950,
            failed_requests: 50,
            success_rate: 0.95,
            avg_response_time_ms: 150.0,
            p50_response_time_ms: 100.0,
            p95_response_time_ms: 300.0,
            p99_response_time_ms: 500.0,
            total_tokens: 500000,
            total_cost: 50.0,
            error_breakdown,
            model_breakdown: HashMap::new(),
        };

        assert_eq!(metrics.provider, "openai");
        assert_eq!(metrics.total_requests, 1000);
        assert_eq!(metrics.error_breakdown.len(), 2);
    }

    #[test]
    fn test_provider_metrics_with_model_breakdown() {
        let mut model_breakdown = HashMap::new();
        model_breakdown.insert(
            "gpt-4".to_string(),
            ModelMetrics {
                model: "gpt-4".to_string(),
                requests: 500,
                successes: 480,
                tokens: 250000,
                cost: 30.0,
                avg_response_time_ms: 200.0,
            },
        );

        let metrics = ProviderMetrics {
            metadata: create_test_metadata(),
            provider: "openai".to_string(),
            period_start: Utc::now(),
            period_end: Utc::now(),
            total_requests: 500,
            successful_requests: 480,
            failed_requests: 20,
            success_rate: 0.96,
            avg_response_time_ms: 200.0,
            p50_response_time_ms: 150.0,
            p95_response_time_ms: 400.0,
            p99_response_time_ms: 600.0,
            total_tokens: 250000,
            total_cost: 30.0,
            error_breakdown: HashMap::new(),
            model_breakdown,
        };

        assert!(metrics.model_breakdown.contains_key("gpt-4"));
    }

    // ==================== SystemMetrics Tests ====================

    #[test]
    fn test_system_metrics_structure() {
        let metrics = SystemMetrics {
            metadata: create_test_metadata(),
            timestamp: Utc::now(),
            cpu_usage: 45.5,
            memory_usage: 8_000_000_000,
            memory_usage_percent: 50.0,
            disk_usage: 100_000_000_000,
            disk_usage_percent: 40.0,
            network_io: NetworkIO::default(),
            active_connections: 100,
            queue_sizes: HashMap::new(),
            thread_pool: ThreadPoolStats::default(),
        };

        assert!((metrics.cpu_usage - 45.5).abs() < f64::EPSILON);
        assert_eq!(metrics.active_connections, 100);
    }

    // ==================== UsageAnalytics Tests ====================

    #[test]
    fn test_usage_analytics_structure() {
        let analytics = UsageAnalytics {
            metadata: create_test_metadata(),
            period: TimePeriod {
                start: Utc::now(),
                end: Utc::now(),
                period_type: PeriodType::Month,
            },
            user_id: Some(Uuid::new_v4()),
            team_id: None,
            total_requests: 10000,
            total_tokens: 5000000,
            total_cost: 500.0,
            model_usage: HashMap::new(),
            provider_usage: HashMap::new(),
            daily_breakdown: vec![],
            top_endpoints: vec![],
        };

        assert!(analytics.user_id.is_some());
        assert!(analytics.team_id.is_none());
        assert_eq!(analytics.total_requests, 10000);
    }

    #[test]
    fn test_usage_analytics_with_breakdowns() {
        let mut model_usage = HashMap::new();
        model_usage.insert(
            "gpt-4".to_string(),
            ModelUsage {
                model: "gpt-4".to_string(),
                requests: 100,
                tokens: 50000,
                cost: 5.0,
                success_rate: 0.95,
                avg_response_time_ms: 150.0,
            },
        );

        let analytics = UsageAnalytics {
            metadata: create_test_metadata(),
            period: TimePeriod {
                start: Utc::now(),
                end: Utc::now(),
                period_type: PeriodType::Day,
            },
            user_id: None,
            team_id: None,
            total_requests: 100,
            total_tokens: 50000,
            total_cost: 5.0,
            model_usage,
            provider_usage: HashMap::new(),
            daily_breakdown: vec![],
            top_endpoints: vec![],
        };

        assert!(analytics.model_usage.contains_key("gpt-4"));
    }

    // ==================== Deserialization Tests ====================

    #[test]
    fn test_period_type_deserialization() {
        let json = r#""day""#;
        let period_type: PeriodType = serde_json::from_str(json).unwrap();
        assert!(matches!(period_type, PeriodType::Day));
    }

    #[test]
    fn test_alert_severity_deserialization() {
        let json = r#""critical""#;
        let severity: AlertSeverity = serde_json::from_str(json).unwrap();
        assert!(matches!(severity, AlertSeverity::Critical));
    }

    #[test]
    fn test_alert_condition_deserialization() {
        let json = r#""provider_down""#;
        let condition: AlertCondition = serde_json::from_str(json).unwrap();
        assert!(matches!(condition, AlertCondition::ProviderDown));
    }
}