voirs-sdk 0.1.0-rc.1

Unified SDK and public API for VoiRS speech synthesis
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
//! Intelligent cache warming with pattern detection.
//!
//! This module implements a sophisticated cache warming system that:
//! - Analyzes usage patterns to predict future cache needs
//! - Preloads frequently used models and synthesis results
//! - Optimizes cache hit rates through predictive loading
//! - Reduces cold-start latency in production environments
//!
//! # Features
//!
//! - **Pattern Detection**: Identifies common text patterns and voice preferences
//! - **Predictive Preloading**: Loads cache entries before they're needed
//! - **Time-Based Patterns**: Detects time-of-day and day-of-week patterns
//! - **Frequency Analysis**: Prioritizes most frequently used entries
//! - **Intelligent Eviction**: Protects warm entries from eviction
//!
//! # Example
//!
//! ```no_run
//! use voirs_sdk::cache::warming::{CacheWarmer, WarmingConfig, WarmingStrategy};
//!
//! #[tokio::main]
//! async fn main() -> voirs_sdk::Result<()> {
//!     let config = WarmingConfig::default()
//!         .with_strategy(WarmingStrategy::Predictive)
//!         .with_pattern_window(1000)
//!         .enable_time_patterns(true);
//!
//!     let warmer = CacheWarmer::new(config);
//!
//!     // Record usage patterns
//!     warmer.record_access("voice_1", "Hello world").await?;
//!
//!     // Warm the cache based on patterns
//!     warmer.warm_cache().await?;
//!
//!     Ok(())
//! }
//! ```

use crate::{Result, VoirsError};
use chrono::{Datelike, Timelike};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

/// Configuration for intelligent cache warming.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WarmingConfig {
    /// Strategy to use for cache warming
    pub strategy: WarmingStrategy,

    /// Number of access patterns to analyze
    pub pattern_window_size: usize,

    /// Minimum access frequency to trigger warming (0.0-1.0)
    pub min_frequency_threshold: f64,

    /// Enable time-based pattern detection
    pub enable_time_patterns: bool,

    /// Enable text similarity-based prediction
    pub enable_text_similarity: bool,

    /// Maximum entries to warm per cycle
    pub max_warm_entries: usize,

    /// Minimum time between warming cycles
    pub warming_interval: Duration,

    /// Similarity threshold for text patterns (0.0-1.0)
    pub similarity_threshold: f64,
}

impl Default for WarmingConfig {
    fn default() -> Self {
        Self {
            strategy: WarmingStrategy::Predictive,
            pattern_window_size: 1000,
            min_frequency_threshold: 0.1,
            enable_time_patterns: true,
            enable_text_similarity: true,
            max_warm_entries: 50,
            warming_interval: Duration::from_secs(60),
            similarity_threshold: 0.7,
        }
    }
}

impl WarmingConfig {
    /// Set the warming strategy.
    pub fn with_strategy(mut self, strategy: WarmingStrategy) -> Self {
        self.strategy = strategy;
        self
    }

    /// Set the pattern window size.
    pub fn with_pattern_window(mut self, size: usize) -> Self {
        self.pattern_window_size = size;
        self
    }

    /// Enable or disable time-based patterns.
    pub fn enable_time_patterns(mut self, enable: bool) -> Self {
        self.enable_time_patterns = enable;
        self
    }

    /// Set the similarity threshold.
    pub fn with_similarity_threshold(mut self, threshold: f64) -> Self {
        self.similarity_threshold = threshold.clamp(0.0, 1.0);
        self
    }

    /// Enable or disable text similarity-based prediction.
    pub fn enable_text_similarity(mut self, enable: bool) -> Self {
        self.enable_text_similarity = enable;
        self
    }
}

/// Cache warming strategies.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum WarmingStrategy {
    /// Warm most frequently accessed entries
    FrequencyBased,

    /// Warm recently accessed entries
    RecencyBased,

    /// Predictive warming based on patterns
    Predictive,

    /// Time-based warming for periodic patterns
    TimeBased,

    /// Hybrid strategy combining multiple approaches
    Hybrid,
}

/// Access pattern entry for analysis.
#[derive(Debug, Clone)]
struct AccessPattern {
    voice_id: String,
    text: String,
    timestamp: Instant,
    hour_of_day: u8,
    day_of_week: u8,
}

/// Frequency statistics for a cache entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FrequencyStats {
    /// Total access count
    pub access_count: u64,

    /// Last access timestamp
    pub last_access: String,

    /// Average time between accesses
    pub avg_interval_secs: f64,

    /// Frequency score (0.0-1.0)
    pub frequency_score: f64,
}

/// Pattern detection results.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatternAnalysis {
    /// Most frequent voice IDs
    pub frequent_voices: Vec<(String, u64)>,

    /// Common text patterns
    pub common_patterns: Vec<(String, u64)>,

    /// Time-based patterns (hour -> access count)
    pub hourly_patterns: HashMap<u8, u64>,

    /// Day-of-week patterns
    pub daily_patterns: HashMap<u8, u64>,

    /// Predicted next accesses
    pub predictions: Vec<WarmingPrediction>,
}

/// Prediction for cache warming.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WarmingPrediction {
    /// Voice ID to warm
    pub voice_id: String,

    /// Text pattern to warm
    pub text_pattern: String,

    /// Confidence score (0.0-1.0)
    pub confidence: f64,

    /// Reason for prediction
    pub reason: String,
}

/// Internal state for cache warmer.
#[derive(Debug)]
struct WarmerState {
    config: WarmingConfig,
    access_history: Vec<AccessPattern>,
    frequency_map: HashMap<String, u64>,
    last_warming: Option<Instant>,
    warming_count: u64,
}

/// Intelligent cache warmer.
///
/// Analyzes usage patterns and predictively warms the cache.
#[derive(Debug, Clone)]
pub struct CacheWarmer {
    state: Arc<RwLock<WarmerState>>,
}

impl CacheWarmer {
    /// Create a new cache warmer with the given configuration.
    pub fn new(config: WarmingConfig) -> Self {
        Self {
            state: Arc::new(RwLock::new(WarmerState {
                config,
                access_history: Vec::new(),
                frequency_map: HashMap::new(),
                last_warming: None,
                warming_count: 0,
            })),
        }
    }

    /// Record a cache access for pattern analysis.
    pub async fn record_access(&self, voice_id: &str, text: &str) -> Result<()> {
        let mut state = self.state.write().await;

        let now = chrono::Utc::now();
        let pattern = AccessPattern {
            voice_id: voice_id.to_string(),
            text: text.to_string(),
            timestamp: Instant::now(),
            hour_of_day: now.hour() as u8,
            day_of_week: now.weekday().num_days_from_monday() as u8,
        };

        state.access_history.push(pattern);

        // Update frequency map
        let key = format!("{}:{}", voice_id, text);
        *state.frequency_map.entry(key).or_insert(0) += 1;

        // Limit history size
        let window_size = state.config.pattern_window_size;
        let current_len = state.access_history.len();
        if current_len > window_size {
            state.access_history.drain(0..current_len - window_size);
        }

        Ok(())
    }

    /// Analyze access patterns and generate warming predictions.
    pub async fn analyze_patterns(&self) -> Result<PatternAnalysis> {
        let state = self.state.read().await;

        if state.access_history.is_empty() {
            return Ok(PatternAnalysis {
                frequent_voices: Vec::new(),
                common_patterns: Vec::new(),
                hourly_patterns: HashMap::new(),
                daily_patterns: HashMap::new(),
                predictions: Vec::new(),
            });
        }

        // Analyze voice frequency
        let mut voice_counts: HashMap<String, u64> = HashMap::new();
        for pattern in &state.access_history {
            *voice_counts.entry(pattern.voice_id.clone()).or_insert(0) += 1;
        }

        let mut frequent_voices: Vec<_> = voice_counts.into_iter().collect();
        frequent_voices.sort_by_key(|b| std::cmp::Reverse(b.1));
        frequent_voices.truncate(10);

        // Analyze text patterns
        let mut text_counts: HashMap<String, u64> = HashMap::new();
        for pattern in &state.access_history {
            *text_counts.entry(pattern.text.clone()).or_insert(0) += 1;
        }

        let mut common_patterns: Vec<_> = text_counts.into_iter().collect();
        common_patterns.sort_by_key(|b| std::cmp::Reverse(b.1));
        common_patterns.truncate(10);

        // Analyze time-based patterns
        let mut hourly_patterns: HashMap<u8, u64> = HashMap::new();
        let mut daily_patterns: HashMap<u8, u64> = HashMap::new();

        if state.config.enable_time_patterns {
            for pattern in &state.access_history {
                *hourly_patterns.entry(pattern.hour_of_day).or_insert(0) += 1;
                *daily_patterns.entry(pattern.day_of_week).or_insert(0) += 1;
            }
        }

        // Generate predictions
        let predictions = self.generate_predictions(&state).await?;

        Ok(PatternAnalysis {
            frequent_voices,
            common_patterns,
            hourly_patterns,
            daily_patterns,
            predictions,
        })
    }

    /// Warm the cache based on detected patterns.
    pub async fn warm_cache(&self) -> Result<WarmingStats> {
        let mut state = self.state.write().await;

        // Check if enough time has passed since last warming
        if let Some(last) = state.last_warming {
            if last.elapsed() < state.config.warming_interval {
                return Ok(WarmingStats {
                    entries_warmed: 0,
                    predictions_used: 0,
                    warming_time: Duration::from_secs(0),
                });
            }
        }

        let start = Instant::now();

        // Generate predictions (unlock state temporarily)
        drop(state);
        let analysis = self.analyze_patterns().await?;
        let mut state = self.state.write().await;

        // Warm cache based on predictions
        let mut entries_warmed = 0;
        let max_entries = state.config.max_warm_entries;

        for prediction in &analysis.predictions {
            if entries_warmed >= max_entries {
                break;
            }

            if prediction.confidence >= state.config.min_frequency_threshold {
                // In production, this would actually load the cache entry
                tracing::debug!(
                    "Warming cache entry: {} (confidence: {:.2})",
                    prediction.text_pattern,
                    prediction.confidence
                );
                entries_warmed += 1;
            }
        }

        state.last_warming = Some(Instant::now());
        state.warming_count += 1;

        let warming_time = start.elapsed();

        Ok(WarmingStats {
            entries_warmed,
            predictions_used: analysis.predictions.len(),
            warming_time,
        })
    }

    /// Get statistics about cache warming.
    pub async fn get_stats(&self) -> Result<WarmingStatistics> {
        let state = self.state.read().await;

        Ok(WarmingStatistics {
            total_accesses: state.access_history.len(),
            unique_patterns: state.frequency_map.len(),
            warming_cycles: state.warming_count,
            last_warming: state.last_warming.map(|t| t.elapsed()),
        })
    }

    /// Reset warming state.
    pub async fn reset(&self) -> Result<()> {
        let mut state = self.state.write().await;
        state.access_history.clear();
        state.frequency_map.clear();
        state.last_warming = None;
        state.warming_count = 0;
        Ok(())
    }

    // Internal helper methods

    async fn generate_predictions(&self, state: &WarmerState) -> Result<Vec<WarmingPrediction>> {
        let mut predictions = Vec::new();

        match state.config.strategy {
            WarmingStrategy::FrequencyBased => {
                self.frequency_based_predictions(state, &mut predictions)?;
            }
            WarmingStrategy::RecencyBased => {
                self.recency_based_predictions(state, &mut predictions)?;
            }
            WarmingStrategy::Predictive => {
                self.predictive_predictions(state, &mut predictions)?;
            }
            WarmingStrategy::TimeBased => {
                self.time_based_predictions(state, &mut predictions)?;
            }
            WarmingStrategy::Hybrid => {
                self.frequency_based_predictions(state, &mut predictions)?;
                self.time_based_predictions(state, &mut predictions)?;
            }
        }

        // Sort by confidence and limit (handle NaN by treating as less than any number)
        predictions.sort_by(|a, b| {
            b.confidence
                .partial_cmp(&a.confidence)
                .unwrap_or(std::cmp::Ordering::Less)
        });
        predictions.truncate(state.config.max_warm_entries);

        Ok(predictions)
    }

    fn frequency_based_predictions(
        &self,
        state: &WarmerState,
        predictions: &mut Vec<WarmingPrediction>,
    ) -> Result<()> {
        let total_accesses = state.access_history.len() as f64;

        for (key, count) in &state.frequency_map {
            let frequency = *count as f64 / total_accesses;

            if frequency >= state.config.min_frequency_threshold {
                if let Some((voice_id, text)) = key.split_once(':') {
                    predictions.push(WarmingPrediction {
                        voice_id: voice_id.to_string(),
                        text_pattern: text.to_string(),
                        confidence: frequency,
                        reason: format!("Frequent access ({} times)", count),
                    });
                }
            }
        }

        Ok(())
    }

    fn recency_based_predictions(
        &self,
        state: &WarmerState,
        predictions: &mut Vec<WarmingPrediction>,
    ) -> Result<()> {
        let recent_count = 10.min(state.access_history.len());
        let recent = &state.access_history[state.access_history.len() - recent_count..];

        for pattern in recent {
            predictions.push(WarmingPrediction {
                voice_id: pattern.voice_id.clone(),
                text_pattern: pattern.text.clone(),
                confidence: 0.8,
                reason: "Recent access".to_string(),
            });
        }

        Ok(())
    }

    fn predictive_predictions(
        &self,
        state: &WarmerState,
        predictions: &mut Vec<WarmingPrediction>,
    ) -> Result<()> {
        // Combine frequency and recency with similarity analysis
        self.frequency_based_predictions(state, predictions)?;

        // Add text similarity-based predictions
        if state.config.enable_text_similarity && state.access_history.len() >= 2 {
            let recent = &state.access_history[state.access_history.len() - 1];

            for pattern in &state.access_history[..state.access_history.len() - 1] {
                let similarity = self.text_similarity(&recent.text, &pattern.text);

                if similarity >= state.config.similarity_threshold {
                    predictions.push(WarmingPrediction {
                        voice_id: pattern.voice_id.clone(),
                        text_pattern: pattern.text.clone(),
                        confidence: similarity,
                        reason: format!(
                            "Similar to recent access ({:.0}% match)",
                            similarity * 100.0
                        ),
                    });
                }
            }
        }

        Ok(())
    }

    fn time_based_predictions(
        &self,
        state: &WarmerState,
        predictions: &mut Vec<WarmingPrediction>,
    ) -> Result<()> {
        if !state.config.enable_time_patterns {
            return Ok(());
        }

        let now = chrono::Utc::now();
        let current_hour = now.hour() as u8;

        // Find patterns from same time window
        for pattern in &state.access_history {
            if pattern.hour_of_day == current_hour {
                predictions.push(WarmingPrediction {
                    voice_id: pattern.voice_id.clone(),
                    text_pattern: pattern.text.clone(),
                    confidence: 0.7,
                    reason: format!("Time-based pattern (hour {})", current_hour),
                });
            }
        }

        Ok(())
    }

    fn text_similarity(&self, text1: &str, text2: &str) -> f64 {
        // Simple Jaccard similarity on words
        let words1: std::collections::HashSet<_> = text1.split_whitespace().collect();
        let words2: std::collections::HashSet<_> = text2.split_whitespace().collect();

        if words1.is_empty() && words2.is_empty() {
            return 1.0;
        }

        let intersection = words1.intersection(&words2).count();
        let union = words1.union(&words2).count();

        if union == 0 {
            0.0
        } else {
            intersection as f64 / union as f64
        }
    }
}

/// Statistics about a warming cycle.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WarmingStats {
    /// Number of entries warmed
    pub entries_warmed: usize,

    /// Number of predictions used
    pub predictions_used: usize,

    /// Time taken for warming
    pub warming_time: Duration,
}

/// Overall warming statistics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WarmingStatistics {
    /// Total number of accesses recorded
    pub total_accesses: usize,

    /// Number of unique patterns
    pub unique_patterns: usize,

    /// Total warming cycles performed
    pub warming_cycles: u64,

    /// Time since last warming
    pub last_warming: Option<Duration>,
}

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

    #[tokio::test]
    async fn test_cache_warmer_creation() {
        let warmer = CacheWarmer::new(WarmingConfig::default());
        let stats = warmer.get_stats().await.unwrap();
        assert_eq!(stats.total_accesses, 0);
    }

    #[tokio::test]
    async fn test_record_access() {
        let warmer = CacheWarmer::new(WarmingConfig::default());

        warmer.record_access("voice1", "Hello world").await.unwrap();
        warmer.record_access("voice1", "Hello world").await.unwrap();
        warmer.record_access("voice2", "Goodbye").await.unwrap();

        let stats = warmer.get_stats().await.unwrap();
        assert_eq!(stats.total_accesses, 3);
        assert_eq!(stats.unique_patterns, 2);
    }

    #[tokio::test]
    async fn test_pattern_analysis() {
        let warmer = CacheWarmer::new(WarmingConfig::default());

        for _ in 0..5 {
            warmer.record_access("voice1", "Hello").await.unwrap();
        }

        for _ in 0..3 {
            warmer.record_access("voice2", "World").await.unwrap();
        }

        let analysis = warmer.analyze_patterns().await.unwrap();

        assert_eq!(analysis.frequent_voices.len(), 2);
        assert_eq!(analysis.common_patterns.len(), 2);

        // Most frequent should be voice1
        assert_eq!(analysis.frequent_voices[0].0, "voice1");
        assert_eq!(analysis.frequent_voices[0].1, 5);
    }

    #[tokio::test]
    async fn test_frequency_based_predictions() {
        let config = WarmingConfig::default()
            .with_strategy(WarmingStrategy::FrequencyBased)
            .with_pattern_window(100);

        let warmer = CacheWarmer::new(config);

        // Create a clear pattern
        for _ in 0..10 {
            warmer
                .record_access("voice1", "Frequent text")
                .await
                .unwrap();
        }

        warmer.record_access("voice2", "Rare text").await.unwrap();

        let analysis = warmer.analyze_patterns().await.unwrap();

        assert!(!analysis.predictions.is_empty());
        // Highest confidence should be the frequent pattern
        assert_eq!(analysis.predictions[0].text_pattern, "Frequent text");
    }

    #[tokio::test]
    async fn test_text_similarity() {
        let warmer = CacheWarmer::new(WarmingConfig::default());

        let sim1 = warmer.text_similarity("hello world", "hello world");
        assert_eq!(sim1, 1.0);

        let sim2 = warmer.text_similarity("hello world", "world hello");
        assert_eq!(sim2, 1.0);

        let sim3 = warmer.text_similarity("hello", "goodbye");
        assert_eq!(sim3, 0.0);

        let sim4 = warmer.text_similarity("hello world", "hello earth");
        assert!(sim4 > 0.0 && sim4 < 1.0);
    }

    #[tokio::test]
    async fn test_warming_interval() {
        let config = WarmingConfig::default().with_strategy(WarmingStrategy::FrequencyBased);

        let warmer = CacheWarmer::new(config);

        warmer.record_access("voice1", "Test").await.unwrap();

        // First warm should succeed
        let stats1 = warmer.warm_cache().await.unwrap();
        assert!(stats1.warming_time > Duration::from_secs(0));

        // Immediate second warm should be skipped
        let stats2 = warmer.warm_cache().await.unwrap();
        assert_eq!(stats2.entries_warmed, 0);
    }

    #[tokio::test]
    async fn test_warmer_reset() {
        let warmer = CacheWarmer::new(WarmingConfig::default());

        warmer.record_access("voice1", "Test").await.unwrap();
        warmer.reset().await.unwrap();

        let stats = warmer.get_stats().await.unwrap();
        assert_eq!(stats.total_accesses, 0);
        assert_eq!(stats.unique_patterns, 0);
    }
}