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

Reduce uneeded calls to DB #510

Merged
merged 3 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions backend/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,21 @@ def _get_all_requirement_nodes_id_in_requirement_node(
def get_sorted_requirement_nodes(
requirement_nodes: list,
requirements_assessed: list | None,
max_score: int,
) -> dict:
"""
Recursive function to build framework groups tree
requirement_nodes: the list of all requirement_nodes
requirements_assessed: the list of all requirements_assessed
max_score: the maximum score. This is an attribute of the framework
Returns a dictionary containing key=name and value={"description": description, "style": "leaf|node"}}
Values are correctly sorted based on order_id
If order_id is missing, sorting is based on created_at

"""

# TODO: pass the framework attributes later on instead of max_score

# cope for old version not creating order_id correctly
for req in requirement_nodes:
if req.order_id is None:
Expand Down Expand Up @@ -264,11 +269,7 @@ def get_sorted_requirement_nodes_rec(
"status": req_as.status if requirements_assessed else None,
"is_scored": req_as.is_scored if requirements_assessed else None,
"score": req_as.score if requirements_assessed else None,
"max_score": (
req_as.compliance_assessment.framework.max_score
if requirements_assessed
else None
),
"max_score": (max_score if requirements_assessed else None),
"status_display": (
req_as.get_status_display() if requirements_assessed else None
),
Expand Down Expand Up @@ -308,7 +309,7 @@ def get_sorted_requirement_nodes_rec(
"status": req_as.status,
"is_scored": req_as.is_scored,
"score": req_as.score,
"max_score": req_as.compliance_assessment.framework.max_score,
"max_score": max_score,
"status_display": req_as.get_status_display(),
"status_i18n": camel_case(req_as.status),
"style": "leaf",
Expand Down
12 changes: 10 additions & 2 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,9 @@ def tree(self, request, pk):
_framework = Framework.objects.get(id=pk)
return Response(
get_sorted_requirement_nodes(
RequirementNode.objects.filter(framework=_framework).all(), None
RequirementNode.objects.filter(framework=_framework).all(),
None,
_framework.max_score,
)
)

Expand Down Expand Up @@ -1366,6 +1368,7 @@ def tree(self, request, pk):
RequirementAssessment.objects.filter(
compliance_assessment=self.get_object()
).all(),
_framework.max_score,
)
implementation_groups = self.get_object().selected_implementation_groups
return Response(
Expand Down Expand Up @@ -1546,7 +1549,11 @@ def generate_html(
).all()

implementation_groups = compliance_assessment.selected_implementation_groups
graph = get_sorted_requirement_nodes(list(requirement_nodes), list(assessments))
graph = get_sorted_requirement_nodes(
list(requirement_nodes),
list(assessments),
compliance_assessment.framework.max_score,
)
graph = filter_graph_by_implementation_groups(graph, implementation_groups)
flattened_graph = flatten_dict(graph)

Expand Down Expand Up @@ -1604,6 +1611,7 @@ def bar_graph(node: RequirementNode):
content = """
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://unpkg.com/dezui@latest">
<script src="https://cdn.tailwindcss.com"></script>
<title>Compliance report</title>
Expand Down
2 changes: 1 addition & 1 deletion backend/library/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def tree(self, request, pk):

preview = preview_library(framework)
return Response(
get_sorted_requirement_nodes(preview.get("requirement_nodes"), None)
get_sorted_requirement_nodes(preview.get("requirement_nodes"), None, None)
)

@action(detail=False, methods=["post"], url_path="upload")
Expand Down
Loading