Skip to content

Commit

Permalink
refactor: Streamline draft aliases api (#7607)
Browse files Browse the repository at this point in the history
* chore: update add-django-cprofile-filter.patch

* fix: only use "draft" state when making aliases

* refactor: eliminate repeated get_state_slug()

On dev, reduces time for a draft-aliases api
call by by 10-15%

* refactor: only annotate inactive drafts

* refactor: de-lint

* refactor: speed up get_draft_authors_emails

Another 20% or so improvement in response time

* fix: guard against null person
  • Loading branch information
jennifer-richards committed Jun 28, 2024
1 parent 0dcccea commit f78b050
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 62 deletions.
122 changes: 75 additions & 47 deletions ietf/doc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from django.conf import settings
from django.contrib import messages
from django.db.models import OuterRef
from django.forms import ValidationError
from django.http import Http404
from django.template.loader import render_to_string
Expand All @@ -39,7 +40,7 @@
from ietf.name.models import DocReminderTypeName, DocRelationshipName
from ietf.group.models import Role, Group, GroupFeatures
from ietf.ietfauth.utils import has_role, is_authorized_in_doc_stream, is_individual_draft_author, is_bofreq_editor
from ietf.person.models import Person
from ietf.person.models import Email, Person
from ietf.review.models import ReviewWish
from ietf.utils import draft, log
from ietf.utils.mail import parseaddr, send_mail
Expand Down Expand Up @@ -1301,9 +1302,13 @@ def get_draft_shepherd_email(self, doc):
def get_draft_authors_emails(self, doc):
"""Get list of authors for the given draft."""
author_emails = set()
for author in doc.documentauthor_set.all():
if author.email and author.email.email_address():
author_emails.add(author.email.email_address())
for email in Email.objects.filter(documentauthor__document=doc):
if email.active:
author_emails.add(email.address)
elif email.person:
person_email = email.person.email_address()
if person_email:
author_emails.add(person_email)
return author_emails

def get_draft_notify_emails(self, doc):
Expand Down Expand Up @@ -1336,59 +1341,82 @@ def get_draft_notify_emails(self, doc):
notify_emails.add(email)
return notify_emails

def _yield_aliases_for_draft(self, doc)-> Iterator[tuple[str, list[str]]]:
alias = doc.name
all = set()

# no suffix and .authors are the same list
emails = self.get_draft_authors_emails(doc)
all.update(emails)
if emails:
yield alias, list(emails)
yield alias + ".authors", list(emails)

# .chairs = group chairs
emails = self.get_draft_chair_emails(doc)
if emails:
all.update(emails)
yield alias + ".chairs", list(emails)

# .ad = sponsoring AD / WG AD (WG document)
emails = self.get_draft_ad_emails(doc)
if emails:
all.update(emails)
yield alias + ".ad", list(emails)

# .notify = notify email list from the Document
emails = self.get_draft_notify_emails(doc)
if emails:
all.update(emails)
yield alias + ".notify", list(emails)

# .shepherd = shepherd email from the Document
emails = self.get_draft_shepherd_email(doc)
if emails:
all.update(emails)
yield alias + ".shepherd", list(emails)

# .all = everything from above
if all:
yield alias + ".all", list(all)

def __iter__(self) -> Iterator[tuple[str, list[str]]]:
# Internet-Drafts with active status or expired within self.days
show_since = timezone.now() - datetime.timedelta(days=self.days)
drafts = self.draft_queryset
active_drafts = drafts.filter(states__slug='active')
inactive_recent_drafts = drafts.exclude(states__slug='active').filter(expires__gte=show_since)
interesting_drafts = active_drafts | inactive_recent_drafts

for this_draft in interesting_drafts.distinct().iterator():
# Look up the draft-active state properly. Doing this with
# states__type_id, states__slug directly in the `filter()`
# works, but it does not work as expected in `exclude()`.
active_state = State.objects.get(type_id="draft", slug="active")
active_drafts = drafts.filter(states=active_state)
for this_draft in active_drafts:
for alias, addresses in self._yield_aliases_for_draft(this_draft):
yield alias, addresses

# Annotate with the draft state slug so we can check for drafts that
# have become RFCs
inactive_recent_drafts = (
drafts.exclude(states=active_state)
.filter(expires__gte=show_since)
.annotate(
# Why _default_manager instead of objects? See:
# https://docs.djangoproject.com/en/4.2/topics/db/managers/#django.db.models.Model._default_manager
draft_state_slug=Document.states.through._default_manager.filter(
document__pk=OuterRef("pk"),
state__type_id="draft"
).values("state__slug"),
)
)
for this_draft in inactive_recent_drafts:
# Omit drafts that became RFCs, unless they were published in the last DEFAULT_YEARS
if this_draft.get_state_slug() == "rfc":
if this_draft.draft_state_slug == "rfc":
rfc = this_draft.became_rfc()
log.assertion("rfc is not None")
if rfc.latest_event(type='published_rfc').time < show_since:
continue

alias = this_draft.name
all = set()

# no suffix and .authors are the same list
emails = self.get_draft_authors_emails(this_draft)
all.update(emails)
if emails:
yield alias, list(emails)
yield alias + ".authors", list(emails)

# .chairs = group chairs
emails = self.get_draft_chair_emails(this_draft)
if emails:
all.update(emails)
yield alias + ".chairs", list(emails)

# .ad = sponsoring AD / WG AD (WG document)
emails = self.get_draft_ad_emails(this_draft)
if emails:
all.update(emails)
yield alias + ".ad", list(emails)

# .notify = notify email list from the Document
emails = self.get_draft_notify_emails(this_draft)
if emails:
all.update(emails)
yield alias + ".notify", list(emails)

# .shepherd = shepherd email from the Document
emails = self.get_draft_shepherd_email(this_draft)
if emails:
all.update(emails)
yield alias + ".shepherd", list(emails)

# .all = everything from above
if all:
yield alias + ".all", list(all)
for alias, addresses in self._yield_aliases_for_draft(this_draft):
yield alias, addresses


def get_doc_email_aliases(name: Optional[str] = None):
Expand Down
71 changes: 56 additions & 15 deletions patch/add-django-cprofile-filter.patch
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
--- django_cprofile_middleware/middleware.py.old 2018-04-04 06:32:29.282187502 -0700
+++ django_cprofile_middleware/middleware.py 2018-04-06 10:11:18.936855634 -0700
@@ -1,4 +1,5 @@
import pstats
+import re

try:
import cProfile as profile
@@ -14,6 +15,15 @@
from django.utils.deprecation import MiddlewareMixin


--- django_cprofile_middleware/middleware.py.old 2024-06-27 21:03:56.975128007 +0000
+++ django_cprofile_middleware/middleware.py 2024-06-27 23:45:59.421683008 +0000
@@ -19,6 +19,16 @@
from django_cprofile_middleware.utils import MiddlewareMixin


+class Stats(pstats.Stats):
+ def filter_stats(self, regex):
+ oldstats = self.stats
Expand All @@ -18,23 +12,70 @@
+ for func, (cc, nc, tt, ct, callers) in oldstats.iteritems():
+ if filter.search(pstats.func_std_string(func)):
+ newstats[func] = (cc, nc, tt, ct, callers)
+
+
class ProfilerMiddleware(MiddlewareMixin):
"""
Simple profile middleware to profile django views. To run it, add ?prof to
@@ -62,8 +72,13 @@
@@ -38,9 +48,11 @@
?download => Download profile file suitable for visualization. For example
in snakeviz or RunSnakeRun

- This is adapted from an example found here:
- http://www.slideshare.net/zeeg/django-con-high-performance-django-presentation.
+ Patched with https://github.com/omarish/django-cprofile-middleware/pull/23
+ for operation with Django 4.2.5+
"""
+ PROFILER_REQUEST_ATTR_NAME = '_django_cprofile_middleware_profiler'
+
def can(self, request):
requires_staff = getattr(
settings, "DJANGO_CPROFILE_MIDDLEWARE_REQUIRE_STAFF", True)
@@ -52,10 +64,11 @@

def process_view(self, request, callback, callback_args, callback_kwargs):
if self.can(request):
- self.profiler = profile.Profile()
+ profiler = profile.Profile()
+ setattr(request, self.PROFILER_REQUEST_ATTR_NAME, profiler)
args = (request,) + callback_args
try:
- return self.profiler.runcall(
+ return profiler.runcall(
callback, *args, **callback_kwargs)
except Exception:
# we want the process_exception middleware to fire
@@ -63,12 +76,13 @@
return

def process_response(self, request, response):
- if self.can(request):
- self.profiler.create_stats()
+ if hasattr(request, self.PROFILER_REQUEST_ATTR_NAME):
+ profiler = getattr(request, self.PROFILER_REQUEST_ATTR_NAME)
+ profiler.create_stats()
if 'download' in request.GET:
import marshal

- output = marshal.dumps(self.profiler.stats)
+ output = marshal.dumps(profiler.stats)
response = HttpResponse(
output, content_type='application/octet-stream')
response['Content-Disposition'] = 'attachment;' \
@@ -76,9 +90,14 @@
response['Content-Length'] = len(output)
else:
io = StringIO()
- stats = pstats.Stats(self.profiler, stream=io)
+ stats = Stats(profiler, stream=io)

- stats.strip_dirs().sort_stats(request.GET.get('sort', 'time'))
+ stats = Stats(self.profiler, stream=io)
+ if request.GET.get('stripdirs', False):
+ stats = stats.strip_dirs()
+ filter = request.GET.get('filter', None)
+ if filter:
+ stats.filter_stats(filter)
+ stats.sort_stats(request.GET.get('psort') or 'time')
stats.print_stats(int(request.GET.get('count', 100)))

response = HttpResponse('<pre>%s</pre>' % io.getvalue())
return response

0 comments on commit f78b050

Please sign in to comment.