From 046df7fd1f60a6e322b0a7bce6aad7aafd1e008d Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 18 Apr 2024 16:28:09 +0100 Subject: [PATCH] maybe --- pom.xml | 1 - .../mcutils/backend/config/MongoConfig.java | 4 +--- .../repository/mongo/MetricsRepository.java | 5 +---- .../redis/EndpointStatusRepository.java | 2 -- .../redis/MinecraftServerCacheRepository.java | 2 -- .../redis/PlayerCacheRepository.java | 2 -- .../redis/PlayerNameCacheRepository.java | 2 -- .../redis/PlayerSkinPartCacheRepository.java | 2 -- .../backend/service/MetricService.java | 21 +++++++++++-------- .../backend/service/metric/Metric.java | 1 - .../metrics/ConnectedSocketsMetric.java | 1 - .../metrics/UniquePlayerLookupsMetric.java | 1 - .../metrics/UniqueServerLookupsMetric.java | 1 - .../metrics/process/CpuUsageMetric.java | 1 - .../metric/metrics/process/MemoryMetric.java | 1 - .../backend/test/config/TestRedisConfig.java | 2 -- .../test/tests/PlayerControllerTests.java | 4 ++++ .../test/tests/ServerControllerTests.java | 4 ++++ 18 files changed, 22 insertions(+), 35 deletions(-) diff --git a/pom.xml b/pom.xml index 089c565..a686a1e 100644 --- a/pom.xml +++ b/pom.xml @@ -174,7 +174,6 @@ de.flapdoodle.embed de.flapdoodle.embed.mongo.spring3x 4.12.6 - test diff --git a/src/main/java/xyz/mcutils/backend/config/MongoConfig.java b/src/main/java/xyz/mcutils/backend/config/MongoConfig.java index 8a93595..2bc2195 100644 --- a/src/main/java/xyz/mcutils/backend/config/MongoConfig.java +++ b/src/main/java/xyz/mcutils/backend/config/MongoConfig.java @@ -1,10 +1,8 @@ package xyz.mcutils.backend.config; -import lombok.extern.log4j.Log4j2; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; @Configuration @EnableMongoRepositories(basePackages = "xyz.mcutils.backend.repository.mongo") -public class MongoConfig { -} +public class MongoConfig { } diff --git a/src/main/java/xyz/mcutils/backend/repository/mongo/MetricsRepository.java b/src/main/java/xyz/mcutils/backend/repository/mongo/MetricsRepository.java index bbada47..1beaa69 100644 --- a/src/main/java/xyz/mcutils/backend/repository/mongo/MetricsRepository.java +++ b/src/main/java/xyz/mcutils/backend/repository/mongo/MetricsRepository.java @@ -1,9 +1,6 @@ package xyz.mcutils.backend.repository.mongo; import org.springframework.data.mongodb.repository.MongoRepository; -import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; -import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; import xyz.mcutils.backend.service.metric.Metric; /** @@ -11,4 +8,4 @@ import xyz.mcutils.backend.service.metric.Metric; * * @author Braydon */ -public interface MetricsRepository extends CrudRepository, String> { } \ No newline at end of file +public interface MetricsRepository extends MongoRepository, String> { } \ No newline at end of file diff --git a/src/main/java/xyz/mcutils/backend/repository/redis/EndpointStatusRepository.java b/src/main/java/xyz/mcutils/backend/repository/redis/EndpointStatusRepository.java index c84d25b..3dcb70c 100644 --- a/src/main/java/xyz/mcutils/backend/repository/redis/EndpointStatusRepository.java +++ b/src/main/java/xyz/mcutils/backend/repository/redis/EndpointStatusRepository.java @@ -1,8 +1,6 @@ package xyz.mcutils.backend.repository.redis; -import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; import xyz.mcutils.backend.model.cache.CachedEndpointStatus; /** diff --git a/src/main/java/xyz/mcutils/backend/repository/redis/MinecraftServerCacheRepository.java b/src/main/java/xyz/mcutils/backend/repository/redis/MinecraftServerCacheRepository.java index fb4cbec..45fef78 100644 --- a/src/main/java/xyz/mcutils/backend/repository/redis/MinecraftServerCacheRepository.java +++ b/src/main/java/xyz/mcutils/backend/repository/redis/MinecraftServerCacheRepository.java @@ -1,8 +1,6 @@ package xyz.mcutils.backend.repository.redis; -import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; import xyz.mcutils.backend.model.cache.CachedMinecraftServer; /** diff --git a/src/main/java/xyz/mcutils/backend/repository/redis/PlayerCacheRepository.java b/src/main/java/xyz/mcutils/backend/repository/redis/PlayerCacheRepository.java index 17d4377..929cdd7 100644 --- a/src/main/java/xyz/mcutils/backend/repository/redis/PlayerCacheRepository.java +++ b/src/main/java/xyz/mcutils/backend/repository/redis/PlayerCacheRepository.java @@ -1,8 +1,6 @@ package xyz.mcutils.backend.repository.redis; -import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; import xyz.mcutils.backend.model.cache.CachedPlayer; import java.util.UUID; diff --git a/src/main/java/xyz/mcutils/backend/repository/redis/PlayerNameCacheRepository.java b/src/main/java/xyz/mcutils/backend/repository/redis/PlayerNameCacheRepository.java index 26c66b4..0ff3a45 100644 --- a/src/main/java/xyz/mcutils/backend/repository/redis/PlayerNameCacheRepository.java +++ b/src/main/java/xyz/mcutils/backend/repository/redis/PlayerNameCacheRepository.java @@ -1,8 +1,6 @@ package xyz.mcutils.backend.repository.redis; -import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; import xyz.mcutils.backend.model.cache.CachedPlayerName; /** diff --git a/src/main/java/xyz/mcutils/backend/repository/redis/PlayerSkinPartCacheRepository.java b/src/main/java/xyz/mcutils/backend/repository/redis/PlayerSkinPartCacheRepository.java index da4560d..a27d281 100644 --- a/src/main/java/xyz/mcutils/backend/repository/redis/PlayerSkinPartCacheRepository.java +++ b/src/main/java/xyz/mcutils/backend/repository/redis/PlayerSkinPartCacheRepository.java @@ -1,8 +1,6 @@ package xyz.mcutils.backend.repository.redis; -import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; import xyz.mcutils.backend.model.cache.CachedPlayerSkinPart; /** diff --git a/src/main/java/xyz/mcutils/backend/service/MetricService.java b/src/main/java/xyz/mcutils/backend/service/MetricService.java index 464496d..0411947 100644 --- a/src/main/java/xyz/mcutils/backend/service/MetricService.java +++ b/src/main/java/xyz/mcutils/backend/service/MetricService.java @@ -7,6 +7,7 @@ import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import xyz.mcutils.backend.common.Timer; +import xyz.mcutils.backend.config.Config; import xyz.mcutils.backend.repository.mongo.MetricsRepository; import xyz.mcutils.backend.service.metric.Metric; import xyz.mcutils.backend.service.metric.metrics.*; @@ -55,17 +56,19 @@ public class MetricService { collectorEnabled.put(metric, metric.isCollector()); } - // Load the metrics from Redis - loadMetrics(); + if (Config.INSTANCE.isProduction()) { + // Load the metrics from Redis + loadMetrics(); - for (Map.Entry, Boolean> entry : collectorEnabled.entrySet()) { - entry.getKey().setCollector(entry.getValue()); + for (Map.Entry, Boolean> entry : collectorEnabled.entrySet()) { + entry.getKey().setCollector(entry.getValue()); + } + + Timer.scheduleRepeating(() -> { + saveMetrics(); + writeToInflux(); + }, saveInterval, saveInterval); } - - Timer.scheduleRepeating(() -> { - saveMetrics(); - writeToInflux(); - }, saveInterval, saveInterval); } /** 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 c36c37b..1322d98 100644 --- a/src/main/java/xyz/mcutils/backend/service/metric/Metric.java +++ b/src/main/java/xyz/mcutils/backend/service/metric/Metric.java @@ -9,7 +9,6 @@ import lombok.ToString; import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Transient; import org.springframework.data.mongodb.core.mapping.Document; -import xyz.mcutils.backend.service.MetricService; @AllArgsConstructor @Getter @Setter @ToString 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 632db1a..59ed4e0 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,6 +1,5 @@ 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; 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 index 2c92ae6..e62bc4f 100644 --- a/src/main/java/xyz/mcutils/backend/service/metric/metrics/UniquePlayerLookupsMetric.java +++ b/src/main/java/xyz/mcutils/backend/service/metric/metrics/UniquePlayerLookupsMetric.java @@ -1,6 +1,5 @@ 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; 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 index e5a3a9c..12921cc 100644 --- a/src/main/java/xyz/mcutils/backend/service/metric/metrics/UniqueServerLookupsMetric.java +++ b/src/main/java/xyz/mcutils/backend/service/metric/metrics/UniqueServerLookupsMetric.java @@ -1,6 +1,5 @@ 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; 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 616127e..40cf914 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,7 +1,6 @@ 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; 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 5eed6e6..2e64aa9 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,6 +1,5 @@ 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 { diff --git a/src/test/java/xyz/mcutils/backend/test/config/TestRedisConfig.java b/src/test/java/xyz/mcutils/backend/test/config/TestRedisConfig.java index a3bba95..d894928 100644 --- a/src/test/java/xyz/mcutils/backend/test/config/TestRedisConfig.java +++ b/src/test/java/xyz/mcutils/backend/test/config/TestRedisConfig.java @@ -4,8 +4,6 @@ import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import lombok.NonNull; import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; -import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; import redis.embedded.RedisServer; import java.io.IOException; diff --git a/src/test/java/xyz/mcutils/backend/test/tests/PlayerControllerTests.java b/src/test/java/xyz/mcutils/backend/test/tests/PlayerControllerTests.java index 93bcc8c..67c1fb3 100644 --- a/src/test/java/xyz/mcutils/backend/test/tests/PlayerControllerTests.java +++ b/src/test/java/xyz/mcutils/backend/test/tests/PlayerControllerTests.java @@ -1,7 +1,10 @@ package xyz.mcutils.backend.test.tests; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo; +import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; @@ -15,6 +18,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest(classes = { TestRedisConfig.class }) @AutoConfigureMockMvc +@TestInstance(TestInstance.Lifecycle.PER_CLASS) class PlayerControllerTests { private final String testPlayerUuid = "eeab5f8a-18dd-4d58-af78-2b3c4543da48"; diff --git a/src/test/java/xyz/mcutils/backend/test/tests/ServerControllerTests.java b/src/test/java/xyz/mcutils/backend/test/tests/ServerControllerTests.java index 27cd0bc..909c839 100644 --- a/src/test/java/xyz/mcutils/backend/test/tests/ServerControllerTests.java +++ b/src/test/java/xyz/mcutils/backend/test/tests/ServerControllerTests.java @@ -1,7 +1,10 @@ package xyz.mcutils.backend.test.tests; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo; +import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; @@ -14,6 +17,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest(classes = { TestRedisConfig.class }) @AutoConfigureMockMvc +@TestInstance(TestInstance.Lifecycle.PER_CLASS) class ServerControllerTests { private final String testServer = "play.hypixel.net";