Browse Source

fff 3

master
gsd 10 months ago
parent
commit
7a335d1eb3
  1. 17
      src/main/java/app/controllers/StatsController.java
  2. 20
      src/main/java/app/updates/OnlineUpdater.java

17
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<Integer, HashMap<List<OnlineUpdater.StatsOfPeakOfDay>, Long>> cache = new HashMap<>();
private HashMap<Map<Integer, String>, HashMap<List<OnlineUpdater.StatsOfPeakOfDay>, Long>> cache = new HashMap<>();
private ServersReader serversReader;
@Autowired
@ -54,17 +55,19 @@ public class StatsController {
}
@GetMapping("/graph/peak/of/days")
public ResponseEntity<List<OnlineUpdater.StatsOfPeakOfDay>> GetPeakOfDays(@RequestParam(required = false, defaultValue = "7") Integer limit) {
public ResponseEntity<List<OnlineUpdater.StatsOfPeakOfDay>> 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")

20
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<MaxPeakOfDay> 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<MaxPeakOfDay> 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<AvgPeakOfDay> 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<AvgPeakOfDay> 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<StatsOfPeakOfDay> getPeakOfDays(int limit) {
List<MaxPeakOfDay> maxPeakOfDays = getMaxPeakOfDays(limit);
List<AvgPeakOfDay> avgPeakOfDays = getAvgPeakOfDays(limit);
public List<StatsOfPeakOfDay> getPeakOfDays(String server_id, int limit) {
List<MaxPeakOfDay> maxPeakOfDays = getMaxPeakOfDays(server_id, limit);
List<AvgPeakOfDay> avgPeakOfDays = getAvgPeakOfDays(server_id, limit);
List<StatsOfPeakOfDay> statsOfPeakOfDays = new ArrayList<>();
for (int i = 0; i < maxPeakOfDays.size(); i++) {
statsOfPeakOfDays.add(new StatsOfPeakOfDay(avgPeakOfDays.get(i), maxPeakOfDays.get(i)));

Loading…
Cancel
Save