From 4fd66dffd3c81ea5294b8fa3b8f65498dfa540e5 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 18 Apr 2024 13:17:41 +0100 Subject: [PATCH] switch to unique player and server lookups not the total --- .../backend/service/MetricService.java | 9 ++--- .../backend/service/PlayerService.java | 5 +-- .../backend/service/ServerService.java | 5 +-- .../backend/service/metric/Metric.java | 3 +- .../metrics/ConnectedSocketsMetric.java | 3 +- .../metrics/TotalPlayerLookupsMetric.java | 10 ----- .../metrics/TotalServerLookupsMetric.java | 10 ----- .../metrics/UniquePlayerLookupsMetric.java | 37 +++++++++++++++++++ .../metrics/UniqueServerLookupsMetric.java | 37 +++++++++++++++++++ .../metrics/process/CpuUsageMetric.java | 3 +- .../metric/metrics/process/MemoryMetric.java | 3 +- .../websocket/MetricsWebSocketHandler.java | 9 ++--- 12 files changed, 94 insertions(+), 40 deletions(-) delete mode 100644 src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalPlayerLookupsMetric.java delete mode 100644 src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalServerLookupsMetric.java create mode 100644 src/main/java/xyz/mcutils/backend/service/metric/metrics/UniquePlayerLookupsMetric.java create mode 100644 src/main/java/xyz/mcutils/backend/service/metric/metrics/UniqueServerLookupsMetric.java diff --git a/src/main/java/xyz/mcutils/backend/service/MetricService.java b/src/main/java/xyz/mcutils/backend/service/MetricService.java index 3613f03..04d2f71 100644 --- a/src/main/java/xyz/mcutils/backend/service/MetricService.java +++ b/src/main/java/xyz/mcutils/backend/service/MetricService.java @@ -44,9 +44,8 @@ public class MetricService { registerMetric(new RequestsPerRouteMetric()); registerMetric(new MemoryMetric()); registerMetric(new CpuUsageMetric()); - registerMetric(new TotalPlayerLookupsMetric()); - registerMetric(new TotalServerLookupsMetric()); registerMetric(new ConnectedSocketsMetric()); + registerMetric(new UniquePlayerLookupsMetric()); // Load the metrics from Redis loadMetrics(); @@ -88,8 +87,8 @@ public class MetricService { */ public void loadMetrics() { log.info("Loading metrics"); - for (Metric metric : metricsRepository.findAll()) { - metrics.put(metric.getClass(), metric); + for (Metric metric : metrics.values()) { + metricsRepository.findById(metric.getId()).ifPresent(loaded -> metrics.put(loaded.getClass(), loaded)); } log.info("Loaded {} metrics", metrics.size()); } @@ -120,7 +119,7 @@ public class MetricService { List points = new ArrayList<>(); for (Metric metric : metrics.values()) { if (metric.isCollector()) { - metric.collect(); + metric.collect(this); } Point point = metric.toPoint(); if (point != null) { diff --git a/src/main/java/xyz/mcutils/backend/service/PlayerService.java b/src/main/java/xyz/mcutils/backend/service/PlayerService.java index f12304a..4fad2a6 100644 --- a/src/main/java/xyz/mcutils/backend/service/PlayerService.java +++ b/src/main/java/xyz/mcutils/backend/service/PlayerService.java @@ -24,8 +24,7 @@ import xyz.mcutils.backend.model.token.MojangUsernameToUuidToken; import xyz.mcutils.backend.repository.PlayerCacheRepository; import xyz.mcutils.backend.repository.PlayerNameCacheRepository; import xyz.mcutils.backend.repository.PlayerSkinPartCacheRepository; -import xyz.mcutils.backend.service.metric.metrics.TotalPlayerLookupsMetric; -import xyz.mcutils.backend.service.metric.metrics.TotalServerLookupsMetric; +import xyz.mcutils.backend.service.metric.metrics.UniquePlayerLookupsMetric; import java.awt.image.BufferedImage; import java.util.Optional; @@ -65,7 +64,7 @@ public class PlayerService { uuid = usernameToUuid(id).getUniqueId(); } - ((TotalPlayerLookupsMetric) metricService.getMetric(TotalPlayerLookupsMetric.class)).increment(); // Increment the total player lookups + ((UniquePlayerLookupsMetric) metricService.getMetric(UniquePlayerLookupsMetric.class)).addLookup(id); // Add the lookup to the unique player lookups Optional cachedPlayer = playerCacheRepository.findById(uuid); if (cachedPlayer.isPresent() && Config.INSTANCE.isProduction()) { // Return the cached player if it exists diff --git a/src/main/java/xyz/mcutils/backend/service/ServerService.java b/src/main/java/xyz/mcutils/backend/service/ServerService.java index 768ae0d..e34cba1 100644 --- a/src/main/java/xyz/mcutils/backend/service/ServerService.java +++ b/src/main/java/xyz/mcutils/backend/service/ServerService.java @@ -15,8 +15,7 @@ import xyz.mcutils.backend.model.dns.impl.SRVRecord; import xyz.mcutils.backend.model.server.JavaMinecraftServer; import xyz.mcutils.backend.model.server.MinecraftServer; import xyz.mcutils.backend.repository.MinecraftServerCacheRepository; -import xyz.mcutils.backend.service.metric.Metric; -import xyz.mcutils.backend.service.metric.metrics.TotalServerLookupsMetric; +import xyz.mcutils.backend.service.metric.metrics.UniqueServerLookupsMetric; import java.net.InetSocketAddress; import java.util.ArrayList; @@ -66,7 +65,7 @@ public class ServerService { String key = "%s-%s:%s".formatted(platformName, hostname, port); log.info("Getting server: {}:{}", hostname, port); - ((TotalServerLookupsMetric) metricService.getMetric(TotalServerLookupsMetric.class)).increment(); // Increment the total server lookups + ((UniqueServerLookupsMetric) metricService.getMetric(UniqueServerLookupsMetric.class)).addLookup(key); // Add the server lookup to the unique server lookups // Check if the server is cached Optional cached = serverCacheRepository.findById(key); diff --git a/src/main/java/xyz/mcutils/backend/service/metric/Metric.java b/src/main/java/xyz/mcutils/backend/service/metric/Metric.java index 9975305..41b7c06 100644 --- a/src/main/java/xyz/mcutils/backend/service/metric/Metric.java +++ b/src/main/java/xyz/mcutils/backend/service/metric/Metric.java @@ -8,6 +8,7 @@ import lombok.Setter; import lombok.ToString; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; +import xyz.mcutils.backend.service.MetricService; @AllArgsConstructor @Getter @Setter @ToString @@ -33,7 +34,7 @@ public abstract class Metric { /** * Collects the metric. */ - public void collect() {} + public void collect(MetricService metricService) {} /** * Gets this point as a {@link Point}. diff --git a/src/main/java/xyz/mcutils/backend/service/metric/metrics/ConnectedSocketsMetric.java b/src/main/java/xyz/mcutils/backend/service/metric/metrics/ConnectedSocketsMetric.java index 59ed4e0..3626c20 100644 --- a/src/main/java/xyz/mcutils/backend/service/metric/metrics/ConnectedSocketsMetric.java +++ b/src/main/java/xyz/mcutils/backend/service/metric/metrics/ConnectedSocketsMetric.java @@ -1,5 +1,6 @@ package xyz.mcutils.backend.service.metric.metrics; +import xyz.mcutils.backend.service.MetricService; import xyz.mcutils.backend.service.metric.impl.IntegerMetric; import xyz.mcutils.backend.websocket.MetricsWebSocketHandler; @@ -15,7 +16,7 @@ public class ConnectedSocketsMetric extends IntegerMetric { } @Override - public void collect() { + public void collect(MetricService metricService) { setValue(MetricsWebSocketHandler.SESSIONS.size()); } } diff --git a/src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalPlayerLookupsMetric.java b/src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalPlayerLookupsMetric.java deleted file mode 100644 index 592a191..0000000 --- a/src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalPlayerLookupsMetric.java +++ /dev/null @@ -1,10 +0,0 @@ -package xyz.mcutils.backend.service.metric.metrics; - -import xyz.mcutils.backend.service.metric.impl.IntegerMetric; - -public class TotalPlayerLookupsMetric extends IntegerMetric { - - public TotalPlayerLookupsMetric() { - super("total_player_lookups"); - } -} diff --git a/src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalServerLookupsMetric.java b/src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalServerLookupsMetric.java deleted file mode 100644 index 7b6b0a7..0000000 --- a/src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalServerLookupsMetric.java +++ /dev/null @@ -1,10 +0,0 @@ -package xyz.mcutils.backend.service.metric.metrics; - -import xyz.mcutils.backend.service.metric.impl.IntegerMetric; - -public class TotalServerLookupsMetric extends IntegerMetric { - - public TotalServerLookupsMetric() { - super("total_server_lookups"); - } -} diff --git a/src/main/java/xyz/mcutils/backend/service/metric/metrics/UniquePlayerLookupsMetric.java b/src/main/java/xyz/mcutils/backend/service/metric/metrics/UniquePlayerLookupsMetric.java new file mode 100644 index 0000000..4096321 --- /dev/null +++ b/src/main/java/xyz/mcutils/backend/service/metric/metrics/UniquePlayerLookupsMetric.java @@ -0,0 +1,37 @@ +package xyz.mcutils.backend.service.metric.metrics; + +import xyz.mcutils.backend.service.MetricService; +import xyz.mcutils.backend.service.metric.impl.IntegerMetric; + +import java.util.ArrayList; +import java.util.List; + +public class UniquePlayerLookupsMetric extends IntegerMetric { + private List uniqueLookups = new ArrayList<>(); + + public UniquePlayerLookupsMetric() { + super("unique_player_lookups"); + } + + @Override + public boolean isCollector() { + return true; + } + + /** + * Adds a lookup to the list of unique lookups. + * + * @param lookup the query that was used to look up a player + */ + public void addLookup(String lookup) { + lookup = lookup.toLowerCase(); + if (!uniqueLookups.contains(lookup)) { + uniqueLookups.add(lookup); + } + } + + @Override + public void collect(MetricService metricService) { + setValue(uniqueLookups.size()); + } +} diff --git a/src/main/java/xyz/mcutils/backend/service/metric/metrics/UniqueServerLookupsMetric.java b/src/main/java/xyz/mcutils/backend/service/metric/metrics/UniqueServerLookupsMetric.java new file mode 100644 index 0000000..e1be045 --- /dev/null +++ b/src/main/java/xyz/mcutils/backend/service/metric/metrics/UniqueServerLookupsMetric.java @@ -0,0 +1,37 @@ +package xyz.mcutils.backend.service.metric.metrics; + +import xyz.mcutils.backend.service.MetricService; +import xyz.mcutils.backend.service.metric.impl.IntegerMetric; + +import java.util.ArrayList; +import java.util.List; + +public class UniqueServerLookupsMetric extends IntegerMetric { + private List uniqueLookups = new ArrayList<>(); + + public UniqueServerLookupsMetric() { + super("unique_server_lookups"); + } + + @Override + public boolean isCollector() { + return true; + } + + /** + * Adds a lookup to the list of unique lookups. + * + * @param lookup the query that was used to look up a player + */ + public void addLookup(String lookup) { + lookup = lookup.toLowerCase(); + if (!uniqueLookups.contains(lookup)) { + uniqueLookups.add(lookup); + } + } + + @Override + public void collect(MetricService metricService) { + setValue(uniqueLookups.size()); + } +} 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 40cf914..771df7c 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 @@ -1,6 +1,7 @@ package xyz.mcutils.backend.service.metric.metrics.process; import com.sun.management.OperatingSystemMXBean; +import xyz.mcutils.backend.service.MetricService; import xyz.mcutils.backend.service.metric.impl.DoubleMetric; import java.lang.management.ManagementFactory; @@ -21,7 +22,7 @@ public class CpuUsageMetric extends DoubleMetric { } @Override - public void collect() { + public void collect(MetricService metricService) { this.setValue(OS_BEAN.getProcessCpuLoad() * 100); } } diff --git a/src/main/java/xyz/mcutils/backend/service/metric/metrics/process/MemoryMetric.java b/src/main/java/xyz/mcutils/backend/service/metric/metrics/process/MemoryMetric.java index 2e64aa9..4d98e21 100644 --- a/src/main/java/xyz/mcutils/backend/service/metric/metrics/process/MemoryMetric.java +++ b/src/main/java/xyz/mcutils/backend/service/metric/metrics/process/MemoryMetric.java @@ -1,5 +1,6 @@ package xyz.mcutils.backend.service.metric.metrics.process; +import xyz.mcutils.backend.service.MetricService; import xyz.mcutils.backend.service.metric.impl.MapMetric; public class MemoryMetric extends MapMetric { @@ -14,7 +15,7 @@ public class MemoryMetric extends MapMetric { } @Override - public void collect() { + public void collect(MetricService metricService) { Runtime runtime = Runtime.getRuntime(); this.getValue().put("total", runtime.maxMemory()); diff --git a/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java b/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java index 4294c0f..aa4b6c2 100644 --- a/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java +++ b/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java @@ -8,11 +8,10 @@ import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.handler.TextWebSocketHandler; import xyz.mcutils.backend.Main; import xyz.mcutils.backend.common.Timer; -import xyz.mcutils.backend.model.metric.WebsocketMetrics; import xyz.mcutils.backend.service.MetricService; -import xyz.mcutils.backend.service.metric.metrics.TotalPlayerLookupsMetric; import xyz.mcutils.backend.service.metric.metrics.TotalRequestsMetric; -import xyz.mcutils.backend.service.metric.metrics.TotalServerLookupsMetric; +import xyz.mcutils.backend.service.metric.metrics.UniquePlayerLookupsMetric; +import xyz.mcutils.backend.service.metric.metrics.UniqueServerLookupsMetric; import java.util.ArrayList; import java.util.List; @@ -44,8 +43,8 @@ public class MetricsWebSocketHandler extends TextWebSocketHandler { try { session.sendMessage(new TextMessage(Main.GSON.toJson(Map.of( "totalRequests", metricService.getMetric(TotalRequestsMetric.class).getValue(), - "totalServerLookups", metricService.getMetric(TotalServerLookupsMetric.class).getValue(), - "totalPlayerLookups", metricService.getMetric(TotalPlayerLookupsMetric.class).getValue() + "uniqueServerLookups", metricService.getMetric(UniqueServerLookupsMetric.class).getValue(), + "uniquePlayerLookups", metricService.getMetric(UniquePlayerLookupsMetric.class).getValue() )))); } catch (Exception e) { log.error("An error occurred while sending metrics to the client", e);