1 Commits
Author SHA1 Message Date
ShayanEdalatjoo a4eb2566c7 Adding progress bar download speed and ETA from Bonus 2026-07-17 23:17:31 +03:30
6 changed files with 74 additions and 23 deletions
+10
View File
@@ -0,0 +1,10 @@
# Default ignored files
/shelf/
/workspace.xml
# Ignored default folder with query files
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/main.iml" filepath="$PROJECT_DIR$/main.iml" />
</modules>
</component>
</project>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
</component>
</project>
-3
View File
@@ -24,8 +24,6 @@ public class DownloadWorker implements Runnable {
chunkStatus.setStartTimeMs(System.currentTimeMillis()); chunkStatus.setStartTimeMs(System.currentTimeMillis());
double downloaded = 0.0; double downloaded = 0.0;
System.out.println("Chunk " + chunkStatus.getChunkId() + " started downloading.");
while (downloaded < chunkStatus.getChunkSizeMB()) { while (downloaded < chunkStatus.getChunkSizeMB()) {
try { try {
int delay = config.getMinStepDelayMs() int delay = config.getMinStepDelayMs()
@@ -50,7 +48,6 @@ public class DownloadWorker implements Runnable {
} }
chunkStatus.setCompleted(true); chunkStatus.setCompleted(true);
chunkStatus.setEndTimeMs(System.currentTimeMillis()); chunkStatus.setEndTimeMs(System.currentTimeMillis());
System.out.println("[Worker-" + chunkStatus.getChunkId() + "] Download completed.");
} }
} }
+44 -20
View File
@@ -16,16 +16,7 @@ public class ProgressMonitor implements Runnable {
@Override @Override
public void run() { public void run() {
// TODO: long startTimeMs = System.currentTimeMillis();
// Repeatedly check chunk progress until all chunks are completed.
// In each loop:
// 1. Read the downloaded size from every chunk
// 2. Add all downloaded amounts to totalDownloadedMB
// 3. Count completed chunks
// 4. Print a progress message
// 5. If completedChunks == chunks.size(), print a final monitor message and stop
// 6. Otherwise sleep for monitorDelayMs and continue
while (true) { while (true) {
double totalDownloadedMB = 0.0; double totalDownloadedMB = 0.0;
@@ -33,29 +24,49 @@ public class ProgressMonitor implements Runnable {
for (ChunkStatus chunk : chunks) { for (ChunkStatus chunk : chunks) {
totalDownloadedMB += chunk.getDownloadedMB(); totalDownloadedMB += chunk.getDownloadedMB();
if (chunk.isCompleted()) { if (chunk.isCompleted()) {
completedChunks++; completedChunks++;
} }
} }
double percent = 0.0; long elapsedMs = System.currentTimeMillis() - startTimeMs;
if (totalSizeMB > 0) { double elapsedSeconds = elapsedMs / 1000.0;
percent = (totalDownloadedMB * 100.0) / totalSizeMB;
} double speedMBps = elapsedSeconds > 0.0
? totalDownloadedMB / elapsedSeconds
: 0.0;
double percent = totalSizeMB > 0
? totalDownloadedMB * 100.0 / totalSizeMB
: 100.0;
percent = Math.max(0.0, Math.min(100.0, percent));
double remainingMB = Math.max(0.0, totalSizeMB - totalDownloadedMB);
long etaSeconds = speedMBps > 0.0
? (long) Math.ceil(remainingMB / speedMBps)
: 0L;
int barWidth = 40;
int filled = (int) Math.round(percent / 100.0 * barWidth);
filled = Math.max(0, Math.min(barWidth, filled));
String bar = "#".repeat(filled) + "-".repeat(barWidth - filled);
System.out.printf( System.out.printf(
"Progress for %s: %.1f/%.1f MB (%.2f%%), completed chunks: %d/%d%n", "\r[%s] %6.2f%% %7.1f/%d MB | %6.2f MB/s | ETA %s | chunks %d/%d",
fileName, bar,
totalDownloadedMB,
(double) totalSizeMB,
percent, percent,
totalDownloadedMB,
totalSizeMB,
speedMBps,
formatEta(etaSeconds),
completedChunks, completedChunks,
chunks.size() chunks.size()
); );
System.out.flush();
if (completedChunks == chunks.size()) { if (completedChunks == chunks.size()) {
System.out.println();
System.out.println("All chunks completed."); System.out.println("All chunks completed.");
break; break;
} }
@@ -63,9 +74,22 @@ public class ProgressMonitor implements Runnable {
try { try {
Thread.sleep(monitorDelayMs); Thread.sleep(monitorDelayMs);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println();
System.out.println("Progress monitor interrupted."); System.out.println("Progress monitor interrupted.");
return; return;
} }
} }
} }
private String formatEta(long totalSeconds) {
long minutes = totalSeconds / 60;
long seconds = totalSeconds % 60;
if (minutes > 0) {
return String.format("%dm %02ds", minutes, seconds);
}
return String.format("%ds", seconds);
}
} }