Merge pull request #207 from nagisa77/codex/add-site-statistics-page-for-dau-chart

Add admin stats page with DAU chart
This commit is contained in:
Tim
2025-07-14 21:39:39 +08:00
committed by GitHub
9 changed files with 204 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
@RestController
@@ -23,4 +24,15 @@ public class StatController {
long count = userVisitService.countDau(date);
return Map.of("dau", count);
}
@GetMapping("/dau-range")
public List<Map<String, Object>> dauRange(@RequestParam(value = "days", defaultValue = "30") int days) {
if (days < 1) days = 1;
LocalDate end = LocalDate.now();
LocalDate start = end.minusDays(days - 1L);
var data = userVisitService.countDauRange(start, end);
return data.entrySet().stream()
.map(e -> Map.of("date", e.getKey().toString(), "value", e.getValue()))
.toList();
}
}

View File

@@ -3,6 +3,8 @@ package com.openisle.repository;
import com.openisle.model.User;
import com.openisle.model.UserVisit;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.time.LocalDate;
import java.util.Optional;
@@ -11,4 +13,7 @@ public interface UserVisitRepository extends JpaRepository<UserVisit, Long> {
Optional<UserVisit> findByUserAndVisitDate(User user, LocalDate visitDate);
long countByUser(User user);
long countByVisitDate(LocalDate visitDate);
@Query("SELECT uv.visitDate AS d, COUNT(uv) AS c FROM UserVisit uv WHERE uv.visitDate BETWEEN :start AND :end GROUP BY uv.visitDate ORDER BY uv.visitDate")
java.util.List<Object[]> countRange(@Param("start") LocalDate start, @Param("end") LocalDate end);
}

View File

@@ -8,6 +8,8 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.LinkedHashMap;
import java.util.Map;
@Service
@RequiredArgsConstructor
@@ -37,4 +39,22 @@ public class UserVisitService {
LocalDate d = date != null ? date : LocalDate.now();
return userVisitRepository.countByVisitDate(d);
}
public Map<LocalDate, Long> countDauRange(LocalDate start, LocalDate end) {
Map<LocalDate, Long> result = new LinkedHashMap<>();
if (start == null || end == null || start.isAfter(end)) {
return result;
}
var list = userVisitRepository.countRange(start, end);
for (var obj : list) {
LocalDate d = (LocalDate) obj[0];
Long c = (Long) obj[1];
result.put(d, c);
}
// fill zero counts for missing dates
for (LocalDate d = start; !d.isAfter(end); d = d.plusDays(1)) {
result.putIfAbsent(d, 0L);
}
return result;
}
}