You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

64 lines
2.6 KiB

package app.controllers;
import app.annotations.enums.CollectStages;
import app.annotations.interfaces.BurstUpdatePlayers;
import app.annotations.interfaces.CollectStatistic;
import app.entities.Stats;
import app.services.ExternalServices;
import app.updates.OnlineUpdater;
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;
@RestController
@RequestMapping("api/stats")
public class StatsController {
private Stats stats;
private ExternalServices externalServices;
private OnlineUpdater onlineUpdater;
private HashMap<Integer, HashMap<List<OnlineUpdater.StatsOfPeakOfDay>, Long>> cache = new HashMap<>();
@Autowired
public StatsController(Stats stats, ExternalServices externalServices, OnlineUpdater onlineUpdater){
this.stats = stats;
this.externalServices = externalServices;
this.onlineUpdater = onlineUpdater;
}
@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<List<OnlineUpdater.StatsOfPeakOfDay>> 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);
}
}