Browse Source

funny graph

master
gsd 10 months ago
parent
commit
2ca0133b55
  1. 16
      src/main/java/app/controllers/StatsController.java

16
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<Integer, HashMap<List<OnlineUpdater.StatsOfPeakOfDay>, 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<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);
}
}

Loading…
Cancel
Save