mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-27 09:00:28 +08:00
Refactored TPSTable#getPeakPlayerCount+derivatives to queries:
Optimizes fetch for the player peaks
This commit is contained in:
parent
35e967e2eb
commit
0f79263424
@ -17,10 +17,12 @@
|
||||
package com.djrapitops.plan.db.sql.queries;
|
||||
|
||||
import com.djrapitops.plan.data.WebUser;
|
||||
import com.djrapitops.plan.data.store.objects.DateObj;
|
||||
import com.djrapitops.plan.db.access.Query;
|
||||
import com.djrapitops.plan.db.access.QueryStatement;
|
||||
import com.djrapitops.plan.db.sql.tables.SecurityTable;
|
||||
import com.djrapitops.plan.db.sql.tables.ServerTable;
|
||||
import com.djrapitops.plan.db.sql.tables.TPSTable;
|
||||
import com.djrapitops.plan.db.sql.tables.UsersTable;
|
||||
import com.djrapitops.plan.system.info.server.Server;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
@ -118,4 +120,33 @@ public class OptionalFetchQueries {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Query<Optional<DateObj<Integer>>> fetchPeakPlayerCount(UUID serverUUID, long afterDate) {
|
||||
String sql = "SELECT " + TPSTable.DATE + ", MAX(" + TPSTable.PLAYERS_ONLINE + ") as max FROM " + TPSTable.TABLE_NAME +
|
||||
" WHERE " + TPSTable.SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID +
|
||||
" AND " + TPSTable.DATE + ">= ?";
|
||||
|
||||
return new QueryStatement<Optional<DateObj<Integer>>>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, serverUUID.toString());
|
||||
statement.setLong(2, afterDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DateObj<Integer>> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
return Optional.of(new DateObj<>(
|
||||
set.getLong(TPSTable.DATE),
|
||||
set.getInt(TPSTable.PLAYERS_ONLINE)
|
||||
));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Query<Optional<DateObj<Integer>>> fetchAllTimePeakPlayerCount(UUID serverUUID) {
|
||||
return db -> db.query(fetchPeakPlayerCount(serverUUID, 0));
|
||||
}
|
||||
}
|
@ -17,14 +17,13 @@
|
||||
package com.djrapitops.plan.db.sql.queries.containers;
|
||||
|
||||
import com.djrapitops.plan.data.container.Session;
|
||||
import com.djrapitops.plan.data.container.TPS;
|
||||
import com.djrapitops.plan.data.store.containers.ServerContainer;
|
||||
import com.djrapitops.plan.data.store.keys.ServerKeys;
|
||||
import com.djrapitops.plan.data.store.mutators.PlayersMutator;
|
||||
import com.djrapitops.plan.data.store.mutators.SessionsMutator;
|
||||
import com.djrapitops.plan.data.store.objects.DateObj;
|
||||
import com.djrapitops.plan.db.SQLDB;
|
||||
import com.djrapitops.plan.db.access.Query;
|
||||
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
|
||||
import com.djrapitops.plan.system.cache.SessionCache;
|
||||
import com.djrapitops.plan.system.info.server.Server;
|
||||
|
||||
@ -67,22 +66,12 @@ public class ServerContainerQuery implements Query<ServerContainer> {
|
||||
|
||||
container.putCachingSupplier(ServerKeys.TPS, () -> db.getTpsTable().getTPSData(serverUUID));
|
||||
container.putCachingSupplier(ServerKeys.PING, () -> PlayersMutator.forContainer(container).pings());
|
||||
container.putCachingSupplier(ServerKeys.ALL_TIME_PEAK_PLAYERS, () -> {
|
||||
Optional<TPS> allTimePeak = db.getTpsTable().getAllTimePeak(serverUUID);
|
||||
if (allTimePeak.isPresent()) {
|
||||
TPS peak = allTimePeak.get();
|
||||
return new DateObj<>(peak.getDate(), peak.getPlayers());
|
||||
}
|
||||
return null;
|
||||
});
|
||||
container.putCachingSupplier(ServerKeys.ALL_TIME_PEAK_PLAYERS, () ->
|
||||
db.query(OptionalFetchQueries.fetchAllTimePeakPlayerCount(serverUUID)).orElse(null)
|
||||
);
|
||||
container.putCachingSupplier(ServerKeys.RECENT_PEAK_PLAYERS, () -> {
|
||||
long twoDaysAgo = System.currentTimeMillis() - (TimeUnit.DAYS.toMillis(2L));
|
||||
Optional<TPS> lastPeak = db.getTpsTable().getPeakPlayerCount(serverUUID, twoDaysAgo);
|
||||
if (lastPeak.isPresent()) {
|
||||
TPS peak = lastPeak.get();
|
||||
return new DateObj<>(peak.getDate(), peak.getPlayers());
|
||||
}
|
||||
return null;
|
||||
return db.query(OptionalFetchQueries.fetchPeakPlayerCount(serverUUID, twoDaysAgo)).orElse(null);
|
||||
});
|
||||
|
||||
container.putCachingSupplier(ServerKeys.COMMAND_USAGE, () -> db.getCommandUseTable().getCommandUse(serverUUID));
|
||||
|
@ -18,6 +18,7 @@ package com.djrapitops.plan.db.sql.tables;
|
||||
|
||||
import com.djrapitops.plan.data.container.TPS;
|
||||
import com.djrapitops.plan.data.container.builders.TPSBuilder;
|
||||
import com.djrapitops.plan.data.store.objects.DateObj;
|
||||
import com.djrapitops.plan.db.DBType;
|
||||
import com.djrapitops.plan.db.SQLDB;
|
||||
import com.djrapitops.plan.db.access.ExecStatement;
|
||||
@ -131,10 +132,10 @@ public class TPSTable extends Table {
|
||||
* Clean the TPS Table of old data.
|
||||
*/
|
||||
public void clean() {
|
||||
Optional<TPS> allTimePeak = getAllTimePeak(getServerUUID());
|
||||
Optional<DateObj<Integer>> allTimePeak = db.query(OptionalFetchQueries.fetchAllTimePeakPlayerCount(getServerUUID()));
|
||||
int p = -1;
|
||||
if (allTimePeak.isPresent()) {
|
||||
p = allTimePeak.get().getPlayers();
|
||||
p = allTimePeak.get().getValue();
|
||||
}
|
||||
final int pValue = p;
|
||||
|
||||
@ -170,50 +171,6 @@ public class TPSTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
public Optional<TPS> getPeakPlayerCount(UUID serverUUID, long afterDate) {
|
||||
String subStatement = "SELECT MAX(" + PLAYERS_ONLINE + ") FROM " + tableName +
|
||||
" WHERE " + SERVER_ID + "=" + serverTable.statementSelectServerID +
|
||||
" AND " + DATE + ">= ?";
|
||||
String sql = Select.all(tableName)
|
||||
.where(SERVER_ID + "=" + serverTable.statementSelectServerID)
|
||||
.and(PLAYERS_ONLINE + "= (" + subStatement + ")")
|
||||
.and(DATE + ">= ?")
|
||||
.toString();
|
||||
|
||||
return query(new QueryStatement<Optional<TPS>>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, serverUUID.toString());
|
||||
statement.setString(2, serverUUID.toString());
|
||||
statement.setLong(3, afterDate);
|
||||
statement.setLong(4, afterDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<TPS> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
TPS tps = TPSBuilder.get()
|
||||
.date(set.getLong(DATE))
|
||||
.tps(set.getDouble(TPS))
|
||||
.playersOnline(set.getInt(PLAYERS_ONLINE))
|
||||
.usedCPU(set.getDouble(CPU_USAGE))
|
||||
.usedMemory(set.getLong(RAM_USAGE))
|
||||
.entities(set.getInt(ENTITIES))
|
||||
.chunksLoaded(set.getInt(CHUNKS))
|
||||
.freeDiskSpace(set.getLong(FREE_DISK))
|
||||
.toTPS();
|
||||
|
||||
return Optional.of(tps);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Optional<TPS> getAllTimePeak(UUID serverUUID) {
|
||||
return getPeakPlayerCount(serverUUID, 0);
|
||||
}
|
||||
|
||||
public List<TPS> getNetworkOnlineData() {
|
||||
Optional<Server> proxyInfo = db.query(OptionalFetchQueries.fetchProxyServerInformation());
|
||||
if (!proxyInfo.isPresent()) {
|
||||
|
Loading…
Reference in New Issue
Block a user