diff --git a/api/api.py b/api/api.py index d8b3ec77f42268dad80493306730a1b5f184e061..7b0acf597018cfb09a7abcb2abe9f5d35a4f19a5 100644 --- a/api/api.py +++ b/api/api.py @@ -71,6 +71,7 @@ routes = [ webapp2_extras.routes.PathPrefixRoute(r'/api/jobs', [ webapp2.Route(r'/next', jobs.Jobs, handler_method='next', methods=['GET']), webapp2.Route(r'/count', jobs.Jobs, handler_method='count', methods=['GET']), + webapp2.Route(r'/stats', jobs.Jobs, handler_method='stats', methods=['GET']), webapp2.Route(r'/addTestJob', jobs.Jobs, handler_method='addTestJob', methods=['GET']), webapp2.Route(r'/reap', jobs.Jobs, handler_method='reap_stale', methods=['POST']), webapp2.Route(r'/<:[^/]+>', jobs.Job, name='job'), diff --git a/api/jobs.py b/api/jobs.py index 4533fd38860933bf7548b593066b1a80a51dbbe9..c784ad02b03abac57348e084b64a394a40aba0f0 100644 --- a/api/jobs.py +++ b/api/jobs.py @@ -217,6 +217,20 @@ class Jobs(base.RequestHandler): return config.db.jobs.count() + def stats(self): + if not self.superuser_request: + self.abort(403, 'Request requires superuser') + result = config.db.jobs.aggregate([{"$group": {"_id": "$state", "count": {"$sum": 1}}}]) + + # Map mongo result to a useful object + states = {} + for r in result: + key = r['_id'] + val = r['count'] + states[key] = val + + return states + def next(self): """ Atomically change a 'pending' job to 'running' and returns it. Updates timestamp.