diff --git a/src/main/java/app/controllers/StatsController.java b/src/main/java/app/controllers/StatsController.java index fb4b4fc..6fd7cd3 100644 --- a/src/main/java/app/controllers/StatsController.java +++ b/src/main/java/app/controllers/StatsController.java @@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.RestController; import java.time.Instant; import java.util.HashMap; import java.util.List; +import java.util.Map; @RestController @RequestMapping("api/stats") @@ -25,7 +26,7 @@ public class StatsController { private Stats stats; private ExternalServices externalServices; private OnlineUpdater onlineUpdater; - private HashMap, Long>> cache = new HashMap<>(); + private HashMap, HashMap, Long>> cache = new HashMap<>(); private ServersReader serversReader; @Autowired @@ -54,17 +55,19 @@ public class StatsController { } @GetMapping("/graph/peak/of/days") - public ResponseEntity> GetPeakOfDays(@RequestParam(required = false, defaultValue = "7") Integer limit) { + public ResponseEntity> GetPeakOfDays( + @RequestParam(required = false, defaultValue = "7") Integer limit, + @RequestParam(required = false, defaultValue = "%") String server_id) { if (limit > 14) return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE); - if (!cache.containsKey(limit)) { - cache.put(limit, new HashMap<>(){{put(onlineUpdater.getPeakOfDays(limit), Instant.now().getEpochSecond());}}); + if (!cache.containsKey(Map.of(limit, server_id))) { + cache.put(Map.of(limit, server_id), new HashMap<>(){{put(onlineUpdater.getPeakOfDays(server_id, 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());}}); + if (Instant.now().getEpochSecond() - cache.get(Map.of(limit, server_id)).values().stream().findFirst().orElse(0L) < 60L) { + cache.put(Map.of(limit, server_id), new HashMap<>(){{put(onlineUpdater.getPeakOfDays(server_id, limit), Instant.now().getEpochSecond());}}); } - return new ResponseEntity<>(cache.get(limit).keySet().stream().findFirst().orElse(null), HttpStatus.OK); + return new ResponseEntity<>(cache.get(Map.of(limit, server_id)).keySet().stream().findFirst().orElse(null), HttpStatus.OK); } @GetMapping("/reload") diff --git a/src/main/java/app/updates/OnlineUpdater.java b/src/main/java/app/updates/OnlineUpdater.java index b94f55c..7c1d78c 100644 --- a/src/main/java/app/updates/OnlineUpdater.java +++ b/src/main/java/app/updates/OnlineUpdater.java @@ -80,7 +80,7 @@ public class OnlineUpdater extends BaseUpdater { } private boolean updateCurrentDayPeak() { - this.current_day_max_player_count = getMaxPeakOfDays(1).get(0).getPlayer_count(); + this.current_day_max_player_count = getMaxPeakOfDays(null,1).get(0).getPlayer_count(); return true; } @@ -88,21 +88,21 @@ public class OnlineUpdater extends BaseUpdater { return current_day_max_player_count; } - public List getMaxPeakOfDays(int limit) { - return jdbcTemplate_ro.query("SELECT SUM(s.player_count) as player_count, s.fulldate as fl FROM (SELECT max(`player_count`) as player_count, cast(FROM_UNIXTIME(`utime`) as date) as fulldate,`server_id` FROM `servers_online` WHERE `player_count` != 0 and `server_id` like '%' GROUP BY cast(FROM_UNIXTIME(`utime`) as date), server_id ORDER BY `fulldate` DESC) s GROUP BY s.fulldate ORDER BY `s`.`fulldate` DESC LIMIT ?", - new Object[]{limit}, + public List getMaxPeakOfDays(String server_id, int limit) { + return jdbcTemplate_ro.query("SELECT SUM(s.player_count) as player_count, s.fulldate as fl FROM (SELECT max(`player_count`) as player_count, cast(FROM_UNIXTIME(`utime`) as date) as fulldate,`server_id` FROM `servers_online` WHERE `player_count` != 0 and `server_id` like ? GROUP BY cast(FROM_UNIXTIME(`utime`) as date), server_id ORDER BY `fulldate` DESC) s GROUP BY s.fulldate ORDER BY `s`.`fulldate` DESC LIMIT ?", + new Object[]{server_id==null||server_id.isEmpty()?"%":server_id, limit}, (rs, rowNum) -> new MaxPeakOfDay(rs)); } - public List getAvgPeakOfDays(int limit) { - return jdbcTemplate_ro.query("SELECT SUM(s.player_count) as player_count, s.fulldate as fl FROM (SELECT avg(`player_count`) as player_count, cast(FROM_UNIXTIME(`utime`) as date) as fulldate,`server_id` FROM `servers_online` WHERE `player_count` != 0 and `server_id` like '%' GROUP BY cast(FROM_UNIXTIME(`utime`) as date), server_id ORDER BY `fulldate` DESC) s GROUP BY s.fulldate ORDER BY `s`.`fulldate` DESC LIMIT ?", - new Object[]{limit}, + public List getAvgPeakOfDays(String server_id, int limit) { + return jdbcTemplate_ro.query("SELECT SUM(s.player_count) as player_count, s.fulldate as fl FROM (SELECT avg(`player_count`) as player_count, cast(FROM_UNIXTIME(`utime`) as date) as fulldate,`server_id` FROM `servers_online` WHERE `player_count` != 0 and `server_id` like ? GROUP BY cast(FROM_UNIXTIME(`utime`) as date), server_id ORDER BY `fulldate` DESC) s GROUP BY s.fulldate ORDER BY `s`.`fulldate` DESC LIMIT ?", + new Object[]{server_id==null||server_id.isEmpty()?"%":server_id, limit}, (rs, rowNum) -> new AvgPeakOfDay(rs)); } - public List getPeakOfDays(int limit) { - List maxPeakOfDays = getMaxPeakOfDays(limit); - List avgPeakOfDays = getAvgPeakOfDays(limit); + public List getPeakOfDays(String server_id, int limit) { + List maxPeakOfDays = getMaxPeakOfDays(server_id, limit); + List avgPeakOfDays = getAvgPeakOfDays(server_id, limit); List statsOfPeakOfDays = new ArrayList<>(); for (int i = 0; i < maxPeakOfDays.size(); i++) { statsOfPeakOfDays.add(new StatsOfPeakOfDay(avgPeakOfDays.get(i), maxPeakOfDays.get(i)));