Skip to content

Commit

Permalink
FFT-95 Optimise edit forecast requests
Browse files Browse the repository at this point in the history
so far so good. the issue is too many db queries.

started at 9000+ now down to 450~.

next steps is to look at the get_budget call
  • Loading branch information
SamDudley committed Nov 15, 2024
1 parent 86f0f58 commit 7f52d90
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ def FILTERS_VERBOSE_LOOKUPS():
"core.no_cache_middleware.NoCacheMiddleware",
"simple_history.middleware.HistoryRequestMiddleware",
"axes.middleware.AxesMiddleware",
"core.middleware.DatabaseQueriesMiddleware",
]

AUTHENTICATION_BACKENDS = [
Expand Down
17 changes: 17 additions & 0 deletions core/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.db import connection


class DatabaseQueriesMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
pre_query_count = len(connection.queries)

response = self.get_response(request)

post_query_count = len(connection.queries)
query_count = post_query_count - pre_query_count
print(f"Database queries {query_count}")

return response
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ services:
depends_on:
- db
- redis
# Required for debuggers to work.
stdin_open: true
tty: true

celery:
image: fft/web:latest
Expand Down
3 changes: 2 additions & 1 deletion forecast/serialisers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def get_month(self, obj):
return obj.financial_period.financial_period_code

def get_actual(self, obj):
if obj.financial_year_id > get_current_financial_year():
if obj.financial_year_id > self.context["current_financial_year"]:
return False
return obj.financial_period.actual_loaded

Expand Down Expand Up @@ -67,6 +67,7 @@ def get_nac_description(self, obj):
return obj.natural_account_code.natural_account_code_description

def get_budget(self, obj):
# FIXME: 400+ queries! try this as a prefetch similar to forecast or change to a lookup
financial_year = self.context["financial_year"]
budget = (
BudgetMonthlyFigure.objects.values(
Expand Down
15 changes: 12 additions & 3 deletions forecast/views/edit_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,32 @@ def get_financial_codes_for_year(cost_centre_code, financial_year):
def get_financial_code_serialiser(cost_centre_code, financial_year):
# Only selects the financial codes relevant to the financial year being edited.
# Financial codes are used in budgets and forecast/actuals.
forecasts = ForecastMonthlyFigure.objects.filter(
forecasts = ForecastMonthlyFigure.objects.select_related("financial_period").filter(
financial_year_id=financial_year,
archived_status__isnull=True,
)
financial_codes = (
get_financial_codes_for_year(cost_centre_code, financial_year)
.select_related("programme", "natural_account_code")
.prefetch_related(
Prefetch(
"forecast_forecastmonthlyfigures",
queryset=forecasts,
to_attr="monthly_figure_items",
),
"forecast_forecastmonthlyfigures__financial_period",
# FIXME: check this isn't needed
# "forecast_forecastmonthlyfigures__financial_period",
)
.order_by(*edit_forecast_order())
)

financial_code_serialiser = FinancialCodeSerializer(
financial_codes, many=True, context={"financial_year": financial_year}
financial_codes,
many=True,
context={
"financial_year": financial_year,
"current_financial_year": get_current_financial_year(),
},
)
return financial_code_serialiser

Expand Down Expand Up @@ -478,6 +486,7 @@ def get_context_data(self, **kwargs):
self.cost_centre_code,
self.financial_year,
)
# FIXME: holy moly it's slow!
serialiser_data = financial_code_serialiser.data
forecast_dump = json.dumps(serialiser_data)
if self.financial_year == get_current_financial_year():
Expand Down

0 comments on commit 7f52d90

Please sign in to comment.