diff --git a/Plan/src/main/java/com/djrapitops/plan/data/store/mutators/TPSMutator.java b/Plan/src/main/java/com/djrapitops/plan/data/store/mutators/TPSMutator.java index 302d8e914..8af6fa550 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/store/mutators/TPSMutator.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/store/mutators/TPSMutator.java @@ -4,6 +4,7 @@ import com.djrapitops.plan.data.container.TPS; import com.djrapitops.plan.data.store.containers.DataContainer; import com.djrapitops.plan.data.store.keys.ServerKeys; import com.djrapitops.plan.system.settings.Settings; +import com.djrapitops.plan.utilities.comparators.TPSComparator; import com.djrapitops.plan.utilities.html.graphs.line.Point; import com.djrapitops.plugin.api.TimeAmount; @@ -89,6 +90,7 @@ public class TPSMutator { public long serverDownTime() { long lastDate = -1; long downTime = 0; + tpsData.sort(new TPSComparator()); for (TPS tps : tpsData) { long date = tps.getDate(); if (lastDate == -1) { @@ -110,6 +112,7 @@ public class TPSMutator { long lastDate = -1; int lastPlayers = 0; long idleTime = 0; + tpsData.sort(new TPSComparator()); for (TPS tps : tpsData) { long date = tps.getDate(); int players = tps.getPlayers(); diff --git a/Plan/src/test/java/com/djrapitops/plan/data/store/mutators/TPSMutatorTest.java b/Plan/src/test/java/com/djrapitops/plan/data/store/mutators/TPSMutatorTest.java new file mode 100644 index 000000000..33d93c5e8 --- /dev/null +++ b/Plan/src/test/java/com/djrapitops/plan/data/store/mutators/TPSMutatorTest.java @@ -0,0 +1,138 @@ +package com.djrapitops.plan.data.store.mutators; + +import com.djrapitops.plan.data.container.TPS; +import com.djrapitops.plan.data.container.builders.TPSBuilder; +import com.djrapitops.plugin.api.TimeAmount; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +import static org.junit.Assert.*; + +/** + * Tests for {@link TPSMutator} + * + * @author Rsl1122 + */ +public class TPSMutatorTest { + + private List testData; + private long time; + + @Before + public void setUp() { + testData = new ArrayList<>(); + + time = System.currentTimeMillis(); + long twoMonthsAgo = time - TimeAmount.MONTH.ms() * 2L; + + for (long date = twoMonthsAgo; date < time; date += TimeAmount.MINUTE.ms()) { + testData.add( + TPSBuilder.get().date(date) + .tps(0.0) + .playersOnline(0) + .usedCPU(0.0) + .usedMemory(0) + .entities(0) + .chunksLoaded(0) + .toTPS() + ); + } + } + + @Test + public void noDownTimeIsCorrect() { + long expected = 0; + long result = new TPSMutator(testData).serverDownTime(); + assertEquals(expected, result); + } + + @Test + public void noDownTimeOnSingleEntry() { + long expected = 0; + long result = new TPSMutator(Collections.singletonList( + TPSBuilder.get().date(time - TimeAmount.DAY.ms()) + .tps(0.0) + .playersOnline(0) + .usedCPU(0.0) + .usedMemory(0) + .entities(0) + .chunksLoaded(0) + .toTPS() + )).serverDownTime(); + assertEquals(expected, result); + } + + @Test + public void fullDownTime() { + long periodLength = TimeAmount.MINUTE.ms() * 5L; + long expected = TimeAmount.MONTH.ms() * 2L - periodLength; + + TPSMutator tpsMutator = new TPSMutator(testData.stream() + .filter(tps -> (tps.getDate() - time) % (periodLength) == 0) + .collect(Collectors.toList())); + assertFalse(tpsMutator.all().isEmpty()); + assertNotEquals(testData, tpsMutator.all()); + + long result = tpsMutator.serverDownTime(); + assertEquals(expected, result); + } + + @Test + public void filteredFullMonthDownTime() { + long periodLength = TimeAmount.MINUTE.ms() * 5L; + long expected = TimeAmount.MONTH.ms() - periodLength; + + long monthAgo = time - TimeAmount.MONTH.ms(); + TPSMutator tpsMutator = new TPSMutator(testData.stream() + .filter(tps -> (tps.getDate() - time) % (periodLength) == 0) + .collect(Collectors.toList())) + .filterDataBetween(monthAgo, time); + + assertFalse(tpsMutator.all().isEmpty()); + assertNotEquals(testData, tpsMutator.filterDataBetween(monthAgo, time).all()); + + long result = tpsMutator.serverDownTime(); + assertEquals(expected, result); + } + + @Test + public void filteredFullMonthDownTimeWhenRandomOrder() { + long periodLength = TimeAmount.MINUTE.ms() * 5L; + long expected = TimeAmount.MONTH.ms() - periodLength; + + List randomOrder = testData; + Collections.shuffle(randomOrder); + long monthAgo = time - TimeAmount.MONTH.ms(); + TPSMutator tpsMutator = new TPSMutator(randomOrder.stream() + .filter(tps -> (tps.getDate() - time) % (periodLength) == 0) + .collect(Collectors.toList())) + .filterDataBetween(monthAgo, time); + + assertFalse(tpsMutator.all().isEmpty()); + assertNotEquals(randomOrder, tpsMutator.filterDataBetween(monthAgo, time).all()); + + long result = tpsMutator.serverDownTime(); + assertEquals(expected, result); + } + + @Test + public void filterWorksCorrectly() { + long monthAgo = time - TimeAmount.MONTH.ms(); + List filtered = new TPSMutator(testData).filterDataBetween(monthAgo, time).all(); + + for (TPS tps : filtered) { + long date = tps.getDate(); + if (date < monthAgo) { + fail("Data from over month ago was present"); + } + if (date > time) { + fail("Data from after 'time' was present"); + } + } + } +} \ No newline at end of file