Merge "Refactored live_*_handler"
This commit is contained in:
commit
7e74e216b9
@ -12,14 +12,10 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import json
|
||||
|
||||
from surveil.api.datamodel.status import live_host
|
||||
from surveil.api.handlers import handler
|
||||
from surveil.api.handlers.status import mongodb_query
|
||||
|
||||
import wsme
|
||||
|
||||
|
||||
class HostHandler(handler.Handler):
|
||||
"""Fulfills a request on the live hosts."""
|
||||
@ -35,21 +31,23 @@ class HostHandler(handler.Handler):
|
||||
def get_all(self, live_query=None):
|
||||
"""Return all live hosts."""
|
||||
|
||||
host_mappings = {
|
||||
"last_check": "last_chk",
|
||||
"description": "display_name",
|
||||
"plugin_output": "output",
|
||||
"acknowledged": "problem_has_been_acknowledged"
|
||||
}
|
||||
|
||||
if live_query:
|
||||
lq_filters, lq_fields = _translate_live_query(live_query)
|
||||
lq = mongodb_query.translate_live_query(live_query.as_dict(),
|
||||
host_mappings)
|
||||
else:
|
||||
lq_filters = {}
|
||||
lq_fields = {}
|
||||
lq = {}
|
||||
|
||||
query, fields = mongodb_query.build_mongodb_query(lq_filters,
|
||||
lq_fields)
|
||||
query = mongodb_query.build_mongodb_query(lq)
|
||||
|
||||
if fields != {}:
|
||||
mongo_dicts = (self.request.mongo_connection.
|
||||
alignak_live.hosts.find(query, fields))
|
||||
else:
|
||||
mongo_dicts = (self.request.mongo_connection.
|
||||
alignak_live.hosts.find(query))
|
||||
alignak_live.hosts.find(*query))
|
||||
|
||||
host_dicts = [
|
||||
_host_dict_from_mongo_item(s) for s in mongo_dicts
|
||||
@ -63,38 +61,6 @@ class HostHandler(handler.Handler):
|
||||
return hosts
|
||||
|
||||
|
||||
def _translate_live_query(live_query):
|
||||
# Mappings
|
||||
mapping = {
|
||||
"last_check": "last_chk",
|
||||
"description": "display_name",
|
||||
"plugin_output": "output",
|
||||
"acknowledged": "problem_has_been_acknowledged"
|
||||
}
|
||||
|
||||
# Load the fields
|
||||
if live_query.fields != wsme.Unset:
|
||||
fields = live_query.fields
|
||||
else:
|
||||
fields = []
|
||||
|
||||
# Translate the fields
|
||||
lq_fields = []
|
||||
for field in fields:
|
||||
lq_fields.append(mapping.get(field, field))
|
||||
|
||||
# Load the filters
|
||||
filters = json.loads(live_query.filters)
|
||||
|
||||
# Translate the filters
|
||||
for filter in filters.values():
|
||||
for field in filter.keys():
|
||||
value = filter.pop(field)
|
||||
filter[mapping.get(field, field)] = value
|
||||
|
||||
return filters, lq_fields
|
||||
|
||||
|
||||
def _host_dict_from_mongo_item(mongo_item):
|
||||
"""Create a dict from a mongodb item."""
|
||||
|
||||
|
@ -12,14 +12,10 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import json
|
||||
|
||||
from surveil.api.datamodel.status import live_service
|
||||
from surveil.api.handlers import handler
|
||||
from surveil.api.handlers.status import mongodb_query
|
||||
|
||||
import wsme
|
||||
|
||||
|
||||
class ServiceHandler(handler.Handler):
|
||||
"""Fulfills a request on live services."""
|
||||
@ -35,21 +31,23 @@ class ServiceHandler(handler.Handler):
|
||||
def get_all(self, live_query=None):
|
||||
"""Return all live services."""
|
||||
|
||||
service_mappings = {
|
||||
"last_check": "last_chk",
|
||||
"description": "service_description",
|
||||
"plugin_output": "output",
|
||||
"acknowledged": "problem_has_been_acknowledged",
|
||||
}
|
||||
|
||||
if live_query:
|
||||
lq_filters, lq_fields = _translate_live_query(live_query)
|
||||
lq = mongodb_query.translate_live_query(live_query.as_dict(),
|
||||
service_mappings)
|
||||
else:
|
||||
lq_filters = {}
|
||||
lq_fields = {}
|
||||
lq = {}
|
||||
|
||||
query, fields = mongodb_query.build_mongodb_query(lq_filters,
|
||||
lq_fields)
|
||||
query = mongodb_query.build_mongodb_query(lq)
|
||||
|
||||
if fields != {}:
|
||||
mongo_dicts = (self.request.mongo_connection.
|
||||
alignak_live.services.find(query, fields))
|
||||
else:
|
||||
mongo_dicts = (self.request.mongo_connection.
|
||||
alignak_live.services.find(query))
|
||||
alignak_live.services.find(*query))
|
||||
|
||||
service_dicts = [
|
||||
_service_dict_from_mongo_item(s) for s in mongo_dicts
|
||||
@ -63,40 +61,6 @@ class ServiceHandler(handler.Handler):
|
||||
return services
|
||||
|
||||
|
||||
def _translate_live_query(live_query):
|
||||
"""Translate field names in a live query so that they match mongodb."""
|
||||
|
||||
# Mappings
|
||||
mapping = {
|
||||
"last_check": "last_chk",
|
||||
"description": "service_description",
|
||||
"plugin_output": "output",
|
||||
"acknowledged": "problem_has_been_acknowledged",
|
||||
}
|
||||
|
||||
# Load the fields
|
||||
if live_query.fields != wsme.Unset:
|
||||
fields = live_query.fields
|
||||
else:
|
||||
fields = []
|
||||
|
||||
# Translate the fields
|
||||
lq_fields = []
|
||||
for field in fields:
|
||||
lq_fields.append(mapping.get(field, field))
|
||||
|
||||
# Load the filters
|
||||
filters = json.loads(live_query.filters)
|
||||
|
||||
# Translate the filters
|
||||
for filter in filters.values():
|
||||
for field in filter.keys():
|
||||
value = filter.pop(field)
|
||||
filter[mapping.get(field, field)] = value
|
||||
|
||||
return filters, lq_fields
|
||||
|
||||
|
||||
def _service_dict_from_mongo_item(mongo_item):
|
||||
"""Create a dict from a mongodb item."""
|
||||
|
||||
|
@ -12,22 +12,32 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import json
|
||||
|
||||
def build_mongodb_query(lq_filters, lq_fields):
|
||||
# Build the query
|
||||
query = {}
|
||||
for filter_name, filter_data in lq_filters.items():
|
||||
|
||||
def build_mongodb_query(live_query):
|
||||
query = []
|
||||
|
||||
# Build the filters
|
||||
filters = {}
|
||||
for filter_name, filter_data in live_query.get('filters', {}).items():
|
||||
for field, values in filter_data.items():
|
||||
query[field] = {
|
||||
filters[field] = {
|
||||
_get_mongo_filter(filter_name): values
|
||||
}
|
||||
|
||||
if filters:
|
||||
query.append(filters)
|
||||
|
||||
# Build the required fields
|
||||
fields = {}
|
||||
for field in lq_fields:
|
||||
for field in live_query.get("fields", []):
|
||||
fields[field] = 1
|
||||
|
||||
return query, fields
|
||||
if fields:
|
||||
query.append(fields)
|
||||
|
||||
return query
|
||||
|
||||
|
||||
def _get_mongo_filter(livequery_filter):
|
||||
@ -36,3 +46,28 @@ def _get_mongo_filter(livequery_filter):
|
||||
"isnot": "$nin"
|
||||
}
|
||||
return filters[livequery_filter]
|
||||
|
||||
|
||||
def translate_live_query(live_query, mappings):
|
||||
"""Translate field names in a live query so that they match mongodb."""
|
||||
|
||||
# Load the fields
|
||||
fields = live_query.get("fields", [])
|
||||
|
||||
# Translate the fields
|
||||
translated_fields = []
|
||||
for field in fields:
|
||||
translated_fields.append(mappings.get(field, field))
|
||||
live_query["fields"] = translated_fields
|
||||
|
||||
# Load the filters
|
||||
filters = json.loads(live_query.get("filters", '{}'))
|
||||
|
||||
# Translate the filters
|
||||
for filter in filters.values():
|
||||
for field in filter.keys():
|
||||
value = filter.pop(field)
|
||||
filter[mappings.get(field, field)] = value
|
||||
live_query["filters"] = filters
|
||||
|
||||
return live_query
|
@ -15,7 +15,6 @@
|
||||
import json
|
||||
|
||||
from surveil.api.datamodel.status import live_query
|
||||
from surveil.api.handlers.status import live_service_handler
|
||||
from surveil.api.handlers.status import mongodb_query
|
||||
from surveil.tests import base
|
||||
|
||||
@ -33,20 +32,25 @@ class MongoDBQueryTest(base.BaseTestCase):
|
||||
})
|
||||
)
|
||||
|
||||
lq_filters, lq_fields = live_service_handler._translate_live_query(
|
||||
query
|
||||
service_mappings = {
|
||||
"last_check": "last_chk",
|
||||
"description": "service_description",
|
||||
"plugin_output": "output",
|
||||
"acknowledged": "problem_has_been_acknowledged",
|
||||
}
|
||||
lq = mongodb_query.translate_live_query(
|
||||
query.as_dict(),
|
||||
service_mappings
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
lq_fields, ['host_name', 'last_chk']
|
||||
lq,
|
||||
{'fields': [u'host_name', 'last_chk'],
|
||||
'filters': {u'isnot': {u'state': [0, 1],
|
||||
'last_chk': [u'test_keystone']}}}
|
||||
)
|
||||
|
||||
self.assertEqual(lq_filters,
|
||||
{'isnot': {'state': [0, 1],
|
||||
'last_chk': ['test_keystone']}})
|
||||
|
||||
query, fields = mongodb_query.build_mongodb_query(lq_filters,
|
||||
lq_fields)
|
||||
query = mongodb_query.build_mongodb_query(lq)
|
||||
|
||||
expected_query = {
|
||||
"state": {"$nin": [0, 1]},
|
||||
@ -58,5 +62,5 @@ class MongoDBQueryTest(base.BaseTestCase):
|
||||
"last_chk": 1
|
||||
}
|
||||
|
||||
self.assertEqual(query, expected_query)
|
||||
self.assertEqual(fields, expected_fields)
|
||||
self.assertEqual(query[0], expected_query)
|
||||
self.assertEqual(query[1], expected_fields)
|
||||
|
Loading…
x
Reference in New Issue
Block a user