This commit is contained in:
Rezak
2026-06-05 14:06:29 +03:30
parent 9e5c715088
commit dc9839c911
5 changed files with 127 additions and 75 deletions
+25 -15
View File
@@ -6,26 +6,19 @@ public class ProgressMonitor implements Runnable {
private final int totalSizeMB;
private final List<ChunkStatus> chunks;
private final long monitorDelayMs;
private final long startTimeMs;
public ProgressMonitor(DownloadConfig config, List<ChunkStatus> chunks) {
this.fileName = config.getFileName();
this.totalSizeMB = config.getTotalSizeMB();
this.chunks = chunks;
this.monitorDelayMs = 500;
this.startTimeMs = System.currentTimeMillis();
}
@Override
public void run() {
// TODO:
// 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
System.out.println("=== Download Progress ===");
while (true) {
double totalDownloadedMB = 0.0;
@@ -44,19 +37,36 @@ public class ProgressMonitor implements Runnable {
percent = (totalDownloadedMB * 100.0) / totalSizeMB;
}
System.out.printf(
"Progress for %s: %.1f/%.1f MB (%.2f%%), completed chunks: %d/%d%n",
long elapsedMs = System.currentTimeMillis() - startTimeMs;
double elapsedSec = elapsedMs / 1000.0;
double speedMBps = (elapsedSec > 0) ? (totalDownloadedMB / elapsedSec) : 0.0;
double remainingMB = totalSizeMB - totalDownloadedMB;
double etaSec = (speedMBps > 0) ? (remainingMB / speedMBps) : 0.0;
int barLength = 30;
int filled = (int) (percent / 100.0 * barLength);
StringBuilder bar = new StringBuilder("[");
for (int i = 0; i < barLength; i++) {
bar.append(i < filled ? "=" : " ");
}
bar.append("]");
System.out.printf("\r%s %s %.1f/%.1f MB (%.1f%%) | Speed: %.1f MB/s | ETA: %.0fs | Chunks: %d/%d",
bar.toString(),
fileName,
totalDownloadedMB,
(double) totalSizeMB,
percent,
speedMBps,
etaSec,
completedChunks,
chunks.size()
);
// TODO:
// If all chunks are completed, print a final message and exit the loop
if (completedChunks == chunks.size()) {
System.out.println("Monitor: All chunks completed.");
break;
}
try {
Thread.sleep(monitorDelayMs);