From 5bce73c97dc4a1b6e81165d27e9377ae32a3a073 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Thu, 6 Jul 2023 10:28:52 +0200 Subject: [PATCH] [IMP] *: optimise loading of home page Fix outstanding query to make a positive `state` filtering, instead of negative, matching 3b52b1aace8674259812a76b1566260937dbcacb. Also manually create a map of stagings (grouped by branch) sharing a single prefetch set. For odoo the mergebot home page has 12 branches in the odoo project and 8 in spreadsheet, 6 stagings each. This means 120 queries to retrieve all the heads (Odoo stagings have 5 heads and spreadsheet have 1, but that seems immaterial). By fixing `_compute_statuses` and creating a single prefetch set for all stagings of all branches we can fetch all the commits in a single query instead of 120. --- forwardport/data/views.xml | 3 +-- runbot_merge/controllers/dashboard.py | 19 ++++++++++++++++++- runbot_merge/models/pull_requests.py | 23 ++++++++++++++++++----- runbot_merge/views/templates.xml | 4 ++-- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/forwardport/data/views.xml b/forwardport/data/views.xml index 1b6069c2..08ef8177 100644 --- a/forwardport/data/views.xml +++ b/forwardport/data/views.xml @@ -10,8 +10,7 @@
diff --git a/runbot_merge/controllers/dashboard.py b/runbot_merge/controllers/dashboard.py index f15c52d2..8a173c77 100644 --- a/runbot_merge/controllers/dashboard.py +++ b/runbot_merge/controllers/dashboard.py @@ -13,8 +13,25 @@ LIMIT = 20 class MergebotDashboard(Controller): @route('/runbot_merge', auth="public", type="http", website=True, sitemap=True) def dashboard(self): + projects = request.env['runbot_merge.project'].with_context(active_test=False).sudo().search([]) + stagings = { + branch: projects.env['runbot_merge.stagings'].search([ + ('target', '=', branch.id)], order='staged_at desc', limit=6) + for project in projects + for branch in project.branch_ids + if branch.active + } + prefetch_set = list({ + id + for stagings in stagings.values() + for id in stagings.ids + }) + for st in stagings.values(): + st._prefetch_ids = prefetch_set + return request.render('runbot_merge.dashboard', { - 'projects': request.env['runbot_merge.project'].with_context(active_test=False).sudo().search([]), + 'projects': projects, + 'stagings_map': stagings, }) @route('/runbot_merge/', auth='public', type='http', website=True, sitemap=False) diff --git a/runbot_merge/models/pull_requests.py b/runbot_merge/models/pull_requests.py index d48ad505..5b3c23d5 100644 --- a/runbot_merge/models/pull_requests.py +++ b/runbot_merge/models/pull_requests.py @@ -1813,12 +1813,25 @@ class Stagings(models.Model): (repo, context, state, url) """ Commits = self.env['runbot_merge.commit'] + heads = { + head: repo + for st in self + for repo, head in json.loads(st.heads).items() + if not repo.endswith('^') + } + all_heads = Commits.search([('sha', 'in', list(heads))]) + commits_map = {commit.sha: commit.id for commit in all_heads} + for st in self: - heads = { - head: repo for repo, head in json.loads(st.heads).items() - if not repo.endswith('^') - } - commits = st.head_ids = Commits.search([('sha', 'in', list(heads.keys()))]) + commits = st.head_ids = Commits._browse( + self.env, + tuple( + commits_map[h] + for repo, h in json.loads(st.heads).items() + if not repo.endswith('^') + ), + all_heads._prefetch_ids + ) if st.statuses_cache: st.statuses = json.loads(st.statuses_cache) continue diff --git a/runbot_merge/views/templates.xml b/runbot_merge/views/templates.xml index 73e8e883..34b403a7 100644 --- a/runbot_merge/views/templates.xml +++ b/runbot_merge/views/templates.xml @@ -72,7 +72,7 @@
-
+

key: @@ -154,7 +154,7 @@