Skip to content

Commit

Permalink
Add support for successful build prefetch (#11613)
Browse files Browse the repository at this point in the history
We use both latest build and latest successful build in the project
listing template.
  • Loading branch information
agjohnson authored Sep 25, 2024
1 parent e5f8092 commit a6e1fde
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
8 changes: 8 additions & 0 deletions readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ class Project(models.Model):

# Property used for storing the latest build for a project when prefetching
LATEST_BUILD_CACHE = "_latest_build"
LATEST_SUCCESSFUL_BUILD_CACHE = "_latest_successful_build"

class Meta:
ordering = ("slug",)
Expand Down Expand Up @@ -920,6 +921,13 @@ def conf_dir(self, version=LATEST):

@property
def has_good_build(self):
# Check if there is `_latest_successful_build` attribute in the Queryset.
# Used for database optimization.
if hasattr(self, self.LATEST_SUCCESSFUL_BUILD_CACHE):
if build_successful := getattr(self, self.LATEST_SUCCESSFUL_BUILD_CACHE):
return build_successful[0]
return None

# Check if there is `_good_build` annotation in the Queryset.
# Used for Database optimization.
if hasattr(self, "_good_build"):
Expand Down
24 changes: 20 additions & 4 deletions readthedocs/projects/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,33 @@ def prefetch_latest_build(self):
from readthedocs.builds.models import Build

# Prefetch the latest build for each project.
subquery = Subquery(
subquery_build_latest = Subquery(
Build.internal.filter(project=OuterRef("project_id"))
.order_by("-date")
.values_list("id", flat=True)[:1]
)
latest_build = Prefetch(
prefetch_build_latest = Prefetch(
"builds",
Build.internal.filter(pk__in=subquery),
Build.internal.filter(pk__in=subquery_build_latest),
to_attr=self.model.LATEST_BUILD_CACHE,
)
return self.prefetch_related(latest_build)

# Prefetch the latest successful build for each project.
subquery_build_successful = Subquery(
Build.internal.filter(project=OuterRef("project_id"))
.order_by("-date")
.values_list("id", flat=True)[:1]
)
prefetch_build_successful = Prefetch(
"builds",
Build.internal.filter(pk__in=subquery_build_successful),
to_attr=self.model.LATEST_SUCCESSFUL_BUILD_CACHE,
)

return self.prefetch_related(
prefetch_build_latest,
prefetch_build_successful,
)

# Aliases

Expand Down

0 comments on commit a6e1fde

Please sign in to comment.