Optimize getting of ReportProfile's from database

This commit is contained in:
Keir Nellyer 2016-07-08 17:14:41 -04:00
parent 1ce1c796f4
commit a0d046ab8b
2 changed files with 40 additions and 32 deletions

View File

@ -46,11 +46,11 @@ public enum ReportCategory
return _humanName;
}
public static ReportCategory getById(int databaseId)
public static ReportCategory getById(int id)
{
for (ReportCategory reportCategory : values())
{
if (reportCategory.getId() == databaseId)
if (reportCategory.getId() == id)
{
return reportCategory;
}

View File

@ -12,19 +12,18 @@ import java.util.stream.Collectors;
import org.bukkit.plugin.java.JavaPlugin;
import com.google.common.base.Preconditions;
import mineplex.serverdata.database.DBPool;
public class ReportProfileRepository
{
// todo test this query
// todo optimize this query so it only needs run once per fetch
private static final String GRAB_RESULT_COUNT = "SELECT COUNT(*) AS count " +
" FROM reports, reportReasons reasons, reportResults results " +
" WHERE results.reportId = reports.id " +
" AND results.reportId = reports.id" +
" AND reasons.reporterId = ?" +
" AND reports.categoryId = ?" +
" AND results.resultId = ?;";
private static final String GRAB_RESULT_COUNT = "SELECT reports.categoryId, results.resultId, COUNT(*) AS count" +
" FROM reports, reportReasons reasons, reportResults results, reportResultTypes resultTypes" +
" WHERE results.reportId = reports.id" +
" AND reasons.reportId = reports.id" +
" AND resultTypes.id = results.resultId" +
" AND reasons.reporterId = ?" +
" GROUP BY reports.categoryId, results.resultId;";
private final JavaPlugin _plugin;
@ -44,34 +43,43 @@ public class ReportProfileRepository
{
ReportProfile reportProfile = new ReportProfile();
for (ReportCategory reportCategory : ReportCategory.values())
try (Connection connection = DBPool.getAccount().getConnection())
{
for (ReportResultType resultType : ReportResultType.values())
{
// only get global stat if global category
// only get non-global stat if non-global category
if (resultType.isGlobalStat() == (reportCategory == ReportCategory.GLOBAL))
{
try (Connection connection = DBPool.getAccount().getConnection())
{
PreparedStatement preparedStatement = connection.prepareStatement(GRAB_RESULT_COUNT);
preparedStatement.setInt(1, accountId); // reporterId
preparedStatement.setInt(2, reportCategory.getId()); // categoryId
preparedStatement.setInt(3, resultType.getId()); // resultId
PreparedStatement preparedStatement = connection.prepareStatement(GRAB_RESULT_COUNT);
preparedStatement.setInt(1, accountId);
ResultSet resultSet = preparedStatement.executeQuery();
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next())
{
reportProfile.setValue(reportCategory, resultType, resultSet.getInt("count"));
}
}
catch (SQLException e)
while (resultSet.next())
{
try
{
int categoryId = resultSet.getInt("reports.categoryId");
int resultTypeId = resultSet.getInt("results.resultId");
int count = resultSet.getInt("count");
ReportCategory category = ReportCategory.getById(categoryId);
ReportResultType resultType = ReportResultType.getById(resultTypeId);
Preconditions.checkNotNull(category, "Invalid category id: " + categoryId);
Preconditions.checkNotNull(resultType, "Invalid result type id: " + resultType);
if (resultType.isGlobalStat())
{
throw new RuntimeException(e);
category = ReportCategory.GLOBAL;
}
reportProfile.setValue(category, resultType, count);
}
catch (Exception ex)
{
_plugin.getLogger().log(Level.SEVERE, "Error getting ReportProfile (id: " + accountId + ").", ex);
}
}
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
return reportProfile;
}).exceptionally(throwable ->