diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2015-06-11 18:23:22 +0000 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2015-06-11 18:23:22 +0000 |
commit | 2a41a85dbc8f53235f965ed467a825d1ed8b1d74 (patch) | |
tree | a8f201627cc2356fbaf9f8bc1e6614d35fe7063e | |
parent | 0ad535a3a4929857b2aef24284d9e6354e9ce3ba (diff) | |
download | morph-cache-server-2a41a85dbc8f53235f965ed467a825d1ed8b1d74.tar.gz |
Add methods for viewing stored builds
-rwxr-xr-x | morph-cache-server | 7 | ||||
-rw-r--r-- | morphcacheserver/artifact_database.py | 35 |
2 files changed, 40 insertions, 2 deletions
diff --git a/morph-cache-server b/morph-cache-server index 61c1640..32c57a5 100755 --- a/morph-cache-server +++ b/morph-cache-server @@ -399,9 +399,12 @@ class MorphCacheServer(cliapp.Application): @app.get('/builds') def get_builds(): - '''Return info on all known builds of a given artifact.''' + '''Return info on all known builds.''' + return {'builds': db.view_artifact_statistics()} - cache_name = self._unescape_parameter(request.query.cache_name) + @app.get('/builds/<cache_name>') + def get_builds_for_artifact(cache_name): + '''Return info on builds of a given artifact.''' results = sorted(db.iter_builds_for_artifact_file(cache_name)) if len(results) == 0: diff --git a/morphcacheserver/artifact_database.py b/morphcacheserver/artifact_database.py index f893b49..3745a61 100644 --- a/morphcacheserver/artifact_database.py +++ b/morphcacheserver/artifact_database.py @@ -140,3 +140,38 @@ class ArtifactDatabase(object): 'build_datetime': build_datetime, 'hash_sha1': hash_sha1 } + + def view_artifact_statistics(self, start=0, page_size=50): + '''Return information on number of builds for each artifact. + + Returns a dict. + + ''' + n_builds_per_artifact_sql = ''' + SELECT builds.cache_name, + COUNT(builds.hash_sha1) as n_builds, + COUNT(DISTINCT builds.hash_sha1) as n_different_builds + FROM artifact_files INNER JOIN builds + ON builds.cache_name = artifact_files.cache_name + GROUP BY builds.cache_name + ''' + + # Return the artifacts with the highest number of mismatched builds + # first, they are the most important. Sort order should be configurable, + # really. + sql = n_builds_per_artifact_sql + ' ORDER BY n_different_builds DESC' + + sql += ' LIMIT %i OFFSET %i' % (page_size, start) + + cursor = self.db.cursor() + logging.debug('Running: %s', sql) + cursor.execute(sql) + + result = [] + for row in cursor: + result.append({ + 'cache_name': row[0], + 'n_builds': row[1], + 'n_different_builds': row[2], + }) + return result |