Skip to content

Commit

Permalink
fix(ZNTA-2710): increase granularity of MT progress
Browse files Browse the repository at this point in the history
  • Loading branch information
djansen-redhat committed Aug 7, 2018
1 parent 5c2c2c6 commit 385d91f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
Expand Up @@ -23,11 +23,9 @@
import org.zanata.async.AsyncTaskHandle;
import org.zanata.async.AsyncTaskKey;
import org.zanata.async.UserTriggeredTaskHandle;
import org.zanata.common.LocaleId;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.ProjectIterationId;

import com.google.common.base.MoreObjects;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
* @author Patrick Huang
Expand All @@ -38,6 +36,8 @@ public class MachineTranslationPrefillTaskHandle extends AsyncTaskHandle<Void>
private static final long serialVersionUID = 1704257523049053603L;
private String triggeredBy;
private String targetVersion;
private double percentage;
private long actualProgress;

public MachineTranslationPrefillTaskHandle(AsyncTaskKey key) {
super.setKeyId(key.id());
Expand Down Expand Up @@ -66,4 +66,22 @@ public String toString() {
.add("triggeredBy", triggeredBy)
.toString();
}

/**
* Set the percentage 'step' for more fine grained representation
* @param units
*/
public void setUnits(long units) {
this.percentage = units == 0 ? 0 :
new BigDecimal((float)100 / (float)units)
.setScale(5, RoundingMode.CEILING).doubleValue();
}

@Override
public long increaseProgress(long units) {
actualProgress += units;
setCurrentProgress(Math.min(Math.round(actualProgress*percentage), 100));
return getCurrentProgress();
}

}
Expand Up @@ -43,6 +43,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zanata.async.Async;
import org.zanata.async.AsyncTaskHandle;
import org.zanata.async.AsyncTaskResult;
import org.zanata.async.handle.MachineTranslationPrefillTaskHandle;
import org.zanata.common.ContentState;
Expand Down Expand Up @@ -209,7 +210,10 @@ public Future<Void> prefillProjectVersionWithMachineTranslation(
log.info("No documents in {}", version.userFriendlyToString());
return AsyncTaskResult.completed();
}
taskHandle.setMaxProgress(documents.size());
// Set taskHandle to count the textflows of all documents
taskHandle.setUnits(
documents.values().stream()
.mapToLong(d -> d.getTextFlows().size()).sum());
HLocale targetLocale = localeService.getByLocaleId(options.getToLocale());

Stopwatch overallStopwatch = Stopwatch.createStarted();
Expand All @@ -231,12 +235,13 @@ public Future<Void> prefillProjectVersionWithMachineTranslation(
Long docId = doc.getId();
if (!attributionService.supportsAttribution(doc)) {
log.warn("Attribution not supported for {}; skipping MT", doc);
taskHandle.increaseProgress(doc.getTextFlows().size());
continue;
}
String requestedBackend = BACKEND_GOOGLE;
String backendId = addMachineTranslationsToDoc(doc, targetLocale,
projectSlug, versionSlug, options.getSaveState(),
options.getOverwriteFuzzy(), requestedBackend);
options.getOverwriteFuzzy(), requestedBackend, taskHandle);
if (backendId != null) {
try {
transactionUtil.run(() -> {
Expand All @@ -249,7 +254,6 @@ public Future<Void> prefillProjectVersionWithMachineTranslation(
throw new RuntimeException("error adding attribution for machine translation", e);
}
}
taskHandle.increaseProgress(1);
}
// Clear the cache again to force recalculation (just in case of
// concurrent activity):
Expand All @@ -262,15 +266,21 @@ public Future<Void> prefillProjectVersionWithMachineTranslation(
}

private @Nullable String addMachineTranslationsToDoc(HDocument doc,
HLocale targetLocale, String projectSlug,
String versionSlug, ContentState saveState, boolean overwriteFuzzy,
String backendId) {
HLocale targetLocale,
String projectSlug,
String versionSlug,
ContentState saveState,
boolean overwriteFuzzy,
String backendId,
AsyncTaskHandle taskHandle) {
DocumentId documentId = new DocumentId(doc.getId(),
doc.getDocId());
entityManager.clear();
List<HTextFlow> textFlowsToTranslate =
getTextFlowsByDocumentIdWithConstraints(targetLocale,
documentId, overwriteFuzzy);
// Increase progress for non-translated items
taskHandle.increaseProgress(doc.getTextFlows().size() - textFlowsToTranslate.size());
if (textFlowsToTranslate.isEmpty()) {
log.info("No eligible text flows in document {}", doc.getQualifiedDocId());
return null;
Expand Down Expand Up @@ -300,6 +310,7 @@ public Future<Void> prefillProjectVersionWithMachineTranslation(
// TODO we only return the backendId from the final batch
backendIdConfirmation = result.getBackendId();
startBatch = batchEnd;
taskHandle.increaseProgress(next.size());
}
if (backendIdConfirmation == null) {
log.warn("Error getting confirmation backend ID for {}", doc.getDocId());
Expand Down

0 comments on commit 385d91f

Please sign in to comment.