diff --git a/src/main/java/it/numble/hittracker/controller/HitTrackerController.java b/src/main/java/it/numble/hittracker/controller/HitTrackerController.java index 5b3a093..10de6f4 100644 --- a/src/main/java/it/numble/hittracker/controller/HitTrackerController.java +++ b/src/main/java/it/numble/hittracker/controller/HitTrackerController.java @@ -30,12 +30,6 @@ public Response hit(@RequestParam String url) { return Response.ok(); } - @PostMapping("/tomorrow") - public Response tomorrow() { - hitTrackerService.tomorrow(); - return Response.ok(); - } - @GetMapping("/statistics") public Response> statistics(@RequestParam String url) { return Response.okWithDetail(hitTrackerService.getStatistics(url)); diff --git a/src/main/java/it/numble/hittracker/scheduler/DailyHitLogScheduler.java b/src/main/java/it/numble/hittracker/scheduler/DailyHitLogScheduler.java new file mode 100644 index 0000000..9a5a950 --- /dev/null +++ b/src/main/java/it/numble/hittracker/scheduler/DailyHitLogScheduler.java @@ -0,0 +1,20 @@ +package it.numble.hittracker.scheduler; + +import it.numble.hittracker.service.HitTrackerService; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +public class DailyHitLogScheduler { + + private final HitTrackerService hitTrackerService; + + public DailyHitLogScheduler(HitTrackerService hitTrackerService) { + this.hitTrackerService = hitTrackerService; + } + + @Scheduled(cron = "0 0 0 * * ?") + public void logDailyHit() { + hitTrackerService.tomorrow(); + } +} diff --git a/src/main/java/it/numble/hittracker/service/HitTrackerService.java b/src/main/java/it/numble/hittracker/service/HitTrackerService.java index 446e220..7b24c23 100644 --- a/src/main/java/it/numble/hittracker/service/HitTrackerService.java +++ b/src/main/java/it/numble/hittracker/service/HitTrackerService.java @@ -38,9 +38,13 @@ public void hit(String url) { hitRepository.hit(url); } + public List getStatistics(String url) { + return urlRepository.findByUrl(url).orElseThrow().getDailyHitLogs(); + } + public void tomorrow() { List logs = dailyHitLogRepository.findAll(); - LocalDate today = logs.stream().map(DailyHitLog::getDate).max(Comparator.naturalOrder()).orElse(LocalDate.now()).plusDays(1); + LocalDate today = LocalDate.now().minusDays(1); // 일간조회수 모두 0으로 초기화 및 저장 List urls = urlRepository.findAll(); @@ -57,10 +61,6 @@ public void tomorrow() { logs.stream().filter(log -> log.isSevenDaysAgo(today)).forEach(dailyHitLogRepository::delete); } - public List getStatistics(String url) { - return urlRepository.findByUrl(url).orElseThrow().getDailyHitLogs(); - } - private void track(String url) { Url urlBeingTracked = new Url(url); urlRepository.save(urlBeingTracked);