Add get_run_count DB API function

This commit adds a new DB API function get_run_count() which, like it's
name implies, returns the number of runs in the DB.

Change-Id: I31507f2e8ee8e3cc9b4587573f3b44b3f10dffee
This commit is contained in:
Matthew Treinish 2017-01-06 16:57:18 -05:00
parent c47cfaa6c7
commit b0b5d2c517
No known key found for this signature in database
GPG Key ID: FD12A0F214C9E177
3 changed files with 24 additions and 0 deletions

View File

@ -0,0 +1,4 @@
---
features:
- Add a new DB API function get_runs_count() which returns the total number of
runs in the DB

View File

@ -1671,6 +1671,17 @@ def get_recent_failed_runs_by_run_metadata(key, value, num_runs=10,
models.Run.run_at.desc()).limit(num_runs).all() models.Run.run_at.desc()).limit(num_runs).all()
def get_runs_count(session=None):
"""Get the number of runs currently in the database
:return count: The number of runs in the DB
:rtype: int
"""
session = session or get_session()
query = db_utils.model_query(models.Run, session)
return query.count()
def get_runs_counts_by_run_metadata(key, value, start_date=None, session=None): def get_runs_counts_by_run_metadata(key, value, start_date=None, session=None):
"""Check runs for a given run metadata pair """Check runs for a given run metadata pair

View File

@ -909,6 +909,15 @@ class TestDatabaseAPI(base.TestCase):
self.assertEqual(1, len(results)) self.assertEqual(1, len(results))
self.assertEqual(run_c.id, results[0].id) self.assertEqual(run_c.id, results[0].id)
def test_get_runs_count(self):
for i in range(0, 100):
api.create_run()
count = api.get_runs_count()
self.assertEqual(100, count)
def test_get_runs_count_zero(self):
self.assertEqual(0, api.get_runs_count())
def test_get_runs_counts_by_run_metadata_no_start_date(self): def test_get_runs_counts_by_run_metadata_no_start_date(self):
run_a = api.create_run(fails=1) run_a = api.create_run(fails=1)
api.create_run() api.create_run()