Fixed TPSMutator calculating downtime incorrectly #672

This commit is contained in:
Rsl1122 2018-08-05 10:35:24 +03:00
parent 128f788232
commit d5965ffdc1
2 changed files with 141 additions and 0 deletions

View File

@ -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();

View File

@ -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<TPS> 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<TPS> 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<TPS> 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");
}
}
}
}