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; 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(); 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); 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 -> stats.keySet().forEach(role ->
{ {
List<String> reportIds = stats.get(role) List<String> reportIds = stats.get(role)
.stream() .stream()
.map(Report::getId) .map(Report::getId)
.filter(Optional::isPresent) .filter(Optional::isPresent)
.limit(5) .map(Optional::get)
.map(Optional::get) .map(String::valueOf)
.map(String::valueOf) .sorted()
.collect(Collectors.toList()); .collect(Collectors.toList());
// don't show handler statistics if user has never handled a report // don't show handler statistics if user has never handled a report
if (role != ReportRole.HANDLER || reportIds.size() > 0) if (role != ReportRole.HANDLER || reportIds.size() > 0)

View File

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