mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-09-07 04:18:26 +02:00
Online sessions to Most recent sessions + fix sorting
This commit is contained in:
parent
d2b57498da
commit
59d1fdb085
@ -57,6 +57,11 @@ public class SessionsMutator {
|
||||
return sessions;
|
||||
}
|
||||
|
||||
public SessionsMutator sort(Comparator<Session> sessionComparator) {
|
||||
sessions.sort(sessionComparator);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SessionsMutator filterBy(Predicate<Session> predicate) {
|
||||
return new SessionsMutator(sessions.stream()
|
||||
.filter(predicate)
|
||||
|
@ -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.tables.*;
|
||||
import com.djrapitops.plan.utilities.comparators.DateHolderRecentComparator;
|
||||
import com.djrapitops.plan.utilities.comparators.SessionStartComparator;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
@ -262,6 +263,7 @@ public class SessionQueries {
|
||||
.flatMap(Collection::stream)
|
||||
.map(SortedMap::values)
|
||||
.flatMap(Collection::stream)
|
||||
.sorted(new SessionStartComparator()) // Disorder arises
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
@ -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.PlayerFetchQueries;
|
||||
import com.djrapitops.plan.db.access.transactions.Transaction;
|
||||
import com.djrapitops.plan.system.cache.SessionCache;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.function.LongSupplier;
|
||||
@ -48,7 +49,9 @@ public class PlayerRegisterTransaction extends Transaction {
|
||||
@Override
|
||||
protected void performOperations() {
|
||||
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));
|
||||
}
|
||||
|
@ -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.objects.*;
|
||||
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.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.paths.DisplaySettings;
|
||||
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.Formatters;
|
||||
import com.djrapitops.plan.utilities.html.graphs.Graphs;
|
||||
@ -54,6 +57,7 @@ public class JSONFactory {
|
||||
|
||||
private final PlanConfig config;
|
||||
private final DBSystem dbSystem;
|
||||
private final ServerInfo serverInfo;
|
||||
private final Graphs graphs;
|
||||
private final Formatters formatters;
|
||||
|
||||
@ -61,11 +65,13 @@ public class JSONFactory {
|
||||
public JSONFactory(
|
||||
PlanConfig config,
|
||||
DBSystem dbSystem,
|
||||
ServerInfo serverInfo,
|
||||
Graphs graphs,
|
||||
Formatters formatters
|
||||
) {
|
||||
this.config = config;
|
||||
this.dbSystem = dbSystem;
|
||||
this.serverInfo = serverInfo;
|
||||
this.graphs = graphs;
|
||||
this.formatters = formatters;
|
||||
}
|
||||
@ -100,21 +106,43 @@ public class JSONFactory {
|
||||
).toJSONString();
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> serverSessionsAsJSONMap(UUID serverUUID) {
|
||||
public List<Map<String, Object>> networkSessionsAsJSONMap(UUID serverUUID) {
|
||||
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);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> serverSessionsAsJSONMap() {
|
||||
public List<Map<String, Object>> networkSessionsAsJSONMap() {
|
||||
Database db = dbSystem.getDatabase();
|
||||
List<Session> sessions = db.query(SessionQueries.fetchLatestSessions(
|
||||
config.get(DisplaySettings.SESSIONS_PER_PAGE)
|
||||
));
|
||||
Integer perPageLimit = 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);
|
||||
//
|
||||
// Add network_server property so that sessions have a server page link
|
||||
sessionMaps.forEach(map -> map.put("network_server", map.get("server_name")));
|
||||
return sessionMaps;
|
||||
}
|
||||
|
@ -56,10 +56,10 @@ public class SessionsJSONHandler implements PageHandler {
|
||||
public Response getResponse(Request request, RequestTarget target) throws WebException {
|
||||
if (target.getParameter("server").isPresent()) {
|
||||
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
|
||||
return new JSONResponse(Collections.singletonMap("sessions", jsonFactory.serverSessionsAsJSONMap()));
|
||||
return new JSONResponse(Collections.singletonMap("sessions", jsonFactory.networkSessionsAsJSONMap()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user