From 46228df56b6dba0f02172c6d057247e16b100e7f Mon Sep 17 00:00:00 2001 From: aviau Date: Mon, 20 Jul 2015 12:01:51 -0400 Subject: [PATCH] Implemented paging for MongoDB objects Change-Id: I3aa92e070c826590aa45d3fda8b5f6f9ca65a7c0 --- surveil/api/datamodel/status/paging.py | 4 +-- .../api/handlers/status/live_host_handler.py | 4 +-- .../handlers/status/live_service_handler.py | 4 +-- surveil/api/handlers/status/mongodb_query.py | 9 +++++- .../api/controllers/v2/status/test_hosts.py | 28 +++++++++++++++++++ .../api/handlers/live/test_mongodb_query.py | 10 +++++-- 6 files changed, 49 insertions(+), 10 deletions(-) diff --git a/surveil/api/datamodel/status/paging.py b/surveil/api/datamodel/status/paging.py index 9680f9d..5f7a534 100644 --- a/surveil/api/datamodel/status/paging.py +++ b/surveil/api/datamodel/status/paging.py @@ -19,10 +19,10 @@ from surveil.api.datamodel import types class Paging(types.Base): - size = wsme.wsattr(int, mandatory=False) + size = wsme.wsattr(int, mandatory=True) """Size of the result.""" - page = wsme.wsattr(int, mandatory=False) + page = wsme.wsattr(int, mandatory=True) """Page number.""" @classmethod diff --git a/surveil/api/handlers/status/live_host_handler.py b/surveil/api/handlers/status/live_host_handler.py index 68f6ad1..9b57288 100644 --- a/surveil/api/handlers/status/live_host_handler.py +++ b/surveil/api/handlers/status/live_host_handler.py @@ -44,10 +44,10 @@ class HostHandler(handler.Handler): else: lq = {} - query = mongodb_query.build_mongodb_query(lq) + query, kwargs = mongodb_query.build_mongodb_query(lq) mongo_dicts = (self.request.mongo_connection. - alignak_live.hosts.find(*query)) + alignak_live.hosts.find(*query, **kwargs)) host_dicts = [ _host_dict_from_mongo_item(s) for s in mongo_dicts diff --git a/surveil/api/handlers/status/live_service_handler.py b/surveil/api/handlers/status/live_service_handler.py index b4e2e32..e33e712 100644 --- a/surveil/api/handlers/status/live_service_handler.py +++ b/surveil/api/handlers/status/live_service_handler.py @@ -44,10 +44,10 @@ class ServiceHandler(handler.Handler): else: lq = {} - query = mongodb_query.build_mongodb_query(lq) + query, kwargs = mongodb_query.build_mongodb_query(lq) mongo_dicts = (self.request.mongo_connection. - alignak_live.services.find(*query)) + alignak_live.services.find(*query, **kwargs)) service_dicts = [ _service_dict_from_mongo_item(s) for s in mongo_dicts diff --git a/surveil/api/handlers/status/mongodb_query.py b/surveil/api/handlers/status/mongodb_query.py index 842b3b6..8b3e79a 100644 --- a/surveil/api/handlers/status/mongodb_query.py +++ b/surveil/api/handlers/status/mongodb_query.py @@ -17,6 +17,7 @@ import json def build_mongodb_query(live_query): query = [] + kwargs = {} # Build the filters filters = {} @@ -37,7 +38,13 @@ def build_mongodb_query(live_query): if fields: query.append(fields) - return query + # Paging + paging = live_query.get('paging', None) + if paging is not None: + kwargs['limit'] = paging.size + kwargs['skip'] = paging.size * paging.page + + return query, kwargs def _get_mongo_filter(livequery_filter): diff --git a/surveil/tests/api/controllers/v2/status/test_hosts.py b/surveil/tests/api/controllers/v2/status/test_hosts.py index 522f6ba..e32830b 100644 --- a/surveil/tests/api/controllers/v2/status/test_hosts.py +++ b/surveil/tests/api/controllers/v2/status/test_hosts.py @@ -165,6 +165,34 @@ class TestStatusHosts(functionalTest.FunctionalTest): self.assert_count_equal_backport(json.loads(response.body.decode()), expected) + def test_query_host_paging(self): + query = { + 'paging': { + 'page': 1, + 'size': 2 + } + } + + response = self.post_json("/v2/status/hosts", params=query) + + self.assertEqual( + json.loads(response.body.decode()), + [ + {'acknowledged': True, + 'address': u'127.0.0.1', + 'childs': [u'test_keystone'], + 'description': u'ws-arbiter', + 'host_name': u'ws-arbiter', + 'last_check': 1429405764, + 'last_state_change': 1429405765, + 'long_output': u'What a;\nlong;\noutput;', + 'parents': [u'parent.com'], + 'plugin_output': u'OK - localhost: rta 0.030ms, lost 0%', + 'services': [], + 'state': u'OK'} + ] + ) + def test_get_specific_host(self): response = self.get("/v2/status/hosts/localhost") diff --git a/surveil/tests/api/handlers/live/test_mongodb_query.py b/surveil/tests/api/handlers/live/test_mongodb_query.py index e68a95c..a5f4a90 100644 --- a/surveil/tests/api/handlers/live/test_mongodb_query.py +++ b/surveil/tests/api/handlers/live/test_mongodb_query.py @@ -15,6 +15,7 @@ import json from surveil.api.datamodel.status import live_query +from surveil.api.datamodel.status import paging from surveil.api.handlers.status import mongodb_query from surveil.tests import base @@ -29,7 +30,8 @@ class MongoDBQueryTest(base.BaseTestCase): "state": [0, 1], "last_check": ["test_keystone"] } - }) + }), + paging=paging.Paging(size=7, page=4) ) service_mappings = { @@ -47,10 +49,11 @@ class MongoDBQueryTest(base.BaseTestCase): lq, {'fields': [u'host_name', 'last_chk'], 'filters': {u'isnot': {u'state': [0, 1], - 'last_chk': [u'test_keystone']}}} + 'last_chk': [u'test_keystone']}}, + 'paging': query.paging}, ) - query = mongodb_query.build_mongodb_query(lq) + query, kwargs = mongodb_query.build_mongodb_query(lq) expected_query = { "state": {"$nin": [0, 1]}, @@ -64,3 +67,4 @@ class MongoDBQueryTest(base.BaseTestCase): self.assertEqual(query[0], expected_query) self.assertEqual(query[1], expected_fields) + self.assertEqual(kwargs, {'limit': 7, 'skip': 28})