Move limit statistic limit to query and sort returned results

This commit is contained in:
Keir Nellyer 2016-08-19 17:01:26 +01:00
parent 834e6b5fdf
commit eabaa1e68a
3 changed files with 30 additions and 23 deletions

View File

@ -523,9 +523,9 @@ public class ReportManager
return future;
}
public CompletableFuture<Multimap<ReportRole, Report>> getStatistics(int accountId)
public CompletableFuture<Multimap<ReportRole, Report>> getStatistics(int accountId, int amount)
{
return _reportRepository.getAccountStatistics(accountId).thenApply(multimap ->
return _reportRepository.getAccountStatistics(accountId, amount).thenApply(multimap ->
{
Multimap<ReportRole, CompletableFuture<Report>> reportMultiMap = HashMultimap.create();

View File

@ -40,17 +40,17 @@ public class ReportStatsCommand extends CommandBase<ReportPlugin>
{
int accountId = _commandCenter.GetClientManager().getAccountId(target);
Plugin.getReportManager().getStatistics(accountId).thenCompose(BukkitFuture.accept(stats ->
Plugin.getReportManager().getStatistics(accountId, 5).thenCompose(BukkitFuture.accept(stats ->
stats.keySet().forEach(role ->
{
List<String> reportIds = stats.get(role)
.stream()
.map(Report::getId)
.filter(Optional::isPresent)
.limit(5)
.map(Optional::get)
.map(String::valueOf)
.collect(Collectors.toList());
.stream()
.map(Report::getId)
.filter(Optional::isPresent)
.map(Optional::get)
.map(String::valueOf)
.sorted()
.collect(Collectors.toList());
// don't show handler statistics if user has never handled a report
if (role != ReportRole.HANDLER || reportIds.size() > 0)

View File

@ -113,17 +113,23 @@ public class ReportRepository
/** STATISTICS **/
private static final String STATISTICS_GET_REPORTS_MADE = "SELECT reports.id FROM reports, reportReasons" +
" WHERE reports.id = reportReasons.reportId" +
" AND reportReasons.reporterId = ?;";
private static final String STATISTICS_GET_REPORTS_MADE = "SELECT reports.id FROM reports, reportReasons\n" +
"WHERE reports.id = reportReasons.reportId\n" +
" AND reportReasons.reporterId = ?\n" +
"ORDER BY reports.id DESC\n" +
"LIMIT ?;";
// TODO: handle when handler isn't necessarily the closer
private static final String STATISTICS_GET_REPORTS_HANDLED = "SELECT reports.id FROM reports, reportHandlers" +
" WHERE reports.id = reportHandlers.reportId" +
" AND reportHandlers.handlerId = ?;";
private static final String STATISTICS_GET_REPORTS_HANDLED = "SELECT reports.id FROM reports, reportHandlers\n" +
"WHERE reports.id = reportHandlers.reportId\n" +
" AND reportHandlers.handlerId = ?\n" +
"ORDER BY reports.id DESC\n" +
"LIMIT ?;";
private static final String STATISTICS_GET_REPORTS_AGAINST = "SELECT reports.id FROM reports" +
" WHERE reports.suspectId = ?;";
private static final String STATISTICS_GET_REPORTS_AGAINST = "SELECT reports.id FROM reports\n" +
"WHERE reports.suspectId = ?\n" +
"ORDER BY reports.id DESC\n" +
"LIMIT ?;";
/** BANS **/
@ -583,16 +589,16 @@ public class ReportRepository
return future;
}
public CompletableFuture<Multimap<ReportRole, Long>> getAccountStatistics(int accountId)
public CompletableFuture<Multimap<ReportRole, Long>> getAccountStatistics(int accountId, int amount)
{
CompletableFuture<Multimap<ReportRole, Long>> future = CompletableFuture.supplyAsync(() ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
Multimap<ReportRole, Long> reportIds = HashMultimap.create();
reportIds.putAll(ReportRole.REPORTER, getReports(connection, STATISTICS_GET_REPORTS_MADE, accountId));
reportIds.putAll(ReportRole.HANDLER, getReports(connection, STATISTICS_GET_REPORTS_HANDLED, accountId));
reportIds.putAll(ReportRole.SUSPECT, getReports(connection, STATISTICS_GET_REPORTS_AGAINST, accountId));
reportIds.putAll(ReportRole.REPORTER, getReports(connection, STATISTICS_GET_REPORTS_MADE, accountId, amount));
reportIds.putAll(ReportRole.HANDLER, getReports(connection, STATISTICS_GET_REPORTS_HANDLED, accountId, amount));
reportIds.putAll(ReportRole.SUSPECT, getReports(connection, STATISTICS_GET_REPORTS_AGAINST, accountId, amount));
return reportIds;
}
catch (SQLException e)
@ -610,10 +616,11 @@ public class ReportRepository
return future;
}
private Set<Long> getReports(Connection connection, String sql, int accountId) throws SQLException
private Set<Long> getReports(Connection connection, String sql, int accountId, int amount) throws SQLException
{
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, accountId);
preparedStatement.setInt(2, amount);
ResultSet resultSet = preparedStatement.executeQuery();
Set<Long> reportIds = new HashSet<>();