Skip to content
51 changes: 30 additions & 21 deletions usaspending_api/spending_explorer/v2/filters/explorer.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
from django.db.models import Exists, F, OuterRef, Sum, TextField, Value
from django.db.models import Exists, F, OuterRef, QuerySet, Sum, TextField, Value
from django_cte import With

from usaspending_api.common.calculations.file_b import FileBCalculations
from usaspending_api.references.models import Agency
from usaspending_api.submissions.models import SubmissionAttributes

# Default maximum records returned by spending explorer (also used by the view)
SPENDING_EXPLORER_LIMIT = 1000

class Explorer(object):

class Explorer:
file_b_calculations = FileBCalculations()

def __init__(self, alt_set, queryset):
def __init__(self, alt_set: QuerySet, queryset: QuerySet, limit: int | None = SPENDING_EXPLORER_LIMIT) -> None:
self.alt_set = alt_set
self.queryset = queryset
self.limit = limit

def budget_function(self):
def _apply_limit(self, queryset: QuerySet) -> QuerySet:
"""Apply the explorer limit to a queryset before it is evaluated"""
if self.limit is not None:
return queryset[: self.limit]
return queryset

def budget_function(self) -> QuerySet:
# Budget Function Queryset
queryset = (
self.queryset.annotate(
Expand All @@ -28,9 +37,9 @@ def budget_function(self):
.order_by("-total")
)

return queryset
return self._apply_limit(queryset)

def budget_subfunction(self):
def budget_subfunction(self) -> QuerySet:
# Budget Sub Function Queryset
queryset = (
self.queryset.annotate(
Expand All @@ -44,9 +53,9 @@ def budget_subfunction(self):
.order_by("-total")
)

return queryset
return self._apply_limit(queryset)

def federal_account(self):
def federal_account(self) -> QuerySet:
# Federal Account Queryset
queryset = (
self.queryset.annotate(
Expand All @@ -61,9 +70,9 @@ def federal_account(self):
.order_by("-total")
)

return queryset
return self._apply_limit(queryset)

def program_activity(self):
def program_activity(self) -> QuerySet:
# Program Activity Queryset
queryset = (
self.queryset.annotate(
Expand All @@ -77,9 +86,9 @@ def program_activity(self):
.order_by("-total")
)

return queryset
return self._apply_limit(queryset)

def object_class(self):
def object_class(self) -> QuerySet:
# Object Classes Queryset
queryset = (
self.queryset.annotate(
Expand All @@ -93,9 +102,9 @@ def object_class(self):
.order_by("-total")
)

return queryset
return self._apply_limit(queryset)

def recipient(self):
def recipient(self) -> QuerySet:
# Recipients Queryset
alt_set = (
self.alt_set.filter(transaction_obligated_amount__isnull=False)
Expand All @@ -110,9 +119,9 @@ def recipient(self):
.order_by("-total")
)

return alt_set
return self._apply_limit(alt_set)

def agency(self):
def agency(self) -> QuerySet:
# Funding Top Tier Agencies Querysets
agency_cte = With(
Agency.objects.filter(toptier_flag=True)
Expand Down Expand Up @@ -142,9 +151,9 @@ def agency(self):
)
)

return queryset
return self._apply_limit(queryset)

def award_category(self):
def award_category(self) -> QuerySet:
# Award Category Queryset
alt_set = (
self.alt_set.annotate(
Expand All @@ -157,9 +166,9 @@ def award_category(self):
.order_by("-total")
)

return alt_set
return self._apply_limit(alt_set)

def award(self):
def award(self) -> QuerySet:
# Awards Queryset
alt_set = (
self.alt_set.annotate(
Expand All @@ -172,4 +181,4 @@ def award(self):
.order_by("-total")
)

return alt_set
return self._apply_limit(alt_set)
Loading
Loading