gravityfile_scan/
progress.rs1use std::path::PathBuf;
4use std::time::{Duration, Instant};
5
6#[derive(Debug, Clone)]
8pub struct ScanProgress {
9 pub files_scanned: u64,
11 pub dirs_scanned: u64,
13 pub bytes_scanned: u64,
15 pub current_path: PathBuf,
17 pub errors_count: u64,
19 pub elapsed: Duration,
21}
22
23impl ScanProgress {
24 pub fn new() -> Self {
26 Self {
27 files_scanned: 0,
28 dirs_scanned: 0,
29 bytes_scanned: 0,
30 current_path: PathBuf::new(),
31 errors_count: 0,
32 elapsed: Duration::ZERO,
33 }
34 }
35
36 pub fn files_per_second(&self) -> f64 {
38 if self.elapsed.as_secs_f64() > 0.0 {
39 self.files_scanned as f64 / self.elapsed.as_secs_f64()
40 } else {
41 0.0
42 }
43 }
44
45 pub fn bytes_per_second(&self) -> f64 {
47 if self.elapsed.as_secs_f64() > 0.0 {
48 self.bytes_scanned as f64 / self.elapsed.as_secs_f64()
49 } else {
50 0.0
51 }
52 }
53
54 pub fn total_items(&self) -> u64 {
56 self.files_scanned + self.dirs_scanned
57 }
58}
59
60impl Default for ScanProgress {
61 fn default() -> Self {
62 Self::new()
63 }
64}
65
66#[allow(dead_code)]
69#[derive(Debug)]
70pub(crate) struct ProgressTracker {
71 start_time: Instant,
72 files_scanned: u64,
73 dirs_scanned: u64,
74 bytes_scanned: u64,
75 errors_count: u64,
76 current_path: PathBuf,
77}
78
79#[allow(dead_code)]
80impl ProgressTracker {
81 pub fn new() -> Self {
82 Self {
83 start_time: Instant::now(),
84 files_scanned: 0,
85 dirs_scanned: 0,
86 bytes_scanned: 0,
87 errors_count: 0,
88 current_path: PathBuf::new(),
89 }
90 }
91
92 pub fn record_file(&mut self, size: u64) {
93 self.files_scanned += 1;
94 self.bytes_scanned += size;
95 }
96
97 pub fn record_dir(&mut self) {
98 self.dirs_scanned += 1;
99 }
100
101 pub fn record_error(&mut self) {
102 self.errors_count += 1;
103 }
104
105 pub fn set_current_path(&mut self, path: PathBuf) {
106 self.current_path = path;
107 }
108
109 pub fn snapshot(&self) -> ScanProgress {
110 ScanProgress {
111 files_scanned: self.files_scanned,
112 dirs_scanned: self.dirs_scanned,
113 bytes_scanned: self.bytes_scanned,
114 current_path: self.current_path.clone(),
115 errors_count: self.errors_count,
116 elapsed: self.start_time.elapsed(),
117 }
118 }
119}
120
121impl Default for ProgressTracker {
122 fn default() -> Self {
123 Self::new()
124 }
125}