summaryrefslogtreecommitdiff
path: root/src/utils/mod.rs
blob: b89357e3c640f4953ad10825d0a83d9854e01a30 (plain)
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
//! Utility functions and types for the geek-szitman-supercamera crate

use std::time::{Duration, Instant};
use tracing::info;

/// Signal handler for graceful shutdown
pub struct SignalHandler {
    shutdown_requested: std::sync::Arc<std::sync::atomic::AtomicBool>,
}

impl SignalHandler {
    /// Create a new signal handler
    pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
        let shutdown_requested = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
        let shutdown_clone = shutdown_requested.clone();

        ctrlc::set_handler(move || {
            info!("Received shutdown signal");
            shutdown_clone.store(true, std::sync::atomic::Ordering::SeqCst);
        })?;

        Ok(Self { shutdown_requested })
    }

    /// Check if shutdown was requested
    pub fn shutdown_requested(&self) -> bool {
        self.shutdown_requested
            .load(std::sync::atomic::Ordering::SeqCst)
    }

    /// Wait for shutdown signal
    pub fn wait_for_shutdown(&self) {
        while !self.shutdown_requested() {
            std::thread::sleep(Duration::from_millis(100));
        }
    }

    /// Request shutdown programmatically
    pub fn request_shutdown(&self) {
        self.shutdown_requested
            .store(true, std::sync::atomic::Ordering::SeqCst);
    }

    /// Wait for shutdown signal with timeout. Returns true if shutdown was requested, false if timed out.
    pub fn wait_for_shutdown_with_timeout(&self, timeout: Duration) -> bool {
        let start = Instant::now();
        while !self.shutdown_requested() {
            if start.elapsed() >= timeout {
                return false; // timed out
            }
            std::thread::sleep(Duration::from_millis(100));
        }
        true
    }
}

/// Performance metrics tracker
pub struct PerformanceTracker {
    start_time: Instant,
    frame_count: u64,
    total_bytes: u64,
    last_fps_update: Instant,
    current_fps: f64,
}

impl PerformanceTracker {
    /// Create a new performance tracker
    pub fn new() -> Self {
        Self {
            start_time: Instant::now(),
            frame_count: 0,
            total_bytes: 0,
            last_fps_update: Instant::now(),
            current_fps: 0.0,
        }
    }

    /// Record a frame
    pub fn record_frame(&mut self, bytes: usize) {
        self.frame_count += 1;
        self.total_bytes += bytes as u64;

        // Update FPS every second
        let now = Instant::now();
        if now.duration_since(self.last_fps_update) >= Duration::from_secs(1) {
            let elapsed = now.duration_since(self.last_fps_update).as_secs_f64();
            self.current_fps = 1.0 / elapsed;
            self.last_fps_update = now;
        }
    }

    /// Get current FPS
    pub fn current_fps(&self) -> f64 {
        self.current_fps
    }

    /// Get total frame count
    pub fn total_frames(&self) -> u64 {
        self.frame_count
    }

    /// Get total bytes processed
    pub fn total_bytes(&self) -> u64 {
        self.total_bytes
    }

    /// Get average frame size
    pub fn average_frame_size(&self) -> f64 {
        if self.frame_count > 0 {
            self.total_bytes as f64 / self.frame_count as f64
        } else {
            0.0
        }
    }

    /// Get uptime
    pub fn uptime(&self) -> Duration {
        self.start_time.elapsed()
    }

    /// Get performance summary
    pub fn summary(&self) -> PerformanceSummary {
        PerformanceSummary {
            uptime: self.uptime(),
            total_frames: self.total_frames(),
            total_bytes: self.total_bytes(),
            current_fps: self.current_fps(),
            average_frame_size: self.average_frame_size(),
        }
    }
}

impl Default for PerformanceTracker {
    fn default() -> Self {
        Self::new()
    }
}

/// Performance summary
#[derive(Debug, Clone)]
pub struct PerformanceSummary {
    pub uptime: Duration,
    pub total_frames: u64,
    pub total_bytes: u64,
    pub current_fps: f64,
    pub average_frame_size: f64,
}

impl PerformanceSummary {
    /// Format uptime as human-readable string
    pub fn uptime_formatted(&self) -> String {
        let secs = self.uptime.as_secs();
        let hours = secs / 3600;
        let minutes = (secs % 3600) / 60;
        let seconds = secs % 60;

        if hours > 0 {
            format!("{hours}h {minutes}m {seconds}s")
        } else if minutes > 0 {
            format!("{minutes}m {seconds}s")
        } else {
            format!("{seconds}s")
        }
    }

    /// Format bytes as human-readable string
    pub fn bytes_formatted(&self) -> String {
        const UNITS: [&str; 4] = ["B", "KB", "MB", "GB"];
        let mut size = self.total_bytes as f64;
        let mut unit_index = 0;

        while size >= 1024.0 && unit_index < UNITS.len() - 1 {
            size /= 1024.0;
            unit_index += 1;
        }

        format!("{:.1} {}", size, UNITS[unit_index])
    }
}

/// Configuration file loader
pub struct ConfigLoader;

impl ConfigLoader {
    /// Load configuration from file
    pub fn load_from_file<T>(path: &str) -> Result<T, Box<dyn std::error::Error>>
    where
        T: serde::de::DeserializeOwned,
    {
        let content = std::fs::read_to_string(path)?;
        let config: T = serde_json::from_str(&content)?;
        Ok(config)
    }

    /// Save configuration to file
    pub fn save_to_file<T>(path: &str, config: &T) -> Result<(), Box<dyn std::error::Error>>
    where
        T: serde::Serialize,
    {
        let content = serde_json::to_string_pretty(config)?;
        std::fs::write(path, content)?;
        Ok(())
    }

    /// Load configuration with fallback to defaults
    pub fn load_with_defaults<T>(path: &str) -> T
    where
        T: serde::de::DeserializeOwned + Default,
    {
        Self::load_from_file(path).unwrap_or_default()
    }
}

/// File utilities
pub struct FileUtils;

impl FileUtils {
    /// Ensure directory exists
    pub fn ensure_dir(path: &str) -> Result<(), Box<dyn std::error::Error>> {
        std::fs::create_dir_all(path)?;
        Ok(())
    }

    /// Get file size
    pub fn get_file_size(path: &str) -> Result<u64, Box<dyn std::error::Error>> {
        let metadata = std::fs::metadata(path)?;
        Ok(metadata.len())
    }

    /// Check if file exists
    pub fn file_exists(path: &str) -> bool {
        std::path::Path::new(path).exists()
    }

    /// Get file extension
    pub fn get_extension(path: &str) -> Option<String> {
        std::path::Path::new(path)
            .extension()
            .and_then(|ext| ext.to_str())
            .map(|s| s.to_string())
    }
}

/// Time utilities
pub struct TimeUtils;

impl TimeUtils {
    /// Format duration as human-readable string
    pub fn format_duration(duration: Duration) -> String {
        let secs = duration.as_secs();
        let millis = duration.subsec_millis();

        if secs > 0 {
            format!("{secs}.{millis:03}s")
        } else {
            format!("{millis}ms")
        }
    }

    /// Sleep with progress callback
    pub fn sleep_with_progress<F>(duration: Duration, mut progress_callback: F)
    where
        F: FnMut(f64),
    {
        let start = Instant::now();
        let total_duration = duration.as_millis() as f64;

        while start.elapsed() < duration {
            let elapsed = start.elapsed().as_millis() as f64;
            let progress = (elapsed / total_duration).min(1.0);
            progress_callback(progress);

            std::thread::sleep(Duration::from_millis(10));
        }

        progress_callback(1.0);
    }
}

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

    #[test]
    fn test_performance_tracker() {
        let mut tracker = PerformanceTracker::new();

        // Record some frames
        tracker.record_frame(1024);
        tracker.record_frame(2048);
        tracker.record_frame(1536);

        assert_eq!(tracker.total_frames(), 3);
        assert_eq!(tracker.total_bytes(), 4608);
        assert_eq!(tracker.average_frame_size(), 1536.0);
    }

    #[test]
    fn test_performance_summary() {
        let mut tracker = PerformanceTracker::new();
        tracker.record_frame(1024);

        let summary = tracker.summary();
        assert_eq!(summary.total_frames, 1);
        assert_eq!(summary.total_bytes, 1024);
        assert_eq!(summary.average_frame_size, 1024.0);

        // Test formatting
        assert!(summary.uptime_formatted().contains("s"));
        assert_eq!(summary.bytes_formatted(), "1.0 KB");
    }

    #[test]
    fn test_file_utils() {
        // Test file existence check
        assert!(FileUtils::file_exists("src/utils/mod.rs"));
        assert!(!FileUtils::file_exists("nonexistent_file.txt"));

        // Test extension extraction
        assert_eq!(
            FileUtils::get_extension("test.txt"),
            Some("txt".to_string())
        );
        assert_eq!(FileUtils::get_extension("no_extension"), None);
    }

    #[test]
    fn test_time_utils() {
        let duration = Duration::from_millis(1500);
        let formatted = TimeUtils::format_duration(duration);
        assert!(formatted.contains("1.500s"));

        let short_duration = Duration::from_millis(500);
        let formatted = TimeUtils::format_duration(short_duration);
        assert!(formatted.contains("500ms"));
    }

    #[test]
    fn test_sleep_with_progress() {
        let mut progress_values = Vec::new();
        let duration = Duration::from_millis(100);

        TimeUtils::sleep_with_progress(duration, |progress| {
            progress_values.push(progress);
        });

        assert!(!progress_values.is_empty());
        assert!(progress_values.last().unwrap() >= &1.0);
    }
}