-
Notifications
You must be signed in to change notification settings - Fork 1
feat: download speed and time estimation #243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AnsisMalins
wants to merge
14
commits into
main
Choose a base branch
from
feat/download-speed-and-eta
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e72a748
feat: download speed and time estimation
AnsisMalins f126037
fix: packages-lock.json
AnsisMalins d6ceba2
fix: address code review feedbck
AnsisMalins 82f83a4
fix: match the TyepScript type to the Rust one
AnsisMalins 956002d
chore: address code review feedback
AnsisMalins f96f1d3
feat: comment out social buttons
AnsisMalins 873a857
Merge branch 'main' into feat/download-speed-and-eta
AnsisMalins a0f5f4a
try update
NickKhalow 8fa7f8f
format
NickKhalow c577b0c
move from usize to u64 and simplify contracts
NickKhalow c847c8b
remove unused errors
NickKhalow b3508f1
default estimator impl
NickKhalow 68e1054
icon side size
NickKhalow 7cac9ca
fix code style
NickKhalow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Generated by Claude. | ||
|
|
||
| use core::f64; | ||
| use std::time::Duration; | ||
|
|
||
| pub struct DownloadSpeedEstimator { | ||
| /// Smoothed bytes per second (exponential moving average). | ||
| bytes_per_second: f64, | ||
| /// EMA smoothing factor in 0..=1. Lower values = smoother. | ||
| alpha: f64, | ||
| } | ||
|
|
||
| impl Default for DownloadSpeedEstimator { | ||
| fn default() -> Self { | ||
| Self::new(0.1) | ||
| } | ||
| } | ||
|
|
||
| impl DownloadSpeedEstimator { | ||
| /// Create a new estimator. | ||
| /// `alpha` controls how quickly the estimate reacts to new samples. | ||
| /// A value around 0.2–0.3 works well for download progress bars. | ||
| const fn new(alpha: f64) -> Self { | ||
| Self { | ||
| bytes_per_second: 0.0, | ||
| alpha: alpha.clamp(0.0, 1.0), | ||
| } | ||
| } | ||
|
|
||
| /// Feed a sample of (`bytes_downloaded`, `time_passed`) for the most recent interval. | ||
| /// `time_passed` is in seconds. | ||
| fn update(&mut self, bytes_downloaded: u64, time_passed: Duration) -> Result<(), Error> { | ||
| if time_passed <= Duration::ZERO { | ||
| return Err(Error::TimeIsNotPositive); | ||
| } | ||
|
|
||
| let bytes_downloaded = u64_to_f64_lossy(bytes_downloaded); | ||
| let sample_bps = bytes_downloaded / time_passed.as_secs_f64(); | ||
|
|
||
| if self.bytes_per_second == 0.0 { | ||
| // First sample — seed the estimate directly. | ||
| self.bytes_per_second = sample_bps; | ||
| } else { | ||
| self.bytes_per_second = | ||
| sample_bps.mul_add(self.alpha, self.bytes_per_second * (1.0 - self.alpha)); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| pub fn try_update(&mut self, bytes_downloaded: u64, time_passed: Duration) { | ||
| if let Err(e) = self.update(bytes_downloaded, time_passed) { | ||
| log::error!("Cannot update estimator: {:?}", e); | ||
| } | ||
| } | ||
|
|
||
| /// Current smoothed bytes-per-second estimate. | ||
| pub const fn bytes_per_second(&self) -> f64 { | ||
| self.bytes_per_second | ||
| } | ||
|
|
||
| /// Estimated milliseconds remaining to download `bytes_remaining`. | ||
| pub fn time_remaining(&self, bytes_remaining: u64) -> Option<f64> { | ||
| if self.bytes_per_second <= 0.0 { | ||
| return None; | ||
| } | ||
|
|
||
| let bytes_remaining = u64_to_f64_lossy(bytes_remaining); | ||
| let seconds_remaining = bytes_remaining / self.bytes_per_second; | ||
| Some(seconds_remaining * 1000.0) | ||
| } | ||
| } | ||
|
|
||
| // It's okay here, because we don't need exact estimation | ||
| #[allow(clippy::cast_precision_loss)] | ||
| #[inline] | ||
| const fn u64_to_f64_lossy(x: u64) -> f64 { | ||
| x as f64 | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub enum Error { | ||
| TimeIsNotPositive, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.