Player & server names to sessions

- Fixed issues with limiting session count setting
This commit is contained in:
Rsl1122 2019-07-27 11:56:19 +03:00
parent a68ac696c4
commit bb59b76c68
7 changed files with 91 additions and 41 deletions

View File

@ -43,7 +43,7 @@ public class Session extends DynamicDataContainer implements DateHolder {
private long afkTime; private long afkTime;
/** /**
* Creates a new session. * Creates a new session based on a join event.
* *
* @param uuid UUID of the Player. * @param uuid UUID of the Player.
* @param serverUUID UUID of the server. * @param serverUUID UUID of the server.
@ -92,7 +92,11 @@ public class Session extends DynamicDataContainer implements DateHolder {
* @param deaths Death count during the session. * @param deaths Death count during the session.
* @param afkTime Time spent AFK during the session. * @param afkTime Time spent AFK during the session.
*/ */
public Session(int id, UUID uuid, UUID serverUUID, long sessionStart, long sessionEnd, int mobKills, int deaths, long afkTime) { public Session(
int id, UUID uuid, UUID serverUUID,
long sessionStart, long sessionEnd,
int mobKills, int deaths, long afkTime
) {
this.sessionStart = sessionStart; this.sessionStart = sessionStart;
worldTimes = new WorldTimes(); worldTimes = new WorldTimes();
playerKills = new ArrayList<>(); playerKills = new ArrayList<>();
@ -236,7 +240,7 @@ public class Session extends DynamicDataContainer implements DateHolder {
public String toString() { public String toString() {
return "Session{" + return "Session{" +
"sessionStart=" + getUnsafe(SessionKeys.START) + "sessionStart=" + getUnsafe(SessionKeys.START) +
", sessionEnd=" + getUnsafe(SessionKeys.END) + ", sessionEnd=" + getValue(SessionKeys.END).orElse(null) +
", worldTimes=" + worldTimes + ", worldTimes=" + worldTimes +
", playerKills=" + playerKills + ", playerKills=" + playerKills +
", mobKills=" + mobKills + ", mobKills=" + mobKills +

View File

@ -49,9 +49,11 @@ public class SessionQueries {
} }
private static final String SELECT_SESSIONS_STATEMENT = SELECT + private static final String SELECT_SESSIONS_STATEMENT = SELECT +
SessionsTable.TABLE_NAME + '.' + SessionsTable.ID + ',' + "s." + SessionsTable.ID + ',' +
SessionsTable.TABLE_NAME + '.' + SessionsTable.USER_UUID + ',' + "s." + SessionsTable.USER_UUID + ',' +
SessionsTable.TABLE_NAME + '.' + SessionsTable.SERVER_UUID + ',' + "s." + SessionsTable.SERVER_UUID + ',' +
"u." + UsersTable.USER_NAME + " as name," +
"server." + ServerTable.NAME + " as server_name," +
SessionsTable.SESSION_START + ',' + SessionsTable.SESSION_START + ',' +
SessionsTable.SESSION_END + ',' + SessionsTable.SESSION_END + ',' +
SessionsTable.MOB_KILLS + ',' + SessionsTable.MOB_KILLS + ',' +
@ -63,14 +65,16 @@ public class SessionQueries {
WorldTimesTable.SPECTATOR + ',' + WorldTimesTable.SPECTATOR + ',' +
WorldTable.NAME + ',' + WorldTable.NAME + ',' +
KillsTable.VICTIM_UUID + ',' + KillsTable.VICTIM_UUID + ',' +
UsersTable.USER_NAME + " as victim_name, " + "v." + UsersTable.USER_NAME + " as victim_name, " +
KillsTable.DATE + ',' + KillsTable.DATE + ',' +
KillsTable.WEAPON + KillsTable.WEAPON +
FROM + SessionsTable.TABLE_NAME + FROM + SessionsTable.TABLE_NAME + " s" +
LEFT_JOIN + KillsTable.TABLE_NAME + " ON " + SessionsTable.TABLE_NAME + '.' + SessionsTable.ID + "=" + KillsTable.TABLE_NAME + '.' + KillsTable.SESSION_ID + INNER_JOIN + UsersTable.TABLE_NAME + " u on u." + UsersTable.USER_UUID + "=s." + SessionsTable.USER_UUID +
LEFT_JOIN + UsersTable.TABLE_NAME + " on " + UsersTable.TABLE_NAME + '.' + UsersTable.USER_UUID + "=" + KillsTable.VICTIM_UUID + INNER_JOIN + ServerTable.TABLE_NAME + " server on server." + ServerTable.SERVER_UUID + "=s." + SessionsTable.SERVER_UUID +
INNER_JOIN + WorldTimesTable.TABLE_NAME + " ON " + SessionsTable.TABLE_NAME + '.' + SessionsTable.ID + "=" + WorldTimesTable.TABLE_NAME + '.' + WorldTimesTable.SESSION_ID + LEFT_JOIN + KillsTable.TABLE_NAME + " ON " + "s." + SessionsTable.ID + '=' + KillsTable.TABLE_NAME + '.' + KillsTable.SESSION_ID +
INNER_JOIN + WorldTable.TABLE_NAME + " ON " + WorldTimesTable.TABLE_NAME + '.' + WorldTimesTable.WORLD_ID + "=" + WorldTable.TABLE_NAME + '.' + WorldTable.ID; LEFT_JOIN + UsersTable.TABLE_NAME + " v on v." + UsersTable.USER_UUID + '=' + KillsTable.VICTIM_UUID +
INNER_JOIN + WorldTimesTable.TABLE_NAME + " ON s." + SessionsTable.ID + '=' + WorldTimesTable.TABLE_NAME + '.' + WorldTimesTable.SESSION_ID +
INNER_JOIN + WorldTable.TABLE_NAME + " ON " + WorldTimesTable.TABLE_NAME + '.' + WorldTimesTable.WORLD_ID + '=' + WorldTable.TABLE_NAME + '.' + WorldTable.ID;
private static final String ORDER_BY_SESSION_START_DESC = ORDER_BY + SessionsTable.SESSION_START + " DESC"; private static final String ORDER_BY_SESSION_START_DESC = ORDER_BY + SessionsTable.SESSION_START + " DESC";
@ -149,7 +153,7 @@ public class SessionQueries {
public static QueryStatement<List<Session>> fetchSessionsOfServerFlat(UUID serverUUID) { public static QueryStatement<List<Session>> fetchSessionsOfServerFlat(UUID serverUUID) {
String sql = SELECT_SESSIONS_STATEMENT + String sql = SELECT_SESSIONS_STATEMENT +
WHERE + SessionsTable.TABLE_NAME + '.' + SessionsTable.SERVER_UUID + "=?" + WHERE + "s." + SessionsTable.SERVER_UUID + "=?" +
ORDER_BY_SESSION_START_DESC; ORDER_BY_SESSION_START_DESC;
return new QueryStatement<List<Session>>(sql, 50000) { return new QueryStatement<List<Session>>(sql, 50000) {
@Override @Override
@ -172,7 +176,7 @@ public class SessionQueries {
*/ */
public static Query<Map<UUID, List<Session>>> fetchSessionsOfPlayer(UUID playerUUID) { public static Query<Map<UUID, List<Session>>> fetchSessionsOfPlayer(UUID playerUUID) {
String sql = SELECT_SESSIONS_STATEMENT + String sql = SELECT_SESSIONS_STATEMENT +
WHERE + SessionsTable.TABLE_NAME + '.' + SessionsTable.USER_UUID + "=?" + WHERE + "s." + SessionsTable.USER_UUID + "=?" +
ORDER_BY_SESSION_START_DESC; ORDER_BY_SESSION_START_DESC;
return new QueryStatement<Map<UUID, List<Session>>>(sql, 50000) { return new QueryStatement<Map<UUID, List<Session>>>(sql, 50000) {
@Override @Override
@ -240,6 +244,9 @@ public class SessionQueries {
playerKills.sort(dateColderRecentComparator); playerKills.sort(dateColderRecentComparator);
} }
session.putRawData(SessionKeys.NAME, set.getString("name"));
session.putRawData(SessionKeys.SERVER_NAME, set.getString("server_name"));
playerSessions.put(sessionStart, session); playerSessions.put(sessionStart, session);
serverSessions.put(playerUUID, playerSessions); serverSessions.put(playerUUID, playerSessions);
tempSessionMap.put(serverUUID, serverSessions); tempSessionMap.put(serverUUID, serverSessions);
@ -255,12 +262,12 @@ public class SessionQueries {
public static Query<List<Session>> fetchServerSessionsWithoutKillOrWorldData(long after, long before, UUID serverUUID) { public static Query<List<Session>> fetchServerSessionsWithoutKillOrWorldData(long after, long before, UUID serverUUID) {
String sql = SELECT + String sql = SELECT +
SessionsTable.ID + ", " + SessionsTable.ID + ',' +
SessionsTable.USER_UUID + ", " + SessionsTable.USER_UUID + ',' +
SessionsTable.SESSION_START + ", " + SessionsTable.SESSION_START + ',' +
SessionsTable.SESSION_END + ", " + SessionsTable.SESSION_END + ',' +
SessionsTable.DEATHS + ", " + SessionsTable.DEATHS + ',' +
SessionsTable.MOB_KILLS + ", " + SessionsTable.MOB_KILLS + ',' +
SessionsTable.AFK_TIME + SessionsTable.AFK_TIME +
FROM + SessionsTable.TABLE_NAME + FROM + SessionsTable.TABLE_NAME +
WHERE + SessionsTable.SERVER_UUID + "=?" + WHERE + SessionsTable.SERVER_UUID + "=?" +
@ -296,31 +303,52 @@ public class SessionQueries {
}; };
} }
public static Query<List<Session>> fetchLatestSessionsOfServer(UUID serverUUID, int limit) { private static Query<Long> fetchLatestSessionStartLimitForServer(UUID serverUUID, int limit) {
String selectLastDateToInclude = SELECT + SessionsTable.TABLE_NAME + '.' + SessionsTable.SESSION_START + String sql = SELECT + SessionsTable.SESSION_START + FROM + SessionsTable.TABLE_NAME +
FROM + SessionsTable.TABLE_NAME + WHERE + SessionsTable.SERVER_UUID + "=?" +
WHERE + SessionsTable.TABLE_NAME + '.' + SessionsTable.SERVER_UUID + "=?" + ORDER_BY_SESSION_START_DESC + " LIMIT ?";
ORDER_BY_SESSION_START_DESC + " LIMIT 1 OFFSET ?";
String sql = SELECT_SESSIONS_STATEMENT + return new QueryStatement<Long>(sql, limit) {
WHERE + SessionsTable.TABLE_NAME + '.' + SessionsTable.SESSION_START + ">=(" + selectLastDateToInclude + ')' +
AND + SessionsTable.TABLE_NAME + '.' + SessionsTable.SERVER_UUID + "=?";
return new QueryStatement<List<Session>>(sql, limit) {
@Override @Override
public void prepare(PreparedStatement statement) throws SQLException { public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString()); statement.setString(1, serverUUID.toString());
statement.setInt(2, limit); statement.setInt(2, limit);
statement.setString(3, serverUUID.toString());
} }
@Override @Override
public List<Session> processResults(ResultSet set) throws SQLException { public Long processResults(ResultSet set) throws SQLException {
return extractDataFromSessionSelectStatement(set); Long last = null;
while (set.next()) {
last = set.getLong(SessionsTable.SESSION_START);
}
return last;
} }
}; };
} }
public static Query<List<Session>> fetchLatestSessionsOfServer(UUID serverUUID, int limit) {
String sql = SELECT_SESSIONS_STATEMENT +
WHERE + "s." + SessionsTable.SERVER_UUID + "=?" +
AND + "s." + SessionsTable.SESSION_START + ">=?" +
ORDER_BY_SESSION_START_DESC;
return db -> {
Long start = db.query(fetchLatestSessionStartLimitForServer(serverUUID, limit));
return db.query(new QueryStatement<List<Session>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString());
statement.setLong(2, start != null ? start : 0L);
}
@Override
public List<Session> processResults(ResultSet set) throws SQLException {
return extractDataFromSessionSelectStatement(set);
}
});
};
}
public static Query<Long> sessionCount(long after, long before, UUID serverUUID) { public static Query<Long> sessionCount(long after, long before, UUID serverUUID) {
String sql = SELECT + "COUNT(1) as count" + String sql = SELECT + "COUNT(1) as count" +
FROM + SessionsTable.TABLE_NAME + FROM + SessionsTable.TABLE_NAME +

View File

@ -32,7 +32,7 @@ public class Sql {
public static final String WHERE = " WHERE "; public static final String WHERE = " WHERE ";
public static final String GROUP_BY = " GROUP BY "; public static final String GROUP_BY = " GROUP BY ";
public static final String ORDER_BY = " ORDER BY "; public static final String ORDER_BY = " ORDER BY ";
public static final String INNER_JOIN = " INNER JOIN "; public static final String INNER_JOIN = " JOIN ";
public static final String LEFT_JOIN = " LEFT JOIN "; public static final String LEFT_JOIN = " LEFT JOIN ";
public static final String UNION = " UNION "; public static final String UNION = " UNION ";
public static final String AND = " AND "; public static final String AND = " AND ";

View File

@ -27,6 +27,7 @@ import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
import com.djrapitops.plan.system.cache.SessionCache; import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.DisplaySettings;
import com.djrapitops.plan.system.settings.paths.TimeSettings; import com.djrapitops.plan.system.settings.paths.TimeSettings;
import com.djrapitops.plan.system.settings.theme.Theme; import com.djrapitops.plan.system.settings.theme.Theme;
import com.djrapitops.plan.system.settings.theme.ThemeVal; import com.djrapitops.plan.system.settings.theme.ThemeVal;
@ -101,6 +102,7 @@ public class PlayerJSONParser {
data.put("player_kills", player.getValue(PlayerKeys.PLAYER_KILLS).orElse(Collections.emptyList())); data.put("player_kills", player.getValue(PlayerKeys.PLAYER_KILLS).orElse(Collections.emptyList()));
data.put("player_deaths", player.getValue(PlayerKeys.PLAYER_DEATHS_KILLS).orElse(Collections.emptyList())); data.put("player_deaths", player.getValue(PlayerKeys.PLAYER_DEATHS_KILLS).orElse(Collections.emptyList()));
data.put("sessions", sessionsMutator.toServerNameJSONMaps(graphs, config.getWorldAliasSettings(), formatters)); data.put("sessions", sessionsMutator.toServerNameJSONMaps(graphs, config.getWorldAliasSettings(), formatters));
data.put("sessions_per_page", config.get(DisplaySettings.SESSIONS_PER_PAGE));
data.put("servers", serverAccordion); data.put("servers", serverAccordion);
data.put("punchcard_series", graphs.special().punchCard(sessionsMutator).getDots()); data.put("punchcard_series", graphs.special().punchCard(sessionsMutator).getDots());
WorldPie worldPie = graphs.pie().worldPie(player.getValue(PlayerKeys.WORLD_TIMES).orElse(new WorldTimes())); WorldPie worldPie = graphs.pie().worldPie(player.getValue(PlayerKeys.WORLD_TIMES).orElse(new WorldTimes()));

View File

@ -31,7 +31,6 @@ import com.djrapitops.plan.system.webserver.response.errors.InternalErrorRespons
import com.djrapitops.plan.system.webserver.response.errors.NotFoundResponse; import com.djrapitops.plan.system.webserver.response.errors.NotFoundResponse;
import com.djrapitops.plan.system.webserver.response.pages.*; import com.djrapitops.plan.system.webserver.response.pages.*;
import com.djrapitops.plan.utilities.html.pages.PageFactory; import com.djrapitops.plan.utilities.html.pages.PageFactory;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.logging.error.ErrorHandler; import com.djrapitops.plugin.logging.error.ErrorHandler;
import javax.inject.Inject; import javax.inject.Inject;
@ -89,7 +88,6 @@ public class ResponseFactory {
public ErrorResponse internalErrorResponse(Throwable e, String s) { public ErrorResponse internalErrorResponse(Throwable e, String s) {
try { try {
errorHandler.log(L.WARN, this.getClass(), e);
return new InternalErrorResponse(s, e, versionCheckSystem, files); return new InternalErrorResponse(s, e, versionCheckSystem, files);
} catch (IOException improperRestartException) { } catch (IOException improperRestartException) {
return new ErrorResponse( return new ErrorResponse(

View File

@ -12,8 +12,12 @@ function loadSessionAccordion(json, error) {
sessionTable.append('<tr><td>No Sessions</td><td>-</td><td>-</td><td>-</td></tr>') sessionTable.append('<tr><td>No Sessions</td><td>-</td><td>-</td><td>-</td></tr>')
} }
// sessions_per_page can be undefined (-> NaN) or higher than amount of sessions.
var limit = json.sessions_per_page ? json.sessions_per_page : sessions.length;
limit = Math.min(limit, sessions.length);
var sessionsHtml = ''; var sessionsHtml = '';
for (var i = 0; i < sessions.length; i++) { for (var i = 0; i < limit; i++) {
var session = sessions[i]; var session = sessions[i];
var title = createAccordionTitle(i, session); var title = createAccordionTitle(i, session);
var body = createAccordionBody(i, session); var body = createAccordionBody(i, session);
@ -22,7 +26,7 @@ function loadSessionAccordion(json, error) {
sessionTable.append(sessionsHtml); sessionTable.append(sessionsHtml);
for (var i = 0; i < sessions.length; i++) { for (var i = 0; i < limit; i++) {
$('#session_h_' + i).click(onOpenSession(i, sessions)); $('#session_h_' + i).click(onOpenSession(i, sessions));
} }
} }

View File

@ -393,6 +393,20 @@ public interface DatabaseTest {
assertEquals(session, savedSessions.get(0)); assertEquals(session, savedSessions.get(0));
} }
@Test
default void mostRecentSessionsCanBeQueried() {
sessionsAreStoredWithAllData();
Session session = new Session(playerUUID, serverUUID(), 12345L, worlds[0], "SURVIVAL");
session.endSession(22345L);
session.setWorldTimes(createWorldTimes());
session.setPlayerKills(createKills());
List<Session> expected = Collections.singletonList(session);
List<Session> result = db().query(SessionQueries.fetchLatestSessionsOfServer(serverUUID(), 1));
assertEquals(expected, result);
}
@Test @Test
default void userInfoTableStoresCorrectUserInformation() { default void userInfoTableStoresCorrectUserInformation() {
saveUserOne(); saveUserOne();
@ -1340,9 +1354,9 @@ public interface DatabaseTest {
} }
@Test @Test
public void activeTunredInactiveQueryHasAllParametersSet() { default void activeTunredInactiveQueryHasAllParametersSet() {
Integer result = db.query(ActivityIndexQueries.countRegularPlayersTurnedInactive( Integer result = db().query(ActivityIndexQueries.countRegularPlayersTurnedInactive(
0, System.currentTimeMillis(), serverUUID, 0, System.currentTimeMillis(), serverUUID(),
TimeUnit.HOURS.toMillis(2L) TimeUnit.HOURS.toMillis(2L)
)); ));
assertNotNull(result); assertNotNull(result);