Skip to content

Commit

Permalink
Quick attempt at making Pull parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
ja-openai committed Jun 27, 2024
1 parent 91bb9d0 commit 58b99c2
Showing 1 changed file with 38 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,6 +36,7 @@
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;
Expand All @@ -50,19 +53,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
Expand Down Expand Up @@ -281,7 +293,24 @@ public PollableFuture<Void> pull(

String currentTags = getCurrentTagsForRepository(repository, projectId);

for (RepositoryLocale repositoryLocale : repositoryLocalesWithoutRootLocale) {
repositoryLocalesWithoutRootLocale.parallelStream()
.forEach(pullLocaleTimed(repository, projectId, pluralSeparator, currentTags));
return null;
}

private Consumer<RepositoryLocale> 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<RepositoryLocale> 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);

Expand All @@ -293,7 +322,7 @@ public PollableFuture<Void> pull(
currentTags,
() -> getCurrentTagsForRepository(repository, projectId));

logger.info("file content from pull: {}", fileContent);
logger.debug("file content from pull: {}", fileContent);

AndroidStringDocumentMapper mapper =
new AndroidStringDocumentMapper(
Expand All @@ -308,9 +337,7 @@ public PollableFuture<Void> pull(
textUnitDTOS.forEach(t -> t.setComment(null));

textUnitBatchImporterService.importTextUnits(textUnitDTOS, false, true);
}

return null;
};
}

/**
Expand Down

0 comments on commit 58b99c2

Please sign in to comment.