Skip to main content

llm_optimizer_processor/analyzer/
mod.rs

1//! Analyzer Engine
2//!
3//! This module provides real-time analysis capabilities for LLM optimization metrics.
4//! It includes 5 specialized analyzers:
5//!
6//! - **Performance Analyzer**: Latency, throughput, token usage, performance degradation
7//! - **Cost Analyzer**: Token costs, budget utilization, cost optimization opportunities
8//! - **Quality Analyzer**: Response quality, error rates, SLA compliance
9//! - **Pattern Analyzer**: Temporal patterns, traffic bursts, seasonality, trends
10//! - **Anomaly Analyzer**: Statistical anomaly detection using Z-score and IQR methods
11//!
12//! # Example
13//!
14//! ```rust,no_run
15//! use processor::analyzer::{PerformanceAnalyzer, Analyzer, AnalyzerEvent};
16//! use chrono::Utc;
17//! use std::collections::HashMap;
18//!
19//! #[tokio::main]
20//! async fn main() -> anyhow::Result<()> {
21//!     // Create and start an analyzer
22//!     let mut analyzer = PerformanceAnalyzer::with_defaults();
23//!     analyzer.start().await?;
24//!
25//!     // Process an event
26//!     let event = AnalyzerEvent::Response {
27//!         timestamp: Utc::now(),
28//!         request_id: "req-123".to_string(),
29//!         model: "gpt-4".to_string(),
30//!         completion_tokens: 100,
31//!         total_tokens: 200,
32//!         latency_ms: 500,
33//!         success: true,
34//!         error: None,
35//!         metadata: HashMap::new(),
36//!     };
37//!
38//!     analyzer.process_event(event).await?;
39//!
40//!     // Generate a report
41//!     let report = analyzer.generate_report().await?;
42//!     println!("Insights: {}", report.insights.len());
43//!     println!("Recommendations: {}", report.recommendations.len());
44//!
45//!     Ok(())
46//! }
47//! ```
48
49// Core modules
50pub mod traits;
51pub mod types;
52pub mod stats;
53
54// Analyzer implementations
55pub mod performance_analyzer;
56pub mod cost_analyzer;
57pub mod quality_analyzer;
58pub mod pattern_analyzer;
59pub mod anomaly_analyzer;
60
61// Re-exports for convenience
62pub use traits::{
63    Analyzer, AnalyzerConfig, AnalyzerConfigBuilder, AnalyzerError, AnalyzerResult, AnalyzerState,
64};
65
66pub use types::{
67    Action, ActionType, Alert, AlertStatus, AnalysisReport, AnalyzerEvent, AnalyzerStats,
68    Confidence, Evidence, EvidenceType, FeedbackType, Impact, ImpactMetric, Insight,
69    InsightCategory, Priority, Recommendation, ReportSummary, RiskLevel, Severity, Threshold,
70    ThresholdOperator,
71};
72
73pub use performance_analyzer::{PerformanceAnalyzer, PerformanceAnalyzerConfig};
74pub use cost_analyzer::{CostAnalyzer, CostAnalyzerConfig, ModelPricing};
75pub use quality_analyzer::{QualityAnalyzer, QualityAnalyzerConfig};
76pub use pattern_analyzer::{PatternAnalyzer, PatternAnalyzerConfig, PatternType};
77pub use anomaly_analyzer::{AnomalyAnalyzer, AnomalyAnalyzerConfig, AnomalyType};
78
79// Statistical utilities
80pub use stats::{
81    calculate_linear_trend, calculate_percentiles, is_outlier_iqr, is_outlier_zscore, mean,
82    percentage_change, std_dev, z_score, CircularBuffer, ExponentialMovingAverage, LinearTrend,
83    SimpleMovingAverage,
84};