From 9f1479a65cdb36571c4069b8020f1a260592fb77 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sat, 16 Feb 2019 13:59:36 +0200 Subject: [PATCH] Refactored TPSTable#getTPSData to a query --- .../containers/ServerContainerQuery.java | 3 +- .../db/access/queries/objects/TPSQueries.java | 80 +++++++++++++++++++ .../plan/db/sql/tables/TPSTable.java | 35 -------- .../databases/sql/operation/SQLFetchOps.java | 2 +- .../com/djrapitops/plan/db/CommonDBTest.java | 4 +- 5 files changed, 84 insertions(+), 40 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/TPSQueries.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/ServerContainerQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/ServerContainerQuery.java index cd7d4d4ae..285e4a287 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/ServerContainerQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/ServerContainerQuery.java @@ -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 { 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) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/TPSQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/TPSQueries.java new file mode 100644 index 000000000..c0be4dcc0 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/TPSQueries.java @@ -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 . + */ +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> fetchTPSDataOfServer(UUID serverUUID) { + String sql = Select.all(TABLE_NAME) + .where(SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID) + .toString(); + + return new QueryStatement>(sql, 50000) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, serverUUID.toString()); + } + + @Override + public List processResults(ResultSet set) throws SQLException { + List 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; + } + }; + } + +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/TPSTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/TPSTable.java index 7e918177a..6a2ab910e 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/TPSTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/TPSTable.java @@ -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 getTPSData(UUID serverUUID) { - String sql = Select.all(tableName) - .where(SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID) - .toString(); - - return query(new QueryStatement>(sql, 50000) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setString(1, serverUUID.toString()); - } - - @Override - public List processResults(ResultSet set) throws SQLException { - List 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 getNetworkOnlineData() { Optional proxyInfo = db.query(ServerQueries.fetchProxyServerInformation()); if (!proxyInfo.isPresent()) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java index e0f26b64b..031778c18 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java @@ -95,7 +95,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations { @Override public List getTPSData(UUID serverUUID) { - return tpsTable.getTPSData(serverUUID); + return db.query(TPSQueries.fetchTPSDataOfServer(serverUUID)); } @Override diff --git a/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java b/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java index cb27b139b..a67505c42 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java @@ -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 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() {