Online sessions to Most recent sessions + fix sorting

This commit is contained in:
Rsl1122 2019-08-26 11:10:11 +03:00
parent d2b57498da
commit 59d1fdb085
5 changed files with 50 additions and 12 deletions

View File

@ -57,6 +57,11 @@ public class SessionsMutator {
return sessions; return sessions;
} }
public SessionsMutator sort(Comparator<Session> sessionComparator) {
sessions.sort(sessionComparator);
return this;
}
public SessionsMutator filterBy(Predicate<Session> predicate) { public SessionsMutator filterBy(Predicate<Session> predicate) {
return new SessionsMutator(sessions.stream() return new SessionsMutator(sessions.stream()
.filter(predicate) .filter(predicate)

View File

@ -29,6 +29,7 @@ import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.parsing.Sql; import com.djrapitops.plan.db.sql.parsing.Sql;
import com.djrapitops.plan.db.sql.tables.*; import com.djrapitops.plan.db.sql.tables.*;
import com.djrapitops.plan.utilities.comparators.DateHolderRecentComparator; import com.djrapitops.plan.utilities.comparators.DateHolderRecentComparator;
import com.djrapitops.plan.utilities.comparators.SessionStartComparator;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
@ -262,6 +263,7 @@ public class SessionQueries {
.flatMap(Collection::stream) .flatMap(Collection::stream)
.map(SortedMap::values) .map(SortedMap::values)
.flatMap(Collection::stream) .flatMap(Collection::stream)
.sorted(new SessionStartComparator()) // Disorder arises
.collect(Collectors.toList()); .collect(Collectors.toList());
} }

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.db.access.transactions.events;
import com.djrapitops.plan.db.access.queries.DataStoreQueries; import com.djrapitops.plan.db.access.queries.DataStoreQueries;
import com.djrapitops.plan.db.access.queries.PlayerFetchQueries; import com.djrapitops.plan.db.access.queries.PlayerFetchQueries;
import com.djrapitops.plan.db.access.transactions.Transaction; import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.system.cache.SessionCache;
import java.util.UUID; import java.util.UUID;
import java.util.function.LongSupplier; import java.util.function.LongSupplier;
@ -48,7 +49,9 @@ public class PlayerRegisterTransaction extends Transaction {
@Override @Override
protected void performOperations() { protected void performOperations() {
if (!query(PlayerFetchQueries.isPlayerRegistered(playerUUID))) { if (!query(PlayerFetchQueries.isPlayerRegistered(playerUUID))) {
execute(DataStoreQueries.registerBaseUser(playerUUID, registered.getAsLong(), playerName)); long registerDate = registered.getAsLong();
execute(DataStoreQueries.registerBaseUser(playerUUID, registerDate, playerName));
SessionCache.getCachedSession(playerUUID).ifPresent(session -> session.setAsFirstSessionIfMatches(registerDate));
} }
execute(DataStoreQueries.updatePlayerName(playerUUID, playerName)); execute(DataStoreQueries.updatePlayerName(playerUUID, playerName));
} }

View File

@ -30,11 +30,14 @@ import com.djrapitops.plan.db.access.queries.containers.AllPlayerContainersQuery
import com.djrapitops.plan.db.access.queries.containers.ServerPlayersTableContainersQuery; import com.djrapitops.plan.db.access.queries.containers.ServerPlayersTableContainersQuery;
import com.djrapitops.plan.db.access.queries.objects.*; import com.djrapitops.plan.db.access.queries.objects.*;
import com.djrapitops.plan.extension.implementation.storage.queries.ExtensionServerPlayerDataTableQuery; import com.djrapitops.plan.extension.implementation.storage.queries.ExtensionServerPlayerDataTableQuery;
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.info.server.Server; import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plan.system.info.server.ServerInfo;
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.DisplaySettings;
import com.djrapitops.plan.system.settings.paths.TimeSettings; import com.djrapitops.plan.system.settings.paths.TimeSettings;
import com.djrapitops.plan.utilities.comparators.SessionStartComparator;
import com.djrapitops.plan.utilities.formatting.Formatter; import com.djrapitops.plan.utilities.formatting.Formatter;
import com.djrapitops.plan.utilities.formatting.Formatters; import com.djrapitops.plan.utilities.formatting.Formatters;
import com.djrapitops.plan.utilities.html.graphs.Graphs; import com.djrapitops.plan.utilities.html.graphs.Graphs;
@ -54,6 +57,7 @@ public class JSONFactory {
private final PlanConfig config; private final PlanConfig config;
private final DBSystem dbSystem; private final DBSystem dbSystem;
private final ServerInfo serverInfo;
private final Graphs graphs; private final Graphs graphs;
private final Formatters formatters; private final Formatters formatters;
@ -61,11 +65,13 @@ public class JSONFactory {
public JSONFactory( public JSONFactory(
PlanConfig config, PlanConfig config,
DBSystem dbSystem, DBSystem dbSystem,
ServerInfo serverInfo,
Graphs graphs, Graphs graphs,
Formatters formatters Formatters formatters
) { ) {
this.config = config; this.config = config;
this.dbSystem = dbSystem; this.dbSystem = dbSystem;
this.serverInfo = serverInfo;
this.graphs = graphs; this.graphs = graphs;
this.formatters = formatters; this.formatters = formatters;
} }
@ -100,21 +106,43 @@ public class JSONFactory {
).toJSONString(); ).toJSONString();
} }
public List<Map<String, Object>> serverSessionsAsJSONMap(UUID serverUUID) { public List<Map<String, Object>> networkSessionsAsJSONMap(UUID serverUUID) {
Database db = dbSystem.getDatabase(); Database db = dbSystem.getDatabase();
List<Session> sessions = db.query(SessionQueries.fetchLatestSessionsOfServer(
serverUUID, config.get(DisplaySettings.SESSIONS_PER_PAGE) Integer perPageLimit = config.get(DisplaySettings.SESSIONS_PER_PAGE);
)); List<Session> sessions = db.query(SessionQueries.fetchLatestSessionsOfServer(serverUUID, perPageLimit));
// Add online sessions
if (serverUUID.equals(serverInfo.getServerUUID())) {
sessions.addAll(SessionCache.getActiveSessions().values());
sessions.sort(new SessionStartComparator());
while (true) {
int size = sessions.size();
if (size <= perPageLimit) break;
sessions.remove(size - 1); // Remove last until it fits.
}
}
return new SessionsMutator(sessions).toPlayerNameJSONMaps(graphs, config.getWorldAliasSettings(), formatters); return new SessionsMutator(sessions).toPlayerNameJSONMaps(graphs, config.getWorldAliasSettings(), formatters);
} }
public List<Map<String, Object>> serverSessionsAsJSONMap() { public List<Map<String, Object>> networkSessionsAsJSONMap() {
Database db = dbSystem.getDatabase(); Database db = dbSystem.getDatabase();
List<Session> sessions = db.query(SessionQueries.fetchLatestSessions( Integer perPageLimit = config.get(DisplaySettings.SESSIONS_PER_PAGE);
config.get(DisplaySettings.SESSIONS_PER_PAGE)
)); List<Session> sessions = db.query(SessionQueries.fetchLatestSessions(perPageLimit));
// Add online sessions
if (serverInfo.getServer().isProxy()) {
sessions.addAll(SessionCache.getActiveSessions().values());
sessions.sort(new SessionStartComparator());
while (true) {
int size = sessions.size();
if (size <= perPageLimit) break;
sessions.remove(size - 1); // Remove last until it fits.
}
}
List<Map<String, Object>> sessionMaps = new SessionsMutator(sessions).toPlayerNameJSONMaps(graphs, config.getWorldAliasSettings(), formatters); List<Map<String, Object>> sessionMaps = new SessionsMutator(sessions).toPlayerNameJSONMaps(graphs, config.getWorldAliasSettings(), formatters);
// // Add network_server property so that sessions have a server page link
sessionMaps.forEach(map -> map.put("network_server", map.get("server_name"))); sessionMaps.forEach(map -> map.put("network_server", map.get("server_name")));
return sessionMaps; return sessionMaps;
} }

View File

@ -56,10 +56,10 @@ public class SessionsJSONHandler implements PageHandler {
public Response getResponse(Request request, RequestTarget target) throws WebException { public Response getResponse(Request request, RequestTarget target) throws WebException {
if (target.getParameter("server").isPresent()) { if (target.getParameter("server").isPresent()) {
UUID serverUUID = identifiers.getServerUUID(target); UUID serverUUID = identifiers.getServerUUID(target);
return new JSONResponse(Collections.singletonMap("sessions", jsonFactory.serverSessionsAsJSONMap(serverUUID))); return new JSONResponse(Collections.singletonMap("sessions", jsonFactory.networkSessionsAsJSONMap(serverUUID)));
} }
// Assume network // Assume network
return new JSONResponse(Collections.singletonMap("sessions", jsonFactory.serverSessionsAsJSONMap())); return new JSONResponse(Collections.singletonMap("sessions", jsonFactory.networkSessionsAsJSONMap()));
} }
@Override @Override