mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-09-02 01:50:00 +02:00
Refactored CommandUseTable#getCommandUse to a query
This commit is contained in:
parent
e309f83c3d
commit
41275a1f84
|
@ -19,6 +19,8 @@ package com.djrapitops.plan.db.sql.queries;
|
||||||
import com.djrapitops.plan.db.access.Query;
|
import com.djrapitops.plan.db.access.Query;
|
||||||
import com.djrapitops.plan.db.access.QueryAllStatement;
|
import com.djrapitops.plan.db.access.QueryAllStatement;
|
||||||
import com.djrapitops.plan.db.access.QueryStatement;
|
import com.djrapitops.plan.db.access.QueryStatement;
|
||||||
|
import com.djrapitops.plan.db.sql.tables.CommandUseTable;
|
||||||
|
import com.djrapitops.plan.db.sql.tables.ServerTable;
|
||||||
import com.djrapitops.plan.db.sql.tables.UserInfoTable;
|
import com.djrapitops.plan.db.sql.tables.UserInfoTable;
|
||||||
import com.djrapitops.plan.db.sql.tables.UsersTable;
|
import com.djrapitops.plan.db.sql.tables.UsersTable;
|
||||||
|
|
||||||
|
@ -102,4 +104,33 @@ public class AggregateQueries {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Count how many times commands have been used on a server.
|
||||||
|
*
|
||||||
|
* @param serverUUID Server UUID of the Plan server.
|
||||||
|
* @return Map: Lowercase used command - Count of use times.
|
||||||
|
*/
|
||||||
|
public static Query<Map<String, Integer>> commandUsageCounts(UUID serverUUID) {
|
||||||
|
String sql = "SELECT " + CommandUseTable.COMMAND + ", " + CommandUseTable.TIMES_USED + " FROM " + CommandUseTable.TABLE_NAME +
|
||||||
|
"WHERE " + CommandUseTable.SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID;
|
||||||
|
|
||||||
|
return new QueryStatement<Map<String, Integer>>(sql, 5000) {
|
||||||
|
@Override
|
||||||
|
public void prepare(PreparedStatement statement) throws SQLException {
|
||||||
|
statement.setString(1, serverUUID.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Integer> processResults(ResultSet set) throws SQLException {
|
||||||
|
Map<String, Integer> commandUse = new HashMap<>();
|
||||||
|
while (set.next()) {
|
||||||
|
String cmd = set.getString(CommandUseTable.COMMAND).toLowerCase();
|
||||||
|
int amountUsed = set.getInt(CommandUseTable.TIMES_USED);
|
||||||
|
commandUse.put(cmd, amountUsed);
|
||||||
|
}
|
||||||
|
return commandUse;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -23,6 +23,7 @@ import com.djrapitops.plan.data.store.mutators.PlayersMutator;
|
||||||
import com.djrapitops.plan.data.store.mutators.SessionsMutator;
|
import com.djrapitops.plan.data.store.mutators.SessionsMutator;
|
||||||
import com.djrapitops.plan.db.SQLDB;
|
import com.djrapitops.plan.db.SQLDB;
|
||||||
import com.djrapitops.plan.db.access.Query;
|
import com.djrapitops.plan.db.access.Query;
|
||||||
|
import com.djrapitops.plan.db.sql.queries.AggregateQueries;
|
||||||
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
|
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
|
||||||
import com.djrapitops.plan.system.cache.SessionCache;
|
import com.djrapitops.plan.system.cache.SessionCache;
|
||||||
import com.djrapitops.plan.system.info.server.Server;
|
import com.djrapitops.plan.system.info.server.Server;
|
||||||
|
@ -74,7 +75,7 @@ public class ServerContainerQuery implements Query<ServerContainer> {
|
||||||
return db.query(OptionalFetchQueries.fetchPeakPlayerCount(serverUUID, twoDaysAgo)).orElse(null);
|
return db.query(OptionalFetchQueries.fetchPeakPlayerCount(serverUUID, twoDaysAgo)).orElse(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
container.putCachingSupplier(ServerKeys.COMMAND_USAGE, () -> db.getCommandUseTable().getCommandUse(serverUUID));
|
container.putCachingSupplier(ServerKeys.COMMAND_USAGE, () -> db.query(AggregateQueries.commandUsageCounts(serverUUID)));
|
||||||
container.putCachingSupplier(ServerKeys.WORLD_TIMES, () -> db.getWorldTimesTable().getWorldTimesOfServer(serverUUID));
|
container.putCachingSupplier(ServerKeys.WORLD_TIMES, () -> db.getWorldTimesTable().getWorldTimesOfServer(serverUUID));
|
||||||
|
|
||||||
// Calculating getters
|
// Calculating getters
|
||||||
|
|
|
@ -27,10 +27,7 @@ import com.djrapitops.plan.db.sql.parsing.Sql;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Table that is in charge of storing command data.
|
* Table that is in charge of storing command data.
|
||||||
|
@ -72,37 +69,6 @@ public class CommandUseTable extends Table {
|
||||||
.toString();
|
.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Used to get all commands used in a server.
|
|
||||||
*
|
|
||||||
* @param serverUUID UUID of the server.
|
|
||||||
* @return command - times used Map
|
|
||||||
*/
|
|
||||||
public Map<String, Integer> getCommandUse(UUID serverUUID) {
|
|
||||||
String sql = Select.from(tableName,
|
|
||||||
COMMAND, TIMES_USED)
|
|
||||||
.where(SERVER_ID + "=" + serverTable.statementSelectServerID)
|
|
||||||
.toString();
|
|
||||||
|
|
||||||
return query(new QueryStatement<Map<String, Integer>>(sql, 5000) {
|
|
||||||
@Override
|
|
||||||
public void prepare(PreparedStatement statement) throws SQLException {
|
|
||||||
statement.setString(1, serverUUID.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<String, Integer> processResults(ResultSet set) throws SQLException {
|
|
||||||
Map<String, Integer> commandUse = new HashMap<>();
|
|
||||||
while (set.next()) {
|
|
||||||
String cmd = set.getString(COMMAND).toLowerCase();
|
|
||||||
int amountUsed = set.getInt(TIMES_USED);
|
|
||||||
commandUse.put(cmd, amountUsed);
|
|
||||||
}
|
|
||||||
return commandUse;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void commandUsed(String command) {
|
public void commandUsed(String command) {
|
||||||
if (command.length() > 20) {
|
if (command.length() > 20) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -31,6 +31,7 @@ import com.djrapitops.plan.data.time.WorldTimes;
|
||||||
import com.djrapitops.plan.db.access.Query;
|
import com.djrapitops.plan.db.access.Query;
|
||||||
import com.djrapitops.plan.db.access.transactions.*;
|
import com.djrapitops.plan.db.access.transactions.*;
|
||||||
import com.djrapitops.plan.db.patches.Patch;
|
import com.djrapitops.plan.db.patches.Patch;
|
||||||
|
import com.djrapitops.plan.db.sql.queries.AggregateQueries;
|
||||||
import com.djrapitops.plan.db.sql.queries.LargeFetchQueries;
|
import com.djrapitops.plan.db.sql.queries.LargeFetchQueries;
|
||||||
import com.djrapitops.plan.db.sql.queries.LargeStoreQueries;
|
import com.djrapitops.plan.db.sql.queries.LargeStoreQueries;
|
||||||
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
|
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
|
||||||
|
@ -172,7 +173,7 @@ public abstract class CommonDBTest {
|
||||||
|
|
||||||
commitTest();
|
commitTest();
|
||||||
|
|
||||||
Map<String, Integer> commandUse = db.getCommandUseTable().getCommandUse(serverUUID);
|
Map<String, Integer> commandUse = db.query(AggregateQueries.commandUsageCounts(serverUUID));
|
||||||
assertEquals(expected, commandUse);
|
assertEquals(expected, commandUse);
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
|
@ -186,7 +187,7 @@ public abstract class CommonDBTest {
|
||||||
expected.put("test", 3);
|
expected.put("test", 3);
|
||||||
expected.put("tp", 6);
|
expected.put("tp", 6);
|
||||||
|
|
||||||
commandUse = db.getCommandUseTable().getCommandUse(serverUUID);
|
commandUse = db.query(AggregateQueries.commandUsageCounts(serverUUID));
|
||||||
|
|
||||||
assertEquals(expected, commandUse);
|
assertEquals(expected, commandUse);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user