Skip to content

Commit

Permalink
SAK-46316 avoid unnecessary getCategory calls as the call is very exp…
Browse files Browse the repository at this point in the history
…ensive (sakaiproject#9859)
  • Loading branch information
ottenhoff authored Sep 24, 2021
1 parent 71d59e1 commit 8635a3e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -760,10 +760,11 @@ public void finalizeGrades(String gradebookUid)
* @param categoryId id of category
* @param isInstructor will determine whether category score includes non-released items
* @param categoryType category type of the gradebook
* @param equalWeightAssignments whether category is equal-weighting regardless of points
* @return percentage and dropped items, or empty if no calculations were made
*
*/
Optional<CategoryScoreData> calculateCategoryScore(Long gradebookId, String studentUuid, Long categoryId, boolean includeNonReleasedItems, int categoryType);
Optional<CategoryScoreData> calculateCategoryScore(Long gradebookId, String studentUuid, Long categoryId, boolean includeNonReleasedItems, int categoryType, Boolean equalWeightAssignments);

/**
* Calculate the category score for the given gradebook, category, assignments in the category and grade map. This doesn't do any
Expand All @@ -775,6 +776,7 @@ public void finalizeGrades(String gradebookUid)
* @param category the category
* @param categoryAssignments list of assignments the student can view, and are in the category
* @param gradeMap map of assignmentId to grade, to use for the calculations
* @param includeNonReleasedItems relevant for student view
* @return percentage and dropped items, or empty if no calculations were made
*/
Optional<CategoryScoreData> calculateCategoryScore(Object gradebook, String studentUuid, CategoryDefinition category,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3012,11 +3012,12 @@ public Optional<CategoryScoreData> calculateCategoryScore(final Object gradebook
}
}

return calculateCategoryScore(studentUuid, category.getId(), gradeRecords, includeNonReleasedItems, gb.getCategory_type());
return calculateCategoryScore(studentUuid, category.getId(), gradeRecords, includeNonReleasedItems, gb.getCategory_type(), category.getEqualWeight());
}

@Override
public Optional<CategoryScoreData> calculateCategoryScore(final Long gradebookId, final String studentUuid, final Long categoryId, final boolean includeNonReleasedItems, int categoryType) {
public Optional<CategoryScoreData> calculateCategoryScore(final Long gradebookId, final String studentUuid, final Long categoryId,
final boolean includeNonReleasedItems, int categoryType, Boolean equalWeightAssignments) {

// get all grade records for the student
@SuppressWarnings({ "unchecked", "rawtypes" })
Expand All @@ -3031,7 +3032,7 @@ public Object doInHibernate(final Session session) throws HibernateException {
// apply the settings
final List<AssignmentGradeRecord> gradeRecords = gradeRecMap.get(studentUuid);

return calculateCategoryScore(studentUuid, categoryId, gradeRecords, includeNonReleasedItems, categoryType);
return calculateCategoryScore(studentUuid, categoryId, gradeRecords, includeNonReleasedItems, categoryType, equalWeightAssignments);
}

/**
Expand All @@ -3043,7 +3044,7 @@ public Object doInHibernate(final Session session) throws HibernateException {
* @return
*/
private Optional<CategoryScoreData> calculateCategoryScore(final String studentUuid, final Long categoryId,
final List<AssignmentGradeRecord> gradeRecords, final boolean includeNonReleasedItems, final int categoryType) {
final List<AssignmentGradeRecord> gradeRecords, final boolean includeNonReleasedItems, final int categoryType, Boolean equalWeightAssignments) {

// validate
if (gradeRecords == null) {
Expand All @@ -3062,7 +3063,6 @@ private Optional<CategoryScoreData> calculateCategoryScore(final String studentU
BigDecimal totalEarned = new BigDecimal("0");
BigDecimal totalEarnedMean = new BigDecimal("0");
BigDecimal totalPossible = new BigDecimal("0");
Category category = getCategory(categoryId);

// apply any drop/keep settings for this category
applyDropScores(gradeRecords, categoryType);
Expand Down Expand Up @@ -3150,7 +3150,11 @@ private Optional<CategoryScoreData> calculateCategoryScore(final String studentU
GradebookService.MATH_CONTEXT)
.multiply(new BigDecimal("100"));

if (category.isEqualWeightAssignments()) {
if (equalWeightAssignments == null) {
Category category = getCategory(categoryId);
equalWeightAssignments = category.isEqualWeightAssignments();
}
if (equalWeightAssignments) {
mean = totalEarnedMean.divide(new BigDecimal(numScored), GradebookService.MATH_CONTEXT).multiply(new BigDecimal("100"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2533,7 +2533,7 @@ public Optional<CategoryScoreData> getCategoryScoreForStudent(final Long categor

final Gradebook gradebook = getGradebook();

final Optional<CategoryScoreData> result = gradebookService.calculateCategoryScore(gradebook.getId(), studentUuid, categoryId, isInstructor, gradebook.getCategory_type());
final Optional<CategoryScoreData> result = gradebookService.calculateCategoryScore(gradebook.getId(), studentUuid, categoryId, isInstructor, gradebook.getCategory_type(), null);
log.debug("Category score for category: {}, student: {}:{}", categoryId, studentUuid, result.map(r -> r.score).orElse(null));

return result;
Expand Down

0 comments on commit 8635a3e

Please sign in to comment.