Skip to main content

dm_database_sqllog2db/tui/
progress.rs

1/// TUI 进度事件系统
2/// 用于导出任务将进度信息通过通道发送给 TUI
3use std::sync::Arc;
4use std::sync::atomic::{AtomicU64, Ordering};
5
6/// 进度事件
7#[derive(Debug, Clone)]
8pub enum ProgressEvent {
9    /// 任务开始
10    Started {
11        total_files: usize,
12        exporter_name: String,
13    },
14    /// 文件开始处理
15    FileStarted {
16        file_index: usize,
17        file_name: String,
18    },
19    /// 批次导出完成
20    BatchExported {
21        file_index: usize,
22        records: usize,
23        errors: usize,
24    },
25    /// 文件处理完成
26    FileCompleted { file_index: usize },
27    /// 所有文件处理完成
28    Completed {
29        total_records: usize,
30        total_errors: usize,
31        elapsed_secs: f64,
32    },
33    /// 错误发生
34    Error { message: String },
35}
36
37/// 共享的进度跟踪器
38/// 用于在导出线程中原子地更新统计信息
39#[derive(Debug, Clone)]
40pub struct ProgressTracker {
41    current_file_index: Arc<AtomicU64>,
42    total_records: Arc<AtomicU64>,
43    total_errors: Arc<AtomicU64>,
44}
45
46impl ProgressTracker {
47    #[must_use]
48    pub fn new() -> Self {
49        Self {
50            current_file_index: Arc::new(AtomicU64::new(0)),
51            total_records: Arc::new(AtomicU64::new(0)),
52            total_errors: Arc::new(AtomicU64::new(0)),
53        }
54    }
55
56    pub fn set_file_index(&self, index: u64) {
57        self.current_file_index.store(index, Ordering::Relaxed);
58    }
59
60    pub fn add_records(&self, count: u64) {
61        self.total_records.fetch_add(count, Ordering::Relaxed);
62    }
63
64    pub fn add_errors(&self, count: u64) {
65        self.total_errors.fetch_add(count, Ordering::Relaxed);
66    }
67
68    #[must_use]
69    pub fn get_file_index(&self) -> u64 {
70        self.current_file_index.load(Ordering::Relaxed)
71    }
72
73    #[must_use]
74    pub fn get_total_records(&self) -> u64 {
75        self.total_records.load(Ordering::Relaxed)
76    }
77
78    #[must_use]
79    pub fn get_total_errors(&self) -> u64 {
80        self.total_errors.load(Ordering::Relaxed)
81    }
82}
83
84impl Default for ProgressTracker {
85    fn default() -> Self {
86        Self::new()
87    }
88}