Reduced database stress by Network and Players pages. (#499)

This commit is contained in:
Rsl1122 2018-01-30 13:11:23 +02:00
parent 0f372b8db8
commit b52782298e
9 changed files with 108 additions and 31 deletions

View File

@ -82,4 +82,6 @@ public interface FetchOperations {
List<WebUser> getWebUsers() throws DBException; List<WebUser> getWebUsers() throws DBException;
Map<Integer, String> getServerNamesByID() throws DBException; Map<Integer, String> getServerNamesByID() throws DBException;
Map<UUID, Map<UUID, List<Session>>> getSessionsInLastMonth() throws DBException;
} }

View File

@ -254,6 +254,15 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
} }
} }
@Override
public Map<UUID, Map<UUID, List<Session>>> getSessionsInLastMonth() throws DBException {
try {
return sessionsTable.getSessionInLastMonth();
} catch (SQLException e) {
throw SQLErrorUtil.getExceptionFor(e);
}
}
@Override @Override
public Set<String> getWorldNames(UUID serverUuid) throws DBException { public Set<String> getWorldNames(UUID serverUuid) throws DBException {
try { try {

View File

@ -47,8 +47,8 @@ public class ServerTable extends Table {
public ServerTable(SQLDB db) { public ServerTable(SQLDB db) {
super("plan_servers", db); super("plan_servers", db);
statementSelectServerID = "(" + Select.from(tableName, tableName + "." + columnServerID).where(columnServerUUID + "=?").toString() + " LIMIT 1)"; statementSelectServerID = "(" + Select.from(tableName, tableName + "." + columnServerID).where(tableName + "." + columnServerUUID + "=?").toString() + " LIMIT 1)";
statementSelectServerNameID = "(" + Select.from(tableName, tableName + "." + columnServerName).where(columnServerID + "=?").toString() + " LIMIT 1)"; statementSelectServerNameID = "(" + Select.from(tableName, tableName + "." + columnServerName).where(tableName + "." + columnServerID + "=?").toString() + " LIMIT 1)";
insertStatement = Insert.values(tableName, insertStatement = Insert.values(tableName,
columnServerUUID, columnServerUUID,
columnServerName, columnServerName,
@ -229,7 +229,7 @@ public class ServerTable extends Table {
}); });
} }
public Map<Integer, UUID> getServerUuids() throws SQLException { public Map<Integer, UUID> getServerUUIDsByID() throws SQLException {
String sql = Select.from(tableName, String sql = Select.from(tableName,
columnServerID, columnServerUUID) columnServerID, columnServerUUID)
.toString(); .toString();

View File

@ -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.Sql;
import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser; import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser;
import com.djrapitops.plan.system.info.server.ServerInfo; 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 com.djrapitops.plugin.utilities.Verify;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -152,7 +154,7 @@ public class SessionsTable extends UserIDTable {
* @throws SQLException DB Error * @throws SQLException DB Error
*/ */
private Map<UUID, List<Session>> getSessionInformation(UUID uuid) throws SQLException { private Map<UUID, List<Session>> getSessionInformation(UUID uuid) throws SQLException {
Map<Integer, UUID> serverUUIDs = serverTable.getServerUuids(); Map<Integer, UUID> serverUUIDs = serverTable.getServerUUIDsByID();
String sql = Select.from(tableName, "*") String sql = Select.from(tableName, "*")
.where(columnUserID + "=" + usersTable.statementSelectID) .where(columnUserID + "=" + usersTable.statementSelectID)
.toString(); .toString();
@ -544,29 +546,26 @@ public class SessionsTable extends UserIDTable {
} }
public Map<UUID, Map<UUID, List<Session>>> getAllSessions(boolean getKillsAndWorldTimes) throws SQLException { public Map<UUID, Map<UUID, List<Session>>> getAllSessions(boolean getKillsAndWorldTimes) throws SQLException {
String usersIDColumn = usersTable + "." + usersTable.getColumnID(); Map<Integer, UUID> uuidsByID = usersTable.getUUIDsByID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid"; Map<Integer, UUID> serverUUIDsByID = serverTable.getServerUUIDsByID();
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
String sql = "SELECT " + String sql = "SELECT " +
tableName + "." + columnID + ", " + columnID + ", " +
columnUserID + ", " +
columnServerID + ", " +
columnSessionStart + ", " + columnSessionStart + ", " +
columnSessionEnd + ", " + columnSessionEnd + ", " +
columnDeaths + ", " + columnDeaths + ", " +
columnMobKills + ", " + columnMobKills +
usersUUIDColumn + ", " + " FROM " + tableName;
serverUUIDColumn +
" FROM " + tableName +
" INNER JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID;
return query(new QueryAllStatement<Map<UUID, Map<UUID, List<Session>>>>(sql, 20000) { return query(new QueryAllStatement<Map<UUID, Map<UUID, List<Session>>>>(sql, 20000) {
@Override @Override
public Map<UUID, Map<UUID, List<Session>>> processResults(ResultSet set) throws SQLException { public Map<UUID, Map<UUID, List<Session>>> processResults(ResultSet set) throws SQLException {
Map<UUID, Map<UUID, List<Session>>> map = new HashMap<>(); Map<UUID, Map<UUID, List<Session>>> map = new HashMap<>();
while (set.next()) { while (set.next()) {
UUID serverUUID = UUID.fromString(set.getString("s_uuid")); UUID serverUUID = serverUUIDsByID.get(set.getInt(columnServerID));
UUID uuid = UUID.fromString(set.getString("uuid")); UUID uuid = uuidsByID.get(set.getInt(columnUserID));
Map<UUID, List<Session>> sessionsByUser = map.getOrDefault(serverUUID, new HashMap<>()); Map<UUID, List<Session>> sessionsByUser = map.getOrDefault(serverUUID, new HashMap<>());
List<Session> sessions = sessionsByUser.getOrDefault(uuid, new ArrayList<>()); List<Session> sessions = sessionsByUser.getOrDefault(uuid, new ArrayList<>());
@ -678,4 +677,53 @@ public class SessionsTable extends UserIDTable {
String getcolumnServerID() { String getcolumnServerID() {
return columnServerID; return columnServerID;
} }
public Map<UUID, Map<UUID, List<Session>>> getSessionInLastMonth() throws SQLException {
Map<Integer, UUID> uuidsByID = usersTable.getUUIDsByID();
Map<Integer, UUID> serverUUIDsByID = serverTable.getServerUUIDsByID();
String sql = "SELECT " +
columnID + ", " +
columnUserID + ", " +
columnServerID + ", " +
columnSessionStart + ", " +
columnSessionEnd + ", " +
columnDeaths + ", " +
columnMobKills +
" FROM " + tableName +
" WHERE " + columnSessionStart + ">?";
return query(new QueryStatement<Map<UUID, Map<UUID, List<Session>>>>(sql, 20000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, MiscUtils.getTime() - TimeAmount.MONTH.ms());
}
@Override
public Map<UUID, Map<UUID, List<Session>>> processResults(ResultSet set) throws SQLException {
Map<UUID, Map<UUID, List<Session>>> map = new HashMap<>();
while (set.next()) {
UUID serverUUID = serverUUIDsByID.get(set.getInt(columnServerID));
UUID uuid = uuidsByID.get(set.getInt(columnUserID));
Map<UUID, List<Session>> sessionsByUser = map.getOrDefault(serverUUID, new HashMap<>());
List<Session> 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;
}
});
}
} }

View File

@ -185,17 +185,10 @@ public class UserInfoTable extends UserIDTable {
} }
public List<UserInfo> getServerUserInfo(UUID serverUUID) throws SQLException { public List<UserInfo> getServerUserInfo(UUID serverUUID) throws SQLException {
String usersIDColumn = usersTable + "." + usersTable.getColumnID(); Map<UUID, String> playerNames = usersTable.getPlayerNames();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid"; Map<Integer, UUID> uuidsByID = usersTable.getUUIDsByID();
String usersNameColumn = usersTable + "." + usersTable.getColumnName() + " as name";
String sql = "SELECT " + String sql = "SELECT * FROM " + tableName +
tableName + "." + columnRegistered + ", " +
columnOP + ", " +
columnBanned + ", " +
usersNameColumn + ", " +
usersUUIDColumn +
" FROM " + tableName +
" LEFT JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
" WHERE " + columnServerID + "=" + serverTable.statementSelectServerID; " WHERE " + columnServerID + "=" + serverTable.statementSelectServerID;
return query(new QueryStatement<List<UserInfo>>(sql, 20000) { return query(new QueryStatement<List<UserInfo>>(sql, 20000) {
@ -211,8 +204,9 @@ public class UserInfoTable extends UserIDTable {
long registered = set.getLong(columnRegistered); long registered = set.getLong(columnRegistered);
boolean opped = set.getBoolean(columnOP); boolean opped = set.getBoolean(columnOP);
boolean banned = set.getBoolean(columnBanned); boolean banned = set.getBoolean(columnBanned);
String name = set.getString("name"); int userId = set.getInt(columnUserID);
UUID uuid = UUID.fromString(set.getString("uuid")); UUID uuid = uuidsByID.get(userId);
String name = playerNames.getOrDefault(uuid, "Unknown");
UserInfo info = new UserInfo(uuid, name, registered, opped, banned); UserInfo info = new UserInfo(uuid, name, registered, opped, banned);
if (!userInfo.contains(info)) { if (!userInfo.contains(info)) {
userInfo.add(info); userInfo.add(info);

View File

@ -433,4 +433,23 @@ public class UsersTable extends UserIDTable {
} }
}); });
} }
public Map<Integer, UUID> getUUIDsByID() throws SQLException {
String sql = Select.from(tableName, columnID, columnUUID).toString();
return query(new QueryAllStatement<Map<Integer, UUID>>(sql, 20000) {
@Override
public Map<Integer, UUID> processResults(ResultSet set) throws SQLException {
Map<Integer, UUID> uuidsByID = new TreeMap<>();
while (set.next()) {
int id = set.getInt(columnID);
UUID uuid = UUID.fromString(set.getString(columnUUID));
uuidsByID.put(id, uuid);
}
return uuidsByID;
}
});
}
} }

View File

@ -74,7 +74,7 @@ public class NetworkPage extends Page {
} }
private void uniquePlayers(long now, Database db) throws DBException { private void uniquePlayers(long now, Database db) throws DBException {
Map<UUID, Map<UUID, List<Session>>> allSessions = db.fetch().getSessionsWithNoExtras(); Map<UUID, Map<UUID, List<Session>>> allSessions = db.fetch().getSessionsInLastMonth();
Map<UUID, List<Session>> userSessions = AnalysisUtils.sortSessionsByUser(allSessions); Map<UUID, List<Session>> userSessions = AnalysisUtils.sortSessionsByUser(allSessions);
long dayAgo = now - TimeAmount.DAY.ms(); long dayAgo = now - TimeAmount.DAY.ms();

View File

@ -6,6 +6,7 @@ package com.djrapitops.plan.system;
import com.djrapitops.plan.Plan; import com.djrapitops.plan.Plan;
import com.djrapitops.plan.api.exceptions.EnableException; import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.system.settings.Settings;
import org.junit.After; import org.junit.After;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.ClassRule; import org.junit.ClassRule;
@ -48,6 +49,8 @@ public class BukkitSystemTest {
@Test @Test
public void testEnable() throws EnableException { public void testEnable() throws EnableException {
Settings.WEBSERVER_PORT.setTemporaryValue(9000);
bukkitSystem = new BukkitSystem(planMock); bukkitSystem = new BukkitSystem(planMock);
bukkitSystem.enable(); bukkitSystem.enable();
} }

View File

@ -52,6 +52,7 @@ public class BungeeSystemTest {
public void testEnable() throws EnableException { public void testEnable() throws EnableException {
bungeeSystem = new BungeeSystem(planMock); bungeeSystem = new BungeeSystem(planMock);
Settings.WEBSERVER_PORT.setTemporaryValue(9000);
Settings.BUNGEE_IP.setTemporaryValue("8.8.8.8"); Settings.BUNGEE_IP.setTemporaryValue("8.8.8.8");
Settings.DB_TYPE.setTemporaryValue("sqlite"); Settings.DB_TYPE.setTemporaryValue("sqlite");
bungeeSystem.setDatabaseSystem(new BukkitDBSystem()); bungeeSystem.setDatabaseSystem(new BukkitDBSystem());
@ -66,6 +67,7 @@ public class BungeeSystemTest {
bungeeSystem = new BungeeSystem(planMock); bungeeSystem = new BungeeSystem(planMock);
Settings.WEBSERVER_PORT.setTemporaryValue(9000);
Settings.DB_TYPE.setTemporaryValue("sqlite"); Settings.DB_TYPE.setTemporaryValue("sqlite");
bungeeSystem.setDatabaseSystem(new BukkitDBSystem()); bungeeSystem.setDatabaseSystem(new BukkitDBSystem());