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

Fixed N+1 query occurrence in the /api/v1/facility/ #2260

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions care/facility/api/serializers/facility.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ class FacilityBasicInfoSerializer(serializers.ModelSerializer):
bed_count = serializers.SerializerMethodField()

def get_bed_count(self, facility):
if hasattr(facility, "bed_count"):
return facility.bed_count
return Bed.objects.filter(facility=facility).count()

def get_patient_count(self, facility):
if hasattr(facility, "patient_count"):
return facility.patient_count
return PatientRegistration.objects.filter(
facility=facility, is_active=True
).count()
Expand Down
13 changes: 10 additions & 3 deletions care/facility/api/viewsets/facility.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.conf import settings
from django.db.models import Count, Q
from django_filters import rest_framework as filters
from djqscsv import render_to_csv_response
from drf_spectacular.utils import extend_schema, extend_schema_view
Expand Down Expand Up @@ -71,8 +72,15 @@ class FacilityViewSet(
):
"""Viewset for facility CRUD operations."""

queryset = Facility.objects.all().select_related(
"ward", "local_body", "district", "state"
queryset = (
Facility.objects.all()
.select_related("ward", "local_body", "district", "state")
.annotate(
bed_count=Count("bed"),
patient_count=Count(
Comment on lines +79 to +80
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we exclude related beds and patients that are deleted?

Copy link
Member

Choose a reason for hiding this comment

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

cc: @sainak

"patientregistration", filter=Q(patientregistration__is_active=True)
),
)
)
permission_classes = (
IsAuthenticated,
Expand Down Expand Up @@ -147,7 +155,6 @@ def list(self, request, *args, **kwargs):
return render_to_csv_response(
queryset, field_header_map=mapping, field_serializer_map=pretty_mapping
)

return super(FacilityViewSet, self).list(request, *args, **kwargs)

@extend_schema(tags=["facility"])
Expand Down
3 changes: 2 additions & 1 deletion care/facility/tests/test_facility_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def test_all_listing(self):
self.assertIs(response.status_code, status.HTTP_200_OK)

def test_listing(self):
dhruv-goyal-10 marked this conversation as resolved.
Show resolved Hide resolved
response = self.client.get("/api/v1/facility/")
with self.assertNumQueries(3):
response = self.client.get("/api/v1/facility/")
self.assertIs(response.status_code, status.HTTP_200_OK)

def test_create(self):
Expand Down