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
This commit is contained in:
James E. Blair 2024-01-03 13:27:52 -08:00
parent 50f068ee6d
commit f99cee543e
2 changed files with 10 additions and 6 deletions

View File

@ -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)

View File

@ -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)