influxdb_builder: Added 'ORDER_BY'

Change-Id: Id2533d2b13fecdb8849233d6ceaf2d3d7dc9730d
This commit is contained in:
aviau 2015-04-29 11:12:18 -04:00
parent 2ec8b4bf27
commit b9776a04e4
6 changed files with 52 additions and 9 deletions

View File

@ -33,7 +33,11 @@ def filter_fields(item_list, live_query):
return filtered_items
def build_influxdb_query(live_query, measurement, group_by=[], limit=None):
def build_influxdb_query(live_query,
measurement,
group_by=[],
order_by=[],
limit=None):
query = ['SELECT * FROM', measurement]
@ -46,6 +50,10 @@ def build_influxdb_query(live_query, measurement, group_by=[], limit=None):
query.append('GROUP BY')
query.append(', '.join(group_by))
if order_by:
query.append('ORDER BY')
query.append(', '.join(order_by))
if limit is not None:
query.append('LIMIT %d' % limit)

View File

@ -28,7 +28,9 @@ class HostHandler(handler.Handler):
cli = self.request.influxdb_client
query = ("SELECT * from HOST_STATE "
"WHERE host_name='%s' "
"GROUP BY * LIMIT 1") % host_name
"GROUP BY * "
"ORDER BY time DESC "
"LIMIT 1") % host_name
response = cli.query(query)
host = live_host.LiveHost(
@ -43,6 +45,7 @@ class HostHandler(handler.Handler):
live_query,
'HOST_STATE',
group_by=['host_name', 'address', 'childs'],
order_by=['time DESC'],
limit=1
)
response = cli.query(query)

View File

@ -28,7 +28,9 @@ class ServiceHandler(handler.Handler):
query = ("SELECT * from SERVICE_STATE "
"WHERE host_name='%s' "
"AND service_description='%s' "
"GROUP BY * LIMIT 1") % (host_name, service_name)
"GROUP BY * "
"ORDER BY time DESC "
"LIMIT 1") % (host_name, service_name)
response = cli.query(query)
host = live_service.LiveService(
@ -43,6 +45,7 @@ class ServiceHandler(handler.Handler):
live_query,
'SERVICE_STATE',
group_by=['host_name', 'service_description'],
order_by=['time DESC'],
limit=1
)

View File

@ -139,7 +139,7 @@ class TestStatusHosts(functionalTest.FunctionalTest):
self.assertEqual(
httpretty.last_request().querystring['q'],
["SELECT * FROM HOST_STATE "
"GROUP BY host_name, address, childs LIMIT 1"]
"GROUP BY host_name, address, childs ORDER BY time DESC LIMIT 1"]
)
@httpretty.activate
@ -198,7 +198,9 @@ class TestStatusHosts(functionalTest.FunctionalTest):
httpretty.last_request().querystring['q'],
["SELECT * FROM HOST_STATE WHERE host_name!='localhost' "
"AND description!='test_keystone' "
"GROUP BY host_name, address, childs LIMIT 1"]
"GROUP BY host_name, address, childs "
"ORDER BY time DESC "
"LIMIT 1"]
)
@httpretty.activate
@ -247,7 +249,9 @@ class TestStatusHosts(functionalTest.FunctionalTest):
self.assertEqual(
httpretty.last_request().querystring['q'],
["SELECT * from HOST_STATE WHERE host_name='localhost'"
" GROUP BY * LIMIT 1"]
" GROUP BY * "
"ORDER BY time DESC "
"LIMIT 1"]
)
@httpretty.activate
@ -314,5 +318,6 @@ class TestStatusHosts(functionalTest.FunctionalTest):
"WHERE host_name='ws-arbiter' "
"AND service_description='check-ws-arbiter' "
"GROUP BY * "
"ORDER BY time DESC "
"LIMIT 1"]
)

View File

@ -108,7 +108,9 @@ class TestStatusServices(functionalTest.FunctionalTest):
self.assertEqual(
httpretty.last_request().querystring['q'],
["SELECT * FROM SERVICE_STATE GROUP BY host_name,"
" service_description LIMIT 1"]
" service_description "
"ORDER BY time DESC "
"LIMIT 1"]
)
@httpretty.activate

View File

@ -86,9 +86,31 @@ class LiveQueryFilterTest(base.BaseTestCase):
result = influxdb_query.build_influxdb_query(query,
measurement,
group_by,
limit)
group_by=group_by,
limit=limit)
expected = "SELECT * FROM ALERT GROUP BY *, host_name LIMIT 10"
self.assertItemsEqual(result, expected)
def test_build_influx_query_orderby(self):
query = live_query.LiveQuery(
fields=json.dumps(['host_name', 'last_check']),
filters=json.dumps({}),
)
measurement = 'ALERT'
group_by = ['*', 'host_name']
order_by = ['time DESC']
limit = 10
result = influxdb_query.build_influxdb_query(query,
measurement,
group_by=group_by,
order_by=order_by,
limit=limit)
expected = ("SELECT * FROM ALERT "
"GROUP BY *, host_name "
"ORDER BY time DESC LIMIT 10")
self.assertItemsEqual(result, expected)