Improve performance

This commit is contained in:
zandercymatics 2024-02-13 13:57:15 -07:00
parent 7ec4b32f88
commit a51949da0b
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7

View file

@ -1,6 +1,8 @@
import logging
import time
from django import forms
from django.db.models.functions import Concat
from django.db.models.functions import Concat, Coalesce
from django.db.models import Value, CharField
from django.http import HttpResponse
from django.shortcuts import redirect
from django_fsm import get_available_FIELD_transitions
@ -12,6 +14,7 @@ from django.http.response import HttpResponseRedirect
from django.urls import reverse
from epplibwrapper.errors import ErrorCode, RegistryError
from registrar.models.domain import Domain
from registrar.models.domain_application import DomainApplication
from registrar.models.user import User
from registrar.utility import csv_export
from registrar.views.utility.mixins import OrderableFieldsMixin
@ -707,6 +710,17 @@ class DomainInformationAdmin(ListHeaderAdmin):
return readonly_fields # Read-only fields for analysts
class Timer:
def __enter__(self):
self.start = time.time()
return self # This allows usage of the instance within the with block
def __exit__(self, *args):
self.end = time.time()
self.duration = self.end - self.start
logger.info(f"Execution time: {self.duration} seconds")
class DomainApplicationAdminForm(forms.ModelForm):
"""Custom form to limit transitions to available transitions"""
@ -753,11 +767,37 @@ class DomainApplicationAdmin(ListHeaderAdmin):
"""Lookup reimplementation, gets users of is_staff.
Returns a list of tuples consisting of (user.id, user)
"""
privileged_users = User.objects.filter(is_staff=True).order_by("first_name", "last_name", "email")
return [(user.id, user) for user in privileged_users]
logger.info("timing lookups")
with Timer() as t:
# Select all investigators that are staff, then order by name and email
privileged_users = (
DomainApplication.objects.select_related("investigator")
.filter(investigator__is_staff=True)
.order_by(
"investigator__first_name",
"investigator__last_name",
"investigator__email"
)
)
# Annotate the full name and return a values list that lookups can use
privileged_users_annotated = privileged_users.annotate(
full_name=Coalesce(
Concat(
"investigator__first_name", Value(" "), "investigator__last_name", output_field=CharField()
),
"investigator__email",
output_field=CharField()
)
).values_list("investigator__id", "full_name")
return privileged_users_annotated
def queryset(self, request, queryset):
"""Custom queryset implementation, filters by investigator"""
logger.info("timing queryset")
with Timer() as t:
if self.value() is None:
return queryset
else:
@ -863,11 +903,15 @@ class DomainApplicationAdmin(ListHeaderAdmin):
# lists in filter_horizontal are not sorted properly, sort them
# by website
def formfield_for_manytomany(self, db_field, request, **kwargs):
logger.info("timing formfield_for_manytomany")
with Timer() as t:
if db_field.name in ("current_websites", "alternative_domains"):
kwargs["queryset"] = models.Website.objects.all().order_by("website") # Sort websites
return super().formfield_for_manytomany(db_field, request, **kwargs)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
logger.info("timing formfield_for_foreignkey")
with Timer() as t:
# Removes invalid investigator options from the investigator dropdown
if db_field.name == "investigator":
kwargs["queryset"] = User.objects.filter(is_staff=True)
@ -876,6 +920,8 @@ class DomainApplicationAdmin(ListHeaderAdmin):
# Trigger action when a fieldset is changed
def save_model(self, request, obj, form, change):
logger.info("timing save_model")
with Timer() as t:
if obj and obj.creator.status != models.User.RESTRICTED:
if change: # Check if the application is being edited
# Get the original application from the database
@ -941,7 +987,8 @@ class DomainApplicationAdmin(ListHeaderAdmin):
admin user permissions and the application creator's status, so
we'll use the baseline readonly_fields and extend it as needed.
"""
logger.info("timing get_readonly_fields")
with Timer() as t:
readonly_fields = list(self.readonly_fields)
# Check if the creator is restricted
@ -961,6 +1008,8 @@ class DomainApplicationAdmin(ListHeaderAdmin):
return readonly_fields
def display_restricted_warning(self, request, obj):
logger.info("timing display_restricted_warning")
with Timer() as t:
if obj and obj.creator.status == models.User.RESTRICTED:
messages.warning(
request,
@ -968,6 +1017,8 @@ class DomainApplicationAdmin(ListHeaderAdmin):
)
def change_view(self, request, object_id, form_url="", extra_context=None):
logger.info("timing change_view")
with Timer() as t:
obj = self.get_object(request, object_id)
self.display_restricted_warning(request, obj)
return super().change_view(request, object_id, form_url, extra_context)