Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validated filter
Browse files Browse the repository at this point in the history
sudip-khanal committed Dec 13, 2024
1 parent 4d61d3d commit c1245a6
Showing 2 changed files with 26 additions and 22 deletions.
27 changes: 16 additions & 11 deletions per/drf_views.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@
from django.conf import settings
from django.db import transaction
from django.db.models import Count, F, Prefetch, Q
from django.db.models.functions import ExtractYear
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.utils.translation import get_language as django_get_language
@@ -932,33 +931,39 @@ def stats(self, request):
"""
Get the Ops Learning stats based on the filters
"""
ops_data = OpsLearning.objects.aggregate(
operations_included=Count("appeal_code", distinct=True),
learning_extracts=Count("id", filter=Q(is_validated=True), distinct=True),
sector_covered=Count("sector_validated", distinct=True),
source_used=Count("appeal_code__appealdocument", distinct=True),
ops_data = (
super()
.get_queryset()
.aggregate(
operations_included=Count("appeal_code", distinct=True),
learning_extracts=Count("id", filter=Q(is_validated=True), distinct=True),
sector_covered=Count("sector_validated", distinct=True),
source_used=Count("appeal_code__appealdocument", distinct=True),
)
)

learning_by_sector = (
SectorTag.objects.filter(title__isnull=False)
.annotate(count=Count("validated_sectors", distinct=True))
.values("id", "title", "count")
.values("title", "count")
)
learning_by_region = (
Region.objects.annotate(count=Count("appeal__opslearning", distinct=True))
Region.objects.filter(appeal__opslearning__is_validated=True)
.annotate(count=Count("appeal__opslearning", distinct=True))
.values("id", "label", "count")
.order_by("label")
)

learning_by_country = (
Country.objects.annotate(operation_count=Count("appeal__opslearning", distinct=True))
Country.objects.filter(appeal__opslearning__is_validated=True)
.annotate(operation_count=Count("appeal__opslearning", distinct=True))
.values("id", "name", "operation_count")
.order_by("name")
)

sources_overtime = {
str(appeal_type_label): OpsLearning.objects.filter(appeal_code__atype=appeal_type)
.annotate(year=ExtractYear(F("appeal_code__start_date")))
str(appeal_type_label): OpsLearning.objects.filter(appeal_code__atype=appeal_type, is_validated=True)
.annotate(year=F(("appeal_code__start_date")))
.values("year")
.annotate(count=Count("appeal_code__appealdocument", distinct=True))
.order_by("year")
21 changes: 10 additions & 11 deletions per/test_views.py
Original file line number Diff line number Diff line change
@@ -250,6 +250,7 @@ def setUpTestData(cls):
AppealDocumentFactory.create(appeal=cls.appeal1)
AppealDocumentFactory.create(appeal=cls.appeal2)

# Create OpsLearning instances with different validation status
cls.ops_learning1 = OpsLearningFactory.create(is_validated=True, appeal_code=cls.appeal1)
cls.ops_learning1.sector_validated.set([cls.sector1])

@@ -279,42 +280,40 @@ def test_ops_learning_stats(self):
for key in expected_keys:
self.assertIn(key, response.data)

# Validate counts
self.assertEqual(response.data["operations_included"], 2)
self.assertEqual(response.data["sources_used"], 2)
self.assertEqual(response.data["learning_extracts"], 1)
self.assertEqual(response.data["sectors_covered"], 2)

# Validate learning_by_region
region_data = response.data["learning_by_region"]
self.assertEqual(len(region_data), 1)
self.assertEqual(region_data[0]["label"], "Region A")
self.assertEqual(region_data[0]["count"], 3)
self.assertEqual(region_data[0]["count"], 1)

# Validate learning_by_sector
sector_data = response.data["learning_by_sector"]
self.assertEqual(len(sector_data), 2)
self.assertEqual(sector_data[0]["title"], "Sector 1")
self.assertEqual(sector_data[0]["count"], 1)
self.assertEqual(sector_data[1]["title"], "Sector 2")
self.assertEqual(sector_data[1]["count"], 2)

# Validate learning_by_country
country_data = response.data["learning_by_country"]
self.assertEqual(len(country_data), 1)
self.assertEqual(country_data[0]["name"], "Country A")
self.assertEqual(country_data[0]["operation_count"], 3)
self.assertEqual(country_data[0]["operation_count"], 1)

# Validate sources_overtime
for appeal_type, label in AppealType.choices:
self.assertIn(label, response.data["sources_overtime"])
for item in response.data["sources_overtime"][label]:
self.assertIn("year", item)
self.assertIn("count", item)
year_str = item["year"]

year_str_iso = year_str.replace(tzinfo=None).isoformat() + "Z"

if label == "DREF":
self.assertEqual(item["year"], 2023)
self.assertEqual(year_str_iso, "2023-01-01T00:00:00Z")
self.assertEqual(item["count"], 1)

elif label == "Emergency Appeal":
self.assertEqual(item["year"], 2023)
self.assertEqual(item["count"], 1)
self.assertEqual(year_str_iso, "2023-02-01T00:00:00Z")
self.assertEqual(item["count"], 0)

0 comments on commit c1245a6

Please sign in to comment.