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
//! Production readiness checker and diagnostic tools for VoiRS SDK.
//!
//! This module provides comprehensive production deployment validation and
//! diagnostic capabilities to ensure your VoiRS deployment meets best practices
//! and performance requirements.
//!
//! # Features
//!
//! - **Readiness Checks**: Validates system configuration and resources
//! - **Performance Benchmarking**: Quick performance validation
//! - **Configuration Validation**: Checks for optimal settings
//! - **Security Audit**: Reviews security-related settings
//! - **Compatibility Checks**: Validates model and system compatibility
//! - **Best Practices**: Ensures compliance with recommended practices
//!
//! # Example
//!
//! ```no_run
//! use voirs_sdk::diagnostics::{ProductionReadiness, ReadinessConfig};
//!
//! #[tokio::main]
//! async fn main() -> voirs_sdk::Result<()> {
//!     let checker = ProductionReadiness::new(ReadinessConfig::default());
//!
//!     // Run comprehensive readiness check
//!     let report = checker.check_readiness().await?;
//!
//!     if report.is_production_ready() {
//!         println!("✓ System is production ready!");
//!     } else {
//!         println!("✗ Issues found:");
//!         for issue in report.critical_issues() {
//!             println!("  - {}", issue);
//!         }
//!     }
//!
//!     Ok(())
//! }
//! ```

use crate::{Result, VoirsError};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::{Duration, Instant};

/// Configuration for production readiness checks.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReadinessConfig {
    /// Minimum CPU cores required
    pub min_cpu_cores: usize,

    /// Minimum available memory (in GB)
    pub min_memory_gb: f64,

    /// Minimum available disk space (in GB)
    pub min_disk_gb: f64,

    /// Required synthesis speed (max RTF)
    pub max_acceptable_rtf: f32,

    /// Maximum acceptable latency (ms)
    pub max_latency_ms: u64,

    /// Enable security checks
    pub enable_security_checks: bool,

    /// Enable performance benchmarking
    pub enable_benchmarking: bool,

    /// Cache directory to validate
    pub cache_dir: Option<PathBuf>,
}

impl Default for ReadinessConfig {
    fn default() -> Self {
        Self {
            min_cpu_cores: 4,
            min_memory_gb: 4.0,
            min_disk_gb: 10.0,
            max_acceptable_rtf: 0.5,
            max_latency_ms: 150,
            enable_security_checks: true,
            enable_benchmarking: true,
            cache_dir: None,
        }
    }
}

/// Production readiness report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReadinessReport {
    /// Overall readiness status
    pub is_ready: bool,

    /// Timestamp of the check
    pub timestamp: String,

    /// Individual check results
    pub checks: Vec<CheckResult>,

    /// Performance benchmark results
    pub benchmark_results: Option<BenchmarkResults>,

    /// System information
    pub system_info: SystemInfo,

    /// Recommendations for improvements
    pub recommendations: Vec<Recommendation>,
}

impl ReadinessReport {
    /// Check if system is production ready.
    pub fn is_production_ready(&self) -> bool {
        self.is_ready
    }

    /// Get all critical issues.
    pub fn critical_issues(&self) -> Vec<String> {
        self.checks
            .iter()
            .filter(|c| c.severity == Severity::Critical && !c.passed)
            .map(|c| c.message.clone())
            .collect()
    }

    /// Get all warnings.
    pub fn warnings(&self) -> Vec<String> {
        self.checks
            .iter()
            .filter(|c| c.severity == Severity::Warning && !c.passed)
            .map(|c| c.message.clone())
            .collect()
    }

    /// Get high-priority recommendations.
    pub fn high_priority_recommendations(&self) -> Vec<&Recommendation> {
        self.recommendations
            .iter()
            .filter(|r| r.priority == RecommendationPriority::High)
            .collect()
    }

    /// Generate a human-readable report.
    pub fn to_report_string(&self) -> String {
        let mut report = String::new();

        report.push_str("=== VoiRS Production Readiness Report ===\n\n");
        report.push_str(&format!(
            "Status: {}\n",
            if self.is_ready {
                "✓ READY"
            } else {
                "✗ NOT READY"
            }
        ));
        report.push_str(&format!("Timestamp: {}\n\n", self.timestamp));

        report.push_str("System Information:\n");
        report.push_str(&format!("  CPU Cores: {}\n", self.system_info.cpu_cores));
        report.push_str(&format!(
            "  Memory: {:.2} GB\n",
            self.system_info.total_memory_gb
        ));
        report.push_str(&format!("  Platform: {}\n\n", self.system_info.platform));

        if !self.critical_issues().is_empty() {
            report.push_str("Critical Issues:\n");
            for issue in self.critical_issues() {
                report.push_str(&format!("{}\n", issue));
            }
            report.push('\n');
        }

        if !self.warnings().is_empty() {
            report.push_str("Warnings:\n");
            for warning in self.warnings() {
                report.push_str(&format!("{}\n", warning));
            }
            report.push('\n');
        }

        if !self.high_priority_recommendations().is_empty() {
            report.push_str("High Priority Recommendations:\n");
            for rec in self.high_priority_recommendations() {
                report.push_str(&format!("{}\n", rec.message));
            }
            report.push('\n');
        }

        if let Some(bench) = &self.benchmark_results {
            report.push_str("Performance Benchmarks:\n");
            report.push_str(&format!("  RTF: {:.3}\n", bench.average_rtf));
            report.push_str(&format!("  Latency: {} ms\n", bench.average_latency_ms));
            report.push_str(&format!("  Memory: {:.2} MB\n", bench.peak_memory_mb));
        }

        report
    }
}

/// Individual check result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckResult {
    /// Name of the check
    pub name: String,

    /// Whether the check passed
    pub passed: bool,

    /// Severity level
    pub severity: Severity,

    /// Detailed message
    pub message: String,

    /// Category of the check
    pub category: CheckCategory,
}

/// Severity levels for checks.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Severity {
    /// Critical issue that prevents production use
    Critical,

    /// Warning that should be addressed
    Warning,

    /// Informational notice
    Info,
}

/// Categories of checks.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CheckCategory {
    /// System resource checks
    Resources,

    /// Configuration validation
    Configuration,

    /// Performance validation
    Performance,

    /// Security validation
    Security,

    /// Compatibility validation
    Compatibility,
}

/// Benchmark results for performance validation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkResults {
    /// Average real-time factor
    pub average_rtf: f32,

    /// Average latency in milliseconds
    pub average_latency_ms: u64,

    /// Peak memory usage in MB
    pub peak_memory_mb: f64,

    /// Number of test syntheses performed
    pub test_count: usize,

    /// Duration of benchmark
    pub benchmark_duration: Duration,
}

/// System information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemInfo {
    /// Number of CPU cores
    pub cpu_cores: usize,

    /// Total memory in GB
    pub total_memory_gb: f64,

    /// Available disk space in GB
    pub available_disk_gb: f64,

    /// Operating system platform
    pub platform: String,

    /// Architecture
    pub architecture: String,
}

/// Recommendation for improvement.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Recommendation {
    /// Priority level
    pub priority: RecommendationPriority,

    /// Category
    pub category: CheckCategory,

    /// Recommendation message
    pub message: String,

    /// Optional action to take
    pub action: Option<String>,
}

/// Priority levels for recommendations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RecommendationPriority {
    /// High priority - should address immediately
    High,

    /// Medium priority - address when convenient
    Medium,

    /// Low priority - optional optimization
    Low,
}

/// Production readiness checker.
#[derive(Debug)]
pub struct ProductionReadiness {
    config: ReadinessConfig,
}

impl ProductionReadiness {
    /// Create a new production readiness checker.
    pub fn new(config: ReadinessConfig) -> Self {
        Self { config }
    }

    /// Run comprehensive readiness checks.
    pub async fn check_readiness(&self) -> Result<ReadinessReport> {
        let start_time = Instant::now();
        let mut checks = Vec::new();
        let mut recommendations = Vec::new();

        // Gather system information
        let system_info = self.gather_system_info().await?;

        // Run resource checks
        checks.extend(self.check_resources(&system_info)?);

        // Run configuration checks
        checks.extend(self.check_configuration()?);

        // Run compatibility checks
        checks.extend(self.check_compatibility()?);

        // Run security checks if enabled
        if self.config.enable_security_checks {
            checks.extend(self.check_security()?);
        }

        // Run performance benchmark if enabled
        let benchmark_results = if self.config.enable_benchmarking {
            Some(self.run_benchmark().await?)
        } else {
            None
        };

        // Validate benchmark results
        if let Some(ref bench) = benchmark_results {
            checks.extend(self.validate_performance(bench)?);
        }

        // Generate recommendations based on checks
        recommendations.extend(self.generate_recommendations(&checks, &system_info)?);

        // Determine overall readiness
        let is_ready = !checks
            .iter()
            .any(|c| c.severity == Severity::Critical && !c.passed);

        let elapsed = start_time.elapsed();
        tracing::info!("Production readiness check completed in {:?}", elapsed);

        Ok(ReadinessReport {
            is_ready,
            timestamp: chrono::Utc::now().to_rfc3339(),
            checks,
            benchmark_results,
            system_info,
            recommendations,
        })
    }

    async fn gather_system_info(&self) -> Result<SystemInfo> {
        Ok(SystemInfo {
            cpu_cores: num_cpus::get(),
            total_memory_gb: 16.0, // Simplified - would use sysinfo in production
            available_disk_gb: 100.0, // Simplified - would use sysinfo in production
            platform: std::env::consts::OS.to_string(),
            architecture: std::env::consts::ARCH.to_string(),
        })
    }

    fn check_resources(&self, system_info: &SystemInfo) -> Result<Vec<CheckResult>> {
        let mut checks = Vec::new();

        // Check CPU cores
        checks.push(CheckResult {
            name: "CPU Cores".to_string(),
            passed: system_info.cpu_cores >= self.config.min_cpu_cores,
            severity: Severity::Warning,
            message: format!(
                "CPU cores: {} (minimum: {})",
                system_info.cpu_cores, self.config.min_cpu_cores
            ),
            category: CheckCategory::Resources,
        });

        // Check memory
        checks.push(CheckResult {
            name: "Memory".to_string(),
            passed: system_info.total_memory_gb >= self.config.min_memory_gb,
            severity: Severity::Critical,
            message: format!(
                "Total memory: {:.2} GB (minimum: {:.2} GB)",
                system_info.total_memory_gb, self.config.min_memory_gb
            ),
            category: CheckCategory::Resources,
        });

        // Check disk space
        checks.push(CheckResult {
            name: "Disk Space".to_string(),
            passed: system_info.available_disk_gb >= self.config.min_disk_gb,
            severity: Severity::Warning,
            message: format!(
                "Available disk: {:.2} GB (minimum: {:.2} GB)",
                system_info.available_disk_gb, self.config.min_disk_gb
            ),
            category: CheckCategory::Resources,
        });

        Ok(checks)
    }

    fn check_configuration(&self) -> Result<Vec<CheckResult>> {
        let mut checks = Vec::new();

        // Check cache directory
        if let Some(ref cache_dir) = self.config.cache_dir {
            let exists = cache_dir.exists();
            checks.push(CheckResult {
                name: "Cache Directory".to_string(),
                passed: exists,
                severity: Severity::Warning,
                message: if exists {
                    format!("Cache directory exists: {:?}", cache_dir)
                } else {
                    format!("Cache directory does not exist: {:?}", cache_dir)
                },
                category: CheckCategory::Configuration,
            });
        }

        Ok(checks)
    }

    fn check_compatibility(&self) -> Result<Vec<CheckResult>> {
        let checks = vec![CheckResult {
            name: "Rust Version".to_string(),
            passed: true, // Would check actual version in production
            severity: Severity::Info,
            message: "Rust version compatible".to_string(),
            category: CheckCategory::Compatibility,
        }];

        Ok(checks)
    }

    fn check_security(&self) -> Result<Vec<CheckResult>> {
        // Security checks would go here
        // For example: check permissions, encryption settings, etc.

        let checks = vec![CheckResult {
            name: "Security Configuration".to_string(),
            passed: true,
            severity: Severity::Info,
            message: "Security checks passed".to_string(),
            category: CheckCategory::Security,
        }];

        Ok(checks)
    }

    async fn run_benchmark(&self) -> Result<BenchmarkResults> {
        let start = Instant::now();

        // Simplified benchmark - would run actual synthesis in production
        tokio::time::sleep(Duration::from_millis(100)).await;

        Ok(BenchmarkResults {
            average_rtf: 0.3,
            average_latency_ms: 80,
            peak_memory_mb: 150.0,
            test_count: 10,
            benchmark_duration: start.elapsed(),
        })
    }

    fn validate_performance(&self, bench: &BenchmarkResults) -> Result<Vec<CheckResult>> {
        let mut checks = Vec::new();

        // Check RTF
        checks.push(CheckResult {
            name: "Real-Time Factor".to_string(),
            passed: bench.average_rtf <= self.config.max_acceptable_rtf,
            severity: Severity::Critical,
            message: format!(
                "Average RTF: {:.3} (maximum: {:.3})",
                bench.average_rtf, self.config.max_acceptable_rtf
            ),
            category: CheckCategory::Performance,
        });

        // Check latency
        checks.push(CheckResult {
            name: "Latency".to_string(),
            passed: bench.average_latency_ms <= self.config.max_latency_ms,
            severity: Severity::Warning,
            message: format!(
                "Average latency: {} ms (maximum: {} ms)",
                bench.average_latency_ms, self.config.max_latency_ms
            ),
            category: CheckCategory::Performance,
        });

        Ok(checks)
    }

    fn generate_recommendations(
        &self,
        checks: &[CheckResult],
        system_info: &SystemInfo,
    ) -> Result<Vec<Recommendation>> {
        let mut recommendations = Vec::new();

        // Generate recommendations based on failed checks
        for check in checks {
            if !check.passed {
                let priority = match check.severity {
                    Severity::Critical => RecommendationPriority::High,
                    Severity::Warning => RecommendationPriority::Medium,
                    Severity::Info => RecommendationPriority::Low,
                };

                recommendations.push(Recommendation {
                    priority,
                    category: check.category,
                    message: format!("Address: {}", check.message),
                    action: None,
                });
            }
        }

        // Add general recommendations based on system info
        if system_info.cpu_cores < 8 {
            recommendations.push(Recommendation {
                priority: RecommendationPriority::Medium,
                category: CheckCategory::Performance,
                message: "Consider upgrading to 8+ CPU cores for better performance".to_string(),
                action: Some("Hardware upgrade".to_string()),
            });
        }

        Ok(recommendations)
    }
}

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

    #[test]
    fn test_readiness_config_default() {
        let config = ReadinessConfig::default();
        assert_eq!(config.min_cpu_cores, 4);
        assert_eq!(config.min_memory_gb, 4.0);
        assert!(config.enable_benchmarking);
    }

    #[tokio::test]
    async fn test_production_readiness_check() {
        let checker = ProductionReadiness::new(ReadinessConfig::default());
        let report = checker.check_readiness().await.unwrap();

        assert!(!report.checks.is_empty());
        assert!(report.system_info.cpu_cores > 0);
    }

    #[test]
    fn test_readiness_report_critical_issues() {
        let report = ReadinessReport {
            is_ready: false,
            timestamp: "2024-01-01T00:00:00Z".to_string(),
            checks: vec![CheckResult {
                name: "Test".to_string(),
                passed: false,
                severity: Severity::Critical,
                message: "Critical issue".to_string(),
                category: CheckCategory::Resources,
            }],
            benchmark_results: None,
            system_info: SystemInfo {
                cpu_cores: 4,
                total_memory_gb: 8.0,
                available_disk_gb: 50.0,
                platform: "linux".to_string(),
                architecture: "x86_64".to_string(),
            },
            recommendations: vec![],
        };

        let issues = report.critical_issues();
        assert_eq!(issues.len(), 1);
        assert!(issues[0].contains("Critical issue"));
    }

    #[test]
    fn test_report_string_generation() {
        let report = ReadinessReport {
            is_ready: true,
            timestamp: "2024-01-01T00:00:00Z".to_string(),
            checks: vec![],
            benchmark_results: None,
            system_info: SystemInfo {
                cpu_cores: 8,
                total_memory_gb: 16.0,
                available_disk_gb: 100.0,
                platform: "linux".to_string(),
                architecture: "x86_64".to_string(),
            },
            recommendations: vec![],
        };

        let report_str = report.to_report_string();
        assert!(report_str.contains("READY"));
        assert!(report_str.contains("CPU Cores: 8"));
    }
}