From eabaa1e68ae481e41f32f8f6457b544500e1aa96 Mon Sep 17 00:00:00 2001 From: Keir Nellyer Date: Fri, 19 Aug 2016 17:01:26 +0100 Subject: [PATCH] Move limit statistic limit to query and sort returned results --- .../mineplex/core/report/ReportManager.java | 4 +-- .../report/command/ReportStatsCommand.java | 16 ++++----- .../core/report/data/ReportRepository.java | 33 +++++++++++-------- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java index 00de61ae6..532951c0d 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java @@ -523,9 +523,9 @@ public class ReportManager return future; } - public CompletableFuture> getStatistics(int accountId) + public CompletableFuture> getStatistics(int accountId, int amount) { - return _reportRepository.getAccountStatistics(accountId).thenApply(multimap -> + return _reportRepository.getAccountStatistics(accountId, amount).thenApply(multimap -> { Multimap> reportMultiMap = HashMultimap.create(); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/report/command/ReportStatsCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/report/command/ReportStatsCommand.java index 5090477d4..38822e321 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/report/command/ReportStatsCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/report/command/ReportStatsCommand.java @@ -40,17 +40,17 @@ public class ReportStatsCommand extends CommandBase { 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 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) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/report/data/ReportRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/report/data/ReportRepository.java index 9b7ddbcb5..0b3a767e7 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/report/data/ReportRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/report/data/ReportRepository.java @@ -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> getAccountStatistics(int accountId) + public CompletableFuture> getAccountStatistics(int accountId, int amount) { CompletableFuture> future = CompletableFuture.supplyAsync(() -> { try (Connection connection = DBPool.getAccount().getConnection()) { Multimap 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 getReports(Connection connection, String sql, int accountId) throws SQLException + private Set 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 reportIds = new HashSet<>();