perf(compliance-assessments): replace per-row progress walk with aggregate#4014
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughAdds ChangesCompliance Assessment Progress Computation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
monsieurswag
left a comment
There was a problem hiding this comment.
There is a small logic bug in the "slow path" assessed increment condition which MUST be fixed.
Also remove the comments, the code is already really clean, the comments just make it less readable.
Once these are fixed, this is ready to be merged.
| if ( | ||
| requirement_assessment.result | ||
| != RequirementAssessment.Result.NOT_ASSESSED | ||
| ) or requirement_assessment.score is not None: | ||
| assessed += 1 |
There was a problem hiding this comment.
The ) or requirement_assessment.score is not None: should be replaced by ) and requirement_assessment.score is not None: At backend/core/models.py:7895:
if (
requirement_assessment.result
!= RequirementAssessment.Result.NOT_ASSESSED
) or requirement_assessment.score is not None:
assessed += 1With the current code a requirement with a None score OR with a NOT_ASSESSED result would still be considered as assessed (would still increment assessed).
There was a problem hiding this comment.
I think it should be left as is, because sometimes, audits can be assessed by just giving a score to the requirement nodes without giving a specific result on them. Putting and would change the original logic. The previous version of the code was like this, with an OR :
def progress(self) -> int:
requirement_assessments = list(
self.get_requirement_assessments(include_non_assessable=False)
)
total_cnt = len(requirement_assessments)
assessed_cnt = len(
[
r
for r in requirement_assessments
if (r.result != RequirementAssessment.Result.NOT_ASSESSED)
or r.score != None
]
)
return int((assessed_cnt / total_cnt) * 100) if total_cnt > 0 else 0We could've put and if the code was written like this:
if (
requirement_assessment.result
== RequirementAssessment.Result.NOT_ASSESSED
) and requirement_assessment.score is None:
continue
assessed += 1There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@backend/core/models.py`:
- Around line 7845-7849: There is a whitespace-only blank line breaking ruff
format in the block around the docstring and the RequirementAssessment query;
remove the stray blank line between the docstring and the requirements =
RequirementAssessment.objects.filter(...) statement (the function that returns
"(total, assessed) counts for assessable requirements") and re-run the formatter
(ruff format) on backend/core/models.py so the file passes `ruff format
--check`.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
Why
The goal is to reduce the cost of calculating
progress, especially during batch operations such as builtin metrics backfilling.Changes
In
backend/core/models.py, theComplianceAssessment.progressproperty was refactored to use a faster helper:_get_progress_counts().Before this change,
progresswas usingget_requirement_assessments(include_non_assessable=False), which loads a large amount of related data we don't need. It was prefetching Questions, Answers, Choices, Evidences, Applied Controls, and other related objects, even thoughprogressonly needs to know 3 things :The new implementation keeps the same logic, but computes way faster than before (~1 second instead of almost 1 minute for 81 audits on my end).
There are now 2 different ways to compute progress:
totalandassessedSummary by CodeRabbit