fix: optimize queries for DocumentChangesFeed (#5675)

This commit is contained in:
Robert Sparks 2023-05-23 10:14:13 -05:00 committed by GitHub
parent 40765d878f
commit 6178519d43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View file

@ -45,7 +45,7 @@ class DocumentChangesFeed(Feed):
return "History of change entries for %s." % obj.display_name()
def items(self, obj):
events = obj.docevent_set.all().order_by("-time","-id")
events = obj.docevent_set.all().order_by("-time","-id").select_related("by", "newrevisiondocevent", "submissiondocevent")
augment_events_with_revision(obj, events)
return events

View file

@ -17,6 +17,7 @@ from zoneinfo import ZoneInfo
from django.conf import settings
from django.contrib import messages
from django.db.models import QuerySet
from django.forms import ValidationError
from django.http import Http404
from django.template.loader import render_to_string
@ -344,11 +345,18 @@ def augment_events_with_revision(doc, events):
"""Take a set of events for doc and add a .rev attribute with the
revision they refer to by checking NewRevisionDocEvents."""
event_revisions = list(NewRevisionDocEvent.objects.filter(doc=doc).order_by('time', 'id').values('id', 'rev', 'time'))
if isinstance(events, QuerySet):
qs = events.filter(newrevisiondocevent__isnull=False)
else:
qs = NewRevisionDocEvent.objects.filter(doc=doc)
event_revisions = list(qs.order_by('time', 'id').values('id', 'rev', 'time'))
if doc.type_id == "draft" and doc.get_state_slug() == "rfc":
# add fake "RFC" revision
e = doc.latest_event(type="published_rfc")
if isinstance(events, QuerySet):
e = events.filter(type="published_rfc").order_by('time').last()
else:
e = doc.latest_event(type="published_rfc")
if e:
event_revisions.append(dict(id=e.id, time=e.time, rev="RFC"))
event_revisions.sort(key=lambda x: (x["time"], x["id"]))