diff --git a/src/main/java/app/controllers/StatsController.java b/src/main/java/app/controllers/StatsController.java index e20f8c3..0315272 100644 --- a/src/main/java/app/controllers/StatsController.java +++ b/src/main/java/app/controllers/StatsController.java @@ -14,6 +14,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.time.Instant; +import java.util.HashMap; import java.util.List; @RestController @@ -22,6 +24,7 @@ public class StatsController { private Stats stats; private ExternalServices externalServices; private OnlineUpdater onlineUpdater; + private HashMap, Long>> cache = new HashMap<>(); @Autowired public StatsController(Stats stats, ExternalServices externalServices, OnlineUpdater onlineUpdater){ @@ -47,8 +50,15 @@ public class StatsController { return new ResponseEntity(externalServices.getServices(), HttpStatus.OK); } - @GetMapping("/graph") - public ResponseEntity GetPeakOfDays() { - return new ResponseEntity(onlineUpdater.getPeakOfDays(7), HttpStatus.OK); + @GetMapping("/graph/peak/of/days") + public ResponseEntity> GetPeakOfDays(@RequestParam(required = false, defaultValue = "7") Integer limit) { + if (!cache.containsKey(limit)) { + cache.put(limit, new HashMap<>(){{put(onlineUpdater.getPeakOfDays(limit), Instant.now().getEpochSecond());}}); + } + if (Instant.now().getEpochSecond() - cache.get(limit).values().stream().findFirst().orElse(0L) < 60L) { + cache.put(limit, new HashMap<>(){{put(onlineUpdater.getPeakOfDays(limit), Instant.now().getEpochSecond());}}); + } + + return new ResponseEntity<>(cache.get(limit).keySet().stream().findFirst().orElse(null), HttpStatus.OK); } }