Skip to content

Commit

Permalink
Updated TextUnitSearcher to exclude AI translations based on duration…
Browse files Browse the repository at this point in the history
… or null id, updated map step to record uploaded file uri in DB
  • Loading branch information
maallen committed Oct 14, 2024
1 parent 9c29c22 commit 526bf24
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class ThirdPartyTextUnit extends AuditableEntity {
foreignKey = @ForeignKey(name = "FK__THIRD_PARTY_TEXT_UNIT__TM_TEXT_UNIT__ID"))
TMTextUnit tmTextUnit;

@Column(name = "uploaded_file_uri")
String uploadedFileUri;

public String getThirdPartyId() {
return thirdPartyId;
}
Expand All @@ -64,4 +67,12 @@ public Asset getAsset() {
public void setAsset(Asset asset) {
this.asset = asset;
}

public String getUploadedFileUri() {
return uploadedFileUri;
}

public void setUploadedFileUri(String uploadedFileUri) {
this.uploadedFileUri = uploadedFileUri;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ void saveMojitoToThirdPartyTextUnitMapping(
thirdPartyTextUnit.setThirdPartyId(
thirdPartyTextUnitForMapping.getId());
thirdPartyTextUnit.setAsset(asset);
thirdPartyTextUnit.setUploadedFileUri(
thirdPartyTextUnitForMapping.getUploadedFileUri());
TMTextUnit tmTextUnit = tmTextUnitRepository.getOne(tmTextUnitId);

if (tmTextUnitAlreadySaved.containsKey(tmTextUnitId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.box.l10n.mojito.entity.Repository;
import com.box.l10n.mojito.quartz.QuartzJobInfo;
import com.box.l10n.mojito.quartz.QuartzPollableTaskScheduler;
import com.box.l10n.mojito.service.ai.translation.AITranslationConfiguration;
import com.box.l10n.mojito.service.assetExtraction.AssetTextUnitToTMTextUnitRepository;
import com.box.l10n.mojito.service.pollableTask.PollableFuture;
import com.box.l10n.mojito.service.thirdparty.smartling.SmartlingFile;
Expand Down Expand Up @@ -42,6 +43,7 @@
import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -100,6 +102,8 @@ public class ThirdPartyTMSSmartling implements ThirdPartyTMS {

private final QuartzPollableTaskScheduler quartzPollableTaskScheduler;

private final AITranslationConfiguration aiTranslationConfiguration;

private final Set<String> supportedImageExtensions =
Sets.newHashSet("png", "jpg", "jpeg", "gif", "tiff");

Expand All @@ -118,7 +122,8 @@ public ThirdPartyTMSSmartling(
ThirdPartyTMSSmartlingGlossary thirdPartyTMSSmartlingGlossary,
AssetTextUnitToTMTextUnitRepository assetTextUnitToTMTextUnitRepository,
MeterRegistry meterRegistry,
QuartzPollableTaskScheduler quartzPollableTaskScheduler) {
QuartzPollableTaskScheduler quartzPollableTaskScheduler,
AITranslationConfiguration aiTranslationConfiguration) {
this(
smartlingClient,
textUnitSearcher,
Expand All @@ -130,7 +135,8 @@ public ThirdPartyTMSSmartling(
assetTextUnitToTMTextUnitRepository,
DEFAULT_BATCH_SIZE,
meterRegistry,
quartzPollableTaskScheduler);
quartzPollableTaskScheduler,
aiTranslationConfiguration);
}

public ThirdPartyTMSSmartling(
Expand All @@ -144,7 +150,8 @@ public ThirdPartyTMSSmartling(
AssetTextUnitToTMTextUnitRepository assetTextUnitToTMTextUnitRepository,
int batchSize,
MeterRegistry meterRegistry,
QuartzPollableTaskScheduler quartzPollableTaskScheduler) {
QuartzPollableTaskScheduler quartzPollableTaskScheduler,
AITranslationConfiguration aiTranslationConfiguration) {
this.smartlingClient = smartlingClient;
this.assetPathAndTextUnitNameKeys = assetPathAndTextUnitNameKeys;
this.textUnitBatchImporterService = textUnitBatchImporterService;
Expand All @@ -156,6 +163,7 @@ public ThirdPartyTMSSmartling(
this.assetTextUnitToTMTextUnitRepository = assetTextUnitToTMTextUnitRepository;
this.meterRegistry = meterRegistry;
this.quartzPollableTaskScheduler = quartzPollableTaskScheduler;
this.aiTranslationConfiguration = aiTranslationConfiguration;
}

@Override
Expand Down Expand Up @@ -337,6 +345,7 @@ public List<ThirdPartyTextUnit> getThirdPartyTextUnits(
thirdPartyTextUnit.setAssetPath(key.getAssetPath());
thirdPartyTextUnit.setName(key.getTextUnitName());
thirdPartyTextUnit.setNamePluralPrefix(isPluralFile(file.getFileUri()));
thirdPartyTextUnit.setUploadedFileUri(file.getFileUri());

return thirdPartyTextUnit;
});
Expand Down Expand Up @@ -968,6 +977,12 @@ private TextUnitSearcherParameters baseParams(
result.setPluralFormsExcluded(pluralFormsExcluded);
result.setSkipTextUnitWithPattern(skipTextUnitsWithPattern);
result.setSkipAssetPathWithPattern(skipAssetsWithPathPattern);
result.setExcludeUnexpiredPendingMT(
aiTranslationConfiguration != null && aiTranslationConfiguration.isEnabled());
result.setAiTranslationExpiryDuration(
aiTranslationConfiguration != null
? aiTranslationConfiguration.getExpiryDuration()
: Duration.ofHours(3));
if (!Strings.isNullOrEmpty(pluralFormOther)) {
result.setPluralFormOther(pluralFormOther);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class ThirdPartyTextUnit implements TextUnitForBatchMatcher {
*/
boolean namePluralPrefix;

String uploadedFileUri;

public String getId() {
return id;
}
Expand Down Expand Up @@ -80,4 +82,12 @@ public boolean isNamePluralPrefix() {
public void setNamePluralPrefix(boolean namePluralPrefix) {
this.namePluralPrefix = namePluralPrefix;
}

public String getUploadedFileUri() {
return uploadedFileUri;
}

public void setUploadedFileUri(String uploadedFileUri) {
this.uploadedFileUri = uploadedFileUri;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.github.pnowy.nc.core.expressions.NativeProjection;
import com.github.pnowy.nc.core.mappers.CriteriaResultTransformer;
import com.google.common.base.Preconditions;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.codec.digest.DigestUtils;
Expand Down Expand Up @@ -172,6 +173,11 @@ NativeCriteria getCriteriaForSearch(TextUnitSearcherParameters searchParameters)
c.addJoin(NativeExps.crossJoin("locale", "l"));
c.addJoin(NativeExps.innerJoin("asset", "a", "a.id", "tu.asset_id"));
c.addJoin(NativeExps.innerJoin("repository", "r", "r.id", "a.repository_id"));
if (searchParameters.isExcludeUnexpiredPendingMT()) {
c.addJoin(
NativeExps.leftJoin(
"tm_text_unit_pending_mt", "tmtupmt", "tmtupmt.tm_text_unit_id", "tu.id"));
}

NativeJunctionExp onClauseRepositoryLocale = NativeExps.conjunction();
onClauseRepositoryLocale.add(new NativeColumnEqExp("rl.locale_id", "l.id"));
Expand Down Expand Up @@ -378,6 +384,18 @@ NativeCriteria getCriteriaForSearch(TextUnitSearcherParameters searchParameters)
new NativeNotILikeExp("a.path", searchParameters.getSkipAssetPathWithPattern()));
}

if (searchParameters.isExcludeUnexpiredPendingMT()) {
ZonedDateTime durationThreshold =
ZonedDateTime.now().minus(searchParameters.getAiTranslationExpiryDuration());

// Include rows where the id in the left table is null (i.e. no match) or the created_date is
// outside the configured expiry duration
NativeJunctionExp mtExclusionFilter = NativeExps.disjunction();
mtExclusionFilter.add(NativeExps.isNull("tmtupmt.id"));
mtExclusionFilter.add(new NativeDateLteExp("tmtupmt.created_date", durationThreshold));
conjunction.add(mtExclusionFilter);
}

StatusFilter statusFilter = searchParameters.getStatusFilter();

if (statusFilter != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.box.l10n.mojito.service.tm.search;

import com.box.l10n.mojito.service.NormalizationUtils;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -50,6 +51,8 @@ public class TextUnitSearcherParameters {
String skipTextUnitWithPattern;
String includeTextUnitsWithPattern;
String skipAssetPathWithPattern;
boolean isExcludeUnexpiredPendingMT = false;
Duration aiTranslationExpiryDuration;

public String getName() {
return name;
Expand Down Expand Up @@ -327,4 +330,20 @@ public boolean isOrderedByTextUnitID() {
public void setOrderByTextUnitID(boolean ordered) {
isOrderedByTextUnitID = ordered;
}

public boolean isExcludeUnexpiredPendingMT() {
return isExcludeUnexpiredPendingMT;
}

public void setExcludeUnexpiredPendingMT(boolean excludeUnexpiredPendingMT) {
isExcludeUnexpiredPendingMT = excludeUnexpiredPendingMT;
}

public Duration getAiTranslationExpiryDuration() {
return aiTranslationExpiryDuration;
}

public void setAiTranslationExpiryDuration(Duration aiTranslationExpiryDuration) {
this.aiTranslationExpiryDuration = aiTranslationExpiryDuration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.box.l10n.mojito.quartz.QuartzJobInfo;
import com.box.l10n.mojito.quartz.QuartzPollableTaskScheduler;
import com.box.l10n.mojito.quartz.QuartzSchedulerManager;
import com.box.l10n.mojito.service.ai.translation.AITranslationConfiguration;
import com.box.l10n.mojito.service.asset.AssetService;
import com.box.l10n.mojito.service.assetExtraction.AssetExtractionRepository;
import com.box.l10n.mojito.service.assetExtraction.AssetExtractionService;
Expand Down Expand Up @@ -161,6 +162,8 @@ public class ThirdPartyTMSSmartlingTest extends ServiceTestBase {

@Mock TextUnitBatchImporterService mockTextUnitBatchImporterService;

@Mock AITranslationConfiguration aiTranslationConfiguration;

@Captor ArgumentCaptor<List<TextUnitDTO>> textUnitListCaptor;

@Captor
Expand Down Expand Up @@ -216,7 +219,8 @@ public void setUp() throws SchedulerException {
mockThirdPartyTMSSmartlingGlossary,
assetTextUnitToTMTextUnitRepository,
meterRegistry,
mockQuartzPollableTaskScheduler);
mockQuartzPollableTaskScheduler,
aiTranslationConfiguration);

mapper = new AndroidStringDocumentMapper(pluralSep, null);
RetryBackoffSpec retryConfiguration =
Expand Down Expand Up @@ -292,7 +296,8 @@ public void testPushInBatchesWithSingularsAndNoPlurals()
assetTextUnitToTMTextUnitRepository,
batchSize,
meterRegistry,
mockQuartzPollableTaskScheduler);
mockQuartzPollableTaskScheduler,
aiTranslationConfiguration);

TM tm = repository.getTm();
Asset asset =
Expand Down Expand Up @@ -346,7 +351,8 @@ public void testRetryDuringPush() throws RepositoryNameAlreadyUsedException {
assetTextUnitToTMTextUnitRepository,
batchSize,
meterRegistry,
mockQuartzPollableTaskScheduler);
mockQuartzPollableTaskScheduler,
aiTranslationConfiguration);
// throw timeout exception for first request, following request should be successful
when(smartlingClient.uploadFile(any(), any(), any(), any(), any(), any(), any()))
.thenThrow(
Expand Down Expand Up @@ -412,7 +418,8 @@ public void testRetriesExhaustedDuringPush() throws RepositoryNameAlreadyUsedExc
assetTextUnitToTMTextUnitRepository,
batchSize,
meterRegistry,
mockQuartzPollableTaskScheduler);
mockQuartzPollableTaskScheduler,
aiTranslationConfiguration);

TM tm = repository.getTm();
Asset asset =
Expand Down Expand Up @@ -463,7 +470,8 @@ public void testPushInBatchesWithNoSingularsAndPlurals()
assetTextUnitToTMTextUnitRepository,
batchSize,
meterRegistry,
mockQuartzPollableTaskScheduler);
mockQuartzPollableTaskScheduler,
aiTranslationConfiguration);

TM tm = repository.getTm();
Asset asset =
Expand Down Expand Up @@ -522,7 +530,8 @@ public void testPushInBatchesWithSingularsAndPlurals() throws RepositoryNameAlre
assetTextUnitToTMTextUnitRepository,
batchSize,
meterRegistry,
mockQuartzPollableTaskScheduler);
mockQuartzPollableTaskScheduler,
aiTranslationConfiguration);

TM tm = repository.getTm();
Asset asset =
Expand Down Expand Up @@ -737,7 +746,8 @@ public void testPullNoBatches() throws RepositoryLocaleCreationException, Interr
mockThirdPartyTMSSmartlingGlossary,
assetTextUnitToTMTextUnitRepository,
meterRegistry,
mockQuartzPollableTaskScheduler);
mockQuartzPollableTaskScheduler,
aiTranslationConfiguration);
tmsSmartling.pull(
repository,
"projectId",
Expand Down Expand Up @@ -809,7 +819,8 @@ public void testPullNoBatchesPluralFix() throws RepositoryLocaleCreationExceptio
mockThirdPartyTMSSmartlingGlossary,
assetTextUnitToTMTextUnitRepository,
meterRegistry,
mockQuartzPollableTaskScheduler);
mockQuartzPollableTaskScheduler,
aiTranslationConfiguration);
tmsSmartling.pull(
repository,
"projectId",
Expand Down Expand Up @@ -872,7 +883,8 @@ public void testPullDryRunNoBatches() throws RepositoryLocaleCreationException {
mockThirdPartyTMSSmartlingGlossary,
assetTextUnitToTMTextUnitRepository,
meterRegistry,
mockQuartzPollableTaskScheduler);
mockQuartzPollableTaskScheduler,
aiTranslationConfiguration);
tmsSmartling.pull(
repository,
"projectId",
Expand Down Expand Up @@ -1461,7 +1473,8 @@ public void testPushTranslationsInBatches()
assetTextUnitToTMTextUnitRepository,
batchSize,
meterRegistry,
mockQuartzPollableTaskScheduler);
mockQuartzPollableTaskScheduler,
aiTranslationConfiguration);
Repository repository =
repositoryService.createRepository(testIdWatcher.getEntityName("batchRepo"));
Locale frCA = localeService.findByBcp47Tag("fr-CA");
Expand Down Expand Up @@ -1615,7 +1628,8 @@ public void testBatchesFor() {
assetTextUnitToTMTextUnitRepository,
3,
meterRegistry,
mockQuartzPollableTaskScheduler);
mockQuartzPollableTaskScheduler,
aiTranslationConfiguration);

assertThat(tmsSmartling.batchesFor(0)).isEqualTo(0);
assertThat(tmsSmartling.batchesFor(1)).isEqualTo(1);
Expand All @@ -1638,7 +1652,8 @@ public void testBatchesFor() {
assetTextUnitToTMTextUnitRepository,
35,
meterRegistry,
mockQuartzPollableTaskScheduler);
mockQuartzPollableTaskScheduler,
aiTranslationConfiguration);

assertThat(tmsSmartling.batchesFor(0)).isEqualTo(0);
assertThat(tmsSmartling.batchesFor(1)).isEqualTo(1);
Expand All @@ -1659,7 +1674,8 @@ public void testBatchesFor() {
assetTextUnitToTMTextUnitRepository,
4231,
meterRegistry,
mockQuartzPollableTaskScheduler);
mockQuartzPollableTaskScheduler,
aiTranslationConfiguration);

assertThat(tmsSmartling.batchesFor(0)).isEqualTo(0);
assertThat(tmsSmartling.batchesFor(1)).isEqualTo(1);
Expand Down

0 comments on commit 526bf24

Please sign in to comment.