pulith-fetch 0.2.0

HTTP downloading with streaming verification and atomic placement
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
//! Extended progress reporting functionality.
//!
//! This module provides enhanced progress reporting with detailed metrics,
//! rate calculations, and historical tracking.

use std::collections::VecDeque;
use std::time::{Instant, SystemTime, UNIX_EPOCH};

use crate::config::FetchPhase;
use crate::progress::PerformanceMetrics;
use crate::progress::Progress;

/// Extended progress information with detailed metrics.
#[derive(Debug, Clone)]
pub struct ExtendedProgress {
    /// Base progress information
    pub base: Progress,

    /// Download rate in bytes per second
    pub rate_bps: Option<f64>,

    /// Estimated time remaining in seconds
    pub eta_seconds: Option<u64>,

    /// Historical progress snapshots for rate calculation
    pub history: VecDeque<ProgressSnapshot>,

    /// Start time of the download
    pub start_time: Instant,

    /// Last update time
    pub last_update: Instant,

    /// Performance metrics collection
    pub performance_metrics: PerformanceMetrics,
}

/// A snapshot of progress at a specific point in time.
#[derive(Debug, Clone)]
pub struct ProgressSnapshot {
    /// Timestamp of the snapshot
    pub timestamp: u64,
    /// Bytes downloaded at that point
    pub bytes_downloaded: u64,
}

impl ExtendedProgress {
    /// Create a new extended progress tracker.
    pub fn new(mut base: Progress) -> Self {
        let now = Instant::now();
        let mut history = VecDeque::with_capacity(100);

        // Add initial snapshot
        history.push_back(ProgressSnapshot {
            timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_millis() as u64,
            bytes_downloaded: base.bytes_downloaded,
        });

        let performance_metrics = base.performance_metrics.take().unwrap_or_default();

        Self {
            base,
            rate_bps: None,
            eta_seconds: None,
            history,
            start_time: now,
            last_update: now,
            performance_metrics,
        }
    }

    /// Update progress with new data and recalculate metrics.
    pub fn update(&mut self, progress: Progress) {
        let now = Instant::now();

        // Add snapshot to history
        self.history.push_back(ProgressSnapshot {
            timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_millis() as u64,
            bytes_downloaded: progress.bytes_downloaded,
        });

        // Keep only recent history (last 100 snapshots)
        if self.history.len() > 100 {
            self.history.pop_front();
        }

        // Recalculate rate and ETA
        self.rate_bps = self.calculate_rate();
        self.eta_seconds = self.calculate_eta();

        // Calculate rate based on recent history
        self.rate_bps = self.calculate_rate();

        // Calculate ETA
        self.eta_seconds = self.calculate_eta();

        // Update base progress
        self.base = progress;
        self.last_update = now;
    }

    /// Calculate download rate based on recent history.
    fn calculate_rate(&self) -> Option<f64> {
        if self.history.len() < 2 {
            return None;
        }

        let recent = &self.history;
        let time_diff = recent.back().unwrap().timestamp - recent.front().unwrap().timestamp;

        if time_diff == 0 {
            return None;
        }

        let bytes_diff =
            recent.back().unwrap().bytes_downloaded - recent.front().unwrap().bytes_downloaded;
        let rate = bytes_diff as f64 / (time_diff as f64 / 1000.0);

        // Apply smoothing to reduce variance
        Some(rate)
    }

    /// Calculate estimated time remaining.
    fn calculate_eta(&self) -> Option<u64> {
        if let (Some(rate), Some(total)) = (self.rate_bps, self.base.total_bytes) {
            if rate > 0.0 && self.base.bytes_downloaded < total {
                let remaining = total - self.base.bytes_downloaded;
                Some((remaining as f64 / rate) as u64)
            } else {
                None
            }
        } else {
            None
        }
    }

    /// Get the current download speed formatted as a human-readable string.
    pub fn speed_string(&self) -> String {
        if let Some(rate) = self.rate_bps {
            if rate >= 1_000_000.0 {
                format!("{:.1} MB/s", rate / 1_000_000.0)
            } else if rate >= 1000.0 {
                format!("{:.1} kB/s", rate / 1000.0)
            } else {
                format!("{:.0} B/s", rate)
            }
        } else {
            "Unknown".to_string()
        }
    }

    /// Get the ETA formatted as a human-readable string.
    pub fn eta_string(&self) -> String {
        if let Some(eta) = self.eta_seconds {
            if eta >= 3600 {
                let hours = eta / 3600;
                let minutes = (eta % 3600) / 60;
                format!("{}h {}m", hours, minutes)
            } else if eta >= 60 {
                format!("{}m", eta / 60)
            } else {
                format!("{}s", eta)
            }
        } else {
            "Unknown".to_string()
        }
    }

    /// Get the elapsed time since download started.
    pub fn elapsed_seconds(&self) -> u64 {
        self.start_time.elapsed().as_secs()
    }

    /// Get the elapsed time formatted as a human-readable string.
    pub fn elapsed_string(&self) -> String {
        let elapsed = self.elapsed_seconds();
        if elapsed >= 3600 {
            let hours = elapsed / 3600;
            let minutes = (elapsed % 3600) / 60;
            format!("{}h {}m", hours, minutes)
        } else if elapsed >= 60 {
            format!("{}m", elapsed / 60)
        } else {
            format!("{}s", elapsed)
        }
    }
}

/// Progress reporter that handles multiple concurrent downloads.
pub struct ProgressReporter {
    /// Active progress trackers
    pub trackers: Vec<ExtendedProgress>,
}

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

impl ProgressReporter {
    /// Create a new progress reporter.
    pub fn new() -> Self {
        Self {
            trackers: Vec::new(),
        }
    }

    /// Add a new progress tracker.
    pub fn add_tracker(&mut self, progress: Progress) -> usize {
        let extended = ExtendedProgress::new(progress);
        self.trackers.push(extended);
        self.trackers.len() - 1
    }

    /// Update a progress tracker by index.
    pub fn update_tracker(&mut self, index: usize, progress: Progress) {
        if let Some(tracker) = self.trackers.get_mut(index) {
            tracker.update(progress);
        }
    }

    /// Get a progress tracker by index.
    pub fn get_tracker(&self, index: usize) -> Option<&ExtendedProgress> {
        self.trackers.get(index)
    }

    /// Get the total progress across all trackers.
    pub fn total_progress(&self) -> Progress {
        let total_bytes: u64 = self.trackers.iter().map(|t| t.base.bytes_downloaded).sum();
        let total_estimated: Option<u64> = self
            .trackers
            .iter()
            .filter_map(|t| t.base.total_bytes)
            .reduce(|acc, x| acc + x);

        Progress {
            phase: if self.trackers.iter().all(|t| t.base.is_completed()) {
                FetchPhase::Completed
            } else if self.trackers.iter().any(|t| t.base.is_retrying()) {
                FetchPhase::Connecting
            } else {
                FetchPhase::Downloading
            },
            bytes_downloaded: total_bytes,
            total_bytes: total_estimated,
            retry_count: self
                .trackers
                .iter()
                .map(|t| t.base.retry_count)
                .max()
                .unwrap_or(0),
            performance_metrics: None,
        }
    }

    /// Get the total download rate across all trackers.
    pub fn total_rate(&self) -> Option<f64> {
        let total_rate: f64 = self.trackers.iter().filter_map(|t| t.rate_bps).sum();

        if total_rate > 0.0 {
            Some(total_rate)
        } else {
            None
        }
    }

    /// Get the total ETA across all trackers.
    pub fn total_eta(&self) -> Option<u64> {
        let total_remaining: u64 = self
            .trackers
            .iter()
            .filter_map(|t| {
                if let (Some(total), Some(downloaded)) =
                    (t.base.total_bytes, Some(t.base.bytes_downloaded))
                {
                    if downloaded < total {
                        Some(total - downloaded)
                    } else {
                        None
                    }
                } else {
                    None
                }
            })
            .sum();

        if let (Some(total_rate), _) = (self.total_rate(), Some(total_remaining)) {
            if total_rate > 0.0 {
                Some((total_remaining as f64 / total_rate) as u64)
            } else {
                None
            }
        } else {
            None
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;
    use tokio::time::sleep;

    #[test]
    fn test_extended_progress_creation() {
        let base = Progress {
            phase: FetchPhase::Downloading,
            bytes_downloaded: 512,
            total_bytes: Some(1024),
            retry_count: 0,
            performance_metrics: None,
        };

        let extended = ExtendedProgress::new(base);

        assert_eq!(extended.base.bytes_downloaded, 512);
        assert_eq!(extended.base.total_bytes, Some(1024));
        assert_eq!(extended.rate_bps, None);
        assert_eq!(extended.eta_seconds, None);
        assert_eq!(extended.history.len(), 1); // Initial snapshot
    }

    #[tokio::test]
    async fn test_rate_calculation() {
        let mut extended = ExtendedProgress::new(Progress {
            phase: FetchPhase::Downloading,
            bytes_downloaded: 0,
            total_bytes: Some(1000),
            retry_count: 0,
            performance_metrics: None,
        });

        // Simulate progress updates with controlled timing
        let _start = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();

        // Add first snapshot
        extended.update(Progress {
            phase: FetchPhase::Downloading,
            bytes_downloaded: 100,
            total_bytes: Some(1000),
            retry_count: 0,
            performance_metrics: None,
        });

        // Add second snapshot after 1 second
        tokio::time::sleep(Duration::from_secs(1)).await;
        extended.update(Progress {
            phase: FetchPhase::Downloading,
            bytes_downloaded: 200,
            total_bytes: Some(1000),
            retry_count: 0,
            performance_metrics: None,
        });

        // Should have a calculated rate
        assert!(extended.rate_bps.is_some());
        assert!(extended.rate_bps.unwrap() > 0.0);

        // Rate should be around 100 bytes per second (200-100 over 1 second)
        // Allow for some timing variance
        let rate = extended.rate_bps.unwrap();
        assert!(rate > 10.0 && rate < 500.0); // Wider range for timing variance
    }

    #[tokio::test]
    async fn test_eta_calculation() {
        let mut extended = ExtendedProgress::new(Progress {
            phase: FetchPhase::Downloading,
            bytes_downloaded: 0,
            total_bytes: Some(1000),
            retry_count: 0,
            performance_metrics: None,
        });

        // Simulate progress updates
        for i in 0..5 {
            let progress = Progress {
                phase: FetchPhase::Downloading,
                bytes_downloaded: (i + 1) * 200,
                total_bytes: Some(1000),
                retry_count: 0,
                performance_metrics: None,
            };
            extended.update(progress);
            sleep(Duration::from_millis(100)).await;
        }

        // Should have an ETA
        assert!(extended.eta_seconds.is_some());
        let eta = extended.eta_seconds.unwrap();

        // With 1000 bytes total and 1000 bytes downloaded, ETA should be minimal
        assert!(eta <= 10); // More lenient threshold
    }

    #[test]
    fn test_speed_string() {
        let mut extended = ExtendedProgress::new(Progress {
            phase: FetchPhase::Downloading,
            bytes_downloaded: 0,
            total_bytes: Some(1024),
            retry_count: 0,
            performance_metrics: None,
        });

        // No rate yet
        assert_eq!(extended.speed_string(), "Unknown");

        // Set a rate
        extended.rate_bps = Some(1024.0);
        assert_eq!(extended.speed_string(), "1.0 kB/s");

        // MB/s
        extended.rate_bps = Some(2_048_000.0);
        assert_eq!(extended.speed_string(), "2.0 MB/s");

        // B/s
        extended.rate_bps = Some(512.0);
        assert_eq!(extended.speed_string(), "512 B/s");
    }

    #[test]
    fn test_eta_string() {
        let mut extended = ExtendedProgress::new(Progress {
            phase: FetchPhase::Downloading,
            bytes_downloaded: 0,
            total_bytes: Some(1024),
            retry_count: 0,
            performance_metrics: None,
        });

        // No ETA yet
        assert_eq!(extended.eta_string(), "Unknown");

        // Seconds
        extended.eta_seconds = Some(30);
        assert_eq!(extended.eta_string(), "30s");

        // Minutes
        extended.eta_seconds = Some(90);
        assert_eq!(extended.eta_string(), "1m");

        // Hours and minutes
        extended.eta_seconds = Some(3661);
        assert_eq!(extended.eta_string(), "1h 1m");

        // Hours only (but will show minutes as 0)
        extended.eta_seconds = Some(7200);
        assert_eq!(extended.eta_string(), "2h 0m");
    }

    #[tokio::test]
    async fn test_elapsed_time() {
        let extended = ExtendedProgress::new(Progress {
            phase: FetchPhase::Downloading,
            bytes_downloaded: 0,
            total_bytes: Some(1024),
            retry_count: 0,
            performance_metrics: None,
        });

        // Initially 0 seconds elapsed
        assert_eq!(extended.elapsed_seconds(), 0);

        // After some time
        sleep(Duration::from_millis(1500)).await;
        assert!(extended.elapsed_seconds() >= 1);

        // Format string
        assert_eq!(extended.elapsed_string(), "1s");

        // Minutes
        sleep(Duration::from_secs(90)).await;
        assert!(extended.elapsed_seconds() >= 91);
        assert_eq!(extended.elapsed_string(), "1m");
    }

    #[test]
    fn test_progress_reporter() {
        let mut reporter = ProgressReporter::new();

        // Add some trackers
        let progress1 = Progress {
            phase: FetchPhase::Downloading,
            bytes_downloaded: 256,
            total_bytes: Some(512),
            retry_count: 0,
            performance_metrics: None,
        };
        let progress2 = Progress {
            phase: FetchPhase::Downloading,
            bytes_downloaded: 128,
            total_bytes: Some(256),
            retry_count: 1,
            performance_metrics: None,
        };

        let id1 = reporter.add_tracker(progress1);
        let id2 = reporter.add_tracker(progress2);

        assert_eq!(id1, 0);
        assert_eq!(id2, 1);
        assert_eq!(reporter.trackers.len(), 2);

        // Update progress
        let updated1 = Progress {
            phase: FetchPhase::Downloading,
            bytes_downloaded: 512,
            total_bytes: Some(512),
            retry_count: 0,
            performance_metrics: None,
        };
        reporter.update_tracker(0, updated1);

        assert_eq!(reporter.get_tracker(0).unwrap().base.bytes_downloaded, 512);

        // Total progress
        let total = reporter.total_progress();
        assert_eq!(total.bytes_downloaded, 640); // 512 + 128
        assert_eq!(total.total_bytes, Some(768)); // 512 + 256
    }

    #[test]
    fn test_total_metrics() {
        let mut reporter = ProgressReporter::new();

        // Add two trackers
        let progress1 = Progress {
            phase: FetchPhase::Downloading,
            bytes_downloaded: 0,
            total_bytes: Some(1000),
            retry_count: 0,
            performance_metrics: None,
        };
        let progress2 = Progress {
            phase: FetchPhase::Downloading,
            bytes_downloaded: 0,
            total_bytes: Some(2000),
            retry_count: 0,
            performance_metrics: None,
        };

        reporter.add_tracker(progress1);
        reporter.add_tracker(progress2);

        // Update progress to simulate downloads
        let updated1 = Progress {
            phase: FetchPhase::Downloading,
            bytes_downloaded: 500,
            total_bytes: Some(1000),
            retry_count: 0,
            performance_metrics: None,
        };
        let updated2 = Progress {
            phase: FetchPhase::Downloading,
            bytes_downloaded: 1000,
            total_bytes: Some(2000),
            retry_count: 0,
            performance_metrics: None,
        };

        reporter.update_tracker(0, updated1);
        reporter.update_tracker(1, updated2);

        // The trackers should have rates calculated from history
        // For this test, we'll just check that the total progress is correct
        let total = reporter.total_progress();
        assert_eq!(total.bytes_downloaded, 1500); // 500 + 1000
        assert_eq!(total.total_bytes, Some(3000)); // 1000 + 2000
    }
}