Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""OWASP Project Health Metrics Ordering."""

from strawberry import auto
import strawberry
from strawberry_django import order_type

from apps.owasp.models.project_health_metrics import ProjectHealthMetrics
Expand All @@ -10,4 +10,10 @@
class ProjectHealthMetricsOrder:
"""Ordering for Project Health Metrics."""

score: auto
score: strawberry.auto

# We need to order by another field in case of equal scores
# to ensure unique metrics in pagination.
# The ORM returns random ordered query set if no order is specified.
# We don't do ordering in the model since we order already in the query.
project__name: strawberry.auto
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any reason for having __ in the name?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

strawberry use these attributes in the order_by method. project_name is not an attribute of the metrics but it is an attribute of the project. And like we access nested attributes in django methods, strawberry does the same.

3 changes: 2 additions & 1 deletion backend/apps/owasp/models/project_health_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ def get_latest_health_metrics() -> models.QuerySet["ProjectHealthMetrics"]:
ProjectHealthMetrics.objects.filter(project=models.OuterRef("project"))
.order_by("-nest_created_at")
.values("nest_created_at")[:1]
)
),
project__is_active=True,
)

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ def test_order_fields(self):
order_fields = {
field.name for field in ProjectHealthMetricsOrder.__strawberry_definition__.fields
}
expected_fields = {"score"}
expected_fields = {"score", "project__name"}
assert expected_fields == order_fields

def test_order_by(self):
"""Test ordering by score."""
order_instance = ProjectHealthMetricsOrder(
score="DESC",
)
order_instance = ProjectHealthMetricsOrder(score="DESC", project__name="ASC")
assert order_instance.score == "DESC"
assert order_instance.project__name == "ASC"
14 changes: 12 additions & 2 deletions frontend/src/app/projects/dashboard/metrics/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ const MetricsPage: FC = () => {
variables: {
filters,
pagination: { offset: 0, limit: PAGINATION_LIMIT },
ordering,
ordering: [
ordering,
{
['project_Name']: 'ASC',
Copy link
Collaborator

Choose a reason for hiding this comment

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

What naming convention was used here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is automatically converted by strawberry. Since the attribute is project__name, it is handled like other attributes. Just first underscore removed and capitalize the second name.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure how it works though. Let's merge it as is and improve sorting capabilities later.

},
],
},
})

Expand Down Expand Up @@ -246,7 +251,12 @@ const MetricsPage: FC = () => {
variables: {
filters,
pagination: newPagination,
ordering,
ordering: [
ordering,
{
['project_Name']: 'ASC',
},
],
},
updateQuery: (prev, { fetchMoreResult }) => {
if (!fetchMoreResult) return prev
Expand Down