Refactored TPSTable#getTPSData to a query

This commit is contained in:
Rsl1122 2019-02-16 13:59:36 +02:00
parent fcdc281219
commit 9f1479a65c
5 changed files with 84 additions and 40 deletions

View File

@ -26,6 +26,7 @@ import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.queries.OptionalFetchQueries;
import com.djrapitops.plan.db.access.queries.ServerAggregateQueries;
import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
import com.djrapitops.plan.db.access.queries.objects.TPSQueries;
import com.djrapitops.plan.db.access.queries.objects.WorldTimesQueries;
import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.info.server.Server;
@ -67,7 +68,7 @@ public class ServerContainerQuery implements Query<ServerContainer> {
container.putCachingSupplier(ServerKeys.PLAYERS, () -> db.query(new ServerPlayerContainersQuery(serverUUID)));
container.putSupplier(ServerKeys.PLAYER_COUNT, () -> container.getUnsafe(ServerKeys.PLAYERS).size());
container.putCachingSupplier(ServerKeys.TPS, () -> db.getTpsTable().getTPSData(serverUUID));
container.putCachingSupplier(ServerKeys.TPS, () -> db.query(TPSQueries.fetchTPSDataOfServer(serverUUID)));
container.putCachingSupplier(ServerKeys.PING, () -> PlayersMutator.forContainer(container).pings());
container.putCachingSupplier(ServerKeys.ALL_TIME_PEAK_PLAYERS, () ->
db.query(OptionalFetchQueries.fetchAllTimePeakPlayerCount(serverUUID)).orElse(null)

View File

@ -0,0 +1,80 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.db.access.queries.objects;
import com.djrapitops.plan.data.container.TPS;
import com.djrapitops.plan.data.container.builders.TPSBuilder;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.parsing.Select;
import com.djrapitops.plan.db.sql.tables.ServerTable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static com.djrapitops.plan.db.sql.tables.TPSTable.*;
/**
* Queries for {@link com.djrapitops.plan.data.container.TPS} objects.
*
* @author Rsl1122
*/
public class TPSQueries {
private TPSQueries() {
/* Static method class */
}
public static Query<List<TPS>> fetchTPSDataOfServer(UUID serverUUID) {
String sql = Select.all(TABLE_NAME)
.where(SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID)
.toString();
return new QueryStatement<List<TPS>>(sql, 50000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString());
}
@Override
public List<TPS> processResults(ResultSet set) throws SQLException {
List<TPS> data = new ArrayList<>();
while (set.next()) {
TPS tps = TPSBuilder.get()
.date(set.getLong(DATE))
.tps(set.getDouble(TPS))
.playersOnline(set.getInt(PLAYERS_ONLINE))
.usedCPU(set.getDouble(CPU_USAGE))
.usedMemory(set.getLong(RAM_USAGE))
.entities(set.getInt(ENTITIES))
.chunksLoaded(set.getInt(CHUNKS))
.freeDiskSpace(set.getLong(FREE_DISK))
.toTPS();
data.add(tps);
}
return data;
}
};
}
}

View File

@ -24,7 +24,6 @@ import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
import com.djrapitops.plan.db.sql.parsing.CreateTableParser;
import com.djrapitops.plan.db.sql.parsing.Select;
import com.djrapitops.plan.db.sql.parsing.Sql;
import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plugin.api.TimeAmount;
@ -89,40 +88,6 @@ public class TPSTable extends Table {
.toString();
}
public List<TPS> getTPSData(UUID serverUUID) {
String sql = Select.all(tableName)
.where(SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID)
.toString();
return query(new QueryStatement<List<TPS>>(sql, 50000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString());
}
@Override
public List<TPS> processResults(ResultSet set) throws SQLException {
List<TPS> data = new ArrayList<>();
while (set.next()) {
TPS tps = TPSBuilder.get()
.date(set.getLong(DATE))
.tps(set.getDouble(TPS))
.playersOnline(set.getInt(PLAYERS_ONLINE))
.usedCPU(set.getDouble(CPU_USAGE))
.usedMemory(set.getLong(RAM_USAGE))
.entities(set.getInt(ENTITIES))
.chunksLoaded(set.getInt(CHUNKS))
.freeDiskSpace(set.getLong(FREE_DISK))
.toTPS();
data.add(tps);
}
return data;
}
});
}
public List<TPS> getNetworkOnlineData() {
Optional<Server> proxyInfo = db.query(ServerQueries.fetchProxyServerInformation());
if (!proxyInfo.isPresent()) {

View File

@ -95,7 +95,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
@Override
public List<TPS> getTPSData(UUID serverUUID) {
return tpsTable.getTPSData(serverUUID);
return db.query(TPSQueries.fetchTPSDataOfServer(serverUUID));
}
@Override

View File

@ -45,7 +45,6 @@ import com.djrapitops.plan.db.access.transactions.init.CleanTransaction;
import com.djrapitops.plan.db.access.transactions.init.CreateIndexTransaction;
import com.djrapitops.plan.db.access.transactions.init.CreateTablesTransaction;
import com.djrapitops.plan.db.patches.Patch;
import com.djrapitops.plan.db.sql.tables.TPSTable;
import com.djrapitops.plan.system.PlanSystem;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.Server;
@ -217,7 +216,6 @@ public abstract class CommonDBTest {
@Test
public void testTPSSaving() throws Exception {
TPSTable tpsTable = db.getTpsTable();
Random r = new Random();
List<TPS> expected = new ArrayList<>();
@ -232,7 +230,7 @@ public abstract class CommonDBTest {
commitTest();
assertEquals(expected, tpsTable.getTPSData(serverUUID));
assertEquals(expected, db.query(TPSQueries.fetchTPSDataOfServer(serverUUID)));
}
private void saveUserOne() {