Moved two peak player count queries to TPSQueries class

This commit is contained in:
Rsl1122 2019-02-16 14:22:25 +02:00
parent f83be34f09
commit 23969192c1
4 changed files with 35 additions and 76 deletions

View File

@ -1,71 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.db.access.queries;
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.ServerTable;
import com.djrapitops.plan.db.sql.tables.TPSTable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;
import java.util.UUID;
/**
* Static method class for queries that return single item if found.
*
* @author Rsl1122
*/
public class OptionalFetchQueries {
private OptionalFetchQueries() {
/* Static method class */
}
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 + ">= ?" +
" GROUP BY " + TPSTable.SERVER_ID;
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));
}
}

View File

@ -23,7 +23,6 @@ import com.djrapitops.plan.data.store.mutators.PlayersMutator;
import com.djrapitops.plan.data.store.mutators.SessionsMutator;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.queries.OptionalFetchQueries;
import com.djrapitops.plan.db.access.queries.ServerAggregateQueries;
import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
import com.djrapitops.plan.db.access.queries.objects.TPSQueries;
@ -71,11 +70,11 @@ public class ServerContainerQuery implements Query<ServerContainer> {
container.putCachingSupplier(ServerKeys.TPS, () -> db.query(TPSQueries.fetchTPSDataOfServer(serverUUID)));
container.putCachingSupplier(ServerKeys.PING, () -> PlayersMutator.forContainer(container).pings());
container.putCachingSupplier(ServerKeys.ALL_TIME_PEAK_PLAYERS, () ->
db.query(OptionalFetchQueries.fetchAllTimePeakPlayerCount(serverUUID)).orElse(null)
db.query(TPSQueries.fetchAllTimePeakPlayerCount(serverUUID)).orElse(null)
);
container.putCachingSupplier(ServerKeys.RECENT_PEAK_PLAYERS, () -> {
long twoDaysAgo = System.currentTimeMillis() - (TimeUnit.DAYS.toMillis(2L));
return db.query(OptionalFetchQueries.fetchPeakPlayerCount(serverUUID, twoDaysAgo)).orElse(null);
return db.query(TPSQueries.fetchPeakPlayerCount(serverUUID, twoDaysAgo)).orElse(null);
});
container.putCachingSupplier(ServerKeys.COMMAND_USAGE, () -> db.query(ServerAggregateQueries.commandUsageCounts(serverUUID)));

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.db.access.queries.objects;
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.access.Query;
import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.access.QueryStatement;
@ -117,4 +118,34 @@ public class TPSQueries {
}
};
}
public static Query<Optional<DateObj<Integer>>> fetchPeakPlayerCount(UUID serverUUID, long afterDate) {
String sql = "SELECT " + DATE + ", MAX(" + PLAYERS_ONLINE + ") as max FROM " + TABLE_NAME +
" WHERE " + SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID +
" AND " + DATE + ">= ?" +
" GROUP BY " + SERVER_ID;
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(DATE),
set.getInt(PLAYERS_ONLINE)
));
}
return Optional.empty();
}
};
}
public static Query<Optional<DateObj<Integer>>> fetchAllTimePeakPlayerCount(UUID serverUUID) {
return db -> db.query(fetchPeakPlayerCount(serverUUID, 0));
}
}

View File

@ -21,7 +21,7 @@ import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.Executable;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.access.queries.OptionalFetchQueries;
import com.djrapitops.plan.db.access.queries.objects.TPSQueries;
import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.db.access.transactions.commands.RemovePlayerTransaction;
import com.djrapitops.plan.db.sql.tables.PingTable;
@ -68,7 +68,7 @@ public class CleanTransaction extends Transaction {
@Override
protected void performOperations() {
Optional<Integer> allTimePeak = query(OptionalFetchQueries.fetchAllTimePeakPlayerCount(serverUUID)).map(DateObj::getValue);
Optional<Integer> allTimePeak = query(TPSQueries.fetchAllTimePeakPlayerCount(serverUUID)).map(DateObj::getValue);
execute(cleanTPSTable(allTimePeak.orElse(-1)));
execute(cleanPingTable());