|
|
|
@ -9,8 +9,11 @@ import com.sun.istack.NotNull;
|
|
|
|
|
import org.eclipse.jgit.api.errors.GitAPIException;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import ru.ulstu.extractor.branch.model.Branch;
|
|
|
|
|
import ru.ulstu.extractor.branch.model.IndexingStatus;
|
|
|
|
|
import ru.ulstu.extractor.branch.service.BranchService;
|
|
|
|
|
import ru.ulstu.extractor.commit.model.Commit;
|
|
|
|
|
import ru.ulstu.extractor.gitrepository.model.GitRepository;
|
|
|
|
@ -20,6 +23,8 @@ import ru.ulstu.extractor.ts.creator.AbstractTimeSeriesCreator;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class IndexService {
|
|
|
|
@ -29,6 +34,7 @@ public class IndexService {
|
|
|
|
|
private final GitRepositoryRepository gitRepositoryRepository;
|
|
|
|
|
private final BranchService branchService;
|
|
|
|
|
private final List<AbstractTimeSeriesCreator> timeSeriesCreators;
|
|
|
|
|
private final static ExecutorService EXECUTOR = Executors.newFixedThreadPool(10);
|
|
|
|
|
|
|
|
|
|
public IndexService(GitRepositoryService gitRepositoryService,
|
|
|
|
|
GitRepositoryRepository gitRepositoryRepository,
|
|
|
|
@ -40,35 +46,64 @@ public class IndexService {
|
|
|
|
|
this.timeSeriesCreators = timeSeriesCreators;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
@Async
|
|
|
|
|
public void index(@NotNull Integer branchId) throws GitAPIException, IOException {
|
|
|
|
|
Branch branch = branchService.findByBranchId(branchId).orElseThrow(() -> new RuntimeException("Ветка репозитория не найдена по id"));
|
|
|
|
|
index(branch.getGitRepository().getUrl(), branch.getName());
|
|
|
|
|
index(branch.getGitRepository(), branch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException {
|
|
|
|
|
GitRepository gitRepository = gitRepositoryRepository.findByUrl(repositoryUrl);
|
|
|
|
|
if (gitRepository == null) {
|
|
|
|
|
gitRepository = gitRepositoryRepository.save(new GitRepository(repositoryUrl));
|
|
|
|
|
}
|
|
|
|
|
Branch branch = branchService.findByRepositoryAndName(gitRepository, branchName);
|
|
|
|
|
if (branch == null) {
|
|
|
|
|
branch = new Branch(gitRepository, branchName);
|
|
|
|
|
@Transactional
|
|
|
|
|
@Async
|
|
|
|
|
public void index(String gitRepositoryUrl, String branchName) throws GitAPIException, IOException {
|
|
|
|
|
GitRepository gitRepository = gitRepositoryService.findByUrlOrCreate(gitRepositoryUrl);
|
|
|
|
|
Branch branch = branchService.findByRepositoryAndNameOrCreate(gitRepository, branchName);
|
|
|
|
|
branch = branchService.updateStatus(branch, IndexingStatus.INDEXING);
|
|
|
|
|
index(gitRepository, branch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
@Async
|
|
|
|
|
public void index(GitRepository gitRepository, Branch branch) throws GitAPIException, IOException {
|
|
|
|
|
branchService.save(branch, Collections.emptyList());
|
|
|
|
|
branch = branchService.updateStatus(branch, IndexingStatus.INDEXING);
|
|
|
|
|
int commitsFrom = 0;
|
|
|
|
|
int commitsTo = COMMITS_PAGE_SIZE;
|
|
|
|
|
List<Commit> commits = gitRepositoryService.getCommits(repositoryUrl, branchName, commitsFrom, commitsTo, true);
|
|
|
|
|
List<Commit> commits = gitRepositoryService.getCommits(gitRepository.getUrl(), branch.getName(), commitsFrom, commitsTo, true);
|
|
|
|
|
while (!commits.isEmpty()) {
|
|
|
|
|
LOG.debug("{} commits loaded.", commits.size());
|
|
|
|
|
branchService.addCommits(branch, commits);
|
|
|
|
|
LOG.debug("{} commits successfully saved. {} {}", commits.size(), repositoryUrl, branchName);
|
|
|
|
|
LOG.debug("{} commits successfully saved. {} {}", commits.size(), gitRepository.getUrl(), branch.getName());
|
|
|
|
|
commitsFrom += COMMITS_PAGE_SIZE;
|
|
|
|
|
commitsTo += COMMITS_PAGE_SIZE;
|
|
|
|
|
commits = gitRepositoryService.getCommits(repositoryUrl, branchName, commitsFrom, commitsTo, false);
|
|
|
|
|
commits = gitRepositoryService.getCommits(gitRepository.getUrl(), branch.getName(), commitsFrom, commitsTo, false);
|
|
|
|
|
}
|
|
|
|
|
Integer repositoryId = gitRepository.getId();
|
|
|
|
|
final Branch branchForSave = branch;
|
|
|
|
|
timeSeriesCreators.forEach(tsCreator -> tsCreator.addTimeSeries(repositoryId, branchForSave));
|
|
|
|
|
LOG.debug("Complete indexing {} branch", branchName);
|
|
|
|
|
branchService.updateStatus(branch, IndexingStatus.FINISHED);
|
|
|
|
|
LOG.debug("Complete indexing {} branch", branch.getName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
@Async
|
|
|
|
|
public void indexFailedBranchesOnStart() {
|
|
|
|
|
LOG.info("Старт проверки незавершенных задач для индексирований...");
|
|
|
|
|
|
|
|
|
|
List<Branch> failedBranches = branchService.findAllByIndexingStatus(IndexingStatus.INDEXING);
|
|
|
|
|
if (failedBranches.size() > 0) {
|
|
|
|
|
LOG.info("Найдено {} незавершенных задач для индексирования", failedBranches.size());
|
|
|
|
|
failedBranches.forEach(failedBranch -> {
|
|
|
|
|
try {
|
|
|
|
|
index(failedBranch.getId());
|
|
|
|
|
LOG.info("Завершено индексирование ветки {}", failedBranch.getName());
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
LOG.warn(ex.getMessage());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
LOG.info("Завершено индексирование незавершенных задач");
|
|
|
|
|
} else {
|
|
|
|
|
LOG.info("Не найдено незавершенных веток для индексирования");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|