oxirs-embed 0.3.1

Knowledge graph embeddings with TransE, ComplEx, and custom models
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
//! Advanced Profiler Types
//!
//! Profiling event types, metric types, trace types, configuration structs,
//! and all supporting enums for the advanced profiler subsystem.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
use tokio::sync::Mutex;

// ─── Configuration ────────────────────────────────────────────────────────────

/// Configuration for advanced profiling
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfilerConfig {
    /// Maximum number of concurrent profiling sessions
    pub max_sessions: usize,
    /// Sampling rate (0.0 to 1.0)
    pub sampling_rate: f64,
    /// Buffer size for performance data
    pub buffer_size: usize,
    /// Analysis window size in seconds
    pub analysis_window_seconds: u64,
    /// Enable memory profiling
    pub enable_memory_profiling: bool,
    /// Enable CPU profiling
    pub enable_cpu_profiling: bool,
    /// Enable GPU profiling
    pub enable_gpu_profiling: bool,
    /// Enable network profiling
    pub enable_network_profiling: bool,
}

impl Default for ProfilerConfig {
    fn default() -> Self {
        Self {
            max_sessions: 10,
            sampling_rate: 0.01,
            buffer_size: 100000,
            analysis_window_seconds: 300,
            enable_memory_profiling: true,
            enable_cpu_profiling: true,
            enable_gpu_profiling: true,
            enable_network_profiling: true,
        }
    }
}

// ─── Session ──────────────────────────────────────────────────────────────────

/// Individual profiling session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfilingSession {
    pub session_id: String,
    pub name: String,
    pub start_time: DateTime<Utc>,
    pub end_time: Option<DateTime<Utc>>,
    pub status: SessionStatus,
    pub metrics: Vec<MetricDataPoint>,
    pub tags: HashMap<String, String>,
}

/// Status of a profiling session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SessionStatus {
    Active,
    Completed,
    Failed(String),
    Cancelled,
}

// ─── Metrics ──────────────────────────────────────────────────────────────────

/// Individual metric data point
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricDataPoint {
    pub timestamp: DateTime<Utc>,
    pub metric_name: String,
    pub value: f64,
    pub unit: String,
    pub metadata: HashMap<String, String>,
    pub thread_id: Option<String>,
    pub component: String,
}

/// Collection statistics
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CollectionStats {
    pub total_points: u64,
    pub collection_rate: f64,
    pub memory_usage_bytes: u64,
    pub drop_rate: f64,
}

/// Performance tracker for specific components
#[derive(Debug, Clone)]
pub struct PerformanceTracker {
    pub name: String,
    pub start_time: Instant,
    pub measurements: Vec<TimedMeasurement>,
    pub state: TrackerState,
}

/// Timed measurement
#[derive(Debug, Clone)]
pub struct TimedMeasurement {
    pub timestamp: Duration,
    pub measurement_type: MeasurementType,
    pub value: f64,
    pub context: String,
}

/// Types of measurements
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MeasurementType {
    Latency,
    Throughput,
    MemoryUsage,
    CpuUsage,
    GpuUsage,
    NetworkLatency,
    DiskIo,
    CacheHitRate,
    ErrorRate,
    QueueLength,
}

/// Tracker state
#[derive(Debug, Clone)]
pub enum TrackerState {
    Active,
    Paused,
    Stopped,
}

// ─── Analysis ─────────────────────────────────────────────────────────────────

/// Analysis algorithm interface
#[derive(Debug, Clone)]
pub struct AnalysisAlgorithm {
    pub name: String,
    pub algorithm_type: AlgorithmType,
    pub parameters: HashMap<String, f64>,
}

/// Types of analysis algorithms
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AlgorithmType {
    TrendAnalysis,
    BottleneckDetection,
    PerformanceRegression,
    ResourceUtilization,
    CapacityPlanning,
    LoadBalancing,
}

// ─── Pattern Detection ────────────────────────────────────────────────────────

/// Detected performance pattern
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformancePattern {
    pub id: String,
    pub pattern_type: PatternType,
    pub confidence: f64,
    pub time_window: (DateTime<Utc>, DateTime<Utc>),
    pub affected_components: Vec<String>,
    pub description: String,
}

/// Types of performance patterns
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PatternType {
    PeriodicSpike,
    GradualDegradation,
    SuddenDrop,
    MemoryLeak,
    ThresholdBreach,
    LoadPattern,
    SeasonalVariation,
}

/// Pattern template for recognition
#[derive(Debug, Clone)]
pub struct PatternTemplate {
    pub name: String,
    pub signature: PatternSignature,
    pub criteria: MatchingCriteria,
}

/// Pattern signature
#[derive(Debug, Clone)]
pub struct PatternSignature {
    pub characteristics: Vec<StatisticalCharacteristic>,
    pub temporal_features: Vec<TemporalFeature>,
}

/// Statistical characteristic
#[derive(Debug, Clone)]
pub struct StatisticalCharacteristic {
    pub metric: String,
    pub property: StatisticalProperty,
    pub value_range: (f64, f64),
}

/// Statistical properties
#[derive(Debug, Clone)]
pub enum StatisticalProperty {
    Mean,
    Median,
    StandardDeviation,
    Variance,
    Skewness,
    Kurtosis,
    Percentile(u8),
}

/// Temporal feature
#[derive(Debug, Clone)]
pub struct TemporalFeature {
    pub feature_type: TemporalFeatureType,
    pub time_scale: Duration,
    pub threshold: f64,
}

/// Types of temporal features
#[derive(Debug, Clone)]
pub enum TemporalFeatureType {
    Periodicity,
    Trend,
    Seasonality,
    Autocorrelation,
    ChangePoint,
}

/// Matching criteria for pattern recognition
#[derive(Debug, Clone)]
pub struct MatchingCriteria {
    pub min_confidence: f64,
    pub min_data_points: usize,
    pub time_window_requirements: TimeWindowRequirements,
}

/// Time window requirements
#[derive(Debug, Clone)]
pub struct TimeWindowRequirements {
    pub min_duration: Duration,
    pub max_duration: Duration,
    pub coverage_ratio: f64,
}

// ─── Anomaly Detection ────────────────────────────────────────────────────────

/// Anomaly detection algorithm
#[derive(Debug, Clone)]
pub struct AnomalyAlgorithm {
    pub name: String,
    pub algorithm_type: AnomalyAlgorithmType,
    pub sensitivity: f64,
    pub config: HashMap<String, f64>,
}

/// Types of anomaly detection algorithms
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AnomalyAlgorithmType {
    StatisticalOutlier,
    IsolationForest,
    LocalOutlierFactor,
    OneClassSvm,
    AutoEncoder,
    TimeSeriesAnomaly,
}

/// Detected performance anomaly
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceAnomaly {
    pub id: String,
    pub anomaly_type: AnomalyType,
    pub severity: AnomalySeverity,
    pub detected_at: DateTime<Utc>,
    pub affected_metrics: Vec<String>,
    pub anomaly_score: f64,
    pub context: AnomalyContext,
}

/// Types of anomalies
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AnomalyType {
    LatencySpike,
    ThroughputDrop,
    MemoryLeak,
    CpuSaturation,
    ErrorRateIncrease,
    ResourceStarvation,
    UnexpectedPattern,
}

/// Anomaly severity levels
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AnomalySeverity {
    Low,
    Medium,
    High,
    Critical,
}

/// Anomaly context information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnomalyContext {
    pub component: String,
    pub related_events: Vec<String>,
    pub environmental_factors: HashMap<String, String>,
    pub potential_causes: Vec<String>,
}

/// Baseline model for normal behavior
#[derive(Debug, Clone)]
pub struct BaselineModel {
    pub name: String,
    pub distribution: StatisticalDistribution,
    pub temporal_characteristics: TemporalCharacteristics,
    pub confidence: f64,
    pub last_updated: DateTime<Utc>,
}

/// Statistical distribution model
#[derive(Debug, Clone)]
pub struct StatisticalDistribution {
    pub distribution_type: DistributionType,
    pub parameters: Vec<f64>,
    pub goodness_of_fit: f64,
}

/// Types of statistical distributions
#[derive(Debug, Clone)]
pub enum DistributionType {
    Normal,
    LogNormal,
    Exponential,
    Gamma,
    Beta,
    Weibull,
    Custom,
}

/// Temporal characteristics of metrics
#[derive(Debug, Clone)]
pub struct TemporalCharacteristics {
    pub seasonality: Vec<SeasonalComponent>,
    pub trend: TrendInformation,
    pub autocorrelation: AutocorrelationStructure,
}

/// Seasonal component
#[derive(Debug, Clone)]
pub struct SeasonalComponent {
    pub period: Duration,
    pub amplitude: f64,
    pub phase: f64,
    pub strength: f64,
}

/// Trend information
#[derive(Debug, Clone)]
pub struct TrendInformation {
    pub direction: TrendDirection,
    pub strength: f64,
    pub linear_coefficient: f64,
    pub polynomial_coefficients: Vec<f64>,
}

/// Trend direction
#[derive(Debug, Clone)]
pub enum TrendDirection {
    Increasing,
    Decreasing,
    Stable,
    Oscillating,
}

/// Autocorrelation structure
#[derive(Debug, Clone)]
pub struct AutocorrelationStructure {
    pub lag_correlations: Vec<(Duration, f64)>,
    pub partial_autocorrelations: Vec<(Duration, f64)>,
    pub significant_lags: Vec<Duration>,
}

// ─── Optimization Recommendations ────────────────────────────────────────────

/// Optimization recommendation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationRecommendation {
    pub id: String,
    pub recommendation_type: RecommendationType,
    pub priority: RecommendationPriority,
    pub component: String,
    pub current_state: String,
    pub recommended_state: String,
    pub expected_improvement: ExpectedImprovement,
    pub implementation_effort: ImplementationEffort,
    pub risk_assessment: RiskAssessment,
    pub description: String,
    pub implementation_steps: Vec<String>,
}

/// Types of optimization recommendations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RecommendationType {
    ResourceScaling,
    ConfigurationTuning,
    CacheOptimization,
    LoadBalancing,
    HardwareUpgrade,
    SoftwareUpdate,
    ArchitecturalChange,
    ProcessOptimization,
}

/// Recommendation priority levels
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RecommendationPriority {
    Low,
    Medium,
    High,
    Critical,
}

/// Expected improvement metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExpectedImprovement {
    pub latency_improvement_percent: f64,
    pub throughput_improvement_percent: f64,
    pub resource_savings_percent: f64,
    pub cost_reduction_percent: f64,
    pub confidence: f64,
}

/// Implementation effort assessment
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImplementationEffort {
    pub estimated_hours: f64,
    pub required_skills: Vec<String>,
    pub complexity: ComplexityLevel,
    pub dependencies: Vec<String>,
}

/// Complexity levels
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ComplexityLevel {
    Low,
    Medium,
    High,
    VeryHigh,
}

/// Risk assessment for recommendations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiskAssessment {
    pub risk_level: RiskLevel,
    pub potential_impacts: Vec<PotentialImpact>,
    pub mitigation_strategies: Vec<String>,
    pub rollback_plan: String,
}

/// Risk levels
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RiskLevel {
    Low,
    Medium,
    High,
    Critical,
}

/// Potential impact of changes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PotentialImpact {
    pub impact_type: ImpactType,
    pub severity: ImpactSeverity,
    pub probability: f64,
    pub description: String,
}

/// Types of potential impacts
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ImpactType {
    PerformanceDegradation,
    ServiceDisruption,
    DataLoss,
    SecurityVulnerability,
    IncreasedCosts,
    UserExperience,
}

/// Impact severity levels
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ImpactSeverity {
    Negligible,
    Minor,
    Moderate,
    Major,
    Severe,
}

/// Recommendation rule
#[derive(Debug, Clone)]
pub struct RecommendationRule {
    pub name: String,
    pub conditions: Vec<TriggerCondition>,
    pub recommendation_template: RecommendationTemplate,
    pub priority: i32,
}

/// Trigger condition for recommendations
#[derive(Debug, Clone)]
pub struct TriggerCondition {
    pub metric: String,
    pub operator: ComparisonOperator,
    pub threshold: f64,
    pub time_window: Duration,
}

/// Comparison operators
#[derive(Debug, Clone)]
pub enum ComparisonOperator {
    GreaterThan,
    LessThan,
    GreaterThanOrEqual,
    LessThanOrEqual,
    Equal,
    NotEqual,
    Between(f64, f64),
}

/// Template for generating recommendations
#[derive(Debug, Clone)]
pub struct RecommendationTemplate {
    pub recommendation_type: RecommendationType,
    pub description_template: String,
    pub default_priority: RecommendationPriority,
    pub default_effort: ImplementationEffort,
}

/// Historical recommendation data
#[derive(Debug, Clone)]
pub struct RecommendationHistory {
    pub recommendation_id: String,
    pub implemented_at: Option<DateTime<Utc>>,
    pub actual_improvement: Option<ExpectedImprovement>,
    pub feedback: Option<String>,
    pub success_rating: Option<f64>,
}

// ─── Analysis Report types ────────────────────────────────────────────────────

/// Performance analysis report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceAnalysisReport {
    pub id: String,
    pub session_id: String,
    pub generated_at: DateTime<Utc>,
    pub analysis_results: Vec<AnalysisResult>,
    pub detected_patterns: Vec<PerformancePattern>,
    pub detected_anomalies: Vec<PerformanceAnomaly>,
    pub health_score: f64,
    pub summary: String,
}

impl PerformanceAnalysisReport {
    pub fn new(session_id: String) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            session_id,
            generated_at: Utc::now(),
            analysis_results: Vec::new(),
            detected_patterns: Vec::new(),
            detected_anomalies: Vec::new(),
            health_score: 100.0,
            summary: "Analysis in progress".to_string(),
        }
    }

    pub fn add_analysis_result(&mut self, result: AnalysisResult) {
        self.analysis_results.push(result);
    }

    pub fn set_detected_patterns(&mut self, patterns: Vec<PerformancePattern>) {
        self.detected_patterns = patterns;
    }

    pub fn set_detected_anomalies(&mut self, anomalies: Vec<PerformanceAnomaly>) {
        self.detected_anomalies = anomalies;
    }
}

/// Result from an analysis algorithm
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnalysisResult {
    pub algorithm_name: String,
    pub result_type: AlgorithmType,
    pub findings: Vec<Finding>,
    pub execution_time: Duration,
}

/// Individual finding from analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Finding {
    pub title: String,
    pub description: String,
    pub severity: FindingSeverity,
    pub confidence: f64,
    pub affected_metrics: Vec<String>,
    pub recommendations: Vec<String>,
}

/// Severity levels for findings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FindingSeverity {
    Info,
    Low,
    Medium,
    High,
    Critical,
}

// ─── Collector internals ──────────────────────────────────────────────────────

/// Performance data collector
#[derive(Debug)]
pub struct PerformanceCollector {
    pub buffer: VecDeque<MetricDataPoint>,
    pub stats: CollectionStats,
    pub trackers: HashMap<String, PerformanceTracker>,
}

// ─── Analyzer structs ─────────────────────────────────────────────────────────

/// Main performance analyzer — holds analysis algorithms and sub-detectors
#[derive(Debug)]
pub struct PerformanceAnalyzer {
    pub algorithms: Vec<AnalysisAlgorithm>,
    pub pattern_detector: PatternDetector,
    pub anomaly_detector: AnomalyDetector,
}

/// Pattern detector — holds pattern templates and detected patterns
#[derive(Debug)]
pub struct PatternDetector {
    pub patterns: Vec<PerformancePattern>,
    pub templates: Vec<PatternTemplate>,
}

/// Anomaly detector — holds anomaly detection algorithms, detected anomalies, and baselines
#[derive(Debug)]
pub struct AnomalyDetector {
    pub algorithms: Vec<AnomalyAlgorithm>,
    pub anomalies: Vec<PerformanceAnomaly>,
    pub baselines: HashMap<String, BaselineModel>,
}

/// Optimization recommender — holds rules, pending recommendations, and history
#[derive(Debug)]
pub struct OptimizationRecommender {
    pub rules: Vec<RecommendationRule>,
    pub recommendations: Vec<OptimizationRecommendation>,
    pub history: VecDeque<RecommendationHistory>,
}

// ─── Top-level profiler ───────────────────────────────────────────────────────

/// Advanced performance profiler for embedding systems
#[derive(Debug)]
pub struct AdvancedProfiler {
    /// Configuration for profiling
    pub(super) config: ProfilerConfig,
    /// Active profiling sessions
    pub(super) sessions: Arc<RwLock<HashMap<String, ProfilingSession>>>,
    /// Performance data collector
    pub(super) collector: Arc<Mutex<PerformanceCollector>>,
    /// Analysis engine
    pub(super) analyzer: PerformanceAnalyzer,
    /// Optimization recommender
    pub(super) recommender: OptimizationRecommender,
}