From 6b9420b34c0afb0bc8c8179adae5efd982c9c536 Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Feb 17 2022 06:43:58 +0000 Subject: [PATCH 1/2] frontend: speed-up the models.Batch related routes We don't have to do a SeqScan through all the Builds when we calculate the Batch tree — we can just go through those builds that really reference some batch. Also, constructing the Batch tree needs one SQL query for each node, and each of those queries took about ~200ms before — there was a missing index. Merges: #2091 Fixes: #2090 --- diff --git a/frontend/coprs_frontend/alembic/versions/9409fc1d5895_build_batch_id_indexed.py b/frontend/coprs_frontend/alembic/versions/9409fc1d5895_build_batch_id_indexed.py new file mode 100644 index 0000000..c59ef4c --- /dev/null +++ b/frontend/coprs_frontend/alembic/versions/9409fc1d5895_build_batch_id_indexed.py @@ -0,0 +1,19 @@ +""" +Build.batch_id indexed + +Revision ID: 9409fc1d5895 +Revises: 58f7510f0fae +Create Date: 2022-02-15 14:38:52.680231 +""" + +from alembic import op + + +revision = '9409fc1d5895' +down_revision = '58f7510f0fae' + +def upgrade(): + op.create_index(op.f('ix_build_batch_id'), 'build', ['batch_id'], unique=False) + +def downgrade(): + op.drop_index(op.f('ix_build_batch_id'), table_name='build') diff --git a/frontend/coprs_frontend/coprs/logic/batches_logic.py b/frontend/coprs_frontend/coprs/logic/batches_logic.py index 25796ff..13f3c12 100644 --- a/frontend/coprs_frontend/coprs/logic/batches_logic.py +++ b/frontend/coprs_frontend/coprs/logic/batches_logic.py @@ -52,7 +52,7 @@ class BatchesLogic: Query for all still not-finished batches, order by id ASC """ batches = set() - query = bl.BuildsLogic.processing_builds() + query = bl.BuildsLogic.processing_builds().filter(Build.batch_id.isnot(None)) for build in query.all(): if build.batch: batches.add(build.batch) diff --git a/frontend/coprs_frontend/coprs/models.py b/frontend/coprs_frontend/coprs/models.py index 58980a4..34e98bf 100644 --- a/frontend/coprs_frontend/coprs/models.py +++ b/frontend/coprs_frontend/coprs/models.py @@ -1012,7 +1012,7 @@ class Build(db.Model, helpers.Serializer): chroots = association_proxy("build_chroots", "mock_chroot") - batch_id = db.Column(db.Integer, db.ForeignKey("batch.id")) + batch_id = db.Column(db.Integer, db.ForeignKey("batch.id"), index=True) batch = db.relationship("Batch", backref=db.backref("builds")) module_id = db.Column(db.Integer, db.ForeignKey("module.id"), index=True) From af7d02f4768e3f26471d8f5aa423cf60bcebeae6 Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Feb 17 2022 06:43:58 +0000 Subject: [PATCH 2/2] frontend: cache the number of currently processed batches This integer is a pretty expensive one, and shown on _so many_ pages. Let's cache it (it will not hurt too much if we show a minute (or so) outdated number). Merges: #2091 --- diff --git a/frontend/coprs_frontend/coprs/logic/batches_logic.py b/frontend/coprs_frontend/coprs/logic/batches_logic.py index 13f3c12..a28fae1 100644 --- a/frontend/coprs_frontend/coprs/logic/batches_logic.py +++ b/frontend/coprs_frontend/coprs/logic/batches_logic.py @@ -4,7 +4,7 @@ Methods for working with build Batches. import anytree -from coprs import db +from coprs import db, cache from coprs.helpers import WorkList from coprs.models import Batch, Build from coprs.exceptions import BadRequest @@ -59,6 +59,17 @@ class BatchesLogic: return batches @classmethod + @cache.memoize(timeout=60) + def pending_batch_count_cached(cls): + """ + Return the number of currently processed Batch instances (where at least + one build is not yet fully finished). This is a pretty expensive number + and yet we show it on every /stats/ page (and on many others) — that's + why we cache it. + """ + return len(cls.pending_batches()) + + @classmethod def pending_batch_trees(cls): """ Get all the currently processing batches, together with all the diff --git a/frontend/coprs_frontend/coprs/logic/complex_logic.py b/frontend/coprs_frontend/coprs/logic/complex_logic.py index 17ba2e0..6ed77e0 100644 --- a/frontend/coprs_frontend/coprs/logic/complex_logic.py +++ b/frontend/coprs_frontend/coprs/logic/complex_logic.py @@ -271,7 +271,7 @@ class ComplexLogic(object): pending=pending, running=running, starting=starting, - batches=len(BatchesLogic.pending_batches()), + batches=BatchesLogic.pending_batch_count_cached(), ) @classmethod