diff --git a/src/main/java/app/controllers/other/DockerController.java b/src/main/java/app/controllers/other/DockerController.java new file mode 100644 index 0000000..6de48d3 --- /dev/null +++ b/src/main/java/app/controllers/other/DockerController.java @@ -0,0 +1,48 @@ +package app.controllers.other; + +import app.annotations.enums.AuthMethod; +import app.annotations.interfaces.CheckWebAccess; +import app.entities.Stats; +import app.entities.server.DockerStats; +import jakarta.servlet.http.HttpServletRequest; +import lombok.AllArgsConstructor; +import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.stream.Collectors; + +@RestController +@RequestMapping("/api/docker") +public class DockerController { + private Stats stats; + + @Autowired + DockerController(Stats stats) { + this.stats = stats; + } + + @GetMapping + @CheckWebAccess(auth_method = AuthMethod.SECRET_KEY) + public List getContainerOnServer(HttpServletRequest httpServletRequest) { + return stats.getServers().entrySet().stream().map(e -> new SrvDc(e.getKey(), e.getValue().getDc())).collect(Collectors.toList()); + } + + @PostMapping("/{srv}") + @CheckWebAccess(auth_method = AuthMethod.SECRET_KEY) + public ResponseEntity setStats(HttpServletRequest httpServletRequest, @PathVariable String srv, @RequestBody DockerStats dockerStats) { + if (!stats.getServers().containsKey(srv)) return new ResponseEntity<>(HttpStatus.NOT_FOUND); + stats.getServers().get(srv).setDockerStats(dockerStats); + return ResponseEntity.ok().build(); + } + + @Data + @AllArgsConstructor + public class SrvDc { + String srv; + String container; + } +} diff --git a/src/main/java/app/entities/server/DockerStats.java b/src/main/java/app/entities/server/DockerStats.java new file mode 100644 index 0000000..2ef429d --- /dev/null +++ b/src/main/java/app/entities/server/DockerStats.java @@ -0,0 +1,37 @@ +package app.entities.server; + +import lombok.Data; + +@Data +public class DockerStats { + CPU cpu = new CPU(); + MEM mem = new MEM(); + NET net = new NET(); + IO io = new IO(); + + float utime = 0; + + @Data + public class CPU { + float percent; + } + + @Data + public class MEM { + float percent; + float usage; + float limit; + } + + @Data + public class NET { + float input; + float output; + } + + @Data + public class IO { + float input; + float output; + } +} diff --git a/src/main/java/app/entities/server/Server.java b/src/main/java/app/entities/server/Server.java index 18bbb1c..59fd199 100644 --- a/src/main/java/app/entities/server/Server.java +++ b/src/main/java/app/entities/server/Server.java @@ -31,6 +31,7 @@ public class Server extends ExternalValveClient { HashMap uniq = new HashMap<>(); List players = new ArrayList<>(); long last_update = 0; + DockerStats dockerStats = new DockerStats(); @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) private String db;