@@ -373,6 +373,68 @@ def _get_milestone_statistics(milestone, maintainer_name=None):
return milestone_statistics
+def _get_maintainers_statistics(milestone, maintainer_names):
+ """
+ The statistics of _get_milestone_statistics(milestone, name) for every
+ name in maintainer_names, with a few queries per layer branch rather
+ than several per maintainer (AI-generated, Claude Opus 5.5)
+ """
+ statistics = {name: {'all': 0, 'up_to_date': 0, 'not_updated': 0,
+ 'cant_be_updated': 0, 'unknown': 0}
+ for name in maintainer_names}
+ if not statistics:
+ return statistics
+
+ for maintplanlayer in milestone.release.plan.maintenanceplanlayerbranch_set.all():
+ layerbranch = maintplanlayer.layerbranch
+
+ recipe_upstream_history = RecipeUpstreamHistory.get_last_by_date_range(
+ layerbranch,
+ milestone.start_date,
+ milestone.end_date
+ )
+ recipe_maintainer_history = Raw.get_remahi_by_end_date(
+ layerbranch.id, milestone.end_date)
+ if not recipe_maintainer_history:
+ continue
+
+ maintainer_recipes = {}
+ for recipesymbol_id, name in RecipeMaintainer.objects.filter(
+ history_id=recipe_maintainer_history[0]).values_list(
+ 'recipesymbol_id', 'maintainer__name'):
+ maintainer_recipes.setdefault(name, set()).add(recipesymbol_id)
+
+ recipe_upstreams = {}
+ if recipe_upstream_history and maintainer_recipes:
+ for ru in RecipeUpstream.objects.filter(
+ history=recipe_upstream_history).values(
+ 'recipesymbol_id', 'status', 'no_update_reason'):
+ recipe_upstreams.setdefault(ru['recipesymbol_id'], []).append(ru)
+
+ for name, maintainer_statistics in statistics.items():
+ recipes = maintainer_recipes.get(name, set())
+ maintainer_statistics['all'] += len(recipes)
+ for recipesymbol_id in recipes:
+ for ru in recipe_upstreams.get(recipesymbol_id, []):
+ if ru['status'] == 'Y':
+ maintainer_statistics['up_to_date'] += 1
+ elif ru['status'] == 'N':
+ if ru['no_update_reason'] == '':
+ maintainer_statistics['not_updated'] += 1
+ else:
+ maintainer_statistics['cant_be_updated'] += 1
+ else:
+ maintainer_statistics['unknown'] += 1
+
+ for maintainer_statistics in statistics.values():
+ maintainer_statistics['percentage'] = '0'
+ if maintainer_statistics['all'] > 0:
+ maintainer_statistics['percentage'] = "%.0f" % \
+ ((float(maintainer_statistics['up_to_date']) /
+ float(maintainer_statistics['all'])) * 100)
+
+ return statistics
+
class RecipeList():
pk = None
name = None
@@ -879,8 +941,18 @@ class MaintainerListView(ListView):
self.intervals = sorted(intervals.keys())
current_date = date.today()
+ maintainer_names = {ml.name for ml in maintainer_list}
+ all_statistics = _get_maintainers_statistics(milestone, maintainer_names)
+ # Upgrades per maintainer in each interval, one query per interval
+ interval_upgrades = []
+ for i in (self.intervals if maintainer_names else []):
+ interval_upgrades.append(dict(RecipeUpgrade.objects.filter(
+ maintainer__name__in=maintainer_names,
+ commit_date__gte=intervals[i]['start_date'],
+ commit_date__lte=intervals[i]['end_date']).values_list(
+ 'maintainer__name').annotate(Count('id')).order_by()))
for ml in maintainer_list:
- milestone_statistics = _get_milestone_statistics(milestone, ml.name)
+ milestone_statistics = all_statistics[ml.name]
ml.recipes_all = milestone_statistics['all']
ml.recipes_up_to_date = ('' if milestone_statistics['up_to_date'] == 0
else milestone_statistics['up_to_date'])
@@ -901,9 +973,7 @@ class MaintainerListView(ListView):
if current_date >= start_date and current_date <= end_date:
self.current_interval = idx
- number = RecipeUpgrade.objects.filter(maintainer__name = ml.name,
- commit_date__gte = start_date,
- commit_date__lte = end_date).count()
+ number = interval_upgrades[idx].get(ml.name, 0)
ml.interval_statistics.append('' if number == 0 else number)
# To add Wk prefix after get statics to avoid sorting problems
new file mode 100644
@@ -0,0 +1,94 @@
+# layerindex-web - tests for the RRS maintainers page
+#
+# Licensed under the MIT license, see COPYING.MIT for details
+#
+# SPDX-License-Identifier: MIT
+#
+# AI-generated, Claude Opus 5.5
+
+# The maintainers page shows each maintainer's recipe statistics and upgrades
+# per week, and must get them without running queries per maintainer.
+
+import datetime
+
+from django.db import connection
+from django.test.utils import CaptureQueriesContext
+
+from rrs.models import (
+ Maintainer,
+ RecipeMaintainer,
+ RecipeMaintainerHistory,
+ RecipeSymbol,
+ RecipeUpgrade,
+ RecipeUpstream,
+ RecipeUpstreamHistory,
+)
+
+URL = '/rrs/maintainers/OE-Core/6.2/M1/'
+# USE_TZ is False
+UPGRADE_DATE = datetime.datetime(2026, 11, 1)
+
+
+def histories(layerbranch):
+ author, _ = Maintainer.objects.get_or_create(name='No maintainer')
+ maintainer_history = RecipeMaintainerHistory.objects.create(
+ title='Update maintainers', date=datetime.datetime(2026, 10, 15), author=author,
+ sha1='0' * 40, layerbranch=layerbranch)
+ upstream_history = RecipeUpstreamHistory.objects.create(
+ layerbranch=layerbranch, start_date=datetime.datetime(2026, 10, 20),
+ end_date=datetime.datetime(2026, 10, 20, 1))
+ return maintainer_history, upstream_history
+
+
+def add_recipe(layerbranch, history, pn, maintainer_name, status, no_update_reason=''):
+ """A recipe maintained by maintainer_name, with an upstream status and one upgrade"""
+ maintainer_history, upstream_history = history
+ maintainer, _ = Maintainer.objects.get_or_create(name=maintainer_name)
+ symbol = RecipeSymbol.objects.create(layerbranch=layerbranch, pn=pn, summary=pn)
+ RecipeMaintainer.objects.create(recipesymbol=symbol, maintainer=maintainer,
+ history=maintainer_history)
+ RecipeUpstream.objects.create(recipesymbol=symbol, history=upstream_history, version='2.0',
+ type='A', status=status, no_update_reason=no_update_reason,
+ date=upstream_history.end_date)
+ RecipeUpgrade.objects.create(recipesymbol=symbol, maintainer=maintainer, version='1.0',
+ author_date=UPGRADE_DATE, commit_date=UPGRADE_DATE,
+ upgrade_type='U')
+
+
+def maintainer_list(client):
+ response = client.get(URL)
+ assert response.status_code == 200
+ return {ml.name: ml for ml in response.context['maintainer_list']}
+
+
+def test_statistics(client, rrs_plan):
+ history = histories(rrs_plan.layerbranch)
+ add_recipe(rrs_plan.layerbranch, history, 'busybox', 'Alice', 'Y')
+ add_recipe(rrs_plan.layerbranch, history, 'zlib', 'Alice', 'N')
+ add_recipe(rrs_plan.layerbranch, history, 'gcc', 'Bob', 'N', 'Needs a newer binutils')
+
+ maintainers = maintainer_list(client)
+
+ alice, bob = maintainers['Alice'], maintainers['Bob']
+ assert (alice.recipes_all, alice.recipes_up_to_date, alice.recipes_not_updated,
+ alice.recipes_cant_be_updated, alice.percentage_done) == (2, 1, 1, '', '50%')
+ assert (bob.recipes_all, bob.recipes_up_to_date, bob.recipes_cant_be_updated,
+ bob.percentage_done) == (1, '', 1, '0%')
+ # One upgrade each, in the week of UPGRADE_DATE
+ assert [n for n in alice.interval_statistics if n] == [2]
+ assert [n for n in bob.interval_statistics if n] == [1]
+ assert alice.interval_statistics.index(2) == bob.interval_statistics.index(1)
+
+
+def test_queries_do_not_grow_with_maintainers(client, rrs_plan):
+ history = histories(rrs_plan.layerbranch)
+ # All 'N', so the plan-wide statistics take the same path both times
+ add_recipe(rrs_plan.layerbranch, history, 'recipe-0', 'Maintainer 0', 'N')
+ maintainer_list(client) # fills the per-process caches first
+ with CaptureQueriesContext(connection) as one:
+ assert 'Maintainer 0' in maintainer_list(client)
+ for i in range(1, 6):
+ add_recipe(rrs_plan.layerbranch, history, f'recipe-{i}', f'Maintainer {i}', 'N')
+ with CaptureQueriesContext(connection) as six:
+ assert len(maintainer_list(client)) == 6
+ assert len(six) == len(one)
The maintainers page ran about a dozen queries per maintainer: the statistics of each maintainer separately, then one count of upgrades per maintainer per interval. Compute every maintainer's statistics from one read of the maintainer and upstream records per layer branch, and count each interval's upgrades for all maintainers in one grouped query. On a copy of the production database the page data for 9 milestones is identical before and after, and OE-Core 6.2 takes 28 queries and 0.08 s instead of 952 queries and 0.75 s. AI-Generated: Claude Opus 5.5 Signed-off-by: Tim Orling <tim.orling@konsulko.com> --- rrs/views.py | 78 +++++++++++++++++++++++++++-- tests/rrs/test_maintainers.py | 94 +++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 tests/rrs/test_maintainers.py