Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions controller/app/src/main/java/org/iiab/controller/RsyncManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ public void startClient(Context context, ShareConfig config, String hostIp, int

String lastFile = "";
int lastEmittedPct = 0; // ADFA-5160: smoothed percent, never walked back
long firstProgressMs = 0L; // ADFA-5160 follow-up: anchor for the ETA warmup
final long etaWarmupMs = 3000L; // hold the ETA until the speed sample settles

while ((line = reader.readLine()) != null) {
if (isCancelled) {
Expand All @@ -162,15 +164,26 @@ public void startClient(Context context, ShareConfig config, String hostIp, int
lastEmittedPct = pct;

// ADFA-5160: rsync's ETA is computed against its per-file plan, so it jumps
// the same way the old percent did. When we have a whole-set total, derive
// the ETA from the bytes still to go and the current speed instead.
String eta = progress.eta;
// the same way the old percent did — and its first samples report a tiny
// instantaneous speed, so an ETA derived then reads as thousands of hours.
// Hold the ETA for a short warmup, then compute it from the AVERAGE speed
// (bytes moved / elapsed), which is steadier than rsync's instantaneous
// figure and smooths the mid-transfer jitter too. The bar and speed keep
// moving during the warmup; only the ETA waits.
if (firstProgressMs == 0L) firstProgressMs = System.currentTimeMillis();
long elapsedMs = System.currentTimeMillis() - firstProgressMs;

String eta;
if (expectedTotalBytes > 0) {
double bps = RsyncProgress.parseSpeedBytesPerSec(progress.speed);
if (bps > 0) {
if (elapsedMs >= etaWarmupMs && progress.bytes > 0) {
double avgBps = progress.bytes / (elapsedMs / 1000.0);
long remaining = Math.max(0L, expectedTotalBytes - progress.bytes);
eta = RsyncProgress.formatEta((long) (remaining / bps));
eta = RsyncProgress.formatEta((long) (remaining / avgBps));
} else {
eta = "…"; // warmup: no bogus figure yet
}
} else {
eta = progress.eta; // no whole-set total: rsync's own ETA
}

String finalFile = lastFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ private RsyncProgress(long bytes, int percent, String speed, String eta) {
private static final Pattern STATS =
Pattern.compile("Total transferred file size:\\s+([\\d,\\.]+)\\s+bytes");

// ADFA-5160: rsync's speed column, e.g. "12.34MB/s" / "512.00B/s". Used to turn the
// whole-set remaining bytes into a whole-set ETA, instead of rsync's own ETA, which is
// computed against its per-file plan and jumps the same way the old percent did.
private static final Pattern SPEED =
Pattern.compile("([\\d\\.]+)([kMGT]?B)/s");

/**
* Parses one rsync {@code --info=progress2} line. Returns {@code null} if the
* line carries no progress token or the percentage is not a number.
Expand Down Expand Up @@ -79,31 +73,6 @@ public static long parseTransferredBytes(String line, long fallback) {
}
}

/**
* Parses a rsync speed column ("12.34MB/s") into bytes per second (1024-based units, as
* rsync prints them). Returns {@code -1} when the string does not match. ADFA-5160.
*/
public static double parseSpeedBytesPerSec(String speed) {
if (speed == null) return -1d;
Matcher m = SPEED.matcher(speed);
if (!m.find()) return -1d;
try {
double n = Double.parseDouble(m.group(1));
double mult;
switch (m.group(2)) {
case "B": mult = 1d; break;
case "kB": mult = 1024d; break;
case "MB": mult = 1024d * 1024d; break;
case "GB": mult = 1024d * 1024d * 1024d; break;
case "TB": mult = 1024d * 1024d * 1024d * 1024d; break;
default: return -1d;
}
return n * mult;
} catch (NumberFormatException e) {
return -1d;
}
}

/** Formats a duration in seconds as rsync's {@code H:MM:SS}. Negatives clamp to zero. */
public static String formatEta(long seconds) {
if (seconds < 0) seconds = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,6 @@ public void returnsFallbackWhenStatsLineAbsent() {
assertEquals(0L, RsyncProgress.parseTransferredBytes(null, 0L));
}

// ADFA-5160: speed -> bytes/sec, for deriving a whole-set ETA.
@Test
public void parsesSpeedToBytesPerSecond() {
assertEquals(1024.0 * 1024.0, RsyncProgress.parseSpeedBytesPerSec("1.00MB/s"), 0.001);
assertEquals(1024.0, RsyncProgress.parseSpeedBytesPerSec("1.00kB/s"), 0.001);
assertEquals(512.0, RsyncProgress.parseSpeedBytesPerSec("512.00B/s"), 0.001);
assertEquals(-1.0, RsyncProgress.parseSpeedBytesPerSec("n/a"), 0.001);
assertEquals(-1.0, RsyncProgress.parseSpeedBytesPerSec(null), 0.001);
}

// ADFA-5160: whole-set ETA formatting, rsync's H:MM:SS.
@Test
public void formatsEtaAsHoursMinutesSeconds() {
Expand Down
Loading