Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Empty Submission Querying in graders_controller #7381

Merged
merged 9 commits into from
Jan 25, 2025

Conversation

hemmatio
Copy link
Member

@hemmatio hemmatio commented Jan 12, 2025

Proposed Changes

(Describe your changes here. Also describe the motivation for your changes: what problem do they solve, or how do they improve the application or codebase? If this pull request fixes an open issue, use a keyword to link this pull request to the issue.)

When "Assign graders to groups with no files submitted" is unselected when assigning graders to groups, MarkUs checks for empty submissions. This PR refactors the filter_empty_submissions method in graders_controller.rb to resolve an N+1 query issue when assigning graders to many submissions. The current implementation performs individual database queries for each grouping to check if a submission is empty.

Fix: Refactored the logic to use a single query that fetches all relevant grouping IDs and their submission statuses, significantly reducing redundant queries.
...

Screenshots of your changes (if applicable)
Associated documentation repository pull request (if applicable)

Type of Change

(Write an X or a brief description next to the type or types that best describe your changes.)

Type Applies?
🚨 Breaking change (fix or feature that would cause existing functionality to change)
New feature (non-breaking change that adds functionality)
🐛 Bug fix (non-breaking change that fixes an issue) X
🎨 User interface change (change to user interface; provide screenshots)
♻️ Refactoring (internal change to codebase, without changing functionality) X
🚦 Test update (change that only adds or modifies tests)
📦 Dependency update (change that updates a dependency)
🔧 Internal (change that only affects developers or continuous integration)

Checklist

(Complete each of the following items for your pull request. Indicate that you have completed an item by changing the [ ] into a [x] in the raw text, or by clicking on the checkbox in the rendered description on GitHub.)

Before opening your pull request:

  • I have performed a self-review of my changes.
    • Check that all changed files included in this pull request are intentional changes.
    • Check that all changes are relevant to the purpose of this pull request, as described above.
  • I have added tests for my changes, if applicable.
    • This is required for all bug fixes and new features.
  • I have updated the project documentation, if applicable.
    • This is required for new features.
  • If this is my first contribution, I have added myself to the list of contributors.

After opening your pull request:

  • I have updated the project Changelog (this is required for all changes).
  • I have verified that the pre-commit.ci checks have passed.
  • I have verified that the CI tests have passed.
  • I have reviewed the test coverage changes reported by Coveralls.
  • I have requested a review from a project maintainer.

Questions and Comments

(Include any questions or comments you have regarding your changes.)
12/01/25: I don't see any necessity for further tests as the ones in graders_controller_spec.rb are quite comprehensive, including cases testing skip_empty_submissions.

23/01/25: Created test case to confirm correct return of filter_empty_submissions

@hemmatio hemmatio marked this pull request as draft January 12, 2025 09:45
@hemmatio hemmatio changed the title Optimize Empty Submission Querying in graders_conroller Optimize Empty Submission Querying in graders_controller Jan 12, 2025
@coveralls
Copy link
Collaborator

coveralls commented Jan 12, 2025

Pull Request Test Coverage Report for Build 12960952565

Details

  • 20 of 20 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.003%) to 91.87%

Totals Coverage Status
Change from base Build 12895454287: 0.003%
Covered Lines: 41290
Relevant Lines: 44262

💛 - Coveralls

@hemmatio hemmatio marked this pull request as ready for review January 13, 2025 01:17
Copy link
Collaborator

@david-yz-liu david-yz-liu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @hemmatio, I left some inline comments; also please do a merge from master to update this branch.

end
submissions = Submission.where(grouping_id: grouping_ids, submission_version_used: true)
.pluck(:grouping_id, :is_empty)
non_empty_grouping_ids = submissions.reject { |_, is_empty| is_empty }.map { |grouping_id, _| grouping_id.to_s }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reject is awkward here; instead, you can move the is_empty check into the .where method above.

submissions = Submission.where(grouping_id: grouping_ids, submission_version_used: true)
.pluck(:grouping_id, :is_empty)
non_empty_grouping_ids = submissions.reject { |_, is_empty| is_empty }.map { |grouping_id, _| grouping_id.to_s }
non_empty_grouping_ids & grouping_ids
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The & grouping_ids isn't necessary, the where filter should only include grouping ids that were included in the original grouping_ids argument

@hemmatio hemmatio requested a review from david-yz-liu January 15, 2025 06:11
@hemmatio
Copy link
Member Author

hemmatio commented Jan 15, 2025

I'm seeing that the coverage decreased. I believe this is because I decreased the total number of lines, so the overall percentage is lower.

Copy link
Collaborator

@david-yz-liu david-yz-liu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hemmatio nice work! I left one inline comment; don't worry about the coverage decrease, I suspect you are correct.

submission && !submission.is_empty
end
Submission.where(grouping_id: grouping_ids, is_empty: false, submission_version_used: true)
.pluck(:grouping_id, :is_empty)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should only pluck grouping_id here (not sure why the tests didn't catch this, perhaps you could look into that)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call for filter_empty_submissions only compares the count before & after. The number of columns didn't affect the count, which is why no tests failed.

filtered_grouping_ids = filter_empty_submissions(grouping_ids)
          if filtered_grouping_ids.count != grouping_ids.count
            found_empty_submission = true
          end

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hemmatio this isn't accurate, the filtered_grouping_ids variable is used a bit further down in Line 137. If that branch is not currently being tested, you should add test cases for it

skip_empty_submissions: 'true'
}

filtered_grouping_ids = GradersController.new.__send__(:filter_empty_submissions, groupings)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary to call this method directly, especially since it's private. If you're trying to assessing that the correct branch of code was executed, you can check the contents of the flash.

@david-yz-liu david-yz-liu merged commit 10a7584 into MarkUsProject:master Jan 25, 2025
6 checks passed
@hemmatio hemmatio deleted the assign-grader-querying branch January 27, 2025 03:14
soheegoo pushed a commit to soheegoo/Markus that referenced this pull request Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants