From f99cee543eef665b787b563f0782b71d60e99db7 Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Wed, 3 Jan 2024 13:27:52 -0800 Subject: [PATCH] Use getBuildTimes for build time estimator The existing build time estimator uses the normal getBuilds query method. We recently added an optimized query for a new build times API endpoint. As one would expect, the build time estimator has limited query needs and should be able to use the new optimized query as well. The one thing missing is a search for a specific result (ie, SUCCESS), so that is added. The unused sort operator is removed. From what I can tell, switching to sorting by end_time should not produce a reduction in performance compared to sorting by id. Change-Id: I1096d466accad5574b6cfa226e68b070f769128f --- zuul/driver/sql/sqlconnection.py | 9 +++++++-- zuul/lib/times.py | 7 +++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/zuul/driver/sql/sqlconnection.py b/zuul/driver/sql/sqlconnection.py index 0c501b50f3..5bad792427 100644 --- a/zuul/driver/sql/sqlconnection.py +++ b/zuul/driver/sql/sqlconnection.py @@ -265,6 +265,7 @@ class DatabaseSession(object): sort_by_buildset=False, limit=50, offset=0, + result=None, exclude_result=None, query_timeout=None): @@ -298,6 +299,7 @@ class DatabaseSession(object): q = self.listFilter(q, ref_table.c.branch, branch) q = self.listFilter(q, ref_table.c.ref, ref) q = self.listFilter(q, build_table.c.job_name, job_name) + q = self.listFilter(q, build_table.c.result, result) q = self.exListFilter(q, build_table.c.result, exclude_result) q = self.listFilter(q, build_table.c.final, final) if start_time: @@ -305,8 +307,11 @@ class DatabaseSession(object): q = q.filter(build_table.c.end_time >= start_time) if end_time: q = q.filter(build_table.c.end_time <= end_time) - # Only complete builds - q = q.filter(build_table.c.result != None) # noqa + # Only complete builds (but if we specify a result, then it's + # obviously complete and this extra filter is not necessary) + if result is None: + q = q.filter(build_table.c.result != None) # noqa + q = q.order_by(build_table.c.end_time.desc()) q = q.limit(limit).offset(offset) diff --git a/zuul/lib/times.py b/zuul/lib/times.py index 2545510930..3cf8a5adfe 100644 --- a/zuul/lib/times.py +++ b/zuul/lib/times.py @@ -23,7 +23,7 @@ class Times: log = logging.getLogger("zuul.times") def __init__(self, sql, statsd): - self.sql = sql + self.sql = sql.connection self.statsd = statsd self.cache = cachetools.TTLCache(8192, 3600) @@ -38,15 +38,14 @@ class Times: def _getTime(self, key): tenant, project, branch, job = key - previous_builds = self.sql.getBuilds( + previous_builds = self.sql.getBuildTimes( tenant=tenant, project=project, branch=branch, job_name=job, final=True, result='SUCCESS', - limit=10, - sort_by_buildset=True) + limit=10) times = [x.duration for x in previous_builds if x.duration] if times: estimate = float(sum(times)) / len(times)