Skip to content

Commit

Permalink
Merge pull request #925 from uktrade/develop
Browse files Browse the repository at this point in the history
RC - Sprint 68 - No Stopping Us #3
  • Loading branch information
Santino-Trade authored Oct 31, 2024
2 parents f04b014 + 78f498f commit c29ff5e
Show file tree
Hide file tree
Showing 22 changed files with 2,462 additions and 2,030 deletions.
16 changes: 15 additions & 1 deletion api/barrier_downloads/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
from api.barriers.models import Barrier, BarrierFilterSet


class BarrierDownloadFilterBackend(DjangoFilterBackend):
"""
We want to override DjangoFilterBackend to read the filters
from request.data instead of request.query_params
"""

def get_filterset_kwargs(self, request, queryset, view):
return {
"data": request.data,
"queryset": queryset,
"request": request,
}


class BarrierDownloadsView(generics.ListCreateAPIView):
queryset = (
Barrier.barriers.annotate(
Expand All @@ -41,7 +55,7 @@ class BarrierDownloadsView(generics.ListCreateAPIView):
)
serializer_class = BarrierDownloadSerializer
filterset_class = BarrierFilterSet
filter_backends = (DjangoFilterBackend,)
filter_backends = (BarrierDownloadFilterBackend,)
pagination_class = PageNumberPagination

def post(self, request, *args, **kwargs):
Expand Down
6 changes: 1 addition & 5 deletions api/barriers/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
from .barriers import ( # noqa
BarrierDetailSerializer,
BarrierListSerializer,
BarrierRelatedListSerializer,
)
from .barriers import BarrierDetailSerializer, BarrierListSerializer # noqa
from .data_workspace import DataWorkspaceSerializer # noqa
from .progress_updates import ProgressUpdateSerializer # noqa
from .public_barriers import PublicBarrierSerializer, PublishedVersionSerializer # noqa
Expand Down
12 changes: 0 additions & 12 deletions api/barriers/serializers/barriers.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,3 @@ def get_current_valuation_assessment(instance):
return f"{rating}"
else:
return None


# TODO : standard list serialiser may suffice and the following not required base on final designs
class BarrierRelatedListSerializer(serializers.Serializer):
summary = serializers.CharField(read_only=True)
title = serializers.CharField(read_only=True)
id = serializers.UUIDField(read_only=True)
reported_on = serializers.DateTimeField(read_only=True)
modified_on = serializers.DateTimeField(read_only=True)
status = StatusField(required=False)
location = serializers.CharField(read_only=True)
similarity = serializers.FloatField(read_only=True)
4 changes: 0 additions & 4 deletions api/barriers/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from api.barriers.views import (
BarrierActivity,
BarrierDashboardSummary,
BarrierDetail,
BarrierFullHistory,
BarrierHibernate,
Expand Down Expand Up @@ -171,9 +170,6 @@
name="unknown-barrier",
),
path("counts", barrier_count, name="barrier-count"),
path(
"dashboard-summary", BarrierDashboardSummary.as_view(), name="barrier-summary"
),
path("reports", BarrierReportList.as_view(), name="list-reports"),
path("reports/<uuid:pk>", BarrierReportDetail.as_view(), name="get-report"),
path(
Expand Down
237 changes: 1 addition & 236 deletions api/barriers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@

from dateutil.parser import parse
from django.db import transaction
from django.db.models import (
Case,
CharField,
F,
IntegerField,
Prefetch,
Q,
Sum,
Value,
When,
)
from django.db.models import Case, CharField, F, Prefetch, Value, When
from django.http import StreamingHttpResponse
from django.shortcuts import get_object_or_404
from django.utils import timezone
Expand Down Expand Up @@ -58,7 +48,6 @@
from api.metadata.constants import (
BARRIER_INTERACTION_TYPE,
BARRIER_SEARCH_ORDERING_CHOICES,
ECONOMIC_ASSESSMENT_IMPACT_MIDPOINTS_NUMERIC_LOOKUP,
BarrierStatus,
PublicBarrierStatus,
)
Expand Down Expand Up @@ -188,230 +177,6 @@ class Meta:
abstract = True


class BarrierDashboardSummary(generics.GenericAPIView):
"""
View to return high level stats to the dashboard
"""

serializer_class = BarrierListSerializer
filterset_class = BarrierFilterSet

filter_backends = (DjangoFilterBackend,)
ordering_fields = (
"reported_on",
"modified_on",
"estimated_resolution_date",
"status",
"priority",
"country",
)
ordering = ("-reported_on",)

def get(self, request):
filtered_queryset = self.filter_queryset(
Barrier.barriers.filter(archived=False)
)

current_user = self.request.user

# Get current financial year
current_year_start = datetime(datetime.now().year, 4, 1)
current_year_end = datetime(datetime.now().year + 1, 3, 31)
previous_year_start = datetime(datetime.now().year - 1, 4, 1)
previous_year_end = datetime(datetime.now().year + 1, 3, 31)

if not current_user.is_anonymous:
user_barrier_count = Barrier.barriers.filter(
created_by=current_user
).count()
user_report_count = Barrier.reports.filter(created_by=current_user).count()
user_open_barrier_count = Barrier.barriers.filter(
created_by=current_user, status=2
).count()

when_assessment = [
When(valuation_assessments__impact=k, then=Value(v))
for k, v in ECONOMIC_ASSESSMENT_IMPACT_MIDPOINTS_NUMERIC_LOOKUP
]

# Resolved vs Estimated barriers values chart
resolved_valuations = (
filtered_queryset.filter(
Q(
estimated_resolution_date__range=[
current_year_start,
current_year_end,
]
)
| Q(
status_date__range=[
current_year_start,
current_year_end,
]
),
Q(status=4) | Q(status=3),
valuation_assessments__archived=False,
)
.annotate(numeric_value=Case(*when_assessment, output_field=IntegerField()))
.aggregate(total=Sum("numeric_value"))
)

resolved_barrier_value = resolved_valuations["total"]

estimated_valuations = (
filtered_queryset.filter(
Q(
estimated_resolution_date__range=[
current_year_start,
current_year_end,
]
)
| Q(
status_date__range=[
current_year_start,
current_year_end,
]
),
Q(status=1) | Q(status=2),
valuation_assessments__archived=False,
)
.annotate(numeric_value=Case(*when_assessment, output_field=IntegerField()))
.aggregate(total=Sum("numeric_value"))
)

estimated_barrier_value = estimated_valuations["total"]

# Total resolved barriers vs open barriers value chart

resolved_barriers = (
filtered_queryset.filter(
Q(status=4) | Q(status=3),
valuation_assessments__archived=False,
)
.annotate(numeric_value=Case(*when_assessment, output_field=IntegerField()))
.aggregate(total=Sum("numeric_value"))
)

total_resolved_barriers = resolved_barriers["total"]

open_barriers = (
filtered_queryset.filter(
Q(status=1) | Q(status=2),
valuation_assessments__archived=False,
)
.annotate(numeric_value=Case(*when_assessment, output_field=IntegerField()))
.aggregate(total=Sum("numeric_value"))
)

open_barriers_value = open_barriers["total"]

# Open barriers by status
whens = [When(status=k, then=Value(v)) for k, v in BarrierStatus.choices]

barrier_by_status = (
filtered_queryset.filter(
valuation_assessments__archived=False,
)
.annotate(status_display=Case(*whens, output_field=CharField()))
.annotate(numeric_value=Case(*when_assessment, output_field=IntegerField()))
.values("status_display")
.annotate(total=Sum("numeric_value"))
.order_by()
)

status_labels = []
status_data = []

for series in barrier_by_status:
status_labels.append(series["status_display"])
status_data.append(series["total"])

# TODO for status filter might need to consider status dates as well as ERD
counts = {
"financial_year": {
"current_start": current_year_start,
"current_end": current_year_end,
"previous_start": previous_year_start,
"previous_end": previous_year_end,
},
"barriers": {
"total": filtered_queryset.count(),
"open": filtered_queryset.filter(status=2).count(),
"paused": filtered_queryset.filter(status=5).count(),
"resolved": filtered_queryset.filter(status=4).count(),
"pb100": filtered_queryset.filter(
top_priority_status__in=["APPROVED", "REMOVAL_PENDING"]
).count(),
"overseas_delivery": filtered_queryset.filter(
priority_level="OVERSEAS"
).count(),
},
"barriers_current_year": {
"total": filtered_queryset.filter(
estimated_resolution_date__range=[
current_year_start,
current_year_end,
]
).count(),
"open": filtered_queryset.filter(
status=2,
estimated_resolution_date__range=[
current_year_start,
current_year_end,
],
).count(),
"paused": filtered_queryset.filter(
status=5,
estimated_resolution_date__range=[
current_year_start,
current_year_end,
],
).count(),
"resolved": filtered_queryset.filter(
status=4,
estimated_resolution_date__range=[
current_year_start,
current_year_end,
],
).count(),
"pb100": filtered_queryset.filter(
top_priority_status__in=["APPROVED", "REMOVAL_PENDING"],
estimated_resolution_date__range=[
current_year_start,
current_year_end,
],
).count(),
"overseas_delivery": filtered_queryset.filter(
priority_level="OVERSEAS",
estimated_resolution_date__range=[
current_year_start,
current_year_end,
],
).count(),
},
"user_counts": {
"user_barrier_count": user_barrier_count,
"user_report_count": user_report_count,
"user_open_barrier_count": user_open_barrier_count,
},
"reports": Barrier.reports.count(),
"barrier_value_chart": {
"resolved_barriers_value": resolved_barrier_value,
"estimated_barriers_value": estimated_barrier_value,
},
"total_value_chart": {
"resolved_barriers_value": total_resolved_barriers,
"open_barriers_value": open_barriers_value,
},
"barriers_by_status_chart": {
"series": status_data,
"labels": status_labels,
},
}

return Response(counts)


class BarrierReportList(BarrierReportBase, generics.ListCreateAPIView):
# TODO - These report views may now be redundant
serializer_class = BarrierReportSerializer
Expand Down
9 changes: 9 additions & 0 deletions api/dashboard/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import logging

from django.apps import AppConfig

logger = logging.getLogger(__name__)


class RelatedBarriersConfig(AppConfig):
name = "api.dashboard"
Empty file added api/dashboard/constants.py
Empty file.
Empty file.
Empty file.
Empty file added api/dashboard/serializers.py
Empty file.
Loading

0 comments on commit c29ff5e

Please sign in to comment.