Optimize getting of ReportProfile's from database
This commit is contained in:
parent
1ce1c796f4
commit
a0d046ab8b
@ -46,11 +46,11 @@ public enum ReportCategory
|
|||||||
return _humanName;
|
return _humanName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ReportCategory getById(int databaseId)
|
public static ReportCategory getById(int id)
|
||||||
{
|
{
|
||||||
for (ReportCategory reportCategory : values())
|
for (ReportCategory reportCategory : values())
|
||||||
{
|
{
|
||||||
if (reportCategory.getId() == databaseId)
|
if (reportCategory.getId() == id)
|
||||||
{
|
{
|
||||||
return reportCategory;
|
return reportCategory;
|
||||||
}
|
}
|
||||||
|
@ -12,19 +12,18 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
import mineplex.serverdata.database.DBPool;
|
import mineplex.serverdata.database.DBPool;
|
||||||
|
|
||||||
public class ReportProfileRepository
|
public class ReportProfileRepository
|
||||||
{
|
{
|
||||||
// todo test this query
|
private static final String GRAB_RESULT_COUNT = "SELECT reports.categoryId, results.resultId, COUNT(*) AS count" +
|
||||||
// todo optimize this query so it only needs run once per fetch
|
" FROM reports, reportReasons reasons, reportResults results, reportResultTypes resultTypes" +
|
||||||
private static final String GRAB_RESULT_COUNT = "SELECT COUNT(*) AS count " +
|
" WHERE results.reportId = reports.id" +
|
||||||
" FROM reports, reportReasons reasons, reportResults results " +
|
" AND reasons.reportId = reports.id" +
|
||||||
" WHERE results.reportId = reports.id " +
|
" AND resultTypes.id = results.resultId" +
|
||||||
" AND results.reportId = reports.id" +
|
" AND reasons.reporterId = ?" +
|
||||||
" AND reasons.reporterId = ?" +
|
" GROUP BY reports.categoryId, results.resultId;";
|
||||||
" AND reports.categoryId = ?" +
|
|
||||||
" AND results.resultId = ?;";
|
|
||||||
|
|
||||||
private final JavaPlugin _plugin;
|
private final JavaPlugin _plugin;
|
||||||
|
|
||||||
@ -44,34 +43,43 @@ public class ReportProfileRepository
|
|||||||
{
|
{
|
||||||
ReportProfile reportProfile = new ReportProfile();
|
ReportProfile reportProfile = new ReportProfile();
|
||||||
|
|
||||||
for (ReportCategory reportCategory : ReportCategory.values())
|
try (Connection connection = DBPool.getAccount().getConnection())
|
||||||
{
|
{
|
||||||
for (ReportResultType resultType : ReportResultType.values())
|
PreparedStatement preparedStatement = connection.prepareStatement(GRAB_RESULT_COUNT);
|
||||||
{
|
preparedStatement.setInt(1, accountId);
|
||||||
// only get global stat if global category
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
// 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
|
|
||||||
|
|
||||||
ResultSet resultSet = preparedStatement.executeQuery();
|
while (resultSet.next())
|
||||||
if (resultSet.next())
|
{
|
||||||
{
|
try
|
||||||
reportProfile.setValue(reportCategory, resultType, resultSet.getInt("count"));
|
{
|
||||||
}
|
int categoryId = resultSet.getInt("reports.categoryId");
|
||||||
}
|
int resultTypeId = resultSet.getInt("results.resultId");
|
||||||
catch (SQLException e)
|
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;
|
return reportProfile;
|
||||||
}).exceptionally(throwable ->
|
}).exceptionally(throwable ->
|
||||||
|
Loading…
Reference in New Issue
Block a user