Skip to content

Commit

Permalink
Merge pull request #2192 from IFRCGo/feature/fix-eventlist-n+1
Browse files Browse the repository at this point in the history
Fix eventlist personnel count query n+1 issue
  • Loading branch information
szabozoltan69 authored Jul 3, 2024
2 parents 777c3a3 + aa8dc01 commit bbb563c
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 165 deletions.
12 changes: 12 additions & 0 deletions api/drf_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ class EventViewset(ReadOnlyVisibilityViewset):

def get_queryset(self, *args, **kwargs):
# import pdb; pdb.set_trace();
today = timezone.now().date().strftime("%Y-%m-%d")
qset = super().get_queryset()
if self.action == "mini_events":
# return Event.objects.filter(parent_event__isnull=True).select_related('dtype')
Expand All @@ -648,6 +649,17 @@ def get_queryset(self, *args, **kwargs):
),
),
)
.annotate(
active_deployments=Count(
"personneldeployment__personnel",
filter=Q(
personneldeployment__personnel__type=Personnel.TypeChoices.RR,
personneldeployment__personnel__start_date__date__lte=today,
personneldeployment__personnel__end_date__date__gte=today,
personneldeployment__personnel__is_active=True,
),
)
)
)

def get_serializer_class(self):
Expand Down
4 changes: 3 additions & 1 deletion api/schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import graphene

# Will be needed later, do not remove:
# import graphql_geojson
from graphene_django.types import DjangoObjectType

from .models import ActionsTaken, Appeal, Country, DisasterType, Event, FieldReport
Expand Down Expand Up @@ -39,7 +42,6 @@ class Query(graphene.ObjectType):
all_countries = graphene.List(CountryObjectType)
all_disasters_types = graphene.List(DisasterObjectType)
all_events = graphene.List(EventType)
all_events = graphene.List(EventType)
all_appeals = graphene.List(AppealType)
all_fieldreports = graphene.List(FieldReportType)

Expand Down
24 changes: 13 additions & 11 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ class ListEventSerializer(ModelSerializer):
field_reports = MiniFieldReportSerializer(many=True, read_only=True)
dtype = DisasterTypeSerializer(required=False)
ifrc_severity_level_display = serializers.CharField(source="get_ifrc_severity_level_display", read_only=True)
active_deployments = serializers.SerializerMethodField()
active_deployments = serializers.IntegerField(read_only=True)

class Meta:
model = Event
Expand Down Expand Up @@ -1019,15 +1019,17 @@ class Meta:
"active_deployments",
)

def get_active_deployments(self, event) -> int:
now = timezone.now()
return Personnel.objects.filter(
type=Personnel.TypeChoices.RR,
start_date__lt=now,
end_date__gt=now,
deployment__event_deployed_to=event,
is_active=True,
).count()

# Instead of the below method we use the queryset's annotate tag:
# def get_active_deployments(self, event) -> int:
# now = timezone.now()
# return Personnel.objects.filter(
# type=Personnel.TypeChoices.RR,
# start_date__lt=now,
# end_date__gt=now,
# deployment__event_deployed_to=event,
# is_active=True,
# ).count()


class SurgeEventSerializer(ModelSerializer):
Expand Down Expand Up @@ -1655,7 +1657,7 @@ def update(self, instance, validated_data):
return instance


# Instead of the below method we use the serializer's annotate tag:
# Instead of the below method we use the queryset's annotate tag:
# @staticmethod
# def get_is_ifrc_admin(obj) -> bool:
# return obj.groups.filter(name__iexact="IFRC Admins").exists()
Expand Down
3 changes: 2 additions & 1 deletion main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
}

GRAPHENE = {"SCHEMA": "api.schema.schema"}
# Not ready yet to use
# GRAPHENE = {"SCHEMA": "api.schema.schema"}

MIDDLEWARE = [
"debug_toolbar.middleware.DebugToolbarMiddleware",
Expand Down
8 changes: 4 additions & 4 deletions main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
SpectacularRedocView,
SpectacularSwaggerView,
)
from graphene_django.views import GraphQLView

# DRF routes
from rest_framework import routers
Expand Down Expand Up @@ -63,6 +62,8 @@
from registrations.drf_views import RegistrationView
from registrations.views import UserExternalTokenViewset, ValidateUser, VerifyEmail

# from graphene_django.views import GraphQLView # will be needed later

router = routers.DefaultRouter()

router.register(r"action", api_views.ActionViewset, basename="action")
Expand Down Expand Up @@ -172,9 +173,8 @@
# url(r"^api/v1/es_search/", EsPageSearch.as_view()),
url(r"^api/v1/search/", HayStackSearch.as_view()),
url(r"^api/v1/es_health/", EsPageHealth.as_view()),
# If we want to use the next one, some fixes needed, e.g.
# stackoverflow.com/questions/47166385/dont-know-how-to-convert-the-django-field-skills-class-taggit-managers-tagga
url(r"^api/v1/graphql/", GraphQLView.as_view(graphiql=True)),
# If we want to use the next one, some permission overthink is needed:
# url(r"^api/v1/graphql/", GraphQLView.as_view(graphiql=True)),
url(r"^api/v1/aggregate/", AggregateByTime.as_view()),
url(r"^api/v1/aggregate_dtype/", AggregateByDtype.as_view()),
url(r"^api/v1/aggregate_area/", AreaAggregate.as_view()),
Expand Down
Loading

0 comments on commit bbb563c

Please sign in to comment.