Add optional start_date parameter to get_recent*() methods

This commit adds an optional start_date parameter to the get_recent*
methods that will only return results after a specified date.

Change-Id: Ib1c899701fdec6d86ced6edca7aa7a8385cefc92
This commit is contained in:
Matthew Treinish 2016-04-18 14:52:20 -04:00
parent 5429a5d7a8
commit 4516b368df
No known key found for this signature in database
GPG Key ID: FD12A0F214C9E177
3 changed files with 72 additions and 10 deletions

View File

@ -0,0 +1,7 @@
---
features:
- An optional `start_date` parameter was added to all DB API functions that
returned a recent runs list. This includes get_recent_successful_runs(),
get_recent_failed_runs(), and get_recent_runs_by_key_value_metadata(). This
is to set a starting point for the list of recent runs returned by these
methods.

View File

@ -1049,53 +1049,68 @@ def get_test_status_time_series(test_id, session=None):
return status_series
def get_recent_successful_runs(num_runs=10, session=None):
def get_recent_successful_runs(num_runs=10, session=None, start_date=None):
"""Return a list of run uuid strings for the most recent successful runs
:param int num_runs: The number of runs to return in the list
:param session: Optional session object if one isn't provided a new session
:param datetime start_date: An optional date to use as the starting point
for getting recent runs. Only runs after this
date will be returned.
:return list: A list of run uuid strings (the id column in the runs table)
for the most recent runs.
"""
session = session or get_session()
results = db_utils.model_query(models.Run, session).order_by(
results = db_utils.model_query(models.Run, session)
results = _filter_runs_by_date(results, start_date)
results = results.order_by(
models.Run.run_at.desc()).filter_by(fails=0).limit(num_runs).all()
return list(map(lambda x: x.uuid, results))
def get_recent_failed_runs(num_runs=10, session=None):
def get_recent_failed_runs(num_runs=10, session=None, start_date=None):
"""Return a list of run uuid strings for the most recent failed runs
:param int num_runs: The number of runs to return in the list
:param session: Optional session object if one isn't provided a new session
:param datetime start_date: An optional date to use as the starting point
for getting recent runs. Only runs after this
date will be returned.
:return list: A list of run uuid strings (the id column in the runs table)
for the most recent runs.
"""
session = session or get_session()
results = db_utils.model_query(models.Run, session).order_by(
results = db_utils.model_query(models.Run, session)
results = _filter_runs_by_date(results, start_date)
results = results.order_by(
models.Run.run_at.desc()).filter(
models.Run.fails > 0).limit(num_runs).all()
return list(map(lambda x: x.uuid, results))
def get_recent_runs_by_key_value_metadata(key, value, num_runs=10,
session=None):
session=None, start_date=None):
"""Get a list of runs for recent runs with a key value metadata pair
:param int num_runs: The number of runs to return in the list
:param session: Optional session object if one isn't provided a new session
:param datetime start_date: An optional date to use as the starting point
for getting recent runs. Only runs after this
date will be returned.
:return list: A list of run objects for the most recent runs.
:rtype subunit2sql.db.models.Run
"""
session = session or get_session()
results = db_utils.model_query(models.Run, session).join(
models.RunMetadata,
models.Run.id == models.RunMetadata.run_id).filter(
models.RunMetadata.key == key,
models.RunMetadata.value == value).order_by(
models.Run.run_at.desc()).limit(num_runs).all()
models.Run.id == models.RunMetadata.run_id)
results = _filter_runs_by_date(results, start_date)
results = results.filter(
models.RunMetadata.key == key,
models.RunMetadata.value == value).order_by(
models.Run.run_at.desc()).limit(num_runs).all()
return results

View File

@ -577,6 +577,22 @@ class TestDatabaseAPI(base.TestCase):
self.assertNotIn(run_b.id, [x.id for x in result])
self.assertIn(run_c.id, [x.id for x in result])
def test_get_recent_runs_by_key_value_metadata_with_start_date(self):
run_a = api.create_run(run_at=datetime.datetime(
1914, 6, 28, 10, 45, 0))
run_b = api.create_run()
run_c = api.create_run()
api.add_run_metadata({'a_key': 'a_value'}, run_a.id)
api.add_run_metadata({'a_key': 'a_value'}, run_c.id)
api.add_run_metadata({'a_key': 'b_value'}, run_b.id)
result = api.get_recent_runs_by_key_value_metadata(
'a_key', 'a_value', start_date=datetime.datetime(
1918, 11, 11, 11, 11, 11))
self.assertEqual(1, len(result))
self.assertNotIn(run_a.id, [x.id for x in result])
self.assertNotIn(run_b.id, [x.id for x in result])
self.assertIn(run_c.id, [x.id for x in result])
def test_get_recent_runs_by_key_value_metadata_one_run(self):
timestamp = datetime.datetime(1914, 6, 28, 10, 45, 0)
run_a = api.create_run(run_at=timestamp)
@ -939,6 +955,18 @@ class TestDatabaseAPI(base.TestCase):
self.assertIn(run_c.uuid, res)
self.assertIn(run_d.uuid, res)
def test_get_recent_failed_runs_with_start_date(self):
api.create_run(fails=1, run_at=datetime.datetime(
1914, 6, 28, 10, 45, 0))
api.create_run()
run_c = api.create_run(fails=2)
run_d = api.create_run(fails=1)
res = api.get_recent_failed_runs(start_date=datetime.datetime(
1918, 11, 11, 11, 11, 11))
self.assertEqual(2, len(res))
self.assertIn(run_c.uuid, res)
self.assertIn(run_d.uuid, res)
def test_get_recent_successful_runs(self):
run_a = api.create_run(passes=1)
run_b = api.create_run()
@ -950,6 +978,18 @@ class TestDatabaseAPI(base.TestCase):
self.assertIn(run_b.uuid, res)
self.assertIn(run_c.uuid, res)
def test_get_recent_successful_runs_with_start_date(self):
api.create_run(passes=1, run_at=datetime.datetime(
1914, 6, 28, 10, 45, 0))
run_b = api.create_run()
run_c = api.create_run(passes=2)
api.create_run(fails=1)
res = api.get_recent_successful_runs(start_date=datetime.datetime(
1918, 11, 11, 11, 11, 11))
self.assertEqual(2, len(res))
self.assertIn(run_b.uuid, res)
self.assertIn(run_c.uuid, res)
def test_get_test_counts_in_date_range_as_str(self):
timestamp_str_a = 'Dec 01 2015'
timestamp_str_b = 'Dec 20 2015'