From ecca157d86b428e50f711da91c35914e793c97fe Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 17 Apr 2024 16:35:52 +0100 Subject: [PATCH] change api format --- .../{EndpointStatus.java => Endpoint.java} | 2 +- .../model/cache/CachedEndpointStatus.java | 32 ++++----------- .../model/cache/CachedMinecraftServer.java | 3 +- .../backend/model/cache/CachedPlayer.java | 2 + .../backend/model/mojang/EndpointStatus.java | 39 +++++++++++++++++++ .../backend/service/MojangService.java | 37 +++++++++--------- .../metrics/process/CpuUsageMetric.java | 1 - 7 files changed, 71 insertions(+), 45 deletions(-) rename src/main/java/xyz/mcutils/backend/common/{EndpointStatus.java => Endpoint.java} (93%) create mode 100644 src/main/java/xyz/mcutils/backend/model/mojang/EndpointStatus.java diff --git a/src/main/java/xyz/mcutils/backend/common/EndpointStatus.java b/src/main/java/xyz/mcutils/backend/common/Endpoint.java similarity index 93% rename from src/main/java/xyz/mcutils/backend/common/EndpointStatus.java rename to src/main/java/xyz/mcutils/backend/common/Endpoint.java index dba480a..289722a 100644 --- a/src/main/java/xyz/mcutils/backend/common/EndpointStatus.java +++ b/src/main/java/xyz/mcutils/backend/common/Endpoint.java @@ -7,7 +7,7 @@ import org.springframework.http.HttpStatusCode; import java.util.List; @AllArgsConstructor @Getter -public class EndpointStatus { +public class Endpoint { /** * The endpoint. diff --git a/src/main/java/xyz/mcutils/backend/model/cache/CachedEndpointStatus.java b/src/main/java/xyz/mcutils/backend/model/cache/CachedEndpointStatus.java index b6e8fe8..a48270c 100644 --- a/src/main/java/xyz/mcutils/backend/model/cache/CachedEndpointStatus.java +++ b/src/main/java/xyz/mcutils/backend/model/cache/CachedEndpointStatus.java @@ -1,16 +1,17 @@ package xyz.mcutils.backend.model.cache; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonUnwrapped; import lombok.*; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; import xyz.mcutils.backend.common.CachedResponse; +import xyz.mcutils.backend.model.mojang.EndpointStatus; import java.io.Serializable; import java.util.Map; @Setter @Getter @ToString -@NoArgsConstructor @RedisHash(value = "mojangEndpointStatus", timeToLive = 60L) // 1 minute (in seconds) public class CachedEndpointStatus extends CachedResponse implements Serializable { @@ -18,34 +19,17 @@ public class CachedEndpointStatus extends CachedResponse implements Serializable * The id for this endpoint cache. */ @Id @NonNull @JsonIgnore - private String id; + private final String id; /** - * The list of endpoints and their status. + * The endpoint cache. */ - private Map endpoints; + @JsonUnwrapped + private final EndpointStatus value; - public CachedEndpointStatus(@NonNull String id, Map endpoints) { + public CachedEndpointStatus(@NonNull String id, EndpointStatus value) { super(Cache.defaultCache()); this.id = id; - this.endpoints = endpoints; - } - - public enum Status { - /** - * The service is online and operational. - */ - ONLINE, - - /** - * The service is online, but may be experiencing issues. - * This could be due to high load or other issues. - */ - DEGRADED, - - /** - * The service is offline and not operational. - */ - OFFLINE + this.value = value; } } \ No newline at end of file diff --git a/src/main/java/xyz/mcutils/backend/model/cache/CachedMinecraftServer.java b/src/main/java/xyz/mcutils/backend/model/cache/CachedMinecraftServer.java index 6216074..bfddbf2 100644 --- a/src/main/java/xyz/mcutils/backend/model/cache/CachedMinecraftServer.java +++ b/src/main/java/xyz/mcutils/backend/model/cache/CachedMinecraftServer.java @@ -1,6 +1,7 @@ package xyz.mcutils.backend.model.cache; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonUnwrapped; import lombok.*; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; @@ -25,7 +26,7 @@ public class CachedMinecraftServer extends CachedResponse implements Serializabl /** * The cached server. */ - @NonNull + @NonNull @JsonUnwrapped private MinecraftServer server; public CachedMinecraftServer(@NonNull String id, @NonNull MinecraftServer server) { diff --git a/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayer.java b/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayer.java index ebfb867..9f2a861 100644 --- a/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayer.java +++ b/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayer.java @@ -1,6 +1,7 @@ package xyz.mcutils.backend.model.cache; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonUnwrapped; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -30,6 +31,7 @@ public class CachedPlayer extends CachedResponse implements Serializable { /** * The player to cache. */ + @JsonUnwrapped private Player player; public CachedPlayer(UUID uniqueId, Player player) { diff --git a/src/main/java/xyz/mcutils/backend/model/mojang/EndpointStatus.java b/src/main/java/xyz/mcutils/backend/model/mojang/EndpointStatus.java new file mode 100644 index 0000000..8edb7fc --- /dev/null +++ b/src/main/java/xyz/mcutils/backend/model/mojang/EndpointStatus.java @@ -0,0 +1,39 @@ +package xyz.mcutils.backend.model.mojang; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NonNull; +import org.springframework.data.annotation.Id; +import xyz.mcutils.backend.common.CachedResponse; +import xyz.mcutils.backend.model.cache.CachedEndpointStatus; + +import java.util.Map; + +@AllArgsConstructor +@Getter +public class EndpointStatus { + + /** + * The list of endpoints and their status. + */ + private final Map endpoints; + + public enum Status { + /** + * The service is online and operational. + */ + ONLINE, + + /** + * The service is online, but may be experiencing issues. + * This could be due to high load or other issues. + */ + DEGRADED, + + /** + * The service is offline and not operational. + */ + OFFLINE + } +} diff --git a/src/main/java/xyz/mcutils/backend/service/MojangService.java b/src/main/java/xyz/mcutils/backend/service/MojangService.java index 36b73f9..e2e393e 100644 --- a/src/main/java/xyz/mcutils/backend/service/MojangService.java +++ b/src/main/java/xyz/mcutils/backend/service/MojangService.java @@ -14,11 +14,12 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import xyz.mcutils.backend.Main; -import xyz.mcutils.backend.common.EndpointStatus; +import xyz.mcutils.backend.common.Endpoint; import xyz.mcutils.backend.common.ExpiringSet; import xyz.mcutils.backend.common.WebRequest; import xyz.mcutils.backend.config.Config; import xyz.mcutils.backend.model.cache.CachedEndpointStatus; +import xyz.mcutils.backend.model.mojang.EndpointStatus; import xyz.mcutils.backend.model.token.MojangProfileToken; import xyz.mcutils.backend.model.token.MojangUsernameToUuidToken; import xyz.mcutils.backend.repository.EndpointStatusRepository; @@ -55,14 +56,14 @@ public class MojangService { * Information about the Mojang API endpoints. */ private static final String MOJANG_ENDPOINT_STATUS_KEY = "mojang"; - private static final List MOJANG_ENDPOINTS = List.of( - new EndpointStatus("https://textures.minecraft.net", List.of(HttpStatus.BAD_REQUEST)), - new EndpointStatus("https://session.minecraft.net", List.of(HttpStatus.NOT_FOUND)), - new EndpointStatus("https://libraries.minecraft.net", List.of(HttpStatus.NOT_FOUND)), - new EndpointStatus("https://assets.mojang.com", List.of(HttpStatus.NOT_FOUND)), - new EndpointStatus("https://api.minecraftservices.com", List.of(HttpStatus.FORBIDDEN)), - new EndpointStatus(API_ENDPOINT, List.of(HttpStatus.OK)), - new EndpointStatus(SESSION_SERVER_ENDPOINT, List.of(HttpStatus.FORBIDDEN)) + private static final List MOJANG_ENDPOINTS = List.of( + new Endpoint("https://textures.minecraft.net", List.of(HttpStatus.BAD_REQUEST)), + new Endpoint("https://session.minecraft.net", List.of(HttpStatus.NOT_FOUND)), + new Endpoint("https://libraries.minecraft.net", List.of(HttpStatus.NOT_FOUND)), + new Endpoint("https://assets.mojang.com", List.of(HttpStatus.NOT_FOUND)), + new Endpoint("https://api.minecraftservices.com", List.of(HttpStatus.FORBIDDEN)), + new Endpoint(API_ENDPOINT, List.of(HttpStatus.OK)), + new Endpoint(SESSION_SERVER_ENDPOINT, List.of(HttpStatus.FORBIDDEN)) ); @Autowired @@ -195,9 +196,9 @@ public class MojangService { } // Fetch the status of the Mojang API endpoints - List> futures = new ArrayList<>(); - for (EndpointStatus endpoint : MOJANG_ENDPOINTS) { - CompletableFuture future = CompletableFuture.supplyAsync(() -> { + List> futures = new ArrayList<>(); + for (Endpoint endpoint : MOJANG_ENDPOINTS) { + CompletableFuture future = CompletableFuture.supplyAsync(() -> { boolean online = false; long start = System.currentTimeMillis(); ResponseEntity response = WebRequest.head(endpoint.getEndpoint(), String.class); @@ -205,9 +206,9 @@ public class MojangService { online = true; } if (online && System.currentTimeMillis() - start > 1000) { // If the response took longer than 1 second - return CachedEndpointStatus.Status.DEGRADED; + return EndpointStatus.Status.DEGRADED; } - return online ? CachedEndpointStatus.Status.ONLINE : CachedEndpointStatus.Status.OFFLINE; + return online ? EndpointStatus.Status.ONLINE : EndpointStatus.Status.OFFLINE; }, Main.EXECUTOR_POOL); futures.add(future); @@ -220,17 +221,17 @@ public class MojangService { } // Process the results - Map endpoints = new HashMap<>(); + Map endpoints = new HashMap<>(); for (int i = 0; i < MOJANG_ENDPOINTS.size(); i++) { - EndpointStatus endpoint = MOJANG_ENDPOINTS.get(i); - CachedEndpointStatus.Status status = futures.get(i).join(); + Endpoint endpoint = MOJANG_ENDPOINTS.get(i); + EndpointStatus.Status status = futures.get(i).join(); endpoints.put(endpoint.getEndpoint(), status); } log.info("Fetched Mojang API status for {} endpoints", endpoints.size()); CachedEndpointStatus status = new CachedEndpointStatus( MOJANG_ENDPOINT_STATUS_KEY, - endpoints + new EndpointStatus(endpoints) ); mojangEndpointStatusRepository.save(status); status.getCache().setCached(false); diff --git a/src/main/java/xyz/mcutils/backend/service/metric/metrics/process/CpuUsageMetric.java b/src/main/java/xyz/mcutils/backend/service/metric/metrics/process/CpuUsageMetric.java index 51d8118..45a6133 100644 --- a/src/main/java/xyz/mcutils/backend/service/metric/metrics/process/CpuUsageMetric.java +++ b/src/main/java/xyz/mcutils/backend/service/metric/metrics/process/CpuUsageMetric.java @@ -15,7 +15,6 @@ public class CpuUsageMetric extends DoubleMetric { super("cpu_usage"); } - @Override public void collect() { this.setValue(OS_BEAN.getProcessCpuLoad() * 100);