Fixed peak TPS query

This commit is contained in:
Rsl1122 2019-02-18 16:48:12 +02:00
parent c60003cda0
commit bbb158ddc5
4 changed files with 29 additions and 6 deletions

View File

@ -120,16 +120,23 @@ public class TPSQueries {
} }
public static Query<Optional<DateObj<Integer>>> fetchPeakPlayerCount(UUID serverUUID, long afterDate) { public static Query<Optional<DateObj<Integer>>> fetchPeakPlayerCount(UUID serverUUID, long afterDate) {
String sql = "SELECT " + DATE + ", MAX(" + PLAYERS_ONLINE + ") as max FROM " + TABLE_NAME + String subQuery = "(" + SELECT + "MAX(" + PLAYERS_ONLINE + ")" + FROM + TABLE_NAME + WHERE + SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID +
" WHERE " + SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID + AND + DATE + ">= ?)";
" AND " + DATE + ">= ?" + String sql = SELECT +
" GROUP BY " + SERVER_ID; DATE + ", " + PLAYERS_ONLINE +
FROM + TABLE_NAME +
WHERE + SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID +
AND + DATE + ">= ?" +
AND + PLAYERS_ONLINE + "=" + subQuery +
ORDER_BY + DATE + " DESC LIMIT 1";
return new QueryStatement<Optional<DateObj<Integer>>>(sql) { return new QueryStatement<Optional<DateObj<Integer>>>(sql) {
@Override @Override
public void prepare(PreparedStatement statement) throws SQLException { public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString()); statement.setString(1, serverUUID.toString());
statement.setLong(2, afterDate); statement.setLong(2, afterDate);
statement.setString(3, serverUUID.toString());
statement.setLong(4, afterDate);
} }
@Override @Override
@ -146,6 +153,6 @@ public class TPSQueries {
} }
public static Query<Optional<DateObj<Integer>>> fetchAllTimePeakPlayerCount(UUID serverUUID) { public static Query<Optional<DateObj<Integer>>> fetchAllTimePeakPlayerCount(UUID serverUUID) {
return db -> db.query(fetchPeakPlayerCount(serverUUID, 0)); return fetchPeakPlayerCount(serverUUID, 0);
} }
} }

View File

@ -27,6 +27,7 @@ public class Sql {
public static final String FROM = " FROM "; public static final String FROM = " FROM ";
public static final String WHERE = " WHERE "; public static final String WHERE = " WHERE ";
public static final String GROUP_BY = " GROUP BY "; public static final String GROUP_BY = " GROUP BY ";
public static final String ORDER_BY = " ORDER BY ";
public static final String INNER_JOIN = " INNER JOIN "; public static final String INNER_JOIN = " INNER JOIN ";
public static final String AND = " AND "; public static final String AND = " AND ";

View File

@ -72,6 +72,7 @@ import java.lang.management.OperatingSystemMXBean;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -994,4 +995,18 @@ public abstract class CommonDBTest {
db.executeTransaction(new CreateIndexTransaction(db.getType())); db.executeTransaction(new CreateIndexTransaction(db.getType()));
} }
@Test
public void playerMaxPeakIsCorrect() {
List<TPS> tpsData = RandomData.randomTPS();
for (TPS tps : tpsData) {
db.executeTransaction(new TPSStoreTransaction(serverUUID, Collections.singletonList(tps)));
}
tpsData.sort(Comparator.comparingInt(TPS::getPlayers));
int expected = tpsData.get(tpsData.size() - 1).getPlayers();
int actual = db.query(TPSQueries.fetchAllTimePeakPlayerCount(serverUUID)).map(DateObj::getValue).orElse(-1);
assertEquals("Wrong return value. " + tpsData.stream().map(TPS::getPlayers).collect(Collectors.toList()).toString(), expected, actual);
}
} }

View File

@ -55,7 +55,7 @@ public class RandomData {
List<TPS> test = new ArrayList<>(); List<TPS> test = new ArrayList<>();
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
int randInt = r.nextInt(); int randInt = r.nextInt();
long randLong = r.nextLong(); long randLong = Math.abs(r.nextLong());
test.add(new TPS(randLong, randLong, randInt, randLong, randLong, randInt, randInt, randLong)); test.add(new TPS(randLong, randLong, randInt, randLong, randLong, randInt, randInt, randLong));
} }
return test; return test;