mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-21 14:30:59 +08:00
feat: add stat service
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.openisle.controller;
|
||||
|
||||
import com.openisle.service.UserVisitService;
|
||||
import com.openisle.service.StatService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -17,6 +18,7 @@ import java.util.Map;
|
||||
@RequiredArgsConstructor
|
||||
public class StatController {
|
||||
private final UserVisitService userVisitService;
|
||||
private final StatService statService;
|
||||
|
||||
@GetMapping("/dau")
|
||||
public Map<String, Long> dau(@RequestParam(value = "date", required = false)
|
||||
@@ -38,4 +40,46 @@ public class StatController {
|
||||
))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/new-users-range")
|
||||
public List<Map<String, Object>> newUsersRange(@RequestParam(value = "days", defaultValue = "30") int days) {
|
||||
if (days < 1) days = 1;
|
||||
LocalDate end = LocalDate.now();
|
||||
LocalDate start = end.minusDays(days - 1L);
|
||||
var data = statService.countNewUsersRange(start, end);
|
||||
return data.entrySet().stream()
|
||||
.map(e -> Map.<String,Object>of(
|
||||
"date", e.getKey().toString(),
|
||||
"value", e.getValue()
|
||||
))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/posts-range")
|
||||
public List<Map<String, Object>> postsRange(@RequestParam(value = "days", defaultValue = "30") int days) {
|
||||
if (days < 1) days = 1;
|
||||
LocalDate end = LocalDate.now();
|
||||
LocalDate start = end.minusDays(days - 1L);
|
||||
var data = statService.countPostsRange(start, end);
|
||||
return data.entrySet().stream()
|
||||
.map(e -> Map.<String,Object>of(
|
||||
"date", e.getKey().toString(),
|
||||
"value", e.getValue()
|
||||
))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/comments-range")
|
||||
public List<Map<String, Object>> commentsRange(@RequestParam(value = "days", defaultValue = "30") int days) {
|
||||
if (days < 1) days = 1;
|
||||
LocalDate end = LocalDate.now();
|
||||
LocalDate start = end.minusDays(days - 1L);
|
||||
var data = statService.countCommentsRange(start, end);
|
||||
return data.entrySet().stream()
|
||||
.map(e -> Map.<String,Object>of(
|
||||
"date", e.getKey().toString(),
|
||||
"value", e.getValue()
|
||||
))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,4 +32,8 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
|
||||
|
||||
long countByAuthor_Id(Long userId);
|
||||
|
||||
@org.springframework.data.jpa.repository.Query("SELECT FUNCTION('date', c.createdAt) AS d, COUNT(c) AS c FROM Comment c " +
|
||||
"WHERE c.createdAt >= :start AND c.createdAt < :end GROUP BY d ORDER BY d")
|
||||
java.util.List<Object[]> countDailyRange(@org.springframework.data.repository.query.Param("start") java.time.LocalDateTime start,
|
||||
@org.springframework.data.repository.query.Param("end") java.time.LocalDateTime end);
|
||||
}
|
||||
|
||||
@@ -95,4 +95,9 @@ public interface PostRepository extends JpaRepository<Post, Long> {
|
||||
long countDistinctByTags_Id(Long tagId);
|
||||
|
||||
long countByAuthor_Id(Long userId);
|
||||
|
||||
@Query("SELECT FUNCTION('date', p.createdAt) AS d, COUNT(p) AS c FROM Post p " +
|
||||
"WHERE p.createdAt >= :start AND p.createdAt < :end GROUP BY d ORDER BY d")
|
||||
java.util.List<Object[]> countDailyRange(@Param("start") LocalDateTime start,
|
||||
@Param("end") LocalDateTime end);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.openisle.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import com.openisle.model.User;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
@@ -12,4 +14,9 @@ public interface UserRepository extends JpaRepository<User, Long> {
|
||||
java.util.List<User> findByRole(com.openisle.model.Role role);
|
||||
long countByExperienceGreaterThanEqual(int experience);
|
||||
long countByCreatedAtBefore(LocalDateTime createdAt);
|
||||
|
||||
@Query("SELECT FUNCTION('date', u.createdAt) AS d, COUNT(u) AS c FROM User u " +
|
||||
"WHERE u.createdAt >= :start AND u.createdAt < :end GROUP BY d ORDER BY d")
|
||||
java.util.List<Object[]> countDailyRange(@Param("start") LocalDateTime start,
|
||||
@Param("end") LocalDateTime end);
|
||||
}
|
||||
|
||||
48
backend/src/main/java/com/openisle/service/StatService.java
Normal file
48
backend/src/main/java/com/openisle/service/StatService.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import com.openisle.repository.UserRepository;
|
||||
import com.openisle.repository.PostRepository;
|
||||
import com.openisle.repository.CommentRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class StatService {
|
||||
private final UserRepository userRepository;
|
||||
private final PostRepository postRepository;
|
||||
private final CommentRepository commentRepository;
|
||||
|
||||
private Map<LocalDate, Long> toDateMap(LocalDate start, LocalDate end, java.util.List<Object[]> list) {
|
||||
Map<LocalDate, Long> result = new LinkedHashMap<>();
|
||||
for (var obj : list) {
|
||||
LocalDate d = (LocalDate) obj[0];
|
||||
Long c = ((Number) obj[1]).longValue();
|
||||
result.put(d, c);
|
||||
}
|
||||
for (LocalDate d = start; !d.isAfter(end); d = d.plusDays(1)) {
|
||||
result.putIfAbsent(d, 0L);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Map<LocalDate, Long> countNewUsersRange(LocalDate start, LocalDate end) {
|
||||
java.util.List<Object[]> list = userRepository.countDailyRange(start.atStartOfDay(), end.plusDays(1).atStartOfDay());
|
||||
return toDateMap(start, end, list);
|
||||
}
|
||||
|
||||
public Map<LocalDate, Long> countPostsRange(LocalDate start, LocalDate end) {
|
||||
java.util.List<Object[]> list = postRepository.countDailyRange(start.atStartOfDay(), end.plusDays(1).atStartOfDay());
|
||||
return toDateMap(start, end, list);
|
||||
}
|
||||
|
||||
public Map<LocalDate, Long> countCommentsRange(LocalDate start, LocalDate end) {
|
||||
java.util.List<Object[]> list = commentRepository.countDailyRange(start.atStartOfDay(), end.plusDays(1).atStartOfDay());
|
||||
return toDateMap(start, end, list);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user