From b52782298eaf3538c5a82b34f0078f0cc10b10cc Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Tue, 30 Jan 2018 13:11:23 +0200 Subject: [PATCH] Reduced database stress by Network and Players pages. (#499) --- .../databases/operation/FetchOperations.java | 2 + .../databases/sql/operation/SQLFetchOps.java | 9 +++ .../databases/sql/tables/ServerTable.java | 6 +- .../databases/sql/tables/SessionsTable.java | 76 +++++++++++++++---- .../databases/sql/tables/UserInfoTable.java | 20 ++--- .../databases/sql/tables/UsersTable.java | 19 +++++ .../webserver/pages/parsing/NetworkPage.java | 2 +- .../plan/system/BukkitSystemTest.java | 3 + .../plan/system/BungeeSystemTest.java | 2 + 9 files changed, 108 insertions(+), 31 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/operation/FetchOperations.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/operation/FetchOperations.java index d768c2ca8..506a50760 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/operation/FetchOperations.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/operation/FetchOperations.java @@ -82,4 +82,6 @@ public interface FetchOperations { List getWebUsers() throws DBException; Map getServerNamesByID() throws DBException; + + Map>> getSessionsInLastMonth() throws DBException; } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java index adbb86358..c68fdc6fe 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java @@ -254,6 +254,15 @@ public class SQLFetchOps extends SQLOps implements FetchOperations { } } + @Override + public Map>> getSessionsInLastMonth() throws DBException { + try { + return sessionsTable.getSessionInLastMonth(); + } catch (SQLException e) { + throw SQLErrorUtil.getExceptionFor(e); + } + } + @Override public Set getWorldNames(UUID serverUuid) throws DBException { try { diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/ServerTable.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/ServerTable.java index b520fc7e6..6968801bc 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/ServerTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/ServerTable.java @@ -47,8 +47,8 @@ public class ServerTable extends Table { public ServerTable(SQLDB db) { super("plan_servers", db); - statementSelectServerID = "(" + Select.from(tableName, tableName + "." + columnServerID).where(columnServerUUID + "=?").toString() + " LIMIT 1)"; - statementSelectServerNameID = "(" + Select.from(tableName, tableName + "." + columnServerName).where(columnServerID + "=?").toString() + " LIMIT 1)"; + statementSelectServerID = "(" + Select.from(tableName, tableName + "." + columnServerID).where(tableName + "." + columnServerUUID + "=?").toString() + " LIMIT 1)"; + statementSelectServerNameID = "(" + Select.from(tableName, tableName + "." + columnServerName).where(tableName + "." + columnServerID + "=?").toString() + " LIMIT 1)"; insertStatement = Insert.values(tableName, columnServerUUID, columnServerName, @@ -229,7 +229,7 @@ public class ServerTable extends Table { }); } - public Map getServerUuids() throws SQLException { + public Map getServerUUIDsByID() throws SQLException { String sql = Select.from(tableName, columnServerID, columnServerUUID) .toString(); diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/SessionsTable.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/SessionsTable.java index fee0c0998..505b7fb1b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/SessionsTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/SessionsTable.java @@ -10,6 +10,8 @@ import com.djrapitops.plan.system.database.databases.sql.statements.Select; import com.djrapitops.plan.system.database.databases.sql.statements.Sql; import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser; import com.djrapitops.plan.system.info.server.ServerInfo; +import com.djrapitops.plan.utilities.MiscUtils; +import com.djrapitops.plugin.api.TimeAmount; import com.djrapitops.plugin.utilities.Verify; import java.sql.PreparedStatement; @@ -152,7 +154,7 @@ public class SessionsTable extends UserIDTable { * @throws SQLException DB Error */ private Map> getSessionInformation(UUID uuid) throws SQLException { - Map serverUUIDs = serverTable.getServerUuids(); + Map serverUUIDs = serverTable.getServerUUIDsByID(); String sql = Select.from(tableName, "*") .where(columnUserID + "=" + usersTable.statementSelectID) .toString(); @@ -544,29 +546,26 @@ public class SessionsTable extends UserIDTable { } public Map>> getAllSessions(boolean getKillsAndWorldTimes) throws SQLException { - String usersIDColumn = usersTable + "." + usersTable.getColumnID(); - String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid"; - String serverIDColumn = serverTable + "." + serverTable.getColumnID(); - String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid"; + Map uuidsByID = usersTable.getUUIDsByID(); + Map serverUUIDsByID = serverTable.getServerUUIDsByID(); + String sql = "SELECT " + - tableName + "." + columnID + ", " + + columnID + ", " + + columnUserID + ", " + + columnServerID + ", " + columnSessionStart + ", " + columnSessionEnd + ", " + columnDeaths + ", " + - columnMobKills + ", " + - usersUUIDColumn + ", " + - serverUUIDColumn + - " FROM " + tableName + - " INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID + - " INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID; + columnMobKills + + " FROM " + tableName; return query(new QueryAllStatement>>>(sql, 20000) { @Override public Map>> processResults(ResultSet set) throws SQLException { Map>> map = new HashMap<>(); while (set.next()) { - UUID serverUUID = UUID.fromString(set.getString("s_uuid")); - UUID uuid = UUID.fromString(set.getString("uuid")); + UUID serverUUID = serverUUIDsByID.get(set.getInt(columnServerID)); + UUID uuid = uuidsByID.get(set.getInt(columnUserID)); Map> sessionsByUser = map.getOrDefault(serverUUID, new HashMap<>()); List sessions = sessionsByUser.getOrDefault(uuid, new ArrayList<>()); @@ -678,4 +677,53 @@ public class SessionsTable extends UserIDTable { String getcolumnServerID() { return columnServerID; } + + public Map>> getSessionInLastMonth() throws SQLException { + Map uuidsByID = usersTable.getUUIDsByID(); + Map serverUUIDsByID = serverTable.getServerUUIDsByID(); + + String sql = "SELECT " + + columnID + ", " + + columnUserID + ", " + + columnServerID + ", " + + columnSessionStart + ", " + + columnSessionEnd + ", " + + columnDeaths + ", " + + columnMobKills + + " FROM " + tableName + + " WHERE " + columnSessionStart + ">?"; + + return query(new QueryStatement>>>(sql, 20000) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setLong(1, MiscUtils.getTime() - TimeAmount.MONTH.ms()); + } + + @Override + public Map>> processResults(ResultSet set) throws SQLException { + Map>> map = new HashMap<>(); + while (set.next()) { + UUID serverUUID = serverUUIDsByID.get(set.getInt(columnServerID)); + UUID uuid = uuidsByID.get(set.getInt(columnUserID)); + + Map> sessionsByUser = map.getOrDefault(serverUUID, new HashMap<>()); + List sessions = sessionsByUser.getOrDefault(uuid, new ArrayList<>()); + + long start = set.getLong(columnSessionStart); + long end = set.getLong(columnSessionEnd); + + int deaths = set.getInt(columnDeaths); + int mobKills = set.getInt(columnMobKills); + int id = set.getInt(columnID); + + Session session = new Session(id, start, end, mobKills, deaths); + sessions.add(session); + + sessionsByUser.put(uuid, sessions); + map.put(serverUUID, sessionsByUser); + } + return map; + } + }); + } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/UserInfoTable.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/UserInfoTable.java index 6f3a9c1eb..6fcc19cd7 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/UserInfoTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/UserInfoTable.java @@ -185,17 +185,10 @@ public class UserInfoTable extends UserIDTable { } public List getServerUserInfo(UUID serverUUID) throws SQLException { - String usersIDColumn = usersTable + "." + usersTable.getColumnID(); - String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid"; - String usersNameColumn = usersTable + "." + usersTable.getColumnName() + " as name"; - String sql = "SELECT " + - tableName + "." + columnRegistered + ", " + - columnOP + ", " + - columnBanned + ", " + - usersNameColumn + ", " + - usersUUIDColumn + - " FROM " + tableName + - " LEFT JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID + + Map playerNames = usersTable.getPlayerNames(); + Map uuidsByID = usersTable.getUUIDsByID(); + + String sql = "SELECT * FROM " + tableName + " WHERE " + columnServerID + "=" + serverTable.statementSelectServerID; return query(new QueryStatement>(sql, 20000) { @@ -211,8 +204,9 @@ public class UserInfoTable extends UserIDTable { long registered = set.getLong(columnRegistered); boolean opped = set.getBoolean(columnOP); boolean banned = set.getBoolean(columnBanned); - String name = set.getString("name"); - UUID uuid = UUID.fromString(set.getString("uuid")); + int userId = set.getInt(columnUserID); + UUID uuid = uuidsByID.get(userId); + String name = playerNames.getOrDefault(uuid, "Unknown"); UserInfo info = new UserInfo(uuid, name, registered, opped, banned); if (!userInfo.contains(info)) { userInfo.add(info); diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/UsersTable.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/UsersTable.java index d2073fbf6..34bc6e202 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/UsersTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/UsersTable.java @@ -433,4 +433,23 @@ public class UsersTable extends UserIDTable { } }); } + + public Map getUUIDsByID() throws SQLException { + String sql = Select.from(tableName, columnID, columnUUID).toString(); + + return query(new QueryAllStatement>(sql, 20000) { + @Override + public Map processResults(ResultSet set) throws SQLException { + Map uuidsByID = new TreeMap<>(); + + while (set.next()) { + int id = set.getInt(columnID); + UUID uuid = UUID.fromString(set.getString(columnUUID)); + uuidsByID.put(id, uuid); + } + + return uuidsByID; + } + }); + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/parsing/NetworkPage.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/parsing/NetworkPage.java index 2f88f48e9..b21ecad3b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/parsing/NetworkPage.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/parsing/NetworkPage.java @@ -74,7 +74,7 @@ public class NetworkPage extends Page { } private void uniquePlayers(long now, Database db) throws DBException { - Map>> allSessions = db.fetch().getSessionsWithNoExtras(); + Map>> allSessions = db.fetch().getSessionsInLastMonth(); Map> userSessions = AnalysisUtils.sortSessionsByUser(allSessions); long dayAgo = now - TimeAmount.DAY.ms(); diff --git a/Plan/src/test/java/com/djrapitops/plan/system/BukkitSystemTest.java b/Plan/src/test/java/com/djrapitops/plan/system/BukkitSystemTest.java index f0f8f2cf2..b23db4562 100644 --- a/Plan/src/test/java/com/djrapitops/plan/system/BukkitSystemTest.java +++ b/Plan/src/test/java/com/djrapitops/plan/system/BukkitSystemTest.java @@ -6,6 +6,7 @@ package com.djrapitops.plan.system; import com.djrapitops.plan.Plan; import com.djrapitops.plan.api.exceptions.EnableException; +import com.djrapitops.plan.system.settings.Settings; import org.junit.After; import org.junit.BeforeClass; import org.junit.ClassRule; @@ -48,6 +49,8 @@ public class BukkitSystemTest { @Test public void testEnable() throws EnableException { + Settings.WEBSERVER_PORT.setTemporaryValue(9000); + bukkitSystem = new BukkitSystem(planMock); bukkitSystem.enable(); } diff --git a/Plan/src/test/java/com/djrapitops/plan/system/BungeeSystemTest.java b/Plan/src/test/java/com/djrapitops/plan/system/BungeeSystemTest.java index e213cd692..13bcdfbff 100644 --- a/Plan/src/test/java/com/djrapitops/plan/system/BungeeSystemTest.java +++ b/Plan/src/test/java/com/djrapitops/plan/system/BungeeSystemTest.java @@ -52,6 +52,7 @@ public class BungeeSystemTest { public void testEnable() throws EnableException { bungeeSystem = new BungeeSystem(planMock); + Settings.WEBSERVER_PORT.setTemporaryValue(9000); Settings.BUNGEE_IP.setTemporaryValue("8.8.8.8"); Settings.DB_TYPE.setTemporaryValue("sqlite"); bungeeSystem.setDatabaseSystem(new BukkitDBSystem()); @@ -66,6 +67,7 @@ public class BungeeSystemTest { bungeeSystem = new BungeeSystem(planMock); + Settings.WEBSERVER_PORT.setTemporaryValue(9000); Settings.DB_TYPE.setTemporaryValue("sqlite"); bungeeSystem.setDatabaseSystem(new BukkitDBSystem());