add loggers

This commit is contained in:
Anton Romanov 2021-04-27 16:28:01 +04:00
parent 4629562e0f
commit 97cefbfbdf
9 changed files with 74 additions and 24 deletions

View File

@ -6,6 +6,8 @@
package ru.ulstu.extractor.controller; package ru.ulstu.extractor.controller;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.slf4j.Logger;
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.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
@ -13,7 +15,9 @@ import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.Commit;
import ru.ulstu.extractor.service.GitRepositoryService; 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 javax.servlet.http.HttpServletRequest;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -22,6 +26,7 @@ import static ru.ulstu.extractor.controller.RepoController.URL;
@RestController @RestController
@RequestMapping(URL) @RequestMapping(URL)
public class RepoController { public class RepoController {
private final static Logger LOG = LoggerFactory.getLogger(RepoController.class);
public static final String URL = "/"; public static final String URL = "/";
private final GitRepositoryService gitRepositoryService; private final GitRepositoryService gitRepositoryService;
private final IndexService indexService; private final IndexService indexService;
@ -34,13 +39,17 @@ public class RepoController {
@GetMapping("commits") @GetMapping("commits")
public List<Commit> getCommits(@RequestParam("repositoryUrl") String repositoryUrl, public List<Commit> getCommits(@RequestParam("repositoryUrl") String repositoryUrl,
@RequestParam("branchName") String branchName) throws GitAPIException, IOException { @RequestParam("branchName") String branchName,
HttpServletRequest request) throws GitAPIException, IOException {
LOG.debug("Get commits {} {}. User ip {}", repositoryUrl, branchName, HttpUtils.getUserIp(request));
return gitRepositoryService.getCommits(repositoryUrl, branchName); return gitRepositoryService.getCommits(repositoryUrl, branchName);
} }
@GetMapping("index") @GetMapping("index")
public Boolean indexRepository(@RequestParam("repositoryUrl") String repositoryUrl, public Boolean indexRepository(@RequestParam("repositoryUrl") String repositoryUrl,
@RequestParam("branchName") String branchName) throws GitAPIException, IOException { @RequestParam("branchName") String branchName,
HttpServletRequest request) throws GitAPIException, IOException {
LOG.debug("Index {} {}. User ip {}", repositoryUrl, branchName, HttpUtils.getUserIp(request));
indexService.index(repositoryUrl, branchName); indexService.index(repositoryUrl, branchName);
return true; return true;
} }

View File

@ -6,6 +6,8 @@
package ru.ulstu.extractor.heuristic.controller; package ru.ulstu.extractor.heuristic.controller;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.slf4j.Logger;
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;
@ -13,13 +15,16 @@ 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.service.GitRepositoryService;
import ru.ulstu.extractor.util.HttpUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping("StructuralUnitController") @RequestMapping("StructuralUnitController")
public class StructuralUnitController { public class StructuralUnitController {
private final static Logger LOG = LoggerFactory.getLogger(StructuralUnitController.class);
private final GitRepositoryService gitRepositoryService; private final GitRepositoryService gitRepositoryService;
public StructuralUnitController(GitRepositoryService gitRepositoryService) { public StructuralUnitController(GitRepositoryService gitRepositoryService) {
@ -27,17 +32,26 @@ public class StructuralUnitController {
} }
@GetMapping("get-entities") @GetMapping("get-entities")
public List<EntityUnit> getEntities(String repositoryUrl, String branchName) throws IOException, GitAPIException { public List<EntityUnit> getEntities(String repositoryUrl,
String branchName,
HttpServletRequest request) throws IOException, GitAPIException {
LOG.debug("Get entities {} {}. User ip {}", repositoryUrl, branchName, HttpUtils.getUserIp(request));
return gitRepositoryService.getEntities(repositoryUrl, branchName); return gitRepositoryService.getEntities(repositoryUrl, branchName);
} }
@GetMapping("get-business-logic") @GetMapping("get-business-logic")
public List<BusinessLogicUnit> getBusinessLogic(String repositoryUrl, String branchName) throws IOException, GitAPIException { public List<BusinessLogicUnit> getBusinessLogic(String repositoryUrl,
String branchName,
HttpServletRequest request) throws IOException, GitAPIException {
LOG.debug("Get business logic {} {}. User ip {}", repositoryUrl, branchName, HttpUtils.getUserIp(request));
return gitRepositoryService.getBusinessLogic(repositoryUrl, branchName); return gitRepositoryService.getBusinessLogic(repositoryUrl, branchName);
} }
@GetMapping("get-resources") @GetMapping("get-resources")
public List<ResourceUnit> getResource(String repositoryUrl, String branchName) throws IOException, GitAPIException { public List<ResourceUnit> getResource(String repositoryUrl,
String branchName,
HttpServletRequest request) throws IOException, GitAPIException {
LOG.debug("Get resources {} {}. User ip {}", repositoryUrl, branchName, HttpUtils.getUserIp(request));
return gitRepositoryService.getResource(repositoryUrl, branchName); return gitRepositoryService.getResource(repositoryUrl, branchName);
} }
} }

View File

@ -29,7 +29,7 @@ public class Commit extends BaseEntity {
@JoinColumn(name = "commit_id") @JoinColumn(name = "commit_id")
@Fetch(FetchMode.SUBSELECT) @Fetch(FetchMode.SUBSELECT)
private List<FileChange> fileChanges = new ArrayList<>(); private List<FileChange> fileChanges = new ArrayList<>();
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
private Branch branch; private Branch branch;
public Commit() { public Commit() {

View File

@ -38,4 +38,5 @@ public interface CommitRepository extends JpaRepository<Commit, Integer> {
@Query("SELECT new ru.ulstu.extractor.model.CommitTimeStatistic(cast(c.date as date), COUNT(DISTINCT c.hash)) FROM Commit c JOIN c.fileChanges f WHERE f.containsEntity = true GROUP by cast(c.date as date) ORDER by cast(c.date as date)") @Query("SELECT new ru.ulstu.extractor.model.CommitTimeStatistic(cast(c.date as date), COUNT(DISTINCT c.hash)) FROM Commit c JOIN c.fileChanges f WHERE f.containsEntity = true GROUP by cast(c.date as date) ORDER by cast(c.date as date)")
List<CommitTimeStatistic> getCommitTimeEntityStatistic(); List<CommitTimeStatistic> getCommitTimeEntityStatistic();
void deleteByBranchIsNull();
} }

View File

@ -5,25 +5,27 @@
package ru.ulstu.extractor.service; package ru.ulstu.extractor.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.extractor.model.Author; import ru.ulstu.extractor.model.Author;
import ru.ulstu.extractor.repository.AuthorRepository; import ru.ulstu.extractor.repository.AuthorRepository;
import javax.transaction.Transactional;
import java.util.Optional; import java.util.Optional;
@Service @Service
public class AuthorService { public class AuthorService {
private final static Logger LOG = LoggerFactory.getLogger(AuthorService.class);
private final AuthorRepository authorRepository; private final AuthorRepository authorRepository;
public AuthorService(AuthorRepository authorRepository) { public AuthorService(AuthorRepository authorRepository) {
this.authorRepository = authorRepository; this.authorRepository = authorRepository;
} }
@Transactional
public Author findOrCreate(Author author) { public Author findOrCreate(Author author) {
Optional<Author> newAuthor = authorRepository.findByName(author.getName()).stream().findAny(); Optional<Author> newAuthor = authorRepository.findByName(author.getName()).stream().findAny();
if (newAuthor.isEmpty()) { if (newAuthor.isEmpty()) {
LOG.debug("Author {} was saved.", author);
return authorRepository.save(author); return authorRepository.save(author);
} }
return newAuthor.get(); return newAuthor.get();

View File

@ -5,17 +5,21 @@
package ru.ulstu.extractor.service; package ru.ulstu.extractor.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
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.repository.BranchRepository;
import javax.transaction.Transactional;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Service @Service
public class BranchService { public class BranchService {
private final static Logger LOG = LoggerFactory.getLogger(BranchService.class);
private final BranchRepository branchRepository; private final BranchRepository branchRepository;
private final CommitService commitService; private final CommitService commitService;
@ -25,13 +29,15 @@ 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) {
List<Commit> commitsToRemove = branch.getCommits(); LOG.debug("Start save {} branch with {} commits ", branch.getName(), commits.size());
branch.getCommits().clear(); List<Integer> commitsToRemoveIds = branch.getCommits().stream().map(BaseEntity::getId).collect(Collectors.toList());
commitService.delete(commitsToRemove);
branch.setCommits(commitService.save(commits)); branch.setCommits(commitService.save(commits));
return branchRepository.save(branch); LOG.debug("Save branch {} ", branch.getName());
branch = branchRepository.save(branch);
LOG.debug("Clear {} commits ", commitsToRemoveIds.size());
commitService.deleteWithEmptyIds();
return branch;
} }
public Branch findByRepositoryAndName(Repository repository, String branchName) { public Branch findByRepositoryAndName(Repository repository, String branchName) {

View File

@ -14,7 +14,6 @@ import org.springframework.stereotype.Service;
import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.Commit;
import ru.ulstu.extractor.repository.CommitRepository; import ru.ulstu.extractor.repository.CommitRepository;
import javax.transaction.Transactional;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -53,7 +52,6 @@ public class CommitService {
commitRepository.deleteAll(commitsToRemove); commitRepository.deleteAll(commitsToRemove);
} }
@Transactional
public List<Commit> save(List<Commit> commits) { public List<Commit> save(List<Commit> commits) {
return commits.stream() return commits.stream()
.map(commit -> { .map(commit -> {
@ -61,6 +59,10 @@ public class CommitService {
return commitRepository.save(commit); return commitRepository.save(commit);
}).collect(Collectors.toList()); }).collect(Collectors.toList());
} }
public void deleteWithEmptyIds() {
commitRepository.deleteByBranchIsNull();
}
} }

View File

@ -7,12 +7,13 @@ package ru.ulstu.extractor.service;
import com.sun.istack.NotNull; import com.sun.istack.NotNull;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
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.CommitRepository;
import ru.ulstu.extractor.repository.RepositoryRepository; import ru.ulstu.extractor.repository.RepositoryRepository;
import java.io.IOException; import java.io.IOException;
@ -20,22 +21,17 @@ import java.util.List;
@Service @Service
public class IndexService { public class IndexService {
private final static Logger LOG = LoggerFactory.getLogger(IndexService.class);
private final GitRepositoryService gitRepositoryService; private final GitRepositoryService gitRepositoryService;
private final RepositoryRepository repositoryRepository; private final RepositoryRepository repositoryRepository;
private final BranchService branchService; private final BranchService branchService;
private final CommitRepository commitRepository;
private final AuthorService authorService;
public IndexService(GitRepositoryService gitRepositoryService, public IndexService(GitRepositoryService gitRepositoryService,
RepositoryRepository repositoryRepository, RepositoryRepository repositoryRepository,
BranchService branchService, BranchService branchService) {
CommitRepository commitRepository,
AuthorService authorService) {
this.gitRepositoryService = gitRepositoryService; this.gitRepositoryService = gitRepositoryService;
this.repositoryRepository = repositoryRepository; this.repositoryRepository = repositoryRepository;
this.branchService = branchService; this.branchService = branchService;
this.commitRepository = commitRepository;
this.authorService = authorService;
} }
@Transactional @Transactional
@ -49,6 +45,8 @@ public class IndexService {
branch = new Branch(repository, branchName); branch = new Branch(repository, branchName);
} }
List<Commit> commits = gitRepositoryService.getCommits(repositoryUrl, branchName); List<Commit> commits = gitRepositoryService.getCommits(repositoryUrl, branchName);
LOG.debug("{} commits loaded.", commits.size());
branchService.save(branch, commits); branchService.save(branch, commits);
LOG.debug("{} commits successfully saved. {} {}", commits.size(), repositoryUrl, branchName);
} }
} }

View File

@ -0,0 +1,18 @@
/*
* 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.util;
import javax.servlet.http.HttpServletRequest;
public class HttpUtils {
public static String getUserIp(HttpServletRequest request) {
String xfHeader = request.getHeader("X-Forwarded-For");
if (xfHeader == null) {
return request.getRemoteAddr();
}
return xfHeader.split(",")[0];
}
}