Merge "Implemented paging for MongoDB objects"
This commit is contained in:
commit
76194ee6a6
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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):
|
||||
|
@ -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")
|
||||
|
@ -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})
|
||||
|
Loading…
x
Reference in New Issue
Block a user