package app.controllers; import app.annotations.enums.CollectStages; import app.annotations.interfaces.*; import app.entities.Stats; import app.services.ExternalServices; import app.services.io.readers.ServersReader; import app.updates.OnlineUpdater; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; 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; import java.util.Map; @RestController @RequestMapping("api/stats") public class StatsController { private Stats stats; private ExternalServices externalServices; private OnlineUpdater onlineUpdater; private HashMap, HashMap, Long>> cache = new HashMap<>(); private ServersReader serversReader; @Autowired public StatsController(Stats stats, ExternalServices externalServices, OnlineUpdater onlineUpdater, ServersReader serversReader){ this.stats = stats; this.externalServices = externalServices; this.onlineUpdater = onlineUpdater; this.serversReader = serversReader; } @GetMapping @BurstUpdatePlayers @CollectStatistic public ResponseEntity GetStats(@RequestParam(name = "filter", required = false) String filter){ stats.getStatistic().setPlayer_max(onlineUpdater.getCurrent_day_max_player_count()); if (filter != null && !filter.isEmpty()) { return new ResponseEntity(stats.getOnlyThis(filter), HttpStatus.OK); } return new ResponseEntity<>(stats, HttpStatus.OK); } @GetMapping("/services") @CollectStatistic public ResponseEntity GetServices() { return new ResponseEntity(externalServices.getServices(), HttpStatus.OK); } @GetMapping("/graph/peak/of/days") 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(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(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(Map.of(limit, server_id)).keySet().stream().findFirst().orElse(null), HttpStatus.OK); } @GetMapping("/reload") @CheckWebAccess @CheckPermitionFlag(flag = "z") @WaitAfterNext(order = "reload_config") @CollectStatistic(stage = CollectStages.COMBINED) public ResponseEntity reloadConfig(HttpServletRequest request) { return new ResponseEntity<>(serversReader.call(false), HttpStatus.OK); } }