litellm-rs 0.1.1

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
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
//! Advanced analytics and reporting system
//!
//! This module provides comprehensive analytics, cost optimization suggestions,
//! and detailed reporting capabilities.

use crate::storage::database::Database;
use crate::utils::error::{GatewayError, Result};
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tracing::info;

/// Analytics engine for processing usage data and generating insights
pub struct AnalyticsEngine {
    /// Database connection
    database: Arc<Database>,
    /// Metrics collector
    metrics_collector: MetricsCollector,
    /// Cost optimizer
    cost_optimizer: CostOptimizer,
    /// Report generator
    report_generator: ReportGenerator,
}

/// Metrics collector for gathering usage statistics
#[derive(Debug, Clone)]
pub struct MetricsCollector {
    /// Request metrics
    request_metrics: HashMap<String, RequestMetrics>,
    /// Provider metrics
    provider_metrics: HashMap<String, ProviderMetrics>,
    /// User metrics
    user_metrics: HashMap<String, UserMetrics>,
    /// Cost metrics
    cost_metrics: CostMetrics,
}

/// Request metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestMetrics {
    /// Total requests
    pub total_requests: u64,
    /// Successful requests
    pub successful_requests: u64,
    /// Failed requests
    pub failed_requests: u64,
    /// Average response time
    pub avg_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,
    /// Time period
    pub period_start: DateTime<Utc>,
    /// End of analysis period
    pub period_end: DateTime<Utc>,
}

/// Provider-specific metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderMetrics {
    /// Provider name
    pub provider_name: String,
    /// Request count
    pub request_count: u64,
    /// Success rate
    pub success_rate: f64,
    /// Average latency
    pub avg_latency_ms: f64,
    /// Error rate
    pub error_rate: f64,
    /// Cost efficiency (tokens per dollar)
    pub cost_efficiency: f64,
    /// Uptime percentage
    pub uptime_percentage: f64,
    /// Rate limit hits
    pub rate_limit_hits: u64,
}

/// User-specific metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserMetrics {
    /// User ID
    pub user_id: String,
    /// Request count
    pub request_count: u64,
    /// Token usage
    pub token_usage: TokenUsage,
    /// Cost breakdown
    pub cost_breakdown: CostBreakdown,
    /// Most used models
    pub top_models: Vec<ModelUsage>,
    /// Usage patterns
    pub usage_patterns: UsagePatterns,
}

/// Token usage statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenUsage {
    /// Input tokens
    pub input_tokens: u64,
    /// Output tokens
    pub output_tokens: u64,
    /// Total tokens
    pub total_tokens: u64,
    /// Average tokens per request
    pub avg_tokens_per_request: f64,
}

/// Cost breakdown
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CostBreakdown {
    /// Total cost
    pub total_cost: f64,
    /// Cost by provider
    pub by_provider: HashMap<String, f64>,
    /// Cost by model
    pub by_model: HashMap<String, f64>,
    /// Cost by operation type
    pub by_operation: HashMap<String, f64>,
    /// Daily costs
    pub daily_costs: Vec<DailyCost>,
}

/// Daily cost information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DailyCost {
    /// Date
    pub date: DateTime<Utc>,
    /// Cost amount
    pub cost: f64,
    /// Request count
    pub requests: u64,
}

/// 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,
}

/// Usage patterns analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UsagePatterns {
    /// Peak usage hours
    pub peak_hours: Vec<u8>,
    /// Usage by day of week
    pub usage_by_weekday: HashMap<String, u64>,
    /// Request size distribution
    pub request_size_distribution: RequestSizeDistribution,
    /// Seasonal trends
    pub seasonal_trends: Vec<SeasonalTrend>,
}

/// Request size distribution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestSizeDistribution {
    /// Small requests (< 100 tokens)
    pub small: u64,
    /// Medium requests (100-1000 tokens)
    pub medium: u64,
    /// Large requests (1000-10000 tokens)
    pub large: u64,
    /// Extra large requests (> 10000 tokens)
    pub extra_large: u64,
}

/// Seasonal trend data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SeasonalTrend {
    /// Period (week, month, quarter)
    pub period: String,
    /// Start date
    pub start_date: DateTime<Utc>,
    /// End date
    pub end_date: DateTime<Utc>,
    /// Usage count
    pub usage: u64,
    /// Growth rate compared to previous period
    pub growth_rate: f64,
}

/// Overall cost metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CostMetrics {
    /// Total cost across all users
    pub total_cost: f64,
    /// Cost by time period
    pub cost_by_period: HashMap<String, f64>,
    /// Cost trends
    pub cost_trends: Vec<CostTrend>,
    /// Budget utilization
    pub budget_utilization: HashMap<String, BudgetUtilization>,
}

/// Cost trend information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CostTrend {
    /// Period
    pub period: DateTime<Utc>,
    /// Cost amount
    pub cost: f64,
    /// Change from previous period
    pub change_percentage: f64,
    /// Projected cost for next period
    pub projected_cost: f64,
}

/// Budget utilization tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BudgetUtilization {
    /// Budget limit
    pub budget_limit: f64,
    /// Current usage
    pub current_usage: f64,
    /// Utilization percentage
    pub utilization_percentage: f64,
    /// Projected end-of-period usage
    pub projected_usage: f64,
    /// Days remaining in period
    pub days_remaining: u32,
}

/// Cost optimization suggestions
pub struct CostOptimizer {
    /// Optimization rules
    optimization_rules: Vec<OptimizationRule>,
}

/// Optimization rule
#[derive(Debug, Clone)]
pub struct OptimizationRule {
    /// Rule name
    pub name: String,
    /// Rule description
    pub description: String,
    /// Potential savings
    pub potential_savings: f64,
    /// Implementation difficulty
    pub difficulty: OptimizationDifficulty,
    /// Rule type
    pub rule_type: OptimizationType,
}

/// Optimization difficulty levels
#[derive(Debug, Clone)]
pub enum OptimizationDifficulty {
    /// Easy to implement optimization
    Easy,
    /// Medium difficulty optimization
    Medium,
    /// Hard to implement optimization
    Hard,
}

/// Types of optimizations
#[derive(Debug, Clone)]
pub enum OptimizationType {
    /// Switch to cheaper provider
    ProviderSwitch,
    /// Use smaller model
    ModelDowngrade,
    /// Implement caching
    Caching,
    /// Batch requests
    Batching,
    /// Optimize prompts
    PromptOptimization,
    /// Use different pricing tier
    PricingTier,
}

/// Cost optimization suggestion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationSuggestion {
    /// Suggestion title
    pub title: String,
    /// Description
    pub description: String,
    /// Potential monthly savings
    pub potential_savings: f64,
    /// Implementation effort
    pub effort: String,
    /// Priority level
    pub priority: u8,
    /// Specific recommendations
    pub recommendations: Vec<String>,
}

/// Report generator for creating detailed reports
pub struct ReportGenerator {
    /// Report templates
    templates: HashMap<String, ReportTemplate>,
}

/// Report template
#[derive(Debug, Clone)]
pub struct ReportTemplate {
    /// Template name
    pub name: String,
    /// Template description
    pub description: String,
    /// Report sections
    pub sections: Vec<ReportSection>,
    /// Output format
    pub format: ReportFormat,
}

/// Report section
#[derive(Debug, Clone)]
pub struct ReportSection {
    /// Section title
    pub title: String,
    /// Section type
    pub section_type: ReportSectionType,
    /// Data queries
    pub queries: Vec<String>,
}

/// Types of report sections
#[derive(Debug, Clone)]
pub enum ReportSectionType {
    /// Summary section
    Summary,
    /// Chart section
    Chart,
    /// Table section
    Table,
    /// Metrics section
    Metrics,
    /// Recommendations section
    Recommendations,
}

/// Report output formats
#[derive(Debug, Clone)]
pub enum ReportFormat {
    /// PDF format
    PDF,
    /// HTML format
    HTML,
    /// JSON format
    JSON,
    /// CSV format
    CSV,
    /// Excel format
    Excel,
}

/// Generated report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeneratedReport {
    /// Report ID
    pub id: String,
    /// Report title
    pub title: String,
    /// Generation timestamp
    pub generated_at: DateTime<Utc>,
    /// Report period
    pub period_start: DateTime<Utc>,
    /// End of report period
    pub period_end: DateTime<Utc>,
    /// Report sections
    pub sections: Vec<ReportSectionData>,
    /// Summary statistics
    pub summary: ReportSummary,
}

/// Report section data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReportSectionData {
    /// Section title
    pub title: String,
    /// Section data
    pub data: serde_json::Value,
    /// Charts or visualizations
    pub charts: Vec<ChartData>,
}

/// Chart data for visualizations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChartData {
    /// Chart type
    pub chart_type: String,
    /// Chart title
    pub title: String,
    /// Data points
    pub data: Vec<DataPoint>,
    /// Chart configuration
    pub config: serde_json::Value,
}

/// Data point for charts
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataPoint {
    /// X-axis value
    pub x: serde_json::Value,
    /// Y-axis value
    pub y: serde_json::Value,
    /// Additional metadata
    pub metadata: Option<serde_json::Value>,
}

/// Report summary
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReportSummary {
    /// Total requests
    pub total_requests: u64,
    /// Total cost
    pub total_cost: f64,
    /// Average response time
    pub avg_response_time: f64,
    /// Success rate
    pub success_rate: f64,
    /// Top insights
    pub key_insights: Vec<String>,
    /// Recommendations
    pub recommendations: Vec<String>,
}

impl AnalyticsEngine {
    /// Create a new analytics engine
    pub fn new(database: Arc<Database>) -> Self {
        Self {
            database,
            metrics_collector: MetricsCollector::new(),
            cost_optimizer: CostOptimizer::new(),
            report_generator: ReportGenerator::new(),
        }
    }

    /// Generate usage analytics for a user
    pub async fn generate_user_analytics(
        &self,
        user_id: &str,
        start_date: DateTime<Utc>,
        end_date: DateTime<Utc>,
    ) -> Result<UserMetrics> {
        info!("Generating analytics for user: {}", user_id);

        // Fetch usage data from database
        let usage_data = self
            .database
            .get_user_usage(user_id, start_date, end_date)
            .await?;

        // Process and analyze the data
        let metrics = self
            .metrics_collector
            .process_user_data(user_id, &usage_data)
            .await?;

        Ok(metrics)
    }

    /// Generate cost optimization suggestions
    pub async fn generate_cost_suggestions(
        &self,
        user_id: &str,
        period_days: u32,
    ) -> Result<Vec<OptimizationSuggestion>> {
        info!(
            "Generating cost optimization suggestions for user: {}",
            user_id
        );

        let end_date = Utc::now();
        let start_date = end_date - Duration::days(period_days as i64);

        // Get user metrics
        let metrics = self
            .generate_user_analytics(user_id, start_date, end_date)
            .await?;

        // Generate suggestions
        let suggestions = self.cost_optimizer.analyze_and_suggest(&metrics).await?;

        Ok(suggestions)
    }

    /// Generate comprehensive report
    pub async fn generate_report(
        &self,
        template_name: &str,
        user_id: Option<&str>,
        start_date: DateTime<Utc>,
        end_date: DateTime<Utc>,
    ) -> Result<GeneratedReport> {
        info!(
            "Generating report: {} for period {} to {}",
            template_name, start_date, end_date
        );

        let report = self
            .report_generator
            .generate(template_name, user_id, start_date, end_date, &self.database)
            .await?;

        Ok(report)
    }
}

impl MetricsCollector {
    /// Create a new metrics collector
    pub fn new() -> Self {
        Self {
            request_metrics: HashMap::new(),
            provider_metrics: HashMap::new(),
            user_metrics: HashMap::new(),
            cost_metrics: CostMetrics {
                total_cost: 0.0,
                cost_by_period: HashMap::new(),
                cost_trends: Vec::new(),
                budget_utilization: HashMap::new(),
            },
        }
    }

    /// Process user data and generate metrics
    pub async fn process_user_data(
        &self,
        user_id: &str,
        usage_data: &[serde_json::Value],
    ) -> Result<UserMetrics> {
        // Process the usage data and calculate metrics
        // This is a simplified implementation

        let request_count = usage_data.len() as u64;
        let total_tokens = usage_data
            .iter()
            .filter_map(|data| data.get("total_tokens")?.as_u64())
            .sum();

        let total_cost = usage_data
            .iter()
            .filter_map(|data| data.get("cost")?.as_f64())
            .sum();

        Ok(UserMetrics {
            user_id: user_id.to_string(),
            request_count,
            token_usage: TokenUsage {
                input_tokens: 0,  // Calculate from data
                output_tokens: 0, // Calculate from data
                total_tokens,
                avg_tokens_per_request: if request_count > 0 {
                    total_tokens as f64 / request_count as f64
                } else {
                    0.0
                },
            },
            cost_breakdown: CostBreakdown {
                total_cost,
                by_provider: HashMap::new(),  // Calculate from data
                by_model: HashMap::new(),     // Calculate from data
                by_operation: HashMap::new(), // Calculate from data
                daily_costs: Vec::new(),      // Calculate from data
            },
            top_models: Vec::new(), // Calculate from data
            usage_patterns: UsagePatterns {
                peak_hours: Vec::new(),           // Calculate from data
                usage_by_weekday: HashMap::new(), // Calculate from data
                request_size_distribution: RequestSizeDistribution {
                    small: 0,
                    medium: 0,
                    large: 0,
                    extra_large: 0,
                },
                seasonal_trends: Vec::new(),
            },
        })
    }
}

impl CostOptimizer {
    /// Create a new cost optimizer
    pub fn new() -> Self {
        Self {
            optimization_rules: Self::default_rules(),
        }
    }

    /// Analyze metrics and generate suggestions
    pub async fn analyze_and_suggest(
        &self,
        metrics: &UserMetrics,
    ) -> Result<Vec<OptimizationSuggestion>> {
        let mut suggestions = Vec::new();

        // Analyze cost patterns and generate suggestions
        if metrics.cost_breakdown.total_cost > 100.0 {
            suggestions.push(OptimizationSuggestion {
                title: "Consider Model Optimization".to_string(),
                description:
                    "Your usage patterns suggest potential savings through model optimization"
                        .to_string(),
                potential_savings: metrics.cost_breakdown.total_cost * 0.2,
                effort: "Medium".to_string(),
                priority: 8,
                recommendations: vec![
                    "Evaluate if smaller models can meet your needs".to_string(),
                    "Implement request caching for repeated queries".to_string(),
                ],
            });
        }

        Ok(suggestions)
    }

    /// Default optimization rules
    fn default_rules() -> Vec<OptimizationRule> {
        vec![
            OptimizationRule {
                name: "Provider Cost Comparison".to_string(),
                description: "Compare costs across different providers".to_string(),
                potential_savings: 0.3,
                difficulty: OptimizationDifficulty::Easy,
                rule_type: OptimizationType::ProviderSwitch,
            },
            OptimizationRule {
                name: "Model Right-sizing".to_string(),
                description: "Use appropriately sized models for tasks".to_string(),
                potential_savings: 0.4,
                difficulty: OptimizationDifficulty::Medium,
                rule_type: OptimizationType::ModelDowngrade,
            },
        ]
    }
}

impl ReportGenerator {
    /// Create a new report generator
    pub fn new() -> Self {
        Self {
            templates: Self::default_templates(),
        }
    }

    /// Generate a report
    pub async fn generate(
        &self,
        template_name: &str,
        _user_id: Option<&str>,
        start_date: DateTime<Utc>,
        end_date: DateTime<Utc>,
        _database: &Database,
    ) -> Result<GeneratedReport> {
        let template = self
            .templates
            .get(template_name)
            .ok_or_else(|| GatewayError::NotFound("Report template not found".to_string()))?;

        // Generate report sections
        let sections = Vec::new(); // Implement section generation

        Ok(GeneratedReport {
            id: uuid::Uuid::new_v4().to_string(),
            title: template.name.clone(),
            generated_at: Utc::now(),
            period_start: start_date,
            period_end: end_date,
            sections,
            summary: ReportSummary {
                total_requests: 0,
                total_cost: 0.0,
                avg_response_time: 0.0,
                success_rate: 0.0,
                key_insights: Vec::new(),
                recommendations: Vec::new(),
            },
        })
    }

    /// Default report templates
    fn default_templates() -> HashMap<String, ReportTemplate> {
        let mut templates = HashMap::new();

        templates.insert(
            "usage_summary".to_string(),
            ReportTemplate {
                name: "Usage Summary Report".to_string(),
                description: "Comprehensive usage and cost summary".to_string(),
                sections: vec![
                    ReportSection {
                        title: "Executive Summary".to_string(),
                        section_type: ReportSectionType::Summary,
                        queries: vec!["summary_stats".to_string()],
                    },
                    ReportSection {
                        title: "Cost Analysis".to_string(),
                        section_type: ReportSectionType::Chart,
                        queries: vec!["cost_trends".to_string()],
                    },
                ],
                format: ReportFormat::PDF,
            },
        );

        templates
    }
}

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

    #[tokio::test]
    async fn test_metrics_collection() {
        let collector = MetricsCollector::new();
        let usage_data = vec![serde_json::json!({
            "total_tokens": 100,
            "cost": 0.01
        })];

        let metrics = collector
            .process_user_data("user123", &usage_data)
            .await
            .unwrap();
        assert_eq!(metrics.request_count, 1);
        assert_eq!(metrics.token_usage.total_tokens, 100);
    }

    #[test]
    fn test_cost_optimizer() {
        let optimizer = CostOptimizer::new();
        assert!(!optimizer.optimization_rules.is_empty());
    }
}