Skip to content

Commit

Permalink
Update AI translation variant status in json sync after file upload (#…
Browse files Browse the repository at this point in the history
…195)

* Update AI translation variant status in json sync after file upload

* Fetch file upload uri for target text units
  • Loading branch information
maallen authored Nov 19, 2024
1 parent b43f61d commit afe235c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void createPendingMTEntitiesInBatches(Long repositoryId, Set<Long> tmText
}

@Transactional
public void updateVariantStatusToMTReview(List<Long> currentVariantIds) {
public void updateVariantStatusToMTReviewNeeded(List<Long> currentVariantIds) {

for (int i = 0; i < currentVariantIds.size(); i += batchSize) {
logger.debug("Updating variant statuses to MT_REVIEW in batches of {}", batchSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
@Component
public class ThirdPartyTMSSmartling implements ThirdPartyTMS {

public static final String PUBLISHED = "PUBLISHED";
public static final String POST_TRANSLATION = "POST_TRANSLATION";

static Logger logger = LoggerFactory.getLogger(ThirdPartyTMSSmartling.class);

/*
Expand Down Expand Up @@ -111,9 +114,6 @@ protected static String getSmartlingLocale(Map<String, String> localeMapping, St
return localeMapping.getOrDefault(localeTag, localeTag);
}

private final String PUBLISHED = "PUBLISHED";
private final String POST_TRANSLATION = "POST_TRANSLATION";

@Autowired
public ThirdPartyTMSSmartling(
SmartlingClient smartlingClient,
Expand Down Expand Up @@ -869,7 +869,7 @@ private SmartlingFile uploadAiTranslationBatch(
options,
localeMapping,
filterTmTextUnitIds);
aiTranslationService.updateVariantStatusToMTReview(
aiTranslationService.updateVariantStatusToMTReviewNeeded(
batch.stream().map(TextUnitDTO::getTmTextUnitCurrentVariantId).toList());
meterRegistry
.counter(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.box.l10n.mojito.service.thirdparty;

import static com.box.l10n.mojito.service.thirdparty.ThirdPartyTMSSmartling.POST_TRANSLATION;
import static com.box.l10n.mojito.service.thirdparty.ThirdPartyTMSSmartling.PUBLISHED;
import static com.box.l10n.mojito.service.thirdparty.ThirdPartyTMSUtils.isFileEqualToPreviousRun;
import static com.box.l10n.mojito.service.thirdparty.smartling.SmartlingFileUtils.isPluralFile;

Expand All @@ -8,6 +10,7 @@
import com.box.l10n.mojito.iterators.PageFetcherOffsetAndLimitSplitIterator;
import com.box.l10n.mojito.iterators.Spliterators;
import com.box.l10n.mojito.service.ai.translation.AITranslationConfiguration;
import com.box.l10n.mojito.service.ai.translation.AITranslationService;
import com.box.l10n.mojito.service.thirdparty.smartling.SmartlingJsonConverter;
import com.box.l10n.mojito.service.thirdparty.smartling.SmartlingOptions;
import com.box.l10n.mojito.service.tm.importer.TextUnitBatchImporterService;
Expand Down Expand Up @@ -36,6 +39,7 @@
import java.util.stream.StreamSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -65,6 +69,8 @@ public class ThirdPartyTMSSmartlingWithJson {

AITranslationConfiguration aiTranslationConfiguration;

AITranslationService aiTranslationService;

int batchSize = 5000;

public ThirdPartyTMSSmartlingWithJson(
Expand All @@ -75,7 +81,8 @@ public ThirdPartyTMSSmartlingWithJson(
SmartlingJsonKeys smartlingJsonKeys,
MeterRegistry meterRegistry,
ThirdPartyFileChecksumRepository thirdPartyFileChecksumRepository,
AITranslationConfiguration aiTranslationConfiguration) {
AITranslationConfiguration aiTranslationConfiguration,
@Autowired(required = false) AITranslationService aiTranslationService) {
this.smartlingClient = smartlingClient;
this.smartlingJsonConverter = smartlingJsonConverter;
this.textUnitSearcher = textUnitSearcher;
Expand All @@ -84,6 +91,7 @@ public ThirdPartyTMSSmartlingWithJson(
this.meterRegistry = meterRegistry;
this.thirdPartyFileChecksumRepository = thirdPartyFileChecksumRepository;
this.aiTranslationConfiguration = aiTranslationConfiguration;
this.aiTranslationService = aiTranslationService;
}

void push(
Expand Down Expand Up @@ -288,7 +296,8 @@ public void pushAiTranslations(
skipTextUnitsWithPattern,
skipAssetsWithPathPattern,
includeTextUnitsWithPattern,
StatusFilter.MT_TRANSLATED),
StatusFilter.MT_TRANSLATED,
true),
false)
.collect(Collectors.groupingBy(TextUnitDTO::getUploadedFileUri));

Expand All @@ -305,7 +314,12 @@ public void pushAiTranslations(
localeMapping,
repositoryLocale,
uploadedFileUri,
textUnitDTOS);
textUnitDTOS,
POST_TRANSLATION);
aiTranslationService.updateVariantStatusToMTReviewNeeded(
textUnitDTOS.stream()
.map(TextUnitDTO::getTmTextUnitCurrentVariantId)
.collect(Collectors.toList()));
meterRegistry
.counter(
"SmartlingSync.uploadAiTranslationsBatch",
Expand All @@ -326,14 +340,32 @@ private void uploadLocalizedFile(
RepositoryLocale repositoryLocale,
String uploadedFileUri,
List<TextUnitDTO> textUnitDTOS) {
uploadLocalizedFile(
projectId, localeMapping, repositoryLocale, uploadedFileUri, textUnitDTOS, PUBLISHED);
}

private void uploadLocalizedFile(
String projectId,
Map<String, String> localeMapping,
RepositoryLocale repositoryLocale,
String uploadedFileUri,
List<TextUnitDTO> textUnitDTOS,
String translationState) {
String fileContent =
smartlingJsonConverter.textUnitDTOsToJsonString(textUnitDTOS, TextUnitDTO::getTarget);
String smartlingLocale = getSmartlingLocale(localeMapping, repositoryLocale);

Mono.fromCallable(
() ->
smartlingClient.uploadLocalizedFile(
projectId, uploadedFileUri, "json", smartlingLocale, fileContent, null, null))
projectId,
uploadedFileUri,
"json",
smartlingLocale,
fileContent,
null,
null,
translationState))
.retryWhen(
smartlingClient
.getRetryConfiguration()
Expand Down Expand Up @@ -442,7 +474,8 @@ PageFetcherOffsetAndLimitSplitIterator<TextUnitDTO> getTargetTextUnitIterator(
String skipTextUnitsWithPattern,
String skipAssetsWithPathPattern,
String includeTextUnitsWithPattern,
StatusFilter statusFilter) {
StatusFilter statusFilter,
boolean isRetrieveFileUploadUri) {

PageFetcherOffsetAndLimitSplitIterator<TextUnitDTO>
textUnitDTOPageFetcherOffsetAndLimitSplitIterator =
Expand All @@ -460,6 +493,7 @@ PageFetcherOffsetAndLimitSplitIterator<TextUnitDTO> getTargetTextUnitIterator(
parameters.setOffset(offset);
parameters.setLimit(limit);
parameters.setPluralFormsFiltered(true);
parameters.setIsRetrieveUploadedFileUri(isRetrieveFileUploadUri);
List<TextUnitDTO> search = textUnitSearcher.search(parameters);
return search;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,7 @@ public void testPushAiTranslationsSingleFile() throws Exception {
eq("POST_TRANSLATION"));
});

verify(aiTranslationService, times(4)).updateVariantStatusToMTReview(any());
verify(aiTranslationService, times(4)).updateVariantStatusToMTReviewNeeded(any());
}

@Test
Expand Down Expand Up @@ -2075,7 +2075,7 @@ public void testPushAiTranslationsMultiFile() throws Exception {
}
});

verify(aiTranslationService, times(20)).updateVariantStatusToMTReview(any());
verify(aiTranslationService, times(20)).updateVariantStatusToMTReviewNeeded(any());
}

private List<TextUnitDTO> searchTextUnits(List<Long> ids) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.box.l10n.mojito.entity.*;
import com.box.l10n.mojito.json.ObjectMapper;
import com.box.l10n.mojito.service.ai.translation.AITranslationConfiguration;
import com.box.l10n.mojito.service.ai.translation.AITranslationService;
import com.box.l10n.mojito.service.asset.AssetService;
import com.box.l10n.mojito.service.assetExtraction.AssetExtractionService;
import com.box.l10n.mojito.service.assetExtraction.ServiceTestBase;
Expand Down Expand Up @@ -86,6 +87,8 @@ public class ThirdPartyTMSSmartlingWithJsonTest extends ServiceTestBase {

@Mock AITranslationConfiguration aiTranslationConfiguration;

@Mock AITranslationService aiTranslationService;

SmartlingJsonConverter smartlingJsonConverter =
new SmartlingJsonConverter(ObjectMapper.withIndentedOutput(), new SmartlingJsonKeys());

Expand Down Expand Up @@ -265,7 +268,8 @@ public void testGetTranslatedUnits() {
null,
meterRegistryMock,
thirdPartyFileChecksumRepositoryMock,
aiTranslationConfiguration);
aiTranslationConfiguration,
aiTranslationService);

ImmutableList<TextUnitDTO> result =
thirdPartyTMSSmartlingWithJson.getTranslatedUnits(
Expand Down

0 comments on commit afe235c

Please sign in to comment.