diff --git a/per/drf_views.py b/per/drf_views.py index 709333990a..9ea1cd50b1 100644 --- a/per/drf_views.py +++ b/per/drf_views.py @@ -946,26 +946,37 @@ def stats(self, request): ) learning_by_sector = ( - SectorTag.objects.filter(validated_sectors__in=queryset, title__isnull=False) + SectorTag.objects.filter(validated_sectors__in=queryset) .annotate(count=Count("validated_sectors", distinct=True)) .values("title", "count") ) - sources_overtime = { + sources_overtime = ( Appeal.objects.filter(opslearning__in=queryset) - .annotate(type=F("atype"), date=F("start_date"), count=Count("opslearning", distinct=True)) + .annotate( + type=F("atype"), + date=F("start_date"), + count=Count("appealdocument", distinct=True), + ) .values("type", "date", "count") - } + ) learning_by_region = ( - Region.objects.filter(appeal__in=queryset.values("appeal_code__id")) - .annotate(region_name=F("label"), count=Count("appeal__opslearning", distinct=True)) + Region.objects.filter(appeal__opslearning__in=queryset) + .annotate( + region_name=F("label"), + count=Count("appeal__opslearning", distinct=True), + ) .values("region_name", "count") ) learning_by_country = ( - Country.objects.filter(appeal__in=queryset.values("appeal_code__id")) - .annotate(country_id=F("id"), country_name=F("name"), count=Count("appeal__opslearning", distinct=True)) + Country.objects.filter(appeal__opslearning__in=queryset) + .annotate( + country_id=F("id"), + country_name=F("name"), + count=Count("appeal__opslearning", distinct=True), + ) .values("country_id", "country_name", "count") ) @@ -979,7 +990,7 @@ def stats(self, request): "sources_overtime": sources_overtime, "learning_by_country": learning_by_country, } - return response.Response(data) + return response.Response(OpsLearningStatSerializer(data).data) class PerDocumentUploadViewSet(viewsets.ModelViewSet): diff --git a/per/serializers.py b/per/serializers.py index 88a8a2ca8b..07714160a4 100644 --- a/per/serializers.py +++ b/per/serializers.py @@ -1273,7 +1273,7 @@ class LearningBySectorSerializer(serializers.Serializer): class LearningSourcesOvertimeSerializer(serializers.Serializer): type = serializers.IntegerField() - date = serializers.DateField() + date = serializers.DateTimeField() count = serializers.IntegerField() diff --git a/per/test_views.py b/per/test_views.py index 9c8ba5fae2..2e52983d35 100644 --- a/per/test_views.py +++ b/per/test_views.py @@ -254,7 +254,7 @@ def setUp(self): self.ops_learning1 = OpsLearningFactory.create(is_validated=True, appeal_code=self.appeal1) self.ops_learning1.sector_validated.set([self.sector1]) - self.ops_learning2 = OpsLearningFactory.create(is_validated=False, appeal_code=self.appeal2) + self.ops_learning2 = OpsLearningFactory.create(is_validated=True, appeal_code=self.appeal2) self.ops_learning2.sector_validated.set([self.sector2]) self.ops_learning3 = OpsLearningFactory.create(is_validated=False, appeal_code=self.appeal2) @@ -279,28 +279,22 @@ def test_ops_learning_stats(self): self.assertIn(key, response.data) # Updated counts based on validated entries - self.assertEqual(response.data["operations_included"], 1) - self.assertEqual(response.data["sources_used"], 1) - self.assertEqual(response.data["learning_extracts"], 1) - self.assertEqual(response.data["sectors_covered"], 1) + self.assertEqual(response.data["operations_included"], 2) + self.assertEqual(response.data["sources_used"], 2) + self.assertEqual(response.data["learning_extracts"], 2) + 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]["region_name"], "Region A") - self.assertEqual(region_data[0]["count"], 1) + self.assertEqual(region_data[0]["count"], 2) # 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) # Validate learning by country country_data = response.data["learning_by_country"] self.assertEqual(len(country_data), 1) - self.assertEqual(country_data[0]["country_name"], "Country A") - self.assertEqual(country_data[0]["count"], 1) sources_overtime = response.data["sources_overtime"] - self.assertEqual(len(sources_overtime), 1) + self.assertEqual(len(sources_overtime), 2)