From 5d0e88400646924cb57cbe56b1a1dced970391c3 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 12 May 2021 13:53:16 +0400 Subject: [PATCH] #29 -- add statistic filer --- .../controller/StatisticController.java | 61 +++++++++---- .../ulstu/extractor/model/mvc/FilterForm.java | 9 ++ .../repository/AuthorRepository.java | 5 +- .../repository/CommitRepository.java | 16 ++-- .../extractor/service/BranchService.java | 4 + .../extractor/service/FilteringService.java | 4 + src/main/resources/templates/statistic.html | 87 +++++++++++++++++-- 7 files changed, 154 insertions(+), 32 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/controller/StatisticController.java b/src/main/java/ru/ulstu/extractor/controller/StatisticController.java index 852c6b4..e522c04 100644 --- a/src/main/java/ru/ulstu/extractor/controller/StatisticController.java +++ b/src/main/java/ru/ulstu/extractor/controller/StatisticController.java @@ -8,10 +8,15 @@ package ru.ulstu.extractor.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import ru.ulstu.extractor.model.mvc.FilterForm; import ru.ulstu.extractor.repository.CommitRepository; +import ru.ulstu.extractor.service.BranchService; +import ru.ulstu.extractor.service.FilteringService; import springfox.documentation.annotations.ApiIgnore; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import static ru.ulstu.extractor.controller.Route.STATISTIC; @@ -20,16 +25,26 @@ import static ru.ulstu.extractor.controller.Route.STATISTIC; @ApiIgnore public class StatisticController { private final CommitRepository commitRepository; + private final FilteringService filteringService; + private final BranchService branchService; - public StatisticController(CommitRepository commitRepository) { + public StatisticController(CommitRepository commitRepository, + FilteringService filteringService, + BranchService branchService) { this.commitRepository = commitRepository; + this.filteringService = filteringService; + this.branchService = branchService; } @GetMapping(STATISTIC) - public String indexBranch(Model model) { + public String getStatistic(Model model, + @RequestParam Optional branchId, + @RequestParam Optional author, + @RequestParam Optional filter, + @RequestParam Optional entity) { List urlCommits = getUrlCommits(); - List timeCommits = getTimeCommits(); - model.addAttribute("commitAuthorData", authorCommits()); + List timeCommits = getTimeCommits(branchId, author); + model.addAttribute("commitAuthorData", authorCommits(branchId, author)); model.addAttribute("commitUrlData", urlCommits); model.addAttribute("commitTimeData", timeCommits); List dates = timeCommits.stream() @@ -41,13 +56,25 @@ public class StatisticController { .map(tc -> tc[0].toString().substring(tc[0].toString().lastIndexOf("/") + 1)) .collect(Collectors.toList()); model.addAttribute("urls", urls); - model.addAttribute("commitEntityData", getEntityCommits()); - model.addAttribute("commitTimeEntityData", allTimeEntityCommits()); + model.addAttribute("commitEntityData", getEntityCommits(branchId, author)); + model.addAttribute("commitTimeEntityData", allTimeEntityCommits(branchId, author)); + FilterForm filterForm = new FilterForm(); + filterForm.setBranchId(branchId.orElse(null)); + filterForm.setAuthor(author.orElse(null)); + filterForm.setFilter(filter.orElse(null)); + filterForm.setEntity(entity.orElse(null)); + model.addAttribute("filterForm", filterForm); + model.addAttribute("entityPresent", filteringService.getEntityPresent()); + model.addAttribute("branches", branchService.findAll()); + model.addAttribute("authors", filteringService.getRepositoryAuthors(branchId.orElse(null))); return STATISTIC; } - public List authorCommits() { - return commitRepository.getCommitAuthorStatistic().stream() + public List authorCommits(Optional branchId, Optional author) { + return commitRepository.getCommitAuthorStatistic( + branchId.orElse(null), + author.orElse(null)) + .stream() .map(stat -> new Object[]{stat.getAuthor(), stat.getCountCommit()}) .collect(Collectors.toList()); } @@ -58,14 +85,14 @@ public class StatisticController { .collect(Collectors.toList()); } - public List getTimeCommits() { - return commitRepository.getCommitTimeStatistic().stream() + public List getTimeCommits(Optional branchId, Optional author) { + return commitRepository.getCommitTimeStatistic(branchId.orElse(null), author.orElse(null)).stream() .map(stat -> new Object[]{stat.getDate(), stat.getCountCommit()}) .collect(Collectors.toList()); } - public List getEntityCommits() { - List entityCommits = commitRepository.getCommitEntityStatistic().stream() + public List getEntityCommits(Optional branchId, Optional author) { + List entityCommits = commitRepository.getCommitEntityStatistic(branchId.orElse(null), author.orElse(null)).stream() .map(stat -> new Object[]{stat.getEntity(), stat.getCountCommit()}) .collect(Collectors.toList()); for (Object[] entityCommit : entityCommits) { @@ -78,15 +105,15 @@ public class StatisticController { return entityCommits; } - public List getTimeEntityCommits() { - return commitRepository.getCommitTimeEntityStatistic().stream() + public List getTimeEntityCommits(Optional branchId, Optional author) { + return commitRepository.getCommitTimeEntityStatistic(branchId.orElse(null), author.orElse(null)).stream() .map(stat -> new Object[]{stat.getDate(), stat.getCountCommit()}) .collect(Collectors.toList()); } - public List allTimeEntityCommits() { - List timeEntityCommits = getTimeEntityCommits(); - List allTimeEntityCommits = commitRepository.getCommitTimeStatistic().stream() + public List allTimeEntityCommits(Optional branchId, Optional author) { + List timeEntityCommits = getTimeEntityCommits(branchId, author); + List allTimeEntityCommits = commitRepository.getCommitTimeStatistic(branchId.orElse(null), author.orElse(null)).stream() .map(stat -> new Object[]{stat.getDate(), stat.getCountCommit()}) .collect(Collectors.toList()); int j = 0; diff --git a/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java b/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java index c4e2aba..1db7616 100644 --- a/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java +++ b/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java @@ -12,6 +12,7 @@ public class FilterForm { private String filter; private String repositoryUrl; private String branchName; + private Integer branchId; private String author; private Boolean entity; private Page commitsPage; @@ -71,6 +72,14 @@ public class FilterForm { this.entity = entity; } + public Integer getBranchId() { + return branchId; + } + + public void setBranchId(Integer branchId) { + this.branchId = branchId; + } + @Override public String toString() { return "FilterForm{" + diff --git a/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java b/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java index c2739d0..26b5f45 100644 --- a/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java +++ b/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java @@ -14,8 +14,11 @@ import ru.ulstu.extractor.model.Repository; import java.util.List; public interface AuthorRepository extends JpaRepository { - @Query("SELECT DISTINCT a.name FROM Commit c, Repository r, Branch b, Author a WHERE c.author = a AND c.branch = b AND r = b.repository AND r = :repository AND b.name = :branchName AND a.name IS NOT NULL AND a.name <> '' ORDER BY a.name") + @Query("SELECT DISTINCT a.name FROM Commit c, Repository r, Branch b, Author a WHERE c.author = a AND c.branch = b AND r = b.repository AND (:repository IS NULL OR r = :repository) AND (:branchName IS NULL OR :branchName = '' OR b.name = :branchName) AND a.name IS NOT NULL AND a.name <> '' ORDER BY a.name") List findByRepositoryAndBranch(@Param("repository") Repository repository, @Param("branchName") String branchName); List findByName(String name); + + @Query("SELECT DISTINCT a.name FROM Commit c, Branch b, Author a WHERE c.author = a AND c.branch = b AND (:branchId IS NULL OR b.id = :branchId) AND a.name IS NOT NULL AND a.name <> '' ORDER BY a.name") + List findByBranchId(Integer branchId); } diff --git a/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java b/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java index 5853524..a1845b4 100644 --- a/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java +++ b/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java @@ -23,20 +23,20 @@ public interface CommitRepository extends JpaRepository { @Query("SELECT DISTINCT c FROM Commit c LEFT JOIN c.branch b LEFT JOIN c.fileChanges f LEFT JOIN c.author a LEFT JOIN b.repository r WHERE r = :repository AND b.name = :branchName AND (:author IS NULL OR :author = '' OR a.name = :author) AND (:filter IS NULL OR :filter = '' OR lower(c.message) LIKE lower(concat('%', :filter,'%'))) AND (:entity IS NULL OR f.containsEntity = :entity)") Page findByRepositoryAndBranch(Pageable pageable, @Param("repository") Repository repository, @Param("branchName") String branchName, @Param("author") String author, @Param("filter") String filter, @Param("entity") Boolean entity); - @Query("SELECT new ru.ulstu.extractor.model.CommitAuthorStatistic(c.author.name, COUNT(DISTINCT c.hash)) FROM Commit c GROUP by c.author.name") - List getCommitAuthorStatistic(); + @Query("SELECT new ru.ulstu.extractor.model.CommitAuthorStatistic(c.author.name, COUNT(DISTINCT c.hash)) FROM Commit c LEFT JOIN c.branch LEFT JOIN c.author a WHERE (:branchId IS NULL OR c.branch.id = :branchId) AND (:author IS NULL OR :author = '' OR a.name = :author) GROUP by c.author.name") + List getCommitAuthorStatistic(@Param("branchId") Integer branchId, @Param("author") String author); @Query("SELECT new ru.ulstu.extractor.model.CommitUrlStatistic(c.branch.repository.url, COUNT(DISTINCT c.hash)) FROM Commit c GROUP by c.branch.repository.url") List getCommitUrlStatistic(); - @Query("SELECT new ru.ulstu.extractor.model.CommitTimeStatistic(extract(month FROM c.date) as month, extract(year FROM c.date) as year, COUNT(DISTINCT c.hash)) FROM Commit c GROUP by extract(month from c.date), extract(year from c.date) ORDER by extract(year from c.date), extract(month from c.date)") - List getCommitTimeStatistic(); + @Query("SELECT new ru.ulstu.extractor.model.CommitTimeStatistic(extract(month FROM c.date) as month, extract(year FROM c.date) as year, COUNT(DISTINCT c.hash)) FROM Commit c LEFT JOIN c.branch LEFT JOIN c.author a WHERE (:branchId IS NULL OR c.branch.id = :branchId) AND (:author IS NULL OR :author = '' OR a.name = :author) GROUP by extract(month from c.date), extract(year from c.date) ORDER by extract(year from c.date), extract(month from c.date)") + List getCommitTimeStatistic(@Param("branchId") Integer branchId, @Param("author") String author); - @Query("SELECT new ru.ulstu.extractor.model.CommitEntityStatistic(f.containsEntity, COUNT(DISTINCT c.hash)) FROM Commit c JOIN c.fileChanges f GROUP by f.containsEntity") - List getCommitEntityStatistic(); + @Query("SELECT new ru.ulstu.extractor.model.CommitEntityStatistic(f.containsEntity, COUNT(DISTINCT c.hash)) FROM Commit c LEFT JOIN c.branch LEFT JOIN c.author a LEFT JOIN c.fileChanges f WHERE (:branchId IS NULL OR c.branch.id = :branchId) AND (:author IS NULL OR :author = '' OR a.name = :author) GROUP by f.containsEntity") + List getCommitEntityStatistic(@Param("branchId") Integer branchId, @Param("author") String author); - @Query("SELECT new ru.ulstu.extractor.model.CommitTimeStatistic(EXTRACT(MONTH FROM c.date), EXTRACT(YEAR FROM c.date), COUNT(DISTINCT c.hash)) FROM Commit c JOIN c.fileChanges f WHERE f.containsEntity = true GROUP by extract(month from c.date), extract(year from c.date) ORDER by extract(year from c.date), extract(month from c.date)") - List getCommitTimeEntityStatistic(); + @Query("SELECT new ru.ulstu.extractor.model.CommitTimeStatistic(EXTRACT(MONTH FROM c.date), EXTRACT(YEAR FROM c.date), COUNT(DISTINCT c.hash)) FROM Commit c LEFT JOIN c.branch LEFT JOIN c.author a LEFT JOIN c.fileChanges f WHERE f.containsEntity = true AND (:branchId IS NULL OR c.branch.id = :branchId) AND (:author IS NULL OR :author = '' OR a.name = :author) GROUP by extract(month from c.date), extract(year from c.date) ORDER by extract(year from c.date), extract(month from c.date)") + List getCommitTimeEntityStatistic(@Param("branchId") Integer branchId, @Param("author") String author); void deleteByBranchIsNull(); } diff --git a/src/main/java/ru/ulstu/extractor/service/BranchService.java b/src/main/java/ru/ulstu/extractor/service/BranchService.java index 596c6e0..8ce9353 100644 --- a/src/main/java/ru/ulstu/extractor/service/BranchService.java +++ b/src/main/java/ru/ulstu/extractor/service/BranchService.java @@ -51,4 +51,8 @@ public class BranchService { public Branch findByRepositoryAndName(Repository repository, String branchName) { return branchRepository.findByRepositoryAndName(repository, branchName); } + + public List findAll() { + return branchRepository.findAll(); + } } diff --git a/src/main/java/ru/ulstu/extractor/service/FilteringService.java b/src/main/java/ru/ulstu/extractor/service/FilteringService.java index 4d0dc8c..221f133 100644 --- a/src/main/java/ru/ulstu/extractor/service/FilteringService.java +++ b/src/main/java/ru/ulstu/extractor/service/FilteringService.java @@ -39,6 +39,10 @@ public class FilteringService { ); } + public List getRepositoryAuthors(Integer branchId) { + return authorRepository.findByBranchId(branchId); + } + public Page getCommits(@NotNull String repositoryUrl, @NotNull String branchName, String author, diff --git a/src/main/resources/templates/statistic.html b/src/main/resources/templates/statistic.html index 7e15db4..e89c1a8 100644 --- a/src/main/resources/templates/statistic.html +++ b/src/main/resources/templates/statistic.html @@ -78,6 +78,7 @@ json.plotOptions = plotOptions; $('#container').highcharts(json); }); + -
-
-
-
-
-
+
+
+
+ Репозиторий-ветка +
+
+ + +
+
+ Автор +
+
+ + +
+
+
+
+ Искать по тексту: +
+
+ +
+
+
+ + +
+
+
+ +
+
+ + + +
+
+
+
+
+
+