diff mbox series

[layerindex-web,4/4] rrs/views.py: fetch the recipe detail page's upgrades in a few queries

Message ID 20261116222410.35784-5-tim.orling@konsulko.com
State New
Headers show
Series Speed up the RRS pages, and test them | expand

Commit Message

Tim Orling Nov. 16, 2026, 10:24 p.m. UTC
The recipe detail page ran about nine queries per upgrade to find the
release and milestone it falls in and whether its author maintained the
recipe then, so linux-yocto, with 282 upgrades, took 3.7 s. Read the
plan's releases and milestones once and pick from them by date, and look
up each maintainer history and maintainer once per page. Releases with
the same dates are picked in id order, as the queries returned them.
Also fetch the other branches' recipes with their layer branch and branch.

On a copy of the production database the page data for 153 OE-Core
recipes is identical before and after, and linux-yocto takes 0.3 s
instead of 3.6 s.

AI-Generated: Claude Opus 5.5
Signed-off-by: Tim Orling <tim.orling@konsulko.com>
---
 rrs/views.py                    | 63 +++++++++++++++++++------
 tests/rrs/test_recipe_detail.py | 84 +++++++++++++++++++++++++++++++++
 2 files changed, 133 insertions(+), 14 deletions(-)
 create mode 100644 tests/rrs/test_recipe_detail.py
diff mbox series

Patch

diff --git a/rrs/views.py b/rrs/views.py
index 81ef35e..529b203 100644
--- a/rrs/views.py
+++ b/rrs/views.py
@@ -754,31 +754,63 @@  class RecipeUpgradeDetail():
         self.upgrade_type = upgrade_type
         self.group = group
 
-def _get_recipe_upgrade_detail(maintplan, recipe_upgrade):
+def _get_release_and_milestone(maintplan, commit_date, lookups):
+    """
+    Release.get_by_date() and Milestone.get_by_release_and_date() for
+    commit_date, from the plan's releases and milestones read once into
+    lookups (AI-generated, Claude Opus 5.5)
+    """
+    if 'releases' not in lookups:
+        # id breaks ties between releases with the same dates as the queries did
+        lookups['releases'] = list(Release.objects.filter(plan=maintplan).order_by('-end_date', 'id'))
+        lookups['milestones'] = {}
+        for milestone in Milestone.objects.filter(release__plan=maintplan) \
+                .exclude(name='All').order_by('-end_date', 'id'):
+            lookups['milestones'].setdefault(milestone.release_id, []).append(milestone)
+    # The queries compare the DateFields with the commit's date
+    commit_day = commit_date.date()
+    release = next((r for r in lookups['releases']
+                    if r.start_date <= commit_day <= r.end_date), None)
+    milestone = None
+    if release:
+        milestone = next((m for m in lookups['milestones'].get(release.id, [])
+                          if m.start_date <= commit_day <= m.end_date), None)
+    return release, milestone
+
+def _get_recipe_upgrade_detail(maintplan, recipe_upgrade, lookups=None):
+    # lookups keeps what several upgrades of a recipe share, so a caller
+    # that passes the same dict for each runs fewer queries
+    if lookups is None:
+        lookups = {}
     release_name = ''
     milestone_name = ''
     recipe_maintainer_history = None
 
-    release = Release.get_by_date(maintplan, recipe_upgrade.commit_date)
+    release, milestone = _get_release_and_milestone(maintplan, recipe_upgrade.commit_date, lookups)
     if release:
         release_name = release.name
-        milestone = Milestone.get_by_release_and_date(release,
-                recipe_upgrade.commit_date)
         if milestone:
             milestone_name = milestone.name
-            recipe_maintainer_history = RecipeMaintainerHistory.get_by_end_date(
-                recipe_upgrade.recipesymbol.layerbranch,
-                milestone.end_date)
+            key = ('maintainer_history', recipe_upgrade.recipesymbol.layerbranch_id, milestone.end_date)
+            if key not in lookups:
+                lookups[key] = RecipeMaintainerHistory.get_by_end_date(
+                    recipe_upgrade.recipesymbol.layerbranch,
+                    milestone.end_date)
+            recipe_maintainer_history = lookups[key]
 
     is_recipe_maintainer = False
     maintainer_name = ''
     if not recipe_upgrade.maintainer is None:
         maintainer_name = recipe_upgrade.maintainer.name
 
-        if not recipe_maintainer_history is None and \
-            RecipeMaintainer.objects.filter(maintainer__name
-            = maintainer_name, history = recipe_maintainer_history) \
-            .count() > 0:
+        key = ('is_maintainer', maintainer_name,
+               recipe_maintainer_history.id if recipe_maintainer_history else None)
+        if key not in lookups:
+            lookups[key] = not recipe_maintainer_history is None and \
+                RecipeMaintainer.objects.filter(maintainer__name
+                = maintainer_name, history = recipe_maintainer_history) \
+                .exists()
+        if lookups[key]:
             is_recipe_maintainer = True
 
     commit_date = recipe_upgrade.commit_date.date().isoformat()
@@ -849,8 +881,11 @@  class RecipeDetailView(DetailView):
         details = []
         multigroup = False
         lastgroup = '' # can't use None here
-        for ru in RecipeUpgrade.objects.filter(recipesymbol=recipesymbol).exclude(upgrade_type='M').order_by('group', '-commit_date', '-id'):
-            details.append(_get_recipe_upgrade_detail(maintplan, ru))
+        lookups = {}
+        # Through the related manager, each upgrade shares recipesymbol
+        for ru in recipesymbol.recipeupgrade_set.select_related('maintainer', 'group') \
+                .exclude(upgrade_type='M').order_by('group', '-commit_date', '-id'):
+            details.append(_get_recipe_upgrade_detail(maintplan, ru, lookups))
             if not multigroup:
                 if lastgroup == '':
                     lastgroup = ru.group
@@ -882,7 +917,7 @@  class RecipeDetailView(DetailView):
             context['recipe_depends'] = []
             context['recipe_distros'] = []
 
-        context['otherbranch_recipes'] = Recipe.objects.filter(layerbranch__layer=recipesymbol.layerbranch.layer, layerbranch__branch__comparison=False, pn=recipesymbol.pn).order_by('layerbranch__branch__sort_priority')
+        context['otherbranch_recipes'] = Recipe.objects.filter(layerbranch__layer=recipesymbol.layerbranch.layer, layerbranch__branch__comparison=False, pn=recipesymbol.pn).select_related('layerbranch__branch').order_by('layerbranch__branch__sort_priority')
 
         return context
 
diff --git a/tests/rrs/test_recipe_detail.py b/tests/rrs/test_recipe_detail.py
new file mode 100644
index 0000000..d29d136
--- /dev/null
+++ b/tests/rrs/test_recipe_detail.py
@@ -0,0 +1,84 @@ 
+# layerindex-web - tests for the RRS recipe detail page
+#
+# Licensed under the MIT license, see COPYING.MIT for details
+#
+# SPDX-License-Identifier: MIT
+#
+# AI-generated, Claude Opus 5.5
+
+# The recipe detail page lists a recipe's upgrades with the release and
+# milestone each falls in, and whether its author maintained the recipe then.
+# It must get them without running queries per upgrade.
+
+import datetime
+
+from django.db import connection
+from django.test.utils import CaptureQueriesContext
+
+from rrs.models import (
+    Maintainer,
+    RecipeMaintainer,
+    RecipeMaintainerHistory,
+    RecipeSymbol,
+    RecipeUpgrade,
+)
+
+
+def setup_recipe(layerbranch):
+    """busybox, maintained by Alice since 2026-10-15"""
+    alice, _ = Maintainer.objects.get_or_create(name='Alice')
+    Maintainer.objects.get_or_create(name='Bob')
+    history = RecipeMaintainerHistory.objects.create(
+        title='Update maintainers', date=datetime.datetime(2026, 10, 15), author=alice,
+        sha1='0' * 40, layerbranch=layerbranch)
+    symbol = RecipeSymbol.objects.create(layerbranch=layerbranch, pn='busybox', summary='busybox')
+    RecipeMaintainer.objects.create(recipesymbol=symbol, maintainer=alice, history=history)
+    return symbol
+
+
+def add_upgrade(symbol, version, when, maintainer_name):
+    # USE_TZ is False
+    RecipeUpgrade.objects.create(recipesymbol=symbol, version=version, author_date=when,
+                                 commit_date=when, upgrade_type='U', sha1='1' * 40,
+                                 maintainer=Maintainer.objects.get(name=maintainer_name))
+
+
+def details(client, symbol):
+    response = client.get(f'/rrs/recipedetail/OE-Core/{symbol.pk}/')
+    assert response.status_code == 200
+    return response.context['recipe_upgrade_details']
+
+
+def test_release_milestone_and_maintainer(client, rrs_plan):
+    symbol = setup_recipe(rrs_plan.layerbranch)
+    add_upgrade(symbol, '1.37.0', datetime.datetime(2026, 11, 1, 10), 'Alice')
+    add_upgrade(symbol, '1.36.1', datetime.datetime(2026, 10, 2, 10), 'Bob')
+    # In release 6.2 but after its only milestone
+    add_upgrade(symbol, '1.38.0', datetime.datetime(2027, 2, 1, 10), 'Alice')
+    # Before any release
+    add_upgrade(symbol, '1.30.0', datetime.datetime(2025, 1, 1, 10), 'Alice')
+
+    rows = {d.version: (d.release_name, d.milestone_name, d.maintainer_name, d.is_recipe_maintainer)
+            for d in details(client, symbol)}
+
+    assert rows == {
+        '1.38.0': ('6.2', '', 'Alice', False),
+        '1.37.0': ('6.2', 'M1', 'Alice', True),
+        '1.36.1': ('6.2', 'M1', 'Bob', False),
+        '1.30.0': ('', '', 'Alice', False),
+    }
+
+
+def test_queries_do_not_grow_with_upgrades(client, rrs_plan):
+    symbol = setup_recipe(rrs_plan.layerbranch)
+    add_upgrade(symbol, '1.0', datetime.datetime(2026, 10, 2), 'Alice')
+    add_upgrade(symbol, '1.1', datetime.datetime(2026, 10, 3), 'Bob')
+    details(client, symbol)  # fills the per-process caches first
+    with CaptureQueriesContext(connection) as two:
+        assert len(details(client, symbol)) == 2
+    for day in range(4, 10):
+        add_upgrade(symbol, f'1.{day - 2}', datetime.datetime(2026, 10, day),
+                    'Alice' if day % 2 else 'Bob')
+    with CaptureQueriesContext(connection) as eight:
+        assert len(details(client, symbol)) == 8
+    assert len(eight) == len(two)