forked from graphhopper/graphhopper
-
Notifications
You must be signed in to change notification settings - Fork 20
feat: add progress bar to file Downloader #105
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
rtroilo
wants to merge
2
commits into
ors_4.0
Choose a base branch
from
feat-1990-2
base: ors_4.0
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
2 commits
Select commit
Hold shift + click to select a range
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
85 changes: 85 additions & 0 deletions
85
core/src/main/java/com/graphhopper/util/ProgressOutputStream.java
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,85 @@ | ||
package com.graphhopper.util; | ||
|
||
import org.apache.commons.lang3.time.DurationFormatUtils; | ||
import org.apache.commons.lang3.time.StopWatch; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.function.Consumer; | ||
|
||
class ProgressOutputStream extends OutputStream { | ||
private final OutputStream out; | ||
private final long max; | ||
private final Consumer<String> log; | ||
private final String progressRenderString; | ||
|
||
private long current = 0; | ||
private long speed = 0; | ||
|
||
private final StopWatch time; | ||
private final StopWatch totalTime; | ||
|
||
|
||
public ProgressOutputStream(OutputStream out, String task, long max, Consumer<String> log) { | ||
this.out = out; | ||
this.max = max; | ||
this.log = log; | ||
if (max > 0) { | ||
this.progressRenderString = "%s %%%dd/%d (%%3d%%%%) [%%.2f MB/s / %%.2f MB/s] %%s" | ||
.formatted(task, Long.toString(max).length(), max); | ||
} else { | ||
this.progressRenderString = "%s %%d/? [%%.2f Mbps / %%.2f Mbps] %%s".formatted(task); | ||
} | ||
|
||
this.time = StopWatch.createStarted(); | ||
this.totalTime = StopWatch.createStarted(); | ||
|
||
} | ||
|
||
private void progress(long n) { | ||
current += n; | ||
speed += n; | ||
if (time.getTime() >= 1000) { | ||
triggerLog(); | ||
time.reset(); | ||
time.start(); | ||
speed = 0; | ||
} | ||
} | ||
|
||
private void triggerLog() { | ||
log.accept(render((speed) / (Math.max(time.getTime(), 1) * 1000.0), (current) / (Math.max(totalTime.getTime(), 1) * 1000.0))); | ||
aoles marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private String render(double mbits, double mbitsTotal) { | ||
if (max > 0) { | ||
return progressRenderString | ||
.formatted(current, (int) ((current / (double) max) * 100), | ||
mbits, mbitsTotal, | ||
DurationFormatUtils.formatDuration(totalTime.getTime(), "mm:ss.SSS")); | ||
} | ||
return progressRenderString | ||
.formatted(current, | ||
mbits, mbitsTotal, | ||
DurationFormatUtils.formatDuration(totalTime.getTime(), "mm:ss.SSS")); | ||
|
||
} | ||
|
||
@Override | ||
public void write(byte[] b, int off, int len) throws IOException { | ||
out.write(b, off, len); | ||
progress(len); | ||
} | ||
|
||
@Override | ||
public void write(int b) throws IOException { | ||
out.write(b); | ||
progress(1); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
out.close(); | ||
triggerLog(); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please consider unifying the render string format between the two scenarios when
max > 0
and otherwise.The formatting should be consistent between both strings and use either
MB/s
(megabytes per second) orMbps
(megabits per second). It should also match the actual calculation in therender
method where the values represent megabits per second calculated viaspeed / (time * 1000.0)
. Should you, however, decide to settle on MB/s rather than Mbps you might want to adjust the corresponding formula.