#58 -- add scheduler

This commit is contained in:
Anton Romanov 2022-06-17 16:33:53 +04:00
parent 5426253b39
commit 08751b9452
19 changed files with 186 additions and 83 deletions

View File

@ -2,8 +2,10 @@ package ru.ulstu.extractor;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication @SpringBootApplication
@EnableScheduling
public class GitExtractorApplication { public class GitExtractorApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(GitExtractorApplication.class, args); SpringApplication.run(GitExtractorApplication.class, args);

View File

@ -0,0 +1,21 @@
package ru.ulstu.extractor.branch;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import ru.ulstu.extractor.model.Branch;
import ru.ulstu.extractor.model.Repository;
import java.util.List;
public interface BranchRepository extends JpaRepository<Branch, Integer> {
Branch findByRepositoryAndName(Repository repository, String name);
@Query("select count(c) from Commit c LEFT JOIN c.branch b LEFT JOIN Repository r where r.id = ?1 AND b.name = ?2")
int getCommitsCount(Integer repositoryId, String name);
List<Branch> findByRepositoryId(Integer repositoryId);
Page<Branch> findByRepository(Repository repository, Pageable pageable);
}

View File

@ -3,16 +3,19 @@
* You may use, distribute and modify this code, please write to: romanov73@gmail.com. * You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*/ */
package ru.ulstu.extractor.service; package ru.ulstu.extractor.branch;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.extractor.model.BaseEntity; import ru.ulstu.extractor.model.BaseEntity;
import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Branch;
import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.Commit;
import ru.ulstu.extractor.model.Repository; import ru.ulstu.extractor.model.Repository;
import ru.ulstu.extractor.repository.BranchRepository; import ru.ulstu.extractor.service.CommitService;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -29,6 +32,7 @@ public class BranchService {
this.commitService = commitService; this.commitService = commitService;
} }
@Transactional
public Branch save(Branch branch, List<Commit> commits) { public Branch save(Branch branch, List<Commit> commits) {
LOG.debug("Start save {} branch with {} commits ", branch.getName(), commits.size()); LOG.debug("Start save {} branch with {} commits ", branch.getName(), commits.size());
List<Integer> commitsToRemoveIds = branch.getCommits().stream().map(BaseEntity::getId).collect(Collectors.toList()); List<Integer> commitsToRemoveIds = branch.getCommits().stream().map(BaseEntity::getId).collect(Collectors.toList());
@ -52,6 +56,14 @@ public class BranchService {
return branchRepository.findByRepositoryAndName(repository, branchName); return branchRepository.findByRepositoryAndName(repository, branchName);
} }
public int getCommitsCount(Integer repositoryId, String branchName) {
return branchRepository.getCommitsCount(repositoryId, branchName);
}
public Page<Branch> findByRepository(Repository repository, Pageable pageable) {
return branchRepository.findByRepository(repository, pageable);
}
public List<Branch> findAll() { public List<Branch> findAll() {
return branchRepository.findAll(); return branchRepository.findAll();
} }

View File

@ -9,8 +9,8 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.extractor.repository.BranchRepository; import ru.ulstu.extractor.branch.BranchRepository;
import ru.ulstu.extractor.repository.RepositoryRepository; import ru.ulstu.extractor.gitrepository.GitRepositoryRepository;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;
import static ru.ulstu.extractor.controller.Route.DELETE_BRANCH; import static ru.ulstu.extractor.controller.Route.DELETE_BRANCH;
@ -19,11 +19,11 @@ import static ru.ulstu.extractor.controller.Route.LIST_REPOSITORY_BRANCHES;
@Controller @Controller
@ApiIgnore @ApiIgnore
public class BranchController { public class BranchController {
private final RepositoryRepository repositoryRepository; private final GitRepositoryRepository gitRepositoryRepository;
private final BranchRepository branchRepository; private final BranchRepository branchRepository;
public BranchController(RepositoryRepository repositoryRepository, BranchRepository branchRepository) { public BranchController(GitRepositoryRepository gitRepositoryRepository, BranchRepository branchRepository) {
this.repositoryRepository = repositoryRepository; this.gitRepositoryRepository = gitRepositoryRepository;
this.branchRepository = branchRepository; this.branchRepository = branchRepository;
} }
@ -32,7 +32,7 @@ public class BranchController {
Model model, Model model,
@RequestParam int repositoryId) { @RequestParam int repositoryId) {
model.addAttribute("branches", branchRepository.findByRepositoryId(repositoryId)); model.addAttribute("branches", branchRepository.findByRepositoryId(repositoryId));
model.addAttribute("repository", repositoryRepository.findById(repositoryId).get()); model.addAttribute("repository", gitRepositoryRepository.findById(repositoryId).get());
return LIST_REPOSITORY_BRANCHES; return LIST_REPOSITORY_BRANCHES;
} }
@ -42,7 +42,7 @@ public class BranchController {
@RequestParam Integer id) { @RequestParam Integer id) {
branchRepository.deleteById(id); branchRepository.deleteById(id);
model.addAttribute("branches", branchRepository.findByRepositoryId(repositoryId)); model.addAttribute("branches", branchRepository.findByRepositoryId(repositoryId));
model.addAttribute("repository", repositoryRepository.findById(repositoryId).get()); model.addAttribute("repository", gitRepositoryRepository.findById(repositoryId).get());
return LIST_REPOSITORY_BRANCHES; return LIST_REPOSITORY_BRANCHES;
} }
} }

View File

@ -15,10 +15,10 @@ import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes; import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import ru.ulstu.extractor.gitrepository.GitRepositoryService;
import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Branch;
import ru.ulstu.extractor.model.mvc.FilterForm; import ru.ulstu.extractor.model.mvc.FilterForm;
import ru.ulstu.extractor.model.mvc.RepoForm; import ru.ulstu.extractor.model.mvc.RepoForm;
import ru.ulstu.extractor.service.GitRepositoryService;
import ru.ulstu.extractor.service.IndexService; import ru.ulstu.extractor.service.IndexService;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;

View File

@ -12,8 +12,8 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.extractor.gitrepository.GitRepositoryService;
import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.Commit;
import ru.ulstu.extractor.service.GitRepositoryService;
import ru.ulstu.extractor.service.IndexService; import ru.ulstu.extractor.service.IndexService;
import ru.ulstu.extractor.util.HttpUtils; import ru.ulstu.extractor.util.HttpUtils;

View File

@ -9,7 +9,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.extractor.repository.RepositoryRepository; import ru.ulstu.extractor.gitrepository.GitRepositoryRepository;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;
import static ru.ulstu.extractor.controller.Route.DELETE_INDEXED_REPOSITORY; import static ru.ulstu.extractor.controller.Route.DELETE_INDEXED_REPOSITORY;
@ -18,23 +18,23 @@ import static ru.ulstu.extractor.controller.Route.LIST_INDEXED_REPOSITORIES;
@Controller @Controller
@ApiIgnore @ApiIgnore
public class RepositoryController { public class RepositoryController {
private final RepositoryRepository repositoryRepository; private final GitRepositoryRepository gitRepositoryRepository;
public RepositoryController(RepositoryRepository repositoryRepository) { public RepositoryController(GitRepositoryRepository gitRepositoryRepository) {
this.repositoryRepository = repositoryRepository; this.gitRepositoryRepository = gitRepositoryRepository;
} }
@GetMapping(LIST_INDEXED_REPOSITORIES) @GetMapping(LIST_INDEXED_REPOSITORIES)
public String indexNewRepo(Model model) { public String indexNewRepo(Model model) {
model.addAttribute("repositories", repositoryRepository.findAll()); model.addAttribute("repositories", gitRepositoryRepository.findAll());
return LIST_INDEXED_REPOSITORIES; return LIST_INDEXED_REPOSITORIES;
} }
@GetMapping(DELETE_INDEXED_REPOSITORY) @GetMapping(DELETE_INDEXED_REPOSITORY)
public String deleteRepo(Model model, public String deleteRepo(Model model,
@RequestParam Integer id) { @RequestParam Integer id) {
repositoryRepository.deleteById(id); gitRepositoryRepository.deleteById(id);
model.addAttribute("repositories", repositoryRepository.findAll()); model.addAttribute("repositories", gitRepositoryRepository.findAll());
return "redirect:/" + LIST_INDEXED_REPOSITORIES; return "redirect:/" + LIST_INDEXED_REPOSITORIES;
} }
} }

View File

@ -9,9 +9,9 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.extractor.branch.BranchService;
import ru.ulstu.extractor.model.mvc.FilterForm; import ru.ulstu.extractor.model.mvc.FilterForm;
import ru.ulstu.extractor.repository.CommitRepository; import ru.ulstu.extractor.repository.CommitRepository;
import ru.ulstu.extractor.service.BranchService;
import ru.ulstu.extractor.service.FilteringService; import ru.ulstu.extractor.service.FilteringService;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;

View File

@ -1,8 +1,8 @@
package ru.ulstu.extractor.repository; package ru.ulstu.extractor.gitrepository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.extractor.model.Repository; import ru.ulstu.extractor.model.Repository;
public interface RepositoryRepository extends JpaRepository<Repository, Integer> { public interface GitRepositoryRepository extends JpaRepository<Repository, Integer> {
Repository findByUrl(String url); Repository findByUrl(String url);
} }

View File

@ -1,9 +1,4 @@
/* package ru.ulstu.extractor.gitrepository;
* Copyright (C) 2021 Anton Romanov - All Rights Reserved
* You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*/
package ru.ulstu.extractor.service;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.CreateBranchCommand; import org.eclipse.jgit.api.CreateBranchCommand;
@ -15,12 +10,13 @@ import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader; import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.ObjectReader; import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.TreeWalk;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit; import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit;
import ru.ulstu.extractor.heuristic.model.EntityUnit; import ru.ulstu.extractor.heuristic.model.EntityUnit;
@ -31,6 +27,7 @@ import ru.ulstu.extractor.model.Branch;
import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.Commit;
import ru.ulstu.extractor.model.FileChange; import ru.ulstu.extractor.model.FileChange;
import ru.ulstu.extractor.model.LineChange; import ru.ulstu.extractor.model.LineChange;
import ru.ulstu.extractor.model.Repository;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -61,18 +58,20 @@ public class GitRepositoryService {
private String customProjectsDir; private String customProjectsDir;
private final ExecutorService executorService = Executors.newFixedThreadPool(4); private final ExecutorService executorService = Executors.newFixedThreadPool(4);
private final ExecutorService executorServiceCommits = Executors.newFixedThreadPool(4); private final ExecutorService executorServiceCommits = Executors.newFixedThreadPool(4);
private final StructuralUnitService structuralUnitService; private final StructuralUnitService structuralUnitService;
private final GitRepositoryRepository gitRepositoryRepository;
public GitRepositoryService(StructuralUnitService structuralUnitService) { public GitRepositoryService(StructuralUnitService structuralUnitService,
GitRepositoryRepository gitRepositoryRepository) {
this.structuralUnitService = structuralUnitService; this.structuralUnitService = structuralUnitService;
this.gitRepositoryRepository = gitRepositoryRepository;
} }
public List<Branch> getRemoteBranches(String url) throws GitAPIException, IOException { public List<Branch> getRemoteBranches(String url) throws GitAPIException, IOException {
LOG.debug("Get remote branches of {}. Clone", url); LOG.debug("Get remote branches of {}. Clone", url);
cloneOrUpdateRepo(url); cloneOrUpdateRepo(url);
LOG.debug("Get remote branches of {}. Get branches", url); LOG.debug("Get remote branches of {}. Get branches", url);
Repository localRepo = new FileRepository(getProjectGitDirectory(url)); org.eclipse.jgit.lib.Repository localRepo = new FileRepository(getProjectGitDirectory(url));
Git git = new Git(localRepo); Git git = new Git(localRepo);
List<Branch> branches = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE) List<Branch> branches = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE)
.call() .call()
@ -88,7 +87,7 @@ public class GitRepositoryService {
LOG.debug("Get local branches of {}. Clone", url); LOG.debug("Get local branches of {}. Clone", url);
cloneOrUpdateRepo(url); cloneOrUpdateRepo(url);
LOG.debug("Get local branches of {}. Get branches", url); LOG.debug("Get local branches of {}. Get branches", url);
Repository localRepo = new FileRepository(getProjectGitDirectory(url)); org.eclipse.jgit.lib.Repository localRepo = new FileRepository(getProjectGitDirectory(url));
Git git = new Git(localRepo); Git git = new Git(localRepo);
List<Branch> branches = git.branchList() List<Branch> branches = git.branchList()
.call() .call()
@ -105,7 +104,7 @@ public class GitRepositoryService {
if (needUpdate) { if (needUpdate) {
cloneOrUpdateRepo(repositoryUrl, branchName); cloneOrUpdateRepo(repositoryUrl, branchName);
} }
Repository localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl)); org.eclipse.jgit.lib.Repository localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl));
Git git = new Git(localRepo); Git git = new Git(localRepo);
List<RevCommit> commits = new ArrayList<>(); List<RevCommit> commits = new ArrayList<>();
@ -144,7 +143,7 @@ public class GitRepositoryService {
return list; return list;
} }
private void checkoutBranch(String repositoryUrl, Git git, Repository localRepo, String branchName) throws GitAPIException, IOException { private void checkoutBranch(String repositoryUrl, Git git, org.eclipse.jgit.lib.Repository localRepo, String branchName) throws GitAPIException, IOException {
LOG.debug("Checkout branch {} {}", repositoryUrl, branchName); LOG.debug("Checkout branch {} {}", repositoryUrl, branchName);
git.pull().call(); git.pull().call();
if (!localRepo.getBranch().equals(branchName)) { if (!localRepo.getBranch().equals(branchName)) {
@ -192,7 +191,7 @@ public class GitRepositoryService {
private void cloneOrUpdateRepo(String repositoryUrl, String branchName) throws GitAPIException, IOException { private void cloneOrUpdateRepo(String repositoryUrl, String branchName) throws GitAPIException, IOException {
Git git; Git git;
Repository localRepo; org.eclipse.jgit.lib.Repository localRepo;
if (projectDirExists(getProjectDirectoryFile(repositoryUrl))) { if (projectDirExists(getProjectDirectoryFile(repositoryUrl))) {
localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl)); localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl));
git = new Git(localRepo); git = new Git(localRepo);
@ -240,7 +239,7 @@ public class GitRepositoryService {
return file.exists(); return file.exists();
} }
private List<FileChange> findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, Repository localRepo) { private List<FileChange> findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, org.eclipse.jgit.lib.Repository localRepo) {
if (laterCommit == null || earlierCommit == null) { if (laterCommit == null || earlierCommit == null) {
return null; return null;
} }
@ -256,7 +255,7 @@ public class GitRepositoryService {
return parseOutputDiff(output, localRepo, laterCommit); return parseOutputDiff(output, localRepo, laterCommit);
} }
private List<FileChange> parseOutputDiff(String output, Repository repository, RevCommit commit) { private List<FileChange> parseOutputDiff(String output, org.eclipse.jgit.lib.Repository repository, RevCommit commit) {
List<FileChange> changes = new ArrayList<>(); List<FileChange> changes = new ArrayList<>();
String[] strings = output.split("\n"); String[] strings = output.split("\n");
FileChange fileChange = new FileChange(); FileChange fileChange = new FileChange();
@ -322,7 +321,7 @@ public class GitRepositoryService {
return changes; return changes;
} }
private String getContent(Repository repository, RevCommit commit, String path) { private String getContent(org.eclipse.jgit.lib.Repository repository, RevCommit commit, String path) {
try (TreeWalk treeWalk = TreeWalk.forPath(repository, path, commit.getTree())) { try (TreeWalk treeWalk = TreeWalk.forPath(repository, path, commit.getTree())) {
if (treeWalk != null) { if (treeWalk != null) {
ObjectId blobId = treeWalk.getObjectId(0); ObjectId blobId = treeWalk.getObjectId(0);
@ -346,4 +345,8 @@ public class GitRepositoryService {
} }
return Optional.empty(); return Optional.empty();
} }
public Page<Repository> findAll(Pageable pageable) {
return gitRepositoryRepository.findAll(pageable);
}
} }

View File

@ -11,10 +11,10 @@ import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.extractor.gitrepository.GitRepositoryService;
import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit; import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit;
import ru.ulstu.extractor.heuristic.model.EntityUnit; import ru.ulstu.extractor.heuristic.model.EntityUnit;
import ru.ulstu.extractor.heuristic.model.ResourceUnit; import ru.ulstu.extractor.heuristic.model.ResourceUnit;
import ru.ulstu.extractor.service.GitRepositoryService;
import ru.ulstu.extractor.util.HttpUtils; import ru.ulstu.extractor.util.HttpUtils;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;

View File

@ -0,0 +1,69 @@
package ru.ulstu.extractor.loader;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import ru.ulstu.extractor.branch.BranchService;
import ru.ulstu.extractor.gitrepository.GitRepositoryService;
import ru.ulstu.extractor.model.Branch;
import ru.ulstu.extractor.model.OffsetablePageRequest;
import ru.ulstu.extractor.model.Repository;
import ru.ulstu.extractor.service.IndexService;
import ru.ulstu.extractor.ts.AbstractTSExtractor;
import java.io.IOException;
import java.util.List;
@Service
public class GitScheduler {
private final static int DEFAULT_PAGE_SIZE = 100;
private final Logger log = LoggerFactory.getLogger(GitScheduler.class);
private final GitRepositoryService gitRepositoryService;
private final BranchService branchService;
private final IndexService indexService;
private final List<AbstractTSExtractor> tsExtractors;
public GitScheduler(GitRepositoryService gitRepositoryService,
BranchService branchService,
IndexService indexService,
List<AbstractTSExtractor> tsExtractors) {
this.gitRepositoryService = gitRepositoryService;
this.branchService = branchService;
this.indexService = indexService;
this.tsExtractors = tsExtractors;
}
@Scheduled(cron = "* */5 * * * *")
public void getCommitsCount() {
log.debug("Load commits count started");
int repositoryPageNumber = 0;
Page<Repository> repositoryPage;
do {
int branchPageNumber = 0;
repositoryPage = gitRepositoryService.findAll(
new OffsetablePageRequest(repositoryPageNumber, DEFAULT_PAGE_SIZE));
Page<Branch> branchPage;
for (Repository repository : repositoryPage.getContent()) {
do {
branchPage = branchService.findByRepository(repository,
new OffsetablePageRequest(branchPageNumber, DEFAULT_PAGE_SIZE));
for (Branch branch : branchPage.getContent()) {
try {
indexService.index(repository.getUrl(), branch.getName());
tsExtractors.forEach(tsExtractor -> tsExtractor.addPoint(repository.getId(), branch.getName()));
} catch (GitAPIException | IOException ex) {
log.warn(ex.getMessage());
ex.printStackTrace();
}
}
branchPageNumber++;
} while (!branchPage.isEmpty());
}
repositoryPageNumber++;
} while (!repositoryPage.isEmpty());
log.debug("Load commits count finished");
}
}

View File

@ -1,13 +0,0 @@
package ru.ulstu.extractor.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.extractor.model.Branch;
import ru.ulstu.extractor.model.Repository;
import java.util.List;
public interface BranchRepository extends JpaRepository<Branch, Integer> {
Branch findByRepositoryAndName(Repository repository, String name);
List<Branch> findByRepositoryId(Integer repositoryId);
}

View File

@ -1,13 +1,10 @@
package ru.ulstu.extractor.repository; package ru.ulstu.extractor.repository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.extractor.model.TimeSeries;
import ru.ulstu.extractor.model.TimeSeriesValue; import ru.ulstu.extractor.model.TimeSeriesValue;
import java.util.List;
public interface TimeSeriesValueRepository extends JpaRepository<TimeSeriesValue, Integer> { public interface TimeSeriesValueRepository extends JpaRepository<TimeSeriesValue, Integer> {
TimeSeriesValue findByTimeSeriesAndName(TimeSeries timeSeries, String name); //TimeSeriesValue findByTimeSeriesAndName(TimeSeries timeSeries, String name);
List<TimeSeriesValue> findTimeSeriesValueById(Integer repositoryId); //List<TimeSeriesValue> findTimeSeriesValueById(Integer repositoryId);
} }

View File

@ -9,10 +9,10 @@ import com.sun.istack.NotNull;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.extractor.gitrepository.GitRepositoryRepository;
import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.Commit;
import ru.ulstu.extractor.repository.AuthorRepository; import ru.ulstu.extractor.repository.AuthorRepository;
import ru.ulstu.extractor.repository.CommitRepository; import ru.ulstu.extractor.repository.CommitRepository;
import ru.ulstu.extractor.repository.RepositoryRepository;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -21,20 +21,20 @@ import java.util.Map;
public class FilteringService { public class FilteringService {
private final AuthorRepository authorRepository; private final AuthorRepository authorRepository;
private final CommitRepository commitRepository; private final CommitRepository commitRepository;
private final RepositoryRepository repositoryRepository; private final GitRepositoryRepository gitRepositoryRepository;
public FilteringService(AuthorRepository authorRepository, public FilteringService(AuthorRepository authorRepository,
CommitRepository commitRepository, CommitRepository commitRepository,
RepositoryRepository repositoryRepository) { GitRepositoryRepository gitRepositoryRepository) {
this.authorRepository = authorRepository; this.authorRepository = authorRepository;
this.commitRepository = commitRepository; this.commitRepository = commitRepository;
this.repositoryRepository = repositoryRepository; this.gitRepositoryRepository = gitRepositoryRepository;
} }
public List<String> getRepositoryAuthors(@NotNull String repositoryUrl, public List<String> getRepositoryAuthors(@NotNull String repositoryUrl,
@NotNull String branchName) { @NotNull String branchName) {
return authorRepository.findByRepositoryAndBranch( return authorRepository.findByRepositoryAndBranch(
repositoryRepository.findByUrl(repositoryUrl), gitRepositoryRepository.findByUrl(repositoryUrl),
branchName branchName
); );
} }
@ -51,7 +51,7 @@ public class FilteringService {
Pageable pageable) { Pageable pageable) {
return commitRepository.findByRepositoryAndBranch( return commitRepository.findByRepositoryAndBranch(
pageable, pageable,
repositoryRepository.findByUrl(repositoryUrl), gitRepositoryRepository.findByUrl(repositoryUrl),
branchName, branchName,
author, author,
filter, filter,

View File

@ -10,10 +10,13 @@ import org.eclipse.jgit.api.errors.GitAPIException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.extractor.branch.BranchService;
import ru.ulstu.extractor.gitrepository.GitRepositoryRepository;
import ru.ulstu.extractor.gitrepository.GitRepositoryService;
import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Branch;
import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.Commit;
import ru.ulstu.extractor.model.Repository; import ru.ulstu.extractor.model.Repository;
import ru.ulstu.extractor.repository.RepositoryRepository;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
@ -24,21 +27,22 @@ public class IndexService {
private final static Logger LOG = LoggerFactory.getLogger(IndexService.class); private final static Logger LOG = LoggerFactory.getLogger(IndexService.class);
private final static int COMMITS_PAGE_SIZE = 10; private final static int COMMITS_PAGE_SIZE = 10;
private final GitRepositoryService gitRepositoryService; private final GitRepositoryService gitRepositoryService;
private final RepositoryRepository repositoryRepository; private final GitRepositoryRepository gitRepositoryRepository;
private final BranchService branchService; private final BranchService branchService;
public IndexService(GitRepositoryService gitRepositoryService, public IndexService(GitRepositoryService gitRepositoryService,
RepositoryRepository repositoryRepository, GitRepositoryRepository gitRepositoryRepository,
BranchService branchService) { BranchService branchService) {
this.gitRepositoryService = gitRepositoryService; this.gitRepositoryService = gitRepositoryService;
this.repositoryRepository = repositoryRepository; this.gitRepositoryRepository = gitRepositoryRepository;
this.branchService = branchService; this.branchService = branchService;
} }
@Transactional
public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException { public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException {
Repository repository = repositoryRepository.findByUrl(repositoryUrl); Repository repository = gitRepositoryRepository.findByUrl(repositoryUrl);
if (repository == null) { if (repository == null) {
repository = repositoryRepository.save(new Repository(repositoryUrl)); repository = gitRepositoryRepository.save(new Repository(repositoryUrl));
} }
Branch branch = branchService.findByRepositoryAndName(repository, branchName); Branch branch = branchService.findByRepositoryAndName(repository, branchName);
if (branch == null) { if (branch == null) {

View File

@ -64,10 +64,10 @@ public class TimeSeriesService {
TimeSeries timeSeries = findOrCreate(timeSeriesName); TimeSeries timeSeries = findOrCreate(timeSeriesName);
timeSeriesValueRepository.save(new TimeSeriesValue(timeSeries, date, value)); timeSeriesValueRepository.save(new TimeSeriesValue(timeSeries, date, value));
} }
//
public TimeSeriesValue findByTimeSeriesAndName(TimeSeries timeSeries, String name) { // public TimeSeriesValue findByTimeSeriesAndName(TimeSeries timeSeries, String name) {
return timeSeriesValueRepository.findByTimeSeriesAndName(timeSeries, name); // return timeSeriesValueRepository.findByTimeSeriesAndName(timeSeries, name);
} // }
public List<TimeSeriesValue> findAll() { public List<TimeSeriesValue> findAll() {
return timeSeriesValueRepository.findAll(); return timeSeriesValueRepository.findAll();

View File

@ -1,22 +1,21 @@
package ru.ulstu.extractor.ts; package ru.ulstu.extractor.ts;
import ru.ulstu.extractor.model.TimeSeriesValue;
import ru.ulstu.extractor.service.TimeSeriesService; import ru.ulstu.extractor.service.TimeSeriesService;
import java.util.List; import java.util.Date;
public abstract class AbstractTSExtractor { public abstract class AbstractTSExtractor {
public abstract String getTSName(); public abstract String getTSName();
public abstract int getNewTSValue(Integer repositoryId, String branchName);
public abstract TimeSeriesService getTimeSeriesService(); public abstract TimeSeriesService getTimeSeriesService();
public void addPoint(int value) { public void addPoint(Integer repositoryId, String branchName) {
getTimeSeriesService().addTimeSeriesValue(
String.format("%s %s %s", getTSName(), repositoryId, branchName),
new Date(),
getNewTSValue(repositoryId, branchName));
} }
public void addTS(List<TimeSeriesValue> values) {
getTimeSeriesService().findOrCreate(getTSName());
}
} }

View File

@ -1,14 +1,18 @@
package ru.ulstu.extractor.ts; package ru.ulstu.extractor.ts;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.BranchService;
import ru.ulstu.extractor.service.TimeSeriesService; import ru.ulstu.extractor.service.TimeSeriesService;
@Component @Component
public class CommitsTS extends AbstractTSExtractor { public class CommitsTS extends AbstractTSExtractor {
private final TimeSeriesService timeSeriesService; private final TimeSeriesService timeSeriesService;
private final BranchService branchService;
public CommitsTS(TimeSeriesService timeSeriesService) { public CommitsTS(TimeSeriesService timeSeriesService,
BranchService branchService) {
this.timeSeriesService = timeSeriesService; this.timeSeriesService = timeSeriesService;
this.branchService = branchService;
} }
@Override @Override
@ -16,6 +20,11 @@ public class CommitsTS extends AbstractTSExtractor {
return "Количество коммитов во времени"; return "Количество коммитов во времени";
} }
@Override
public int getNewTSValue(Integer repositoryId, String branchName) {
return branchService.getCommitsCount(repositoryId, branchName);
}
@Override @Override
public TimeSeriesService getTimeSeriesService() { public TimeSeriesService getTimeSeriesService() {
return timeSeriesService; return timeSeriesService;