mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-21 05:50:18 +08:00
Refactored SessionsTable
This commit is contained in:
parent
4f75d23865
commit
da21aa4efd
@ -5,11 +5,13 @@ import main.java.com.djrapitops.plan.Plan;
|
|||||||
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException;
|
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException;
|
||||||
import main.java.com.djrapitops.plan.data.Session;
|
import main.java.com.djrapitops.plan.data.Session;
|
||||||
import main.java.com.djrapitops.plan.database.databases.SQLDB;
|
import main.java.com.djrapitops.plan.database.databases.SQLDB;
|
||||||
|
import main.java.com.djrapitops.plan.database.processing.ExecStatement;
|
||||||
|
import main.java.com.djrapitops.plan.database.processing.QueryAllStatement;
|
||||||
|
import main.java.com.djrapitops.plan.database.processing.QueryStatement;
|
||||||
import main.java.com.djrapitops.plan.database.sql.Select;
|
import main.java.com.djrapitops.plan.database.sql.Select;
|
||||||
import main.java.com.djrapitops.plan.database.sql.Sql;
|
import main.java.com.djrapitops.plan.database.sql.Sql;
|
||||||
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
|
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@ -95,22 +97,17 @@ public class SessionsTable extends UserIDTable {
|
|||||||
* @throws SQLException DB Error
|
* @throws SQLException DB Error
|
||||||
*/
|
*/
|
||||||
private void saveSessionInformation(UUID uuid, Session session) throws SQLException {
|
private void saveSessionInformation(UUID uuid, Session session) throws SQLException {
|
||||||
PreparedStatement statement = null;
|
execute(new ExecStatement(insertStatement) {
|
||||||
try (Connection connection = getConnection()){
|
@Override
|
||||||
statement = connection.prepareStatement(insertStatement);
|
public void prepare(PreparedStatement statement) throws SQLException {
|
||||||
statement.setString(1, uuid.toString());
|
statement.setString(1, uuid.toString());
|
||||||
|
statement.setLong(2, session.getSessionStart());
|
||||||
statement.setLong(2, session.getSessionStart());
|
statement.setLong(3, session.getSessionEnd());
|
||||||
statement.setLong(3, session.getSessionEnd());
|
statement.setInt(4, session.getDeaths());
|
||||||
statement.setInt(4, session.getDeaths());
|
statement.setInt(5, session.getMobKills());
|
||||||
statement.setInt(5, session.getMobKills());
|
statement.setString(6, Plan.getServerUUID().toString());
|
||||||
statement.setString(6, Plan.getServerUUID().toString());
|
}
|
||||||
|
});
|
||||||
statement.execute();
|
|
||||||
commit(connection);
|
|
||||||
} finally {
|
|
||||||
close(statement);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -121,24 +118,27 @@ public class SessionsTable extends UserIDTable {
|
|||||||
* @return ID of the inserted session or -1 if session has not been inserted.
|
* @return ID of the inserted session or -1 if session has not been inserted.
|
||||||
*/
|
*/
|
||||||
private int getSessionID(UUID uuid, Session session) throws SQLException {
|
private int getSessionID(UUID uuid, Session session) throws SQLException {
|
||||||
PreparedStatement statement = null;
|
String sql = "SELECT " + columnID + " FROM " + tableName +
|
||||||
ResultSet set = null;
|
" WHERE " + columnUserID + "=" + usersTable.statementSelectID +
|
||||||
try (Connection connection = getConnection()){
|
" AND " + columnSessionStart + "=?" +
|
||||||
statement = connection.prepareStatement("SELECT " + columnID + " FROM " + tableName +
|
" AND " + columnSessionEnd + "=?";
|
||||||
" WHERE " + columnUserID + "=" + usersTable.statementSelectID +
|
|
||||||
" AND " + columnSessionStart + "=?" +
|
return query(new QueryStatement<Integer>(sql) {
|
||||||
" AND " + columnSessionEnd + "=?");
|
@Override
|
||||||
statement.setString(1, uuid.toString());
|
public void prepare(PreparedStatement statement) throws SQLException {
|
||||||
statement.setLong(2, session.getSessionStart());
|
statement.setString(1, uuid.toString());
|
||||||
statement.setLong(3, session.getSessionEnd());
|
statement.setLong(2, session.getSessionStart());
|
||||||
set = statement.executeQuery();
|
statement.setLong(3, session.getSessionEnd());
|
||||||
if (set.next()) {
|
|
||||||
return set.getInt(columnID);
|
|
||||||
}
|
}
|
||||||
return -1;
|
|
||||||
} finally {
|
@Override
|
||||||
close(set, statement);
|
public Integer processResults(ResultSet set) throws SQLException {
|
||||||
}
|
if (set.next()) {
|
||||||
|
return set.getInt(columnID);
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -153,35 +153,38 @@ public class SessionsTable extends UserIDTable {
|
|||||||
*/
|
*/
|
||||||
private Map<String, List<Session>> getSessionInformation(UUID uuid) throws SQLException {
|
private Map<String, List<Session>> getSessionInformation(UUID uuid) throws SQLException {
|
||||||
Map<Integer, String> serverNames = serverTable.getServerNames();
|
Map<Integer, String> serverNames = serverTable.getServerNames();
|
||||||
Map<String, List<Session>> sessionsByServer = new HashMap<>();
|
String sql = Select.from(tableName, "*")
|
||||||
PreparedStatement statement = null;
|
.where(columnUserID + "=" + usersTable.statementSelectID)
|
||||||
ResultSet set = null;
|
.toString();
|
||||||
try (Connection connection = getConnection()){
|
|
||||||
statement = connection.prepareStatement(Select.from(tableName, "*")
|
|
||||||
.where(columnUserID + "=" + usersTable.statementSelectID)
|
|
||||||
.toString());
|
|
||||||
statement.setString(1, uuid.toString());
|
|
||||||
set = statement.executeQuery();
|
|
||||||
while (set.next()) {
|
|
||||||
int id = set.getInt(columnID);
|
|
||||||
long start = set.getLong(columnSessionStart);
|
|
||||||
long end = set.getLong(columnSessionEnd);
|
|
||||||
String serverName = serverNames.get(set.getInt(columnServerID));
|
|
||||||
|
|
||||||
if (serverName == null) {
|
return query(new QueryStatement<Map<String, List<Session>>>(sql, 10000) {
|
||||||
throw new IllegalStateException("Server not present");
|
@Override
|
||||||
}
|
public void prepare(PreparedStatement statement) throws SQLException {
|
||||||
|
statement.setString(1, uuid.toString());
|
||||||
int deaths = set.getInt(columnDeaths);
|
|
||||||
int mobKills = set.getInt(columnMobKills);
|
|
||||||
List<Session> sessions = sessionsByServer.getOrDefault(serverName, new ArrayList<>());
|
|
||||||
sessions.add(new Session(id, start, end, mobKills, deaths));
|
|
||||||
sessionsByServer.put(serverName, sessions);
|
|
||||||
}
|
}
|
||||||
return sessionsByServer;
|
|
||||||
} finally {
|
@Override
|
||||||
close(set, statement);
|
public Map<String, List<Session>> processResults(ResultSet set) throws SQLException {
|
||||||
}
|
Map<String, List<Session>> sessionsByServer = new HashMap<>();
|
||||||
|
while (set.next()) {
|
||||||
|
int id = set.getInt(columnID);
|
||||||
|
long start = set.getLong(columnSessionStart);
|
||||||
|
long end = set.getLong(columnSessionEnd);
|
||||||
|
String serverName = serverNames.get(set.getInt(columnServerID));
|
||||||
|
|
||||||
|
if (serverName == null) {
|
||||||
|
throw new IllegalStateException("Server not present");
|
||||||
|
}
|
||||||
|
|
||||||
|
int deaths = set.getInt(columnDeaths);
|
||||||
|
int mobKills = set.getInt(columnMobKills);
|
||||||
|
List<Session> sessions = sessionsByServer.getOrDefault(serverName, new ArrayList<>());
|
||||||
|
sessions.add(new Session(id, start, end, mobKills, deaths));
|
||||||
|
sessionsByServer.put(serverName, sessions);
|
||||||
|
}
|
||||||
|
return sessionsByServer;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, List<Session>> getSessions(UUID uuid) throws SQLException {
|
public Map<String, List<Session>> getSessions(UUID uuid) throws SQLException {
|
||||||
@ -238,26 +241,29 @@ public class SessionsTable extends UserIDTable {
|
|||||||
* @throws SQLException DB Error
|
* @throws SQLException DB Error
|
||||||
*/
|
*/
|
||||||
public long getPlaytime(UUID uuid, UUID serverUUID, long afterDate) throws SQLException {
|
public long getPlaytime(UUID uuid, UUID serverUUID, long afterDate) throws SQLException {
|
||||||
PreparedStatement statement = null;
|
String sql = "SELECT" +
|
||||||
ResultSet set = null;
|
" (SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime" +
|
||||||
try (Connection connection = getConnection()){
|
" FROM " + tableName +
|
||||||
statement = connection.prepareStatement("SELECT" +
|
" WHERE " + columnSessionStart + ">?" +
|
||||||
" (SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime" +
|
" AND " + columnUserID + "=" + usersTable.statementSelectID +
|
||||||
" FROM " + tableName +
|
" AND " + columnServerID + "=" + serverTable.statementSelectServerID;
|
||||||
" WHERE " + columnSessionStart + ">?" +
|
|
||||||
" AND " + columnUserID + "=" + usersTable.statementSelectID +
|
return query(new QueryStatement<Long>(sql) {
|
||||||
" AND " + columnServerID + "=" + serverTable.statementSelectServerID);
|
@Override
|
||||||
statement.setLong(1, afterDate);
|
public void prepare(PreparedStatement statement) throws SQLException {
|
||||||
statement.setString(2, uuid.toString());
|
statement.setLong(1, afterDate);
|
||||||
statement.setString(3, serverUUID.toString());
|
statement.setString(2, uuid.toString());
|
||||||
set = statement.executeQuery();
|
statement.setString(3, serverUUID.toString());
|
||||||
if (set.next()) {
|
|
||||||
return set.getLong("playtime");
|
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
} finally {
|
@Override
|
||||||
close(set, statement);
|
public Long processResults(ResultSet set) throws SQLException {
|
||||||
}
|
if (set.next()) {
|
||||||
|
return set.getLong("playtime");
|
||||||
|
}
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -281,29 +287,31 @@ public class SessionsTable extends UserIDTable {
|
|||||||
*/
|
*/
|
||||||
public Map<String, Long> getPlaytimeByServer(UUID uuid, long afterDate) throws SQLException {
|
public Map<String, Long> getPlaytimeByServer(UUID uuid, long afterDate) throws SQLException {
|
||||||
Map<Integer, String> serverNames = serverTable.getServerNames();
|
Map<Integer, String> serverNames = serverTable.getServerNames();
|
||||||
Map<String, Long> playtimes = new HashMap<>();
|
String sql = "SELECT " +
|
||||||
PreparedStatement statement = null;
|
"(SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime, " +
|
||||||
ResultSet set = null;
|
columnServerID +
|
||||||
try (Connection connection = getConnection()){
|
" FROM " + tableName +
|
||||||
statement = connection.prepareStatement("SELECT " +
|
" WHERE " + columnSessionStart + ">?" +
|
||||||
"(SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime, " +
|
" AND " + columnUserID + "=" + usersTable.statementSelectID +
|
||||||
columnServerID +
|
" GROUP BY " + columnServerID;
|
||||||
" FROM " + tableName +
|
return query(new QueryStatement<Map<String, Long>>(sql, 100) {
|
||||||
" WHERE " + columnSessionStart + ">?" +
|
@Override
|
||||||
" AND " + columnUserID + "=" + usersTable.statementSelectID +
|
public void prepare(PreparedStatement statement) throws SQLException {
|
||||||
" GROUP BY " + columnServerID);
|
statement.setLong(1, afterDate);
|
||||||
statement.setLong(1, afterDate);
|
statement.setString(2, uuid.toString());
|
||||||
statement.setString(2, uuid.toString());
|
|
||||||
set = statement.executeQuery();
|
|
||||||
while (set.next()) {
|
|
||||||
String serverName = serverNames.get(set.getInt(columnServerID));
|
|
||||||
long playtime = set.getLong("playtime");
|
|
||||||
playtimes.put(serverName, playtime);
|
|
||||||
}
|
}
|
||||||
return playtimes;
|
|
||||||
} finally {
|
@Override
|
||||||
close(set, statement);
|
public Map<String, Long> processResults(ResultSet set) throws SQLException {
|
||||||
}
|
Map<String, Long> playtimes = new HashMap<>();
|
||||||
|
while (set.next()) {
|
||||||
|
String serverName = serverNames.get(set.getInt(columnServerID));
|
||||||
|
long playtime = set.getLong("playtime");
|
||||||
|
playtimes.put(serverName, playtime);
|
||||||
|
}
|
||||||
|
return playtimes;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -347,25 +355,27 @@ public class SessionsTable extends UserIDTable {
|
|||||||
* @throws SQLException DB Error
|
* @throws SQLException DB Error
|
||||||
*/
|
*/
|
||||||
public long getPlaytimeOfServer(UUID serverUUID, long afterDate) throws SQLException {
|
public long getPlaytimeOfServer(UUID serverUUID, long afterDate) throws SQLException {
|
||||||
PreparedStatement statement = null;
|
String sql = "SELECT" +
|
||||||
ResultSet set = null;
|
" (SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime" +
|
||||||
try (Connection connection = getConnection()){
|
" FROM " + tableName +
|
||||||
statement = connection.prepareStatement("SELECT" +
|
" WHERE " + columnSessionStart + ">?" +
|
||||||
" (SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime" +
|
" AND " + columnServerID + "=" + serverTable.statementSelectServerID;
|
||||||
" FROM " + tableName +
|
|
||||||
" WHERE " + columnSessionStart + ">?" +
|
return query(new QueryStatement<Long>(sql) {
|
||||||
" AND " + columnServerID + "=" + serverTable.statementSelectServerID);
|
@Override
|
||||||
statement.setFetchSize(1000);
|
public void prepare(PreparedStatement statement) throws SQLException {
|
||||||
statement.setLong(1, afterDate);
|
statement.setLong(1, afterDate);
|
||||||
statement.setString(2, serverUUID.toString());
|
statement.setString(2, serverUUID.toString());
|
||||||
set = statement.executeQuery();
|
|
||||||
if (set.next()) {
|
|
||||||
return set.getLong("playtime");
|
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
} finally {
|
@Override
|
||||||
close(set, statement);
|
public Long processResults(ResultSet set) throws SQLException {
|
||||||
}
|
if (set.next()) {
|
||||||
|
return set.getLong("playtime");
|
||||||
|
}
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -413,26 +423,29 @@ public class SessionsTable extends UserIDTable {
|
|||||||
* @throws SQLException DB Error
|
* @throws SQLException DB Error
|
||||||
*/
|
*/
|
||||||
public int getSessionCount(UUID uuid, UUID serverUUID, long afterDate) throws SQLException {
|
public int getSessionCount(UUID uuid, UUID serverUUID, long afterDate) throws SQLException {
|
||||||
PreparedStatement statement = null;
|
String sql = "SELECT" +
|
||||||
ResultSet set = null;
|
" COUNT(*) as logintimes" +
|
||||||
try (Connection connection = getConnection()){
|
" FROM " + tableName +
|
||||||
statement = connection.prepareStatement("SELECT" +
|
" WHERE (" + columnSessionStart + " >= ?)" +
|
||||||
" COUNT(*) as logintimes" +
|
" AND " + columnUserID + "=" + usersTable.statementSelectID +
|
||||||
" FROM " + tableName +
|
" AND " + columnServerID + "=" + serverTable.statementSelectServerID;
|
||||||
" WHERE (" + columnSessionStart + " >= ?)" +
|
|
||||||
" AND " + columnUserID + "=" + usersTable.statementSelectID +
|
return query(new QueryStatement<Integer>(sql) {
|
||||||
" AND " + columnServerID + "=" + serverTable.statementSelectServerID);
|
@Override
|
||||||
statement.setLong(1, afterDate);
|
public void prepare(PreparedStatement statement) throws SQLException {
|
||||||
statement.setString(2, uuid.toString());
|
statement.setLong(1, afterDate);
|
||||||
statement.setString(3, serverUUID.toString());
|
statement.setString(2, uuid.toString());
|
||||||
set = statement.executeQuery();
|
statement.setString(3, serverUUID.toString());
|
||||||
if (set.next()) {
|
|
||||||
return set.getInt("logintimes");
|
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
} finally {
|
@Override
|
||||||
close(set, statement);
|
public Integer processResults(ResultSet set) throws SQLException {
|
||||||
}
|
if (set.next()) {
|
||||||
|
return set.getInt("logintimes");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getColumnID() {
|
public String getColumnID() {
|
||||||
@ -449,175 +462,168 @@ public class SessionsTable extends UserIDTable {
|
|||||||
return new HashMap<>();
|
return new HashMap<>();
|
||||||
}
|
}
|
||||||
int serverID = id.get();
|
int serverID = id.get();
|
||||||
Map<UUID, List<Session>> sessionsByUser = new HashMap<>();
|
|
||||||
PreparedStatement statement = null;
|
|
||||||
ResultSet set = null;
|
|
||||||
try (Connection connection = getConnection()){
|
|
||||||
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
|
||||||
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
|
|
||||||
|
|
||||||
statement = connection.prepareStatement("SELECT " +
|
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
||||||
columnSessionStart + ", " +
|
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
|
||||||
columnSessionEnd + ", " +
|
String sql = "SELECT " +
|
||||||
columnDeaths + ", " +
|
columnSessionStart + ", " +
|
||||||
columnMobKills + ", " +
|
columnSessionEnd + ", " +
|
||||||
usersUUIDColumn +
|
columnDeaths + ", " +
|
||||||
" FROM " + tableName +
|
columnMobKills + ", " +
|
||||||
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
|
usersUUIDColumn +
|
||||||
" WHERE " + columnServerID + "=" + serverTable.statementSelectServerID
|
" FROM " + tableName +
|
||||||
);
|
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
|
||||||
statement.setFetchSize(5000);
|
" WHERE " + columnServerID + "=" + serverTable.statementSelectServerID;
|
||||||
statement.setString(1, serverUUID.toString());
|
|
||||||
set = statement.executeQuery();
|
|
||||||
while (set.next()) {
|
|
||||||
UUID uuid = UUID.fromString(set.getString("uuid"));
|
|
||||||
long start = set.getLong(columnSessionStart);
|
|
||||||
long end = set.getLong(columnSessionEnd);
|
|
||||||
|
|
||||||
int deaths = set.getInt(columnDeaths);
|
return query(new QueryStatement<Map<UUID, List<Session>>>(sql, 5000) {
|
||||||
int mobKills = set.getInt(columnMobKills);
|
@Override
|
||||||
List<Session> sessions = sessionsByUser.getOrDefault(uuid, new ArrayList<>());
|
public void prepare(PreparedStatement statement) throws SQLException {
|
||||||
sessions.add(new Session(serverID, start, end, mobKills, deaths));
|
statement.setString(1, serverUUID.toString());
|
||||||
sessionsByUser.put(uuid, sessions);
|
|
||||||
}
|
}
|
||||||
return sessionsByUser;
|
|
||||||
} finally {
|
@Override
|
||||||
close(set, statement);
|
public Map<UUID, List<Session>> processResults(ResultSet set) throws SQLException {
|
||||||
}
|
Map<UUID, List<Session>> sessionsByUser = new HashMap<>();
|
||||||
|
while (set.next()) {
|
||||||
|
UUID uuid = UUID.fromString(set.getString("uuid"));
|
||||||
|
long start = set.getLong(columnSessionStart);
|
||||||
|
long end = set.getLong(columnSessionEnd);
|
||||||
|
|
||||||
|
int deaths = set.getInt(columnDeaths);
|
||||||
|
int mobKills = set.getInt(columnMobKills);
|
||||||
|
List<Session> sessions = sessionsByUser.getOrDefault(uuid, new ArrayList<>());
|
||||||
|
sessions.add(new Session(serverID, start, end, mobKills, deaths));
|
||||||
|
sessionsByUser.put(uuid, sessions);
|
||||||
|
}
|
||||||
|
return sessionsByUser;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Write tests for this method
|
// TODO Write tests for this method
|
||||||
public long getLastSeen(UUID uuid) throws SQLException {
|
public long getLastSeen(UUID uuid) throws SQLException {
|
||||||
PreparedStatement statement = null;
|
String sql = "SELECT" +
|
||||||
ResultSet set = null;
|
" MAX(" + columnSessionEnd + ") as last_seen" +
|
||||||
try (Connection connection = getConnection()){
|
" FROM " + tableName +
|
||||||
statement = connection.prepareStatement("SELECT" +
|
" WHERE " + columnUserID + "=" + usersTable.statementSelectID;
|
||||||
" MAX(" + columnSessionEnd + ") as last_seen" +
|
|
||||||
" FROM " + tableName +
|
return query(new QueryStatement<Long>(sql) {
|
||||||
" WHERE " + columnUserID + "=" + usersTable.statementSelectID);
|
@Override
|
||||||
statement.setString(1, uuid.toString());
|
public void prepare(PreparedStatement statement) throws SQLException {
|
||||||
set = statement.executeQuery();
|
statement.setString(1, uuid.toString());
|
||||||
if (set.next()) {
|
|
||||||
return set.getLong("last_seen");
|
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
} finally {
|
@Override
|
||||||
close(set, statement);
|
public Long processResults(ResultSet set) throws SQLException {
|
||||||
}
|
if (set.next()) {
|
||||||
|
return set.getLong("last_seen");
|
||||||
|
}
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Write tests for this method
|
// TODO Write tests for this method
|
||||||
public Map<UUID, Long> getLastSeenForAllPlayers() throws SQLException {
|
public Map<UUID, Long> getLastSeenForAllPlayers() throws SQLException {
|
||||||
PreparedStatement statement = null;
|
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
||||||
ResultSet set = null;
|
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
|
||||||
try (Connection connection = getConnection()){
|
String sql = "SELECT" +
|
||||||
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
" MAX(" + columnSessionEnd + ") as last_seen, " +
|
||||||
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
|
usersUUIDColumn +
|
||||||
|
" FROM " + tableName +
|
||||||
|
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
|
||||||
|
" GROUP BY uuid";
|
||||||
|
|
||||||
statement = connection.prepareStatement("SELECT" +
|
return query(new QueryAllStatement<Map<UUID, Long>>(sql, 20000) {
|
||||||
" MAX(" + columnSessionEnd + ") as last_seen, " +
|
@Override
|
||||||
usersUUIDColumn +
|
public Map<UUID, Long> processResults(ResultSet set) throws SQLException {
|
||||||
" FROM " + tableName +
|
Map<UUID, Long> lastSeenMap = new HashMap<>();
|
||||||
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
|
while (set.next()) {
|
||||||
" GROUP BY uuid");
|
UUID uuid = UUID.fromString(set.getString("uuid"));
|
||||||
statement.setFetchSize(20000);
|
long lastSeen = set.getLong("last_seen");
|
||||||
set = statement.executeQuery();
|
lastSeenMap.put(uuid, lastSeen);
|
||||||
Map<UUID, Long> lastSeenMap = new HashMap<>();
|
}
|
||||||
while (set.next()) {
|
return lastSeenMap;
|
||||||
UUID uuid = UUID.fromString(set.getString("uuid"));
|
|
||||||
long lastSeen = set.getLong("last_seen");
|
|
||||||
lastSeenMap.put(uuid, lastSeen);
|
|
||||||
}
|
}
|
||||||
return lastSeenMap;
|
});
|
||||||
} finally {
|
|
||||||
close(set, statement);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<UUID, Map<UUID, List<Session>>> getAllSessions(boolean getKillsAndWorldTimes) throws SQLException {
|
public Map<UUID, Map<UUID, List<Session>>> getAllSessions(boolean getKillsAndWorldTimes) throws SQLException {
|
||||||
PreparedStatement statement = null;
|
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
||||||
ResultSet set = null;
|
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
|
||||||
try (Connection connection = getConnection()){
|
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
|
||||||
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
|
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
|
||||||
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
|
String sql = "SELECT " +
|
||||||
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
|
tableName + "." + columnID + ", " +
|
||||||
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
|
columnSessionStart + ", " +
|
||||||
|
columnSessionEnd + ", " +
|
||||||
|
columnDeaths + ", " +
|
||||||
|
columnMobKills + ", " +
|
||||||
|
usersUUIDColumn + ", " +
|
||||||
|
serverUUIDColumn +
|
||||||
|
" FROM " + tableName +
|
||||||
|
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
|
||||||
|
" JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID;
|
||||||
|
|
||||||
statement = connection.prepareStatement("SELECT " +
|
return query(new QueryAllStatement<Map<UUID, Map<UUID, List<Session>>>>(sql, 20000) {
|
||||||
tableName + "." + columnID + ", " +
|
@Override
|
||||||
columnSessionStart + ", " +
|
public Map<UUID, Map<UUID, List<Session>>> processResults(ResultSet set) throws SQLException {
|
||||||
columnSessionEnd + ", " +
|
Map<UUID, Map<UUID, List<Session>>> map = new HashMap<>();
|
||||||
columnDeaths + ", " +
|
while (set.next()) {
|
||||||
columnMobKills + ", " +
|
UUID serverUUID = UUID.fromString(set.getString("s_uuid"));
|
||||||
usersUUIDColumn + ", " +
|
UUID uuid = UUID.fromString(set.getString("uuid"));
|
||||||
serverUUIDColumn +
|
|
||||||
" FROM " + tableName +
|
|
||||||
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
|
|
||||||
" JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID
|
|
||||||
);
|
|
||||||
statement.setFetchSize(20000);
|
|
||||||
set = statement.executeQuery();
|
|
||||||
|
|
||||||
Map<UUID, Map<UUID, List<Session>>> map = new HashMap<>();
|
Map<UUID, List<Session>> sessionsByUser = map.getOrDefault(serverUUID, new HashMap<>());
|
||||||
while (set.next()) {
|
List<Session> sessions = sessionsByUser.getOrDefault(uuid, new ArrayList<>());
|
||||||
UUID serverUUID = UUID.fromString(set.getString("s_uuid"));
|
|
||||||
UUID uuid = UUID.fromString(set.getString("uuid"));
|
|
||||||
|
|
||||||
Map<UUID, List<Session>> sessionsByUser = map.getOrDefault(serverUUID, new HashMap<>());
|
long start = set.getLong(columnSessionStart);
|
||||||
List<Session> sessions = sessionsByUser.getOrDefault(uuid, new ArrayList<>());
|
long end = set.getLong(columnSessionEnd);
|
||||||
|
|
||||||
long start = set.getLong(columnSessionStart);
|
int deaths = set.getInt(columnDeaths);
|
||||||
long end = set.getLong(columnSessionEnd);
|
int mobKills = set.getInt(columnMobKills);
|
||||||
|
int id = set.getInt(columnID);
|
||||||
|
|
||||||
int deaths = set.getInt(columnDeaths);
|
Session session = new Session(id, start, end, mobKills, deaths);
|
||||||
int mobKills = set.getInt(columnMobKills);
|
sessions.add(session);
|
||||||
int id = set.getInt(columnID);
|
|
||||||
|
|
||||||
Session session = new Session(id, start, end, mobKills, deaths);
|
sessionsByUser.put(uuid, sessions);
|
||||||
sessions.add(session);
|
map.put(serverUUID, sessionsByUser);
|
||||||
|
}
|
||||||
sessionsByUser.put(uuid, sessions);
|
if (getKillsAndWorldTimes) {
|
||||||
map.put(serverUUID, sessionsByUser);
|
db.getKillsTable().addKillsToSessions(map);
|
||||||
|
db.getWorldTimesTable().addWorldTimesToSessions(map);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
}
|
}
|
||||||
if (getKillsAndWorldTimes) {
|
});
|
||||||
db.getKillsTable().addKillsToSessions(map);
|
|
||||||
db.getWorldTimesTable().addWorldTimesToSessions(map);
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
} finally {
|
|
||||||
close(set, statement);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void insertSessions(Map<UUID, Map<UUID, List<Session>>> allSessions, boolean saveKillsAndWorldTimes) throws SQLException {
|
public void insertSessions(Map<UUID, Map<UUID, List<Session>>> allSessions, boolean saveKillsAndWorldTimes) throws SQLException {
|
||||||
if (Verify.isEmpty(allSessions)) {
|
if (Verify.isEmpty(allSessions)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PreparedStatement statement = null;
|
|
||||||
try (Connection connection = getConnection()){
|
|
||||||
statement = connection.prepareStatement(insertStatement);
|
|
||||||
for (UUID serverUUID : allSessions.keySet()) {
|
|
||||||
for (Map.Entry<UUID, List<Session>> entry : allSessions.get(serverUUID).entrySet()) {
|
|
||||||
UUID uuid = entry.getKey();
|
|
||||||
List<Session> sessions = entry.getValue();
|
|
||||||
|
|
||||||
for (Session session : sessions) {
|
executeBatch(new ExecStatement(insertStatement) {
|
||||||
statement.setString(1, uuid.toString());
|
@Override
|
||||||
statement.setLong(2, session.getSessionStart());
|
public void prepare(PreparedStatement statement) throws SQLException {
|
||||||
statement.setLong(3, session.getSessionEnd());
|
for (UUID serverUUID : allSessions.keySet()) {
|
||||||
statement.setInt(4, session.getDeaths());
|
for (Map.Entry<UUID, List<Session>> entry : allSessions.get(serverUUID).entrySet()) {
|
||||||
statement.setInt(5, session.getMobKills());
|
UUID uuid = entry.getKey();
|
||||||
statement.setString(6, serverUUID.toString());
|
List<Session> sessions = entry.getValue();
|
||||||
statement.addBatch();
|
|
||||||
|
for (Session session : sessions) {
|
||||||
|
statement.setString(1, uuid.toString());
|
||||||
|
statement.setLong(2, session.getSessionStart());
|
||||||
|
statement.setLong(3, session.getSessionEnd());
|
||||||
|
statement.setInt(4, session.getDeaths());
|
||||||
|
statement.setInt(5, session.getMobKills());
|
||||||
|
statement.setString(6, serverUUID.toString());
|
||||||
|
statement.addBatch();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
statement.executeBatch();
|
|
||||||
commit(connection);
|
|
||||||
} finally {
|
|
||||||
close(statement);
|
|
||||||
}
|
|
||||||
if (saveKillsAndWorldTimes) {
|
if (saveKillsAndWorldTimes) {
|
||||||
Map<UUID, Map<UUID, List<Session>>> savedSessions = getAllSessions(false);
|
Map<UUID, Map<UUID, List<Session>>> savedSessions = getAllSessions(false);
|
||||||
matchSessionIDs(allSessions, savedSessions);
|
matchSessionIDs(allSessions, savedSessions);
|
||||||
|
Loading…
Reference in New Issue
Block a user