diff options
-rw-r--r-- | app/handlers/__init__.py | 1 | ||||
-rw-r--r-- | app/handlers/count.py | 84 | ||||
-rw-r--r-- | app/urls.py | 5 |
3 files changed, 90 insertions, 0 deletions
diff --git a/app/handlers/__init__.py b/app/handlers/__init__.py index 5ff5b3c..4003003 100644 --- a/app/handlers/__init__.py +++ b/app/handlers/__init__.py @@ -17,3 +17,4 @@ from boot import * from defconf import * from job import * from subscription import * +from count import * diff --git a/app/handlers/count.py b/app/handlers/count.py new file mode 100644 index 0000000..e002cee --- /dev/null +++ b/app/handlers/count.py @@ -0,0 +1,84 @@ +# Copyright (C) 2014 Linaro Ltd. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +"""Handle the /count URLs used to count objects in the database.""" + +import tornado + +from functools import partial +from tornado.web import ( + asynchronous, +) + +from handlers.base import BaseHandler +from models.boot import BOOT_COLLECTION +from models.defconfig import DEFCONFIG_COLLECTION +from models.job import JOB_COLLECTION +from models.subscription import SUBSCRIPTION_COLLECTION +from utils.db import count + +# All the available collections as key-value. The key is the same used for the +# URL configuration. +COLLECTIONS = { + 'boot': BOOT_COLLECTION, + 'defconfig': DEFCONFIG_COLLECTION, + 'job': JOB_COLLECTION, + 'subscription': SUBSCRIPTION_COLLECTION, +} + + +class CountHandler(BaseHandler): + """Handle the /count URLs.""" + + def __init__(self, application, request, **kwargs): + super(CountHandler, self).__init__(application, request, **kwargs) + + @asynchronous + def get(self, *args, **kwargs): + if kwargs and kwargs.get('collection', None): + if kwargs['collection'] in COLLECTIONS.keys(): + self.executor.submit( + partial(self._count_one_collection, kwargs['collection']) + ).add_done_callback( + lambda future: + tornado.ioloop.IOLoop.instance().add_callback( + partial( + self._create_valid_response, future.result() + ) + ) + ) + else: + self.write_error(404) + else: + self.executor.submit( + partial(self._count_all_collections) + ).add_done_callback( + lambda future: tornado.ioloop.IOLoop.instance().add_callback( + partial(self._get_callback, future.result()) + ) + ) + + def _count_one_collection(self, collection): + pass + + def _count_all_collections(self): + result = dict(result=[]) + + for key, val in COLLECTIONS.iteritems(): + result['result'].append( + dict(collection=key, count=count(self.db[val])) + ) + + return result diff --git a/app/urls.py b/app/urls.py index 90cec3e..5cf4f8c 100644 --- a/app/urls.py +++ b/app/urls.py @@ -22,6 +22,7 @@ from handlers import ( DefConfHandler, JobHandler, SubscriptionHandler, + CountHandler, ) _JOB_URL = url(r'/api/job(?P<sl>/)?(?P<id>.*)', JobHandler, name='job') @@ -34,9 +35,13 @@ _SUBSCRIPTION_URL = url( name='subscription', ) _BOOT_URL = url(r'/api/boot(?P<sl>/)?(?P<id>.*)', BootHandler, name='boot') +_COUNT_URL = url( + r'/api/count(?P<sl>/)?(?P<collection>.*)', CountHandler, name='count' +) APP_URLS = [ _BOOT_URL, + _COUNT_URL, _DEFCONF_URL, _JOB_URL, _SUBSCRIPTION_URL, |