SessionTable

This commit is contained in:
Rsl1122 2017-08-23 10:51:46 +03:00
parent c1399a52e2
commit c6fbb12d2d
7 changed files with 290 additions and 43 deletions

View File

@ -197,4 +197,8 @@ public class Session {
public long getSessionID() { public long getSessionID() {
return sessionID; return sessionID;
} }
public void setSessionID(long sessionID) {
this.sessionID = sessionID;
}
} }

View File

@ -5,6 +5,8 @@
*/ */
package main.java.com.djrapitops.plan.data; package main.java.com.djrapitops.plan.data;
import com.google.common.base.Objects;
/** /**
* Class containing single datapoint of TPS / Players online / CPU Usage / Used Memory / Entity Count / Chunks loaded. * Class containing single datapoint of TPS / Players online / CPU Usage / Used Memory / Entity Count / Chunks loaded.
* *
@ -106,37 +108,34 @@ public class TPS {
} }
@Override @Override
public int hashCode() { public boolean equals(Object o) {
int hash = 3; if (this == o) return true;
hash = 97 * hash + (int) (this.date ^ (this.date >>> 32)); if (o == null || getClass() != o.getClass()) return false;
hash = 97 * hash + (int) (Double.doubleToLongBits(this.ticksPerSecond) ^ (Double.doubleToLongBits(this.ticksPerSecond) >>> 32)); TPS tps = (TPS) o;
hash = 97 * hash + this.players; return date == tps.date &&
return hash; Double.compare(tps.ticksPerSecond, ticksPerSecond) == 0 &&
players == tps.players &&
Double.compare(tps.cpuUsage, cpuUsage) == 0 &&
usedMemory == tps.usedMemory &&
entityCount == tps.entityCount &&
chunksLoaded == tps.chunksLoaded;
} }
@Override @Override
public boolean equals(Object obj) { public int hashCode() {
if (this == obj) { return Objects.hashCode(date, ticksPerSecond, players, cpuUsage, usedMemory, entityCount, chunksLoaded);
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final TPS other = (TPS) obj;
return date == other.date
&& Double.compare(this.ticksPerSecond, other.ticksPerSecond) == 0
&& this.players == other.players
&& Double.compare(cpuUsage, other.cpuUsage) == 0;
} }
@Override @Override
public String toString() { public String toString() {
return "TPS{" + date + "|" + ticksPerSecond + "|" + players + "|" + cpuUsage + "}"; return "TPS{" +
"date=" + date +
", ticksPerSecond=" + ticksPerSecond +
", players=" + players +
", cpuUsage=" + cpuUsage +
", usedMemory=" + usedMemory +
", entityCount=" + entityCount +
", chunksLoaded=" + chunksLoaded +
'}';
} }
} }

View File

@ -195,8 +195,8 @@ public abstract class SQLDB extends Database {
public Table[] getAllTablesInRemoveOrder() { public Table[] getAllTablesInRemoveOrder() {
return new Table[]{ return new Table[]{
ipsTable, ipsTable,
nicknamesTable, sessionsTable, killsTable, nicknamesTable, killsTable, worldTimesTable,
worldTimesTable, worldTable, usersTable, sessionsTable, worldTable, usersTable,
commandUseTable, tpsTable, serverTable}; commandUseTable, tpsTable, serverTable};
} }

View File

@ -3,6 +3,7 @@ package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.data.KillData; import main.java.com.djrapitops.plan.data.KillData;
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.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;
@ -232,4 +233,8 @@ public class KillsTable extends Table {
public void savePlayerKills(UUID uuid, List<KillData> playerKills) { public void savePlayerKills(UUID uuid, List<KillData> playerKills) {
// TODO savePlayerKills // TODO savePlayerKills
} }
public void addKillsToSessions(List<Session> allSessions) {
// TODO addKillsToSessions
}
} }

View File

@ -12,6 +12,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* @author Rsl1122 * @author Rsl1122
@ -68,7 +69,6 @@ public class SessionsTable extends UserIDTable {
/** /**
* Removes User's Sessions from the Database. * Removes User's Sessions from the Database.
* <p> * <p>
* // TODO KILLS SHOULD BE REMOVED FIRST.
* *
* @param userId * @param userId
* @return * @return
@ -77,7 +77,36 @@ public class SessionsTable extends UserIDTable {
return super.removeDataOf(userId); return super.removeDataOf(userId);
} }
public void saveSessionInformation(UUID uuid, Session session) throws SQLException { /**
* Used to save a session, with all it's information into the database.
* <p>
* Also saves WorldTimes and Kills.
*
* @param uuid UUID of the player.
* @param session Session of the player that has ended ({@code endSession} has been called)
* @throws SQLException
*/
public void saveSession(UUID uuid, Session session) throws SQLException {
saveSessionInformation(uuid, session);
long sessionID = getSessionID(uuid, session);
if (sessionID == -1) {
throw new IllegalStateException("Session was not Saved!");
}
session.setSessionID(sessionID);
db.getWorldTimesTable().saveWorldTimes(session.getWorldTimes());
db.getKillsTable().savePlayerKills(uuid, session.getPlayerKills());
}
/**
* Saves Session's Information to the Session Table.
* <p>
* Does not save Kills or WorldTimes.
*
* @param uuid UUID of the player.
* @param session Session of the player that has ended ({@code endSession} has been called)
* @throws SQLException
*/
private void saveSessionInformation(UUID uuid, Session session) throws SQLException {
PreparedStatement statement = null; PreparedStatement statement = null;
try { try {
statement = prepareStatement("INSERT INTO " + tableName + " (" statement = prepareStatement("INSERT INTO " + tableName + " ("
@ -103,12 +132,47 @@ public class SessionsTable extends UserIDTable {
} finally { } finally {
close(statement); close(statement);
} }
db.getWorldTimesTable().saveWorldTimes(session.getWorldTimes());
db.getKillsTable().savePlayerKills(uuid, session.getPlayerKills());
} }
public Map<String, List<Session>> getSessions(UUID uuid) throws SQLException { /**
* Used to get the sessionID of a newly inserted row.
*
* @param uuid UUID of the player
* @param session session inserted.
* @return ID of the inserted session or -1 if session has not been inserted.
*/
private long getSessionID(UUID uuid, Session session) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT " + columnSessionID + " FROM " + tableName +
" WHERE " + columnUserID + "=" + usersTable.statementSelectID +
" AND " + columnSessionStart + "=?" +
" AND " + columnSessionEnd + "=?");
statement.setString(1, uuid.toString());
statement.setLong(2, session.getSessionStart());
statement.setLong(3, session.getSessionEnd());
set = statement.executeQuery();
if (set.next()) {
return set.getLong(columnSessionID);
}
return -1L;
} finally {
close(set, statement);
}
}
/**
* Returns a Map containing Lists of sessions, key as ServerName.
* <p>
* Does not include Kills or WorldTimes.
* Use {@code getSessions} to get full Sessions.
*
* @param uuid UUID of the player
* @return Map with Sessions that don't contain Kills or WorldTimes.
* @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<>(); Map<String, List<Session>> sessionsByServer = new HashMap<>();
PreparedStatement statement = null; PreparedStatement statement = null;
@ -136,23 +200,56 @@ public class SessionsTable extends UserIDTable {
} }
} }
public Map<String, List<Session>> getSessions(UUID uuid) throws SQLException {
Map<String, List<Session>> sessions = getSessionInformation(uuid);
List<Session> allSessions = sessions.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
db.getKillsTable().addKillsToSessions(allSessions);
db.getWorldTimesTable().addWorldTimesToSessions(allSessions);
return sessions;
}
/**
* Get Total Playtime of a Player on THIS server.
*
* @param uuid UUID of the player.
* @return Milliseconds played on THIS server. 0 if player or server not found.
* @throws SQLException
*/
public long getPlaytime(UUID uuid) throws SQLException { public long getPlaytime(UUID uuid) throws SQLException {
return getPlaytime(uuid, Plan.getServerUUID()); return getPlaytime(uuid, Plan.getServerUUID());
} }
/**
* Get Total Playtime of a Player on a server.
*
* @param uuid UUID of the player.
* @param serverUUID UUID of the server. @see ServerTable
* @return Milliseconds played on the server. 0 if player or server not found.
* @throws SQLException
*/
public long getPlaytime(UUID uuid, UUID serverUUID) throws SQLException { public long getPlaytime(UUID uuid, UUID serverUUID) throws SQLException {
return getPlaytime(uuid, serverUUID, 0L); return getPlaytime(uuid, serverUUID, 0L);
} }
/**
* Used to get Playtime after Epoch ms on a server.
*
* @param uuid UUID of the player.
* @param serverUUID UUID of the server. @see ServerTable
* @param afterDate Epoch ms (Playtime after this date is calculated)
* @return Milliseconds played after given epoch ms on the server. 0 if player or server not found.
* @throws SQLException
*/
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; PreparedStatement statement = null;
ResultSet set = null; ResultSet set = null;
try { try {
statement = prepareStatement("SELECT FROM " + tableName + " " statement = prepareStatement("SELECT" +
+ "(SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime " " (SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime" +
+ "WHERE " + columnSessionStart + ">? AND " " FROM " + tableName +
+ columnUserID + "=" + usersTable.statementSelectID + " AND " " WHERE " + columnSessionStart + ">?" +
+ columnServerID + "=" + serverTable.statementSelectServerID); " AND " + columnUserID + "=" + usersTable.statementSelectID +
" AND " + columnServerID + "=" + serverTable.statementSelectServerID);
statement.setLong(1, afterDate); statement.setLong(1, afterDate);
statement.setString(2, uuid.toString()); statement.setString(2, uuid.toString());
statement.setString(3, serverUUID.toString()); statement.setString(3, serverUUID.toString());
@ -166,19 +263,155 @@ public class SessionsTable extends UserIDTable {
} }
} }
/**
* Used to get Totals of Playtime in a Map, sorted by ServerNames.
*
* @param uuid UUID of the Player.
* @return key - ServerName, value ms played
* @throws SQLException
*/
public Map<String, Long> getPlaytimeByServer(UUID uuid) throws SQLException { public Map<String, Long> getPlaytimeByServer(UUID uuid) throws SQLException {
return getPlaytimeByServer(uuid, 0L);
}
/**
* Used to get Playtimes after a date in a Map, sorted by ServerNames.
*
* @param uuid UUID of the Player.
* @param afterDate Epoch ms (Playtime after this date is calculated)
* @return key - ServerName, value ms played
* @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<>(); Map<String, Long> playtimes = new HashMap<>();
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet set = null; ResultSet set = null;
try { try {
statement = prepareStatement("SELECT FROM " + tableName + " " statement = prepareStatement("SELECT " +
+ "(SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime " "(SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime, " +
+ "WHERE " + columnSessionStart + ">? AND " columnServerID + "," +
+ columnUserID + "=" + usersTable.statementSelectID); // TODO CONTINUE " FROM " + tableName +
" WHERE " + columnSessionStart + ">?" +
" AND " + columnUserID + "=" + usersTable.statementSelectID);
statement.setLong(1, afterDate);
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; return playtimes;
} finally { } finally {
close(set, statement); close(set, statement);
} }
} }
/**
* Used to get the Total Playtime of a Server.
*
* @param serverUUID UUID of the server.
* @return Milliseconds played on the server. 0 if server not found.
* @throws SQLException
*/
public long getPlaytimeOfServer(UUID serverUUID) throws SQLException {
return getPlaytimeOfServer(serverUUID, 0L);
}
/**
* Used to get Playtime after a date of a Server.
*
* @param serverUUID UUID of the server.
* @param afterDate Epoch ms (Playtime after this date is calculated)
* @return Milliseconds played after given epoch ms on the server. 0 if server not found.
* @throws SQLException
*/
public long getPlaytimeOfServer(UUID serverUUID, long afterDate) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT" +
" (SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime" +
" FROM " + tableName +
" WHERE " + columnSessionStart + ">?" +
" AND " + columnServerID + "=" + serverTable.statementSelectServerID);
statement.setLong(1, afterDate);
statement.setString(2, serverUUID.toString());
set = statement.executeQuery();
if (set.next()) {
return set.getLong("playtime");
}
return 0;
} finally {
close(set, statement);
}
}
/**
* Used to get total Session count of a Player on THIS server.
*
* @param uuid UUID of the player.
* @return How many sessions player has. 0 if player or server not found.
* @throws SQLException
*/
public int getSessionCount(UUID uuid) throws SQLException {
return getSessionCount(uuid, 0L);
}
/**
* Used to get total Session count of a Player on THIS server after a given epoch ms.
*
* @param uuid UUID of the player.
* @param afterDate Epoch ms (Session count after this date is calculated)
* @return How many sessions player has. 0 if player or server not found.
* @throws SQLException
*/
public int getSessionCount(UUID uuid, long afterDate) throws SQLException {
return getSessionCount(uuid, Plan.getServerUUID(), afterDate);
}
/**
* Used to get total Session count of a Player on a server.
*
* @param uuid UUID of the player.
* @param serverUUID UUID of the server.
* @return How many sessions player has. 0 if player or server not found.
* @throws SQLException
*/
public int getSessionCount(UUID uuid, UUID serverUUID) throws SQLException {
return getSessionCount(uuid, serverUUID, 0L);
}
/**
* Used to get total Session count of a Player on a server after a given epoch ms.
*
* @param uuid UUID of the player.
* @param serverUUID UUID of the server.
* @param afterDate Epoch ms (Session count after this date is calculated)
* @return How many sessions player has. 0 if player or server not found.
* @throws SQLException
*/
public int getSessionCount(UUID uuid, UUID serverUUID, long afterDate) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT" +
" (COUNT(" + columnSessionID + ") - SUM(" + columnSessionStart + ")) as logintimes" +
" FROM " + tableName +
" WHERE " + columnSessionStart + ">?" +
" AND " + columnUserID + "=" + usersTable.statementSelectID +
" AND " + columnServerID + "=" + serverTable.statementSelectServerID);
statement.setLong(1, afterDate);
statement.setString(2, uuid.toString());
statement.setString(3, serverUUID.toString());
set = statement.executeQuery();
if (set.next()) {
return set.getInt("logintimes");
}
return 0;
} finally {
close(set, statement);
}
}
} }

View File

@ -2,6 +2,7 @@ package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.data.time.WorldTimes; import main.java.com.djrapitops.plan.data.time.WorldTimes;
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.sql.Sql; import main.java.com.djrapitops.plan.database.sql.Sql;
@ -12,6 +13,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -317,4 +319,8 @@ public class WorldTimesTable extends UserIDTable {
public void saveWorldTimes(WorldTimes worldTimes) { public void saveWorldTimes(WorldTimes worldTimes) {
// TODO saveWorldTimes (INSERT) // TODO saveWorldTimes (INSERT)
} }
public void addWorldTimesToSessions(List<Session> allSessions) {
// TODO saveWorldTimes
}
} }

View File

@ -298,7 +298,7 @@ public class Analysis {
activity.addBan(uuid); activity.addBan(uuid);
} else if (uData.getLoginTimes() == 1) { } else if (uData.getLoginTimes() == 1) {
activity.addJoinedOnce(uuid); activity.addJoinedOnce(uuid);
// TODO } else if (AnalysisUtils.isActive(now, uData.getLastPlayed(), playTime, uData.getLoginTimes())) { // TODO } else if (AnalysisUtils.isActive(now, uData.getLastPlayed(), playTime, uData.getSessionCount())) {
// activity.addActive(uuid); // activity.addActive(uuid);
} else { } else {
activity.addInActive(uuid); activity.addInActive(uuid);