diff --git a/data/demo.mv.db b/data/demo.mv.db index 7454a2b717bca086ba694b4c3425baa4d43f456b..4776c8ec4a71454d5ff7b0224e7905e703e36cf4 100644 Binary files a/data/demo.mv.db and b/data/demo.mv.db differ diff --git a/src/main/java/com/dre0059/articleprocessor/controller/StatisticsController.java b/src/main/java/com/dre0059/articleprocessor/controller/StatisticsController.java index 6bf4eae4831f42945f9ec28b00eed431117d703e..8a8348b0787987466eedf61554ed28b26eabfd2a 100644 --- a/src/main/java/com/dre0059/articleprocessor/controller/StatisticsController.java +++ b/src/main/java/com/dre0059/articleprocessor/controller/StatisticsController.java @@ -2,9 +2,11 @@ package com.dre0059.articleprocessor.controller; import com.dre0059.articleprocessor.model.Dokument; import com.dre0059.articleprocessor.repository.DocumentRepository; +import com.dre0059.articleprocessor.repository.ReferenceRepository; 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 java.util.*; import java.util.stream.Collectors; @@ -13,29 +15,62 @@ import java.util.stream.Collectors; public class StatisticsController { private final DocumentRepository documentRepository; + private final ReferenceRepository referenceRepository; - public StatisticsController(DocumentRepository documentRepository) { + public StatisticsController(DocumentRepository documentRepository, + ReferenceRepository referenceRepository) { this.documentRepository = documentRepository; + this.referenceRepository = referenceRepository; } @GetMapping("/statistics") - public String statistics(Model model) { + public String statistics( + @RequestParam(value = "category", required = false, defaultValue = "") String category, + Model model + ) { + // 1) Všetky dokumenty List<Dokument> documents = documentRepository.findAll(); - // Status count + // 2) Status count Map<String, Long> statusCount = documents.stream() .collect(Collectors.groupingBy(Dokument::getStatus, Collectors.counting())); - // odfiltruj PDF ktorĂ© nemajĂş kategoriu + // 3) Category count (len tie, ktorĂ© majĂş kategĂłriu) Map<String, Long> categoryCount = documents.stream() - .filter(doc -> doc.getCategory() != null) // đź’ˇ Tu je fix + .filter(d -> d.getCategory() != null) .collect(Collectors.groupingBy( - doc -> doc.getCategory().getName(), + d -> d.getCategory().getName(), Collectors.counting() )); + // 4) Zoznam všetkĂ˝ch kategĂłriĂ (poradie podÄľa vstupu do mapy) + List<String> categories = new ArrayList<>(categoryCount.keySet()); + + // 5) Vyberieme "selectedCategory": + // - ak prišlo z parametra, pouĹľijeme ho + // - inak vezmeme prvĂş kategĂłriu zo zoznamu (ak existuje) + String selectedCategory = category.trim().isEmpty() + ? (categories.isEmpty() ? "" : categories.get(0)) + : category.trim(); + + // 6) SpoÄŤĂtame referencie podÄľa rokov pre vybranĂş kategĂłriu + Map<Integer, Long> referenceCounts = new LinkedHashMap<>(); + if (!selectedCategory.isEmpty()) { + List<Object[]> raw = referenceRepository + .countReferencesByYearForCategory(selectedCategory); + // raw: [ [year, count], [year, count], ... ] uĹľ zoradenĂ© podÄľa roku v JPQL + raw.forEach(record -> { + Integer year = (Integer) record[0]; + Long count = (Long) record[1]; + referenceCounts.put(year, count); + }); + } + + // 7) Pridáme všetko do modelu model.addAttribute("statusCount", statusCount); model.addAttribute("categoryCount", categoryCount); + model.addAttribute("referenceCounts", referenceCounts); + model.addAttribute("selectedCategory", selectedCategory); return "statistics"; } diff --git a/src/main/java/com/dre0059/articleprocessor/repository/DocumentRepository.java b/src/main/java/com/dre0059/articleprocessor/repository/DocumentRepository.java index fb94ac6e3f7840c731f70154964d91e91d0e67fd..7d5c73ec61c3067a6381bb92fce226bb6ddff92c 100644 --- a/src/main/java/com/dre0059/articleprocessor/repository/DocumentRepository.java +++ b/src/main/java/com/dre0059/articleprocessor/repository/DocumentRepository.java @@ -46,20 +46,5 @@ public interface DocumentRepository extends JpaRepository<Dokument, Long> { ) List<Dokument> getReferencedDocumentsById(@Param("id") Long fromDocumentId); -} - -/* - // save only if all authors are the same - @Query(""" - SELECT COUNT(d) > 0 - FROM Dokument d - WHERE d.title = :title - AND SIZE(d.authors) = :authorCount - AND EXISTS ( - SELECT 1 FROM Dokument d2 JOIN d2.authors a2 - WHERE d2.id = d.id AND a2 IN :authors - ) - """) - boolean existsByTitleAndAuthors(@Param("title") String title, @Param("authors") List<Author> authors, @Param("authorCount") int authorCount); - */ \ No newline at end of file +} diff --git a/src/main/java/com/dre0059/articleprocessor/repository/ReferenceRepository.java b/src/main/java/com/dre0059/articleprocessor/repository/ReferenceRepository.java index f5cee29d8bc26ef2697f817d39329bbab1d2a7c7..2551ed8c6704c7be0f2636ab689d64d2f8089741 100644 --- a/src/main/java/com/dre0059/articleprocessor/repository/ReferenceRepository.java +++ b/src/main/java/com/dre0059/articleprocessor/repository/ReferenceRepository.java @@ -2,8 +2,21 @@ package com.dre0059.articleprocessor.repository; import com.dre0059.articleprocessor.model.Reference; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import java.util.List; public interface ReferenceRepository extends JpaRepository<Reference, Long> { + + @Query(""" + SELECT r.toDocument.publicationYear, COUNT(r) + FROM Reference r + WHERE r.fromDocument.category.name = :category + GROUP BY r.toDocument.publicationYear + ORDER BY r.toDocument.publicationYear + """) + List<Object[]> countReferencesByYearForCategory(@Param("category") String category); + + } diff --git a/src/main/resources/templates/statistics.html b/src/main/resources/templates/statistics.html index 18fa6da95e0cb7d99f40d7c7364d3fc6102e4f1b..bdae8971d82f962f715406e27b490b48643c37d7 100644 --- a/src/main/resources/templates/statistics.html +++ b/src/main/resources/templates/statistics.html @@ -3,7 +3,7 @@ <head> <meta charset="UTF-8"> <title>Document Statistics</title> - <script src="https://cdn.plot.ly/plotly-2.24.1.min.js"></script> + <script src="https://cdn.plot.ly/plotly-latest.min.js"></script> <link rel="stylesheet" th:href="@{/webjars/bootstrap/5.1.3/css/bootstrap.min.css}"> <link rel="icon" type="image/x-icon" href="assets/favicon.ico" />