zuul/zuul/lib/times.py
James E. Blair f99cee543e 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
2024-01-04 06:30:52 -08:00

69 lines
1.9 KiB
Python

# Copyright 2021 Acme Gating, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
import cachetools
class Times:
"""Obtain estimated build times"""
log = logging.getLogger("zuul.times")
def __init__(self, sql, statsd):
self.sql = sql.connection
self.statsd = statsd
self.cache = cachetools.TTLCache(8192, 3600)
def start(self):
pass
def stop(self):
pass
def join(self):
pass
def _getTime(self, key):
tenant, project, branch, job = key
previous_builds = self.sql.getBuildTimes(
tenant=tenant,
project=project,
branch=branch,
job_name=job,
final=True,
result='SUCCESS',
limit=10)
times = [x.duration for x in previous_builds if x.duration]
if times:
estimate = float(sum(times)) / len(times)
self.cache.setdefault(key, estimate)
return estimate
return None
# Don't cache a zero value, so that new jobs get an estimated
# time ASAP.
def getEstimatedTime(self, tenant, project, branch, job):
key = (tenant, project, branch, job)
ret = self.cache.get(key)
if ret is not None:
return ret
if self.statsd:
with self.statsd.timer('zuul.scheduler.time_query'):
return self._getTime(key)
else:
return self._getTime(key)