add loggers
This commit is contained in:
parent
4629562e0f
commit
97cefbfbdf
@ -6,6 +6,8 @@
|
||||
package ru.ulstu.extractor.controller;
|
||||
|
||||
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.RequestMapping;
|
||||
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.service.GitRepositoryService;
|
||||
import ru.ulstu.extractor.service.IndexService;
|
||||
import ru.ulstu.extractor.util.HttpUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@ -22,6 +26,7 @@ import static ru.ulstu.extractor.controller.RepoController.URL;
|
||||
@RestController
|
||||
@RequestMapping(URL)
|
||||
public class RepoController {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(RepoController.class);
|
||||
public static final String URL = "/";
|
||||
private final GitRepositoryService gitRepositoryService;
|
||||
private final IndexService indexService;
|
||||
@ -34,13 +39,17 @@ public class RepoController {
|
||||
|
||||
@GetMapping("commits")
|
||||
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);
|
||||
}
|
||||
|
||||
@GetMapping("index")
|
||||
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);
|
||||
return true;
|
||||
}
|
||||
|
@ -6,6 +6,8 @@
|
||||
package ru.ulstu.extractor.heuristic.controller;
|
||||
|
||||
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.RequestMapping;
|
||||
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.ResourceUnit;
|
||||
import ru.ulstu.extractor.service.GitRepositoryService;
|
||||
import ru.ulstu.extractor.util.HttpUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("StructuralUnitController")
|
||||
public class StructuralUnitController {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(StructuralUnitController.class);
|
||||
private final GitRepositoryService gitRepositoryService;
|
||||
|
||||
public StructuralUnitController(GitRepositoryService gitRepositoryService) {
|
||||
@ -27,17 +32,26 @@ public class StructuralUnitController {
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ public class Commit extends BaseEntity {
|
||||
@JoinColumn(name = "commit_id")
|
||||
@Fetch(FetchMode.SUBSELECT)
|
||||
private List<FileChange> fileChanges = new ArrayList<>();
|
||||
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
private Branch branch;
|
||||
|
||||
public Commit() {
|
||||
|
@ -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)")
|
||||
List<CommitTimeStatistic> getCommitTimeEntityStatistic();
|
||||
|
||||
void deleteByBranchIsNull();
|
||||
}
|
||||
|
@ -5,25 +5,27 @@
|
||||
|
||||
package ru.ulstu.extractor.service;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.extractor.model.Author;
|
||||
import ru.ulstu.extractor.repository.AuthorRepository;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class AuthorService {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(AuthorService.class);
|
||||
private final AuthorRepository authorRepository;
|
||||
|
||||
public AuthorService(AuthorRepository authorRepository) {
|
||||
this.authorRepository = authorRepository;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Author findOrCreate(Author author) {
|
||||
Optional<Author> newAuthor = authorRepository.findByName(author.getName()).stream().findAny();
|
||||
if (newAuthor.isEmpty()) {
|
||||
LOG.debug("Author {} was saved.", author);
|
||||
return authorRepository.save(author);
|
||||
}
|
||||
return newAuthor.get();
|
||||
|
@ -5,17 +5,21 @@
|
||||
|
||||
package ru.ulstu.extractor.service;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.extractor.model.BaseEntity;
|
||||
import ru.ulstu.extractor.model.Branch;
|
||||
import ru.ulstu.extractor.model.Commit;
|
||||
import ru.ulstu.extractor.model.Repository;
|
||||
import ru.ulstu.extractor.repository.BranchRepository;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class BranchService {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(BranchService.class);
|
||||
private final BranchRepository branchRepository;
|
||||
private final CommitService commitService;
|
||||
|
||||
@ -25,13 +29,15 @@ public class BranchService {
|
||||
this.commitService = commitService;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Branch save(Branch branch, List<Commit> commits) {
|
||||
List<Commit> commitsToRemove = branch.getCommits();
|
||||
branch.getCommits().clear();
|
||||
commitService.delete(commitsToRemove);
|
||||
LOG.debug("Start save {} branch with {} commits ", branch.getName(), commits.size());
|
||||
List<Integer> commitsToRemoveIds = branch.getCommits().stream().map(BaseEntity::getId).collect(Collectors.toList());
|
||||
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) {
|
||||
|
@ -14,7 +14,6 @@ import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.extractor.model.Commit;
|
||||
import ru.ulstu.extractor.repository.CommitRepository;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -53,7 +52,6 @@ public class CommitService {
|
||||
commitRepository.deleteAll(commitsToRemove);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Commit> save(List<Commit> commits) {
|
||||
return commits.stream()
|
||||
.map(commit -> {
|
||||
@ -61,6 +59,10 @@ public class CommitService {
|
||||
return commitRepository.save(commit);
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void deleteWithEmptyIds() {
|
||||
commitRepository.deleteByBranchIsNull();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -7,12 +7,13 @@ package ru.ulstu.extractor.service;
|
||||
|
||||
import com.sun.istack.NotNull;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.ulstu.extractor.model.Branch;
|
||||
import ru.ulstu.extractor.model.Commit;
|
||||
import ru.ulstu.extractor.model.Repository;
|
||||
import ru.ulstu.extractor.repository.CommitRepository;
|
||||
import ru.ulstu.extractor.repository.RepositoryRepository;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -20,22 +21,17 @@ import java.util.List;
|
||||
|
||||
@Service
|
||||
public class IndexService {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(IndexService.class);
|
||||
private final GitRepositoryService gitRepositoryService;
|
||||
private final RepositoryRepository repositoryRepository;
|
||||
private final BranchService branchService;
|
||||
private final CommitRepository commitRepository;
|
||||
private final AuthorService authorService;
|
||||
|
||||
public IndexService(GitRepositoryService gitRepositoryService,
|
||||
RepositoryRepository repositoryRepository,
|
||||
BranchService branchService,
|
||||
CommitRepository commitRepository,
|
||||
AuthorService authorService) {
|
||||
BranchService branchService) {
|
||||
this.gitRepositoryService = gitRepositoryService;
|
||||
this.repositoryRepository = repositoryRepository;
|
||||
this.branchService = branchService;
|
||||
this.commitRepository = commitRepository;
|
||||
this.authorService = authorService;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@ -49,6 +45,8 @@ public class IndexService {
|
||||
branch = new Branch(repository, branchName);
|
||||
}
|
||||
List<Commit> commits = gitRepositoryService.getCommits(repositoryUrl, branchName);
|
||||
LOG.debug("{} commits loaded.", commits.size());
|
||||
branchService.save(branch, commits);
|
||||
LOG.debug("{} commits successfully saved. {} {}", commits.size(), repositoryUrl, branchName);
|
||||
}
|
||||
}
|
||||
|
18
src/main/java/ru/ulstu/extractor/util/HttpUtils.java
Normal file
18
src/main/java/ru/ulstu/extractor/util/HttpUtils.java
Normal 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];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user