Skip to content

Commit

Permalink
Fix duplicate stats generation in country page
Browse files Browse the repository at this point in the history
  • Loading branch information
k9845 committed Jun 19, 2024
1 parent 9c4108d commit 00a6177
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
12 changes: 7 additions & 5 deletions api/drf_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,12 @@ def get_country_figure(self, request, pk):
end_date = request.GET.get("end_date", end_date)
appeal_conditions = Q(atype=AppealType.APPEAL)

all_appealhistory = AppealHistory.objects.select_related("appeal").filter(appeal__code__isnull=False)
all_appealhistory = all_appealhistory.filter(country=country)
all_appealhistory = AppealHistory.objects.select_related("appeal").filter(
appeal__code__isnull=False, needs_confirmation=True
)
all_appealhistory = all_appealhistory.filter(country=country).order_by().values("aid")
if start_date and end_date:
all_appealhistory = all_appealhistory.filter(start_date__lte=end_date, end_date__gte=start_date)
all_appealhistory = all_appealhistory.filter(start_date__gte=start_date, end_date__lte=end_date)
appeals_aggregated = all_appealhistory.annotate(
appeal_with_dref=Count(
Case(
Expand Down Expand Up @@ -358,7 +360,7 @@ def get_country_disaster_monthly_count(self, request, pk):
countries__in=[country.id],
dtype__isnull=False,
)
.annotate(date=TruncMonth("created_at"))
.annotate(date=TruncMonth("disaster_start_date"))
.values("date", "countries", "dtype")
.annotate(
appeal_targeted_population=Coalesce(
Expand Down Expand Up @@ -415,7 +417,7 @@ def get_country_historical_disaster(self, request, pk):
countries__in=[country.id],
dtype__isnull=False,
)
.annotate(date=TruncMonth("created_at"))
.annotate(date=TruncMonth("disaster_start_date"))
.values("date", "dtype", "countries")
.annotate(
appeal_targeted_population=Coalesce(
Expand Down
1 change: 1 addition & 0 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2118,6 +2118,7 @@ class AggregateHeaderFiguresSerializer(serializers.Serializer):
amount_requested = serializers.IntegerField()
amount_requested_dref_included = serializers.IntegerField()
amount_funded = serializers.IntegerField()
amount_funded_dref_included = serializers.IntegerField()


# SearchPage Serializer
Expand Down
6 changes: 5 additions & 1 deletion api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ def test_appeal_key_figure(self):
atype=AppealType.APPEAL,
start_date="2024-1-1",
end_date="2024-1-1",
needs_confirmation=True,
)
AppealHistoryFactory.create(
appeal=appeal2,
Expand All @@ -573,6 +574,7 @@ def test_appeal_key_figure(self):
atype=AppealType.DREF,
start_date="2024-2-2",
end_date="2024-2-2",
needs_confirmation=True,
)
AppealHistoryFactory.create(
appeal=appeal3,
Expand All @@ -584,6 +586,7 @@ def test_appeal_key_figure(self):
atype=AppealType.APPEAL,
start_date="2024-3-3",
end_date="2024-3-3",
needs_confirmation=True,
)
AppealHistoryFactory.create(
appeal=appeal3,
Expand All @@ -595,11 +598,12 @@ def test_appeal_key_figure(self):
atype=AppealType.APPEAL,
start_date="2024-4-4",
end_date="2024-4-4",
needs_confirmation=True,
)
url = f"/api/v2/country/{country1.id}/figure/"
self.client.force_authenticate(self.user)
response = self.client.get(url)
self.assert_200(response)
self.assertIsNotNone(response.json())
self.assertEqual(response.data["active_drefs"], 1)
self.assertEqual(response.data["active_appeals"], 3)
self.assertEqual(response.data["active_appeals"], 2) # distinct `appeal`
4 changes: 4 additions & 0 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,9 @@ def get(self, request):
),
# Active Appeals' funded amount, which are not DREF
amof=Case(When(appeal_conditions, then=F("amount_funded")), output_field=IntegerField()),
amofdref=Case(
When(Q(end_date__gt=date) & Q(start_date__lt=date), then=F("amount_funded")), output_field=IntegerField()
),
).aggregate(
active_drefs=Sum("actd"),
active_appeals=Sum("acta"),
Expand All @@ -657,6 +660,7 @@ def get(self, request):
amount_requested=Sum("amor"),
amount_requested_dref_included=Sum("amordref"),
amount_funded=Sum("amof"),
amount_funded_dref_included=Sum("amofdref"),
)

return Response(AggregateHeaderFiguresSerializer(appeals_aggregated).data)
Expand Down

0 comments on commit 00a6177

Please sign in to comment.