markdown-translator 0.1.1

A translation library with DeepLX API integration, rate limiting, and smart text chunking
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
//! 统一的翻译引擎模块
//!
//! 这个模块合并了原来的翻译引擎和服务,提供了一个统一的、
//! 函数式风格的翻译处理系统。
//!
//! ## 设计原则
//!
//! - **函数式组合**: 使用函数组合和管道操作
//! - **不可变性**: 优先使用不可变数据结构
//! - **错误处理**: 统一的Result类型和错误传播
//! - **异步友好**: 原生支持async/await模式

use std::sync::Arc;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::time::{Instant, Duration};

use crate::{
    TranslationService as BaseTranslationService,
    functional::{TextItem, TextFilter, BatchManager, Batch},
    collector::{DomNode, TextCollector},
    simple_config::SimpleTranslationConfig,
    error::{TranslationError, Result as TranslationResult},
};

// ============================================================================
// 统计和监控
// ============================================================================

/// 翻译引擎统计信息
#[derive(Debug, Default)]
pub struct EngineStats {
    /// 处理的文本总数
    pub texts_processed: AtomicUsize,
    /// 翻译的批次总数
    pub batches_processed: AtomicUsize,
    /// 缓存命中次数
    pub cache_hits: AtomicUsize,
    /// 缓存未命中次数
    pub cache_misses: AtomicUsize,
    /// 翻译API调用次数
    pub api_calls: AtomicUsize,
    /// 总翻译时间(毫秒)
    pub total_translation_time_ms: AtomicU64,
    /// 错误次数
    pub error_count: AtomicUsize,
}

impl EngineStats {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn texts_processed(&self) -> usize {
        self.texts_processed.load(Ordering::Relaxed)
    }

    pub fn batches_processed(&self) -> usize {
        self.batches_processed.load(Ordering::Relaxed)
    }

    pub fn cache_hit_rate(&self) -> f64 {
        let hits = self.cache_hits.load(Ordering::Relaxed);
        let misses = self.cache_misses.load(Ordering::Relaxed);
        let total = hits + misses;
        if total == 0 {
            0.0
        } else {
            hits as f64 / total as f64
        }
    }

    pub fn average_translation_time_ms(&self) -> f64 {
        let total_time = self.total_translation_time_ms.load(Ordering::Relaxed);
        let api_calls = self.api_calls.load(Ordering::Relaxed);
        if api_calls == 0 {
            0.0
        } else {
            total_time as f64 / api_calls as f64
        }
    }

    pub fn error_rate(&self) -> f64 {
        let errors = self.error_count.load(Ordering::Relaxed);
        let total = self.api_calls.load(Ordering::Relaxed);
        if total == 0 {
            0.0
        } else {
            errors as f64 / total as f64
        }
    }

    fn increment_texts_processed(&self, count: usize) {
        self.texts_processed.fetch_add(count, Ordering::Relaxed);
    }

    fn increment_batches_processed(&self) {
        self.batches_processed.fetch_add(1, Ordering::Relaxed);
    }

    fn increment_cache_hits(&self) {
        self.cache_hits.fetch_add(1, Ordering::Relaxed);
    }

    fn increment_cache_misses(&self) {
        self.cache_misses.fetch_add(1, Ordering::Relaxed);
    }

    fn increment_api_calls(&self) {
        self.api_calls.fetch_add(1, Ordering::Relaxed);
    }

    fn add_translation_time(&self, duration: Duration) {
        self.total_translation_time_ms.fetch_add(
            duration.as_millis() as u64, 
            Ordering::Relaxed
        );
    }

    fn increment_errors(&self) {
        self.error_count.fetch_add(1, Ordering::Relaxed);
    }
}

// ============================================================================
// 简单缓存实现
// ============================================================================

use std::collections::HashMap;
use std::sync::Mutex;

/// 简单的内存缓存
struct SimpleCache {
    cache: Mutex<HashMap<String, (String, Instant)>>,
    ttl: Duration,
}

impl SimpleCache {
    fn new(ttl: Duration) -> Self {
        Self {
            cache: Mutex::new(HashMap::new()),
            ttl,
        }
    }

    fn get(&self, key: &str) -> Option<String> {
        let mut cache = self.cache.lock().ok()?;
        
        if let Some((value, timestamp)) = cache.get(key) {
            if timestamp.elapsed() < self.ttl {
                return Some(value.clone());
            } else {
                cache.remove(key);
            }
        }
        
        None
    }

    fn set(&self, key: String, value: String) {
        if let Ok(mut cache) = self.cache.lock() {
            cache.insert(key, (value, Instant::now()));
        }
    }

    fn clear_expired(&self) {
        if let Ok(mut cache) = self.cache.lock() {
            let now = Instant::now();
            cache.retain(|_, (_, timestamp)| now.duration_since(*timestamp) < self.ttl);
        }
    }
}

// ============================================================================
// 统一翻译引擎
// ============================================================================

/// 统一的翻译引擎
/// 
/// 合并了原来的翻译引擎和服务功能,提供简化的API和更好的性能。
pub struct UnifiedTranslationEngine {
    /// 基础翻译服务
    base_service: BaseTranslationService,
    /// 配置
    config: SimpleTranslationConfig,
    /// 文本过滤器
    filter: TextFilter,
    /// 文本收集器
    collector: TextCollector,
    /// 批次管理器
    batch_manager: BatchManager,
    /// 简单缓存
    cache: Option<SimpleCache>,
    /// 统计信息
    stats: Arc<EngineStats>,
}

impl UnifiedTranslationEngine {
    /// 创建新的翻译引擎
    pub fn new(config: SimpleTranslationConfig) -> TranslationResult<Self> {
        let base_service = BaseTranslationService::new(crate::types::TranslationConfig {
            enabled: config.enabled,
            source_lang: config.source_lang.clone(),
            target_lang: config.target_lang.clone(),
            deeplx_api_url: config.api_url.clone(),
            max_requests_per_second: config.requests_per_second,
            max_text_length: config.max_text_length,
            max_paragraphs_per_request: 10, // 固定值
        });

        let cache = if config.cache_enabled {
            Some(SimpleCache::new(config.cache_ttl()))
        } else {
            None
        };

        Ok(Self {
            base_service,
            config,
            filter: TextFilter::new(),
            collector: TextCollector::new(),
            batch_manager: BatchManager::new(),
            cache,
            stats: Arc::new(EngineStats::new()),
        })
    }

    /// 快速创建引擎
    pub fn quick(target_lang: &str, api_url: Option<&str>) -> TranslationResult<Self> {
        let config = crate::simple_config::quick_config(target_lang, api_url);
        Self::new(config)
    }

    /// 翻译单个文本
    pub async fn translate_text(&self, text: &str) -> TranslationResult<String> {
        if !self.config.enabled {
            return Ok(text.to_string());
        }

        if !self.filter.should_translate(text) {
            return Ok(text.to_string());
        }

        // 检查缓存
        let cache_key = format!("{}:{}", text, self.config.target_lang);
        if let Some(ref cache) = self.cache {
            if let Some(cached) = cache.get(&cache_key) {
                self.stats.increment_cache_hits();
                return Ok(cached);
            }
            self.stats.increment_cache_misses();
        }

        // 执行翻译
        let start = Instant::now();
        let result = self.base_service.translate(text).await
            .map_err(|e| TranslationError::ApiError { code: 500, message: e.to_string() })?;
        
        let duration = start.elapsed();
        self.stats.add_translation_time(duration);
        self.stats.increment_api_calls();
        self.stats.increment_texts_processed(1);

        // 存储到缓存
        if let Some(ref cache) = self.cache {
            cache.set(cache_key, result.clone());
        }

        Ok(result)
    }

    /// 翻译文本列表
    pub async fn translate_texts(&self, texts: &[String]) -> TranslationResult<Vec<String>> {
        if !self.config.enabled {
            return Ok(texts.to_vec());
        }

        // 过滤可翻译文本
        let translatable_items: Vec<TextItem> = texts
            .iter()
            .enumerate()
            .filter_map(|(i, text)| {
                if self.filter.should_translate(text) {
                    Some(crate::functional::create_text_item(
                        text.clone(), 
                        format!("text[{}]", i)
                    ))
                } else {
                    None
                }
            })
            .collect();

        // 创建批次
        let batches = self.batch_manager.create_batches(translatable_items);
        
        // 翻译每个批次
        let mut results = HashMap::new();
        for batch in batches {
            let batch_result = self.translate_batch(&batch).await?;
            for (item, translated) in batch.items.iter().zip(batch_result.iter()) {
                results.insert(item.location.clone(), translated.clone());
            }
        }

        // 构建结果向量
        let translated: Vec<String> = texts
            .iter()
            .enumerate()
            .map(|(i, original)| {
                let location = format!("text[{}]", i);
                results.get(&location).cloned().unwrap_or_else(|| original.clone())
            })
            .collect();

        Ok(translated)
    }

    /// 翻译批次
    async fn translate_batch(&self, batch: &Batch) -> TranslationResult<Vec<String>> {
        if batch.items.is_empty() {
            return Ok(Vec::new());
        }

        let texts: Vec<&str> = batch.items.iter().map(|item| item.text.as_str()).collect();
        let combined_text = texts.join("\n\n");

        let start = Instant::now();
        let translated = self.base_service.translate(&combined_text).await
            .map_err(|e| {
                self.stats.increment_errors();
                TranslationError::ApiError { code: 500, message: e.to_string() }
            })?;

        let duration = start.elapsed();
        self.stats.add_translation_time(duration);
        self.stats.increment_api_calls();
        self.stats.increment_batches_processed();
        self.stats.increment_texts_processed(batch.items.len());

        // 简单的分割逻辑(实际应用中需要更复杂的处理)
        let translated_parts: Vec<String> = translated
            .split("\n\n")
            .map(|s| s.trim().to_string())
            .collect();

        // 确保结果数量匹配
        if translated_parts.len() == texts.len() {
            Ok(translated_parts)
        } else {
            // 如果分割失败,返回原始文本
            Ok(texts.iter().map(|s| s.to_string()).collect())
        }
    }

    /// 从DOM节点收集并翻译文本
    pub async fn translate_dom_texts(&self, root: &dyn DomNode) -> TranslationResult<Vec<(String, String)>> {
        if !self.config.enabled {
            return Ok(Vec::new());
        }

        // 收集文本
        let text_items = self.collector.collect_texts(root);
        
        // 创建批次
        let batches = self.batch_manager.create_batches(text_items);
        
        // 翻译所有批次
        let mut results = Vec::new();
        for batch in batches {
            let translations = self.translate_batch(&batch).await?;
            for (item, translation) in batch.items.iter().zip(translations.iter()) {
                results.push((item.location.clone(), translation.clone()));
            }
        }

        Ok(results)
    }

    /// 获取统计信息
    pub fn stats(&self) -> Arc<EngineStats> {
        Arc::clone(&self.stats)
    }

    /// 获取配置
    pub fn config(&self) -> &SimpleTranslationConfig {
        &self.config
    }

    /// 清理过期缓存
    pub fn cleanup_cache(&self) {
        if let Some(ref cache) = self.cache {
            cache.clear_expired();
        }
    }

    /// 检查引擎健康状态
    pub fn health_check(&self) -> EngineHealth {
        let stats = &self.stats;
        
        let error_rate = stats.error_rate();
        let avg_time = stats.average_translation_time_ms();
        
        let status = if error_rate > 0.5 {
            HealthStatus::Critical
        } else if error_rate > 0.2 || avg_time > 5000.0 {
            HealthStatus::Warning
        } else {
            HealthStatus::Healthy
        };

        EngineHealth {
            status,
            error_rate,
            average_response_time_ms: avg_time,
            cache_hit_rate: stats.cache_hit_rate(),
            total_requests: stats.api_calls.load(Ordering::Relaxed),
        }
    }
}

// ============================================================================
// 健康检查
// ============================================================================

/// 引擎健康状态
#[derive(Debug, Clone)]
pub struct EngineHealth {
    pub status: HealthStatus,
    pub error_rate: f64,
    pub average_response_time_ms: f64,
    pub cache_hit_rate: f64,
    pub total_requests: usize,
}

/// 健康状态枚举
#[derive(Debug, Clone, PartialEq)]
pub enum HealthStatus {
    Healthy,
    Warning,
    Critical,
}

// ============================================================================
// 便利函数
// ============================================================================

/// 创建快速翻译引擎
pub fn create_engine(target_lang: &str, api_url: Option<&str>) -> TranslationResult<UnifiedTranslationEngine> {
    UnifiedTranslationEngine::quick(target_lang, api_url)
}

/// 创建开发环境引擎
pub fn create_dev_engine() -> TranslationResult<UnifiedTranslationEngine> {
    let config = crate::simple_config::presets::development();
    UnifiedTranslationEngine::new(config)
}

/// 创建生产环境引擎
pub fn create_prod_engine() -> TranslationResult<UnifiedTranslationEngine> {
    let config = crate::simple_config::presets::production();
    UnifiedTranslationEngine::new(config)
}

// ============================================================================
// 测试
// ============================================================================

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

    #[tokio::test]
    async fn test_engine_creation() {
        let engine = UnifiedTranslationEngine::quick("zh", Some("http://localhost:1188/translate"));
        assert!(engine.is_ok());
    }

    #[tokio::test]
    async fn test_engine_stats() {
        let engine = UnifiedTranslationEngine::quick("zh", Some("http://localhost:1188/translate"))
            .expect("Failed to create engine");
        
        let stats = engine.stats();
        assert_eq!(stats.texts_processed(), 0);
        assert_eq!(stats.batches_processed(), 0);
    }

    #[tokio::test]
    async fn test_health_check() {
        let engine = UnifiedTranslationEngine::quick("zh", Some("http://localhost:1188/translate"))
            .expect("Failed to create engine");
        
        let health = engine.health_check();
        assert_eq!(health.status, HealthStatus::Healthy);
    }

    #[tokio::test]
    async fn test_disabled_engine() {
        let mut config = crate::simple_config::quick_config("zh", Some("http://localhost:1188/translate"));
        config.enabled = false;
        
        let engine = UnifiedTranslationEngine::new(config)
            .expect("Failed to create engine");
        
        let result = engine.translate_text("Hello World").await.unwrap();
        assert_eq!(result, "Hello World");
    }

    #[tokio::test]
    async fn test_dom_text_collection() {
        // 创建禁用翻译的引擎来避免网络请求
        let mut config = crate::simple_config::quick_config("zh", Some("http://localhost:1188/translate"));
        config.enabled = false;
        
        let engine = UnifiedTranslationEngine::new(config)
            .expect("Failed to create engine");
        
        let root = TestDomNode::new_element("div")
            .with_child(TestDomNode::new_text("Hello World"));
        
        let results = engine.translate_dom_texts(&root).await;
        assert!(results.is_ok());
    }

    #[test]
    fn test_convenience_functions() {
        let engine = create_engine("zh", Some("http://localhost:1188/translate"));
        assert!(engine.is_ok());

        let dev_engine = create_dev_engine();
        assert!(dev_engine.is_ok());

        let prod_engine = create_prod_engine();
        assert!(prod_engine.is_ok());
    }
}