From a2cb0e38a34be9301019dbd08419b6f9a00ca4d5 Mon Sep 17 00:00:00 2001 From: Jean Aurambault Date: Thu, 27 Jun 2024 14:22:46 -0700 Subject: [PATCH] Quick attempt at making Pull parallel --- .../thirdparty/ThirdPartyTMSPhrase.java | 50 ++++++++++++++----- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/webapp/src/main/java/com/box/l10n/mojito/service/thirdparty/ThirdPartyTMSPhrase.java b/webapp/src/main/java/com/box/l10n/mojito/service/thirdparty/ThirdPartyTMSPhrase.java index ba278cf1b5..aca841d596 100644 --- a/webapp/src/main/java/com/box/l10n/mojito/service/thirdparty/ThirdPartyTMSPhrase.java +++ b/webapp/src/main/java/com/box/l10n/mojito/service/thirdparty/ThirdPartyTMSPhrase.java @@ -22,6 +22,8 @@ import com.google.common.collect.ImmutableList; import com.phrase.client.model.Tag; import com.phrase.client.model.TranslationKey; +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.Timer; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.AbstractMap.SimpleEntry; @@ -34,10 +36,10 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import java.util.function.Consumer; import java.util.stream.Collectors; 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; @@ -50,19 +52,28 @@ public class ThirdPartyTMSPhrase implements ThirdPartyTMS { static Logger logger = LoggerFactory.getLogger(ThirdPartyTMSPhrase.class); - @Autowired TextUnitSearcher textUnitSearcher = new TextUnitSearcher(); + TextUnitSearcher textUnitSearcher = new TextUnitSearcher(); - @Autowired TextUnitBatchImporterService textUnitBatchImporterService; + TextUnitBatchImporterService textUnitBatchImporterService; - @Autowired(required = false) PhraseClient phraseClient; - @Autowired RepositoryService repositoryService; + RepositoryService repositoryService; - public ThirdPartyTMSPhrase() {} + MeterRegistry meterRegistry; - public ThirdPartyTMSPhrase(PhraseClient phraseClient) { + public ThirdPartyTMSPhrase( + TextUnitSearcher textUnitSearcher, + TextUnitBatchImporterService textUnitBatchImporterService, + PhraseClient phraseClient, + RepositoryService repositoryService, + MeterRegistry meterRegistry) { + + this.textUnitSearcher = textUnitSearcher; + this.textUnitBatchImporterService = textUnitBatchImporterService; this.phraseClient = phraseClient; + this.repositoryService = repositoryService; + this.meterRegistry = meterRegistry; } @Override @@ -281,7 +292,24 @@ public PollableFuture pull( String currentTags = getCurrentTagsForRepository(repository, projectId); - for (RepositoryLocale repositoryLocale : repositoryLocalesWithoutRootLocale) { + repositoryLocalesWithoutRootLocale.parallelStream() + .forEach(pullLocaleTimed(repository, projectId, pluralSeparator, currentTags)); + return null; + } + + private Consumer pullLocaleTimed( + Repository repository, String projectId, String pluralSeparator, String currentTags) { + try (var timer = + Timer.resource(meterRegistry, "ThirdPartyTMSPhrase.pullLocale") + .tag("repository", repository.getName())) { + + return pullLocale(repository, projectId, pluralSeparator, currentTags); + } + } + + private Consumer pullLocale( + Repository repository, String projectId, String pluralSeparator, String currentTags) { + return repositoryLocale -> { String localeTag = repositoryLocale.getLocale().getBcp47Tag(); logger.info("Downloading locale: {} from Phrase with tags: {}", localeTag, currentTags); @@ -293,7 +321,7 @@ public PollableFuture pull( currentTags, () -> getCurrentTagsForRepository(repository, projectId)); - logger.info("file content from pull: {}", fileContent); + logger.debug("file content from pull: {}", fileContent); AndroidStringDocumentMapper mapper = new AndroidStringDocumentMapper( @@ -308,9 +336,7 @@ public PollableFuture pull( textUnitDTOS.forEach(t -> t.setComment(null)); textUnitBatchImporterService.importTextUnits(textUnitDTOS, false, true); - } - - return null; + }; } /**