change api format

This commit is contained in:
Lee 2024-04-17 16:35:52 +01:00
parent 632d33197d
commit ecca157d86
7 changed files with 71 additions and 45 deletions

@ -7,7 +7,7 @@ import org.springframework.http.HttpStatusCode;
import java.util.List; import java.util.List;
@AllArgsConstructor @Getter @AllArgsConstructor @Getter
public class EndpointStatus { public class Endpoint {
/** /**
* The endpoint. * The endpoint.

@ -1,16 +1,17 @@
package xyz.mcutils.backend.model.cache; package xyz.mcutils.backend.model.cache;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import lombok.*; import lombok.*;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash; import org.springframework.data.redis.core.RedisHash;
import xyz.mcutils.backend.common.CachedResponse; import xyz.mcutils.backend.common.CachedResponse;
import xyz.mcutils.backend.model.mojang.EndpointStatus;
import java.io.Serializable; import java.io.Serializable;
import java.util.Map; import java.util.Map;
@Setter @Getter @ToString @Setter @Getter @ToString
@NoArgsConstructor
@RedisHash(value = "mojangEndpointStatus", timeToLive = 60L) // 1 minute (in seconds) @RedisHash(value = "mojangEndpointStatus", timeToLive = 60L) // 1 minute (in seconds)
public class CachedEndpointStatus extends CachedResponse implements Serializable { public class CachedEndpointStatus extends CachedResponse implements Serializable {
@ -18,34 +19,17 @@ public class CachedEndpointStatus extends CachedResponse implements Serializable
* The id for this endpoint cache. * The id for this endpoint cache.
*/ */
@Id @NonNull @JsonIgnore @Id @NonNull @JsonIgnore
private String id; private final String id;
/** /**
* The list of endpoints and their status. * The endpoint cache.
*/ */
private Map<String, Status> endpoints; @JsonUnwrapped
private final EndpointStatus value;
public CachedEndpointStatus(@NonNull String id, Map<String, Status> endpoints) { public CachedEndpointStatus(@NonNull String id, EndpointStatus value) {
super(Cache.defaultCache()); super(Cache.defaultCache());
this.id = id; this.id = id;
this.endpoints = endpoints; this.value = value;
}
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
} }
} }

@ -1,6 +1,7 @@
package xyz.mcutils.backend.model.cache; package xyz.mcutils.backend.model.cache;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import lombok.*; import lombok.*;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash; import org.springframework.data.redis.core.RedisHash;
@ -25,7 +26,7 @@ public class CachedMinecraftServer extends CachedResponse implements Serializabl
/** /**
* The cached server. * The cached server.
*/ */
@NonNull @NonNull @JsonUnwrapped
private MinecraftServer server; private MinecraftServer server;
public CachedMinecraftServer(@NonNull String id, @NonNull MinecraftServer server) { public CachedMinecraftServer(@NonNull String id, @NonNull MinecraftServer server) {

@ -1,6 +1,7 @@
package xyz.mcutils.backend.model.cache; package xyz.mcutils.backend.model.cache;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
@ -30,6 +31,7 @@ public class CachedPlayer extends CachedResponse implements Serializable {
/** /**
* The player to cache. * The player to cache.
*/ */
@JsonUnwrapped
private Player player; private Player player;
public CachedPlayer(UUID uniqueId, Player player) { public CachedPlayer(UUID uniqueId, Player player) {

@ -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<String, Status> 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
}
}

@ -14,11 +14,12 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import xyz.mcutils.backend.Main; 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.ExpiringSet;
import xyz.mcutils.backend.common.WebRequest; import xyz.mcutils.backend.common.WebRequest;
import xyz.mcutils.backend.config.Config; import xyz.mcutils.backend.config.Config;
import xyz.mcutils.backend.model.cache.CachedEndpointStatus; 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.MojangProfileToken;
import xyz.mcutils.backend.model.token.MojangUsernameToUuidToken; import xyz.mcutils.backend.model.token.MojangUsernameToUuidToken;
import xyz.mcutils.backend.repository.EndpointStatusRepository; import xyz.mcutils.backend.repository.EndpointStatusRepository;
@ -55,14 +56,14 @@ public class MojangService {
* Information about the Mojang API endpoints. * Information about the Mojang API endpoints.
*/ */
private static final String MOJANG_ENDPOINT_STATUS_KEY = "mojang"; private static final String MOJANG_ENDPOINT_STATUS_KEY = "mojang";
private static final List<EndpointStatus> MOJANG_ENDPOINTS = List.of( private static final List<Endpoint> MOJANG_ENDPOINTS = List.of(
new EndpointStatus("https://textures.minecraft.net", List.of(HttpStatus.BAD_REQUEST)), new Endpoint("https://textures.minecraft.net", List.of(HttpStatus.BAD_REQUEST)),
new EndpointStatus("https://session.minecraft.net", List.of(HttpStatus.NOT_FOUND)), new Endpoint("https://session.minecraft.net", List.of(HttpStatus.NOT_FOUND)),
new EndpointStatus("https://libraries.minecraft.net", List.of(HttpStatus.NOT_FOUND)), new Endpoint("https://libraries.minecraft.net", List.of(HttpStatus.NOT_FOUND)),
new EndpointStatus("https://assets.mojang.com", List.of(HttpStatus.NOT_FOUND)), new Endpoint("https://assets.mojang.com", List.of(HttpStatus.NOT_FOUND)),
new EndpointStatus("https://api.minecraftservices.com", List.of(HttpStatus.FORBIDDEN)), new Endpoint("https://api.minecraftservices.com", List.of(HttpStatus.FORBIDDEN)),
new EndpointStatus(API_ENDPOINT, List.of(HttpStatus.OK)), new Endpoint(API_ENDPOINT, List.of(HttpStatus.OK)),
new EndpointStatus(SESSION_SERVER_ENDPOINT, List.of(HttpStatus.FORBIDDEN)) new Endpoint(SESSION_SERVER_ENDPOINT, List.of(HttpStatus.FORBIDDEN))
); );
@Autowired @Autowired
@ -195,9 +196,9 @@ public class MojangService {
} }
// Fetch the status of the Mojang API endpoints // Fetch the status of the Mojang API endpoints
List<CompletableFuture<CachedEndpointStatus.Status>> futures = new ArrayList<>(); List<CompletableFuture<EndpointStatus.Status>> futures = new ArrayList<>();
for (EndpointStatus endpoint : MOJANG_ENDPOINTS) { for (Endpoint endpoint : MOJANG_ENDPOINTS) {
CompletableFuture<CachedEndpointStatus.Status> future = CompletableFuture.supplyAsync(() -> { CompletableFuture<EndpointStatus.Status> future = CompletableFuture.supplyAsync(() -> {
boolean online = false; boolean online = false;
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
ResponseEntity<?> response = WebRequest.head(endpoint.getEndpoint(), String.class); ResponseEntity<?> response = WebRequest.head(endpoint.getEndpoint(), String.class);
@ -205,9 +206,9 @@ public class MojangService {
online = true; online = true;
} }
if (online && System.currentTimeMillis() - start > 1000) { // If the response took longer than 1 second 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); }, Main.EXECUTOR_POOL);
futures.add(future); futures.add(future);
@ -220,17 +221,17 @@ public class MojangService {
} }
// Process the results // Process the results
Map<String, CachedEndpointStatus.Status> endpoints = new HashMap<>(); Map<String, EndpointStatus.Status> endpoints = new HashMap<>();
for (int i = 0; i < MOJANG_ENDPOINTS.size(); i++) { for (int i = 0; i < MOJANG_ENDPOINTS.size(); i++) {
EndpointStatus endpoint = MOJANG_ENDPOINTS.get(i); Endpoint endpoint = MOJANG_ENDPOINTS.get(i);
CachedEndpointStatus.Status status = futures.get(i).join(); EndpointStatus.Status status = futures.get(i).join();
endpoints.put(endpoint.getEndpoint(), status); endpoints.put(endpoint.getEndpoint(), status);
} }
log.info("Fetched Mojang API status for {} endpoints", endpoints.size()); log.info("Fetched Mojang API status for {} endpoints", endpoints.size());
CachedEndpointStatus status = new CachedEndpointStatus( CachedEndpointStatus status = new CachedEndpointStatus(
MOJANG_ENDPOINT_STATUS_KEY, MOJANG_ENDPOINT_STATUS_KEY,
endpoints new EndpointStatus(endpoints)
); );
mojangEndpointStatusRepository.save(status); mojangEndpointStatusRepository.save(status);
status.getCache().setCached(false); status.getCache().setCached(false);

@ -15,7 +15,6 @@ public class CpuUsageMetric extends DoubleMetric {
super("cpu_usage"); super("cpu_usage");
} }
@Override @Override
public void collect() { public void collect() {
this.setValue(OS_BEAN.getProcessCpuLoad() * 100); this.setValue(OS_BEAN.getProcessCpuLoad() * 100);