Merge "[MongoDB] Add regex to complex queries"
This commit is contained in:
commit
926f781096
@ -81,7 +81,7 @@ def _list_to_regexp(items, regexp_prefix=""):
|
|||||||
class ValidatedComplexQuery(object):
|
class ValidatedComplexQuery(object):
|
||||||
complex_operators = ["and", "or"]
|
complex_operators = ["and", "or"]
|
||||||
order_directions = ["asc", "desc"]
|
order_directions = ["asc", "desc"]
|
||||||
simple_ops = ["=", "!=", "<", ">", "<=", "=<", ">=", "=>"]
|
simple_ops = ["=", "!=", "<", ">", "<=", "=<", ">=", "=>", "=~"]
|
||||||
regexp_prefix = "(?i)"
|
regexp_prefix = "(?i)"
|
||||||
|
|
||||||
complex_ops = _list_to_regexp(complex_operators, regexp_prefix)
|
complex_ops = _list_to_regexp(complex_operators, regexp_prefix)
|
||||||
|
@ -275,7 +275,8 @@ class QueryTransformer(object):
|
|||||||
">=": "$gte",
|
">=": "$gte",
|
||||||
"=>": "$gte",
|
"=>": "$gte",
|
||||||
"!=": "$ne",
|
"!=": "$ne",
|
||||||
"in": "$in"}
|
"in": "$in",
|
||||||
|
"=~": "$regex"}
|
||||||
|
|
||||||
complex_operators = {"or": "$or",
|
complex_operators = {"or": "$or",
|
||||||
"and": "$and"}
|
"and": "$and"}
|
||||||
|
@ -781,6 +781,34 @@ class ComplexSampleQueryTest(DBTestBase,
|
|||||||
del d['recorded_at']
|
del d['recorded_at']
|
||||||
self.assertIn(d, self.msgs)
|
self.assertIn(d, self.msgs)
|
||||||
|
|
||||||
|
@tests_db.run_with('mongodb')
|
||||||
|
def test_query_complex_filter_with_regexp(self):
|
||||||
|
self._create_samples()
|
||||||
|
complex_regex_filter = {"and": [
|
||||||
|
{"=~": {"resource_id": "resource-id.*"}},
|
||||||
|
{"=": {"counter_volume": 0.4}}]}
|
||||||
|
results = list(
|
||||||
|
self.conn.query_samples(filter_expr=complex_regex_filter))
|
||||||
|
self.assertEqual(3, len(results))
|
||||||
|
for sample_item in results:
|
||||||
|
self.assertIn(sample_item.resource_id,
|
||||||
|
set(["resource-id-42",
|
||||||
|
"resource-id-43",
|
||||||
|
"resource-id-44"]))
|
||||||
|
|
||||||
|
@tests_db.run_with('mongodb')
|
||||||
|
def test_query_complex_filter_with_regexp_metadata(self):
|
||||||
|
self._create_samples()
|
||||||
|
complex_regex_filter = {"and": [
|
||||||
|
{"=~": {"resource_metadata.a_string_key": "meta-value.*"}},
|
||||||
|
{"=": {"counter_volume": 0.4}}]}
|
||||||
|
results = list(
|
||||||
|
self.conn.query_samples(filter_expr=complex_regex_filter))
|
||||||
|
self.assertEqual(3, len(results))
|
||||||
|
for sample_item in results:
|
||||||
|
self.assertEqual("meta-value0.4",
|
||||||
|
sample_item.resource_metadata['a_string_key'])
|
||||||
|
|
||||||
def test_no_filter_with_zero_limit(self):
|
def test_no_filter_with_zero_limit(self):
|
||||||
limit = 0
|
limit = 0
|
||||||
results = list(self.conn.query_samples(limit=limit))
|
results = list(self.conn.query_samples(limit=limit))
|
||||||
@ -2939,6 +2967,20 @@ class ComplexAlarmQueryTest(AlarmTestBase,
|
|||||||
self.assertIn(a.name, set(["yellow-alert", "red-alert"]))
|
self.assertIn(a.name, set(["yellow-alert", "red-alert"]))
|
||||||
self.assertTrue(a.enabled)
|
self.assertTrue(a.enabled)
|
||||||
|
|
||||||
|
@tests_db.run_with('mongodb')
|
||||||
|
def test_filter_with_regexp(self):
|
||||||
|
self.add_some_alarms()
|
||||||
|
filter_expr = {"and":
|
||||||
|
[{"or": [{"=": {"name": "yellow-alert"}},
|
||||||
|
{"=": {"name": "red-alert"}}]},
|
||||||
|
{"=~": {"description": "yel.*"}}]}
|
||||||
|
|
||||||
|
result = list(self.alarm_conn.query_alarms(filter_expr=filter_expr))
|
||||||
|
|
||||||
|
self.assertEqual(1, len(result))
|
||||||
|
for a in result:
|
||||||
|
self.assertEqual("yellow", a.description)
|
||||||
|
|
||||||
def test_filter_for_alarm_id(self):
|
def test_filter_for_alarm_id(self):
|
||||||
self.add_some_alarms()
|
self.add_some_alarms()
|
||||||
filter_expr = {"=": {"alarm_id": "0r4ng3"}}
|
filter_expr = {"=": {"alarm_id": "0r4ng3"}}
|
||||||
@ -3053,6 +3095,15 @@ class ComplexAlarmHistoryQueryTest(AlarmTestBase,
|
|||||||
self.alarm_conn.query_alarm_history(filter_expr=self.filter_expr))
|
self.alarm_conn.query_alarm_history(filter_expr=self.filter_expr))
|
||||||
self.assertEqual(2, len(history))
|
self.assertEqual(2, len(history))
|
||||||
|
|
||||||
|
@tests_db.run_with('mongodb')
|
||||||
|
def test_alarm_history_with_regexp(self):
|
||||||
|
filter_expr = {"and":
|
||||||
|
[{"=~": {"type": "(rule)|(state)"}},
|
||||||
|
{"=": {"alarm_id": "0r4ng3"}}]}
|
||||||
|
history = list(
|
||||||
|
self.alarm_conn.query_alarm_history(filter_expr=filter_expr))
|
||||||
|
self.assertEqual(2, len(history))
|
||||||
|
|
||||||
def test_alarm_history_with_filter_and_orderby(self):
|
def test_alarm_history_with_filter_and_orderby(self):
|
||||||
history = list(
|
history = list(
|
||||||
self.alarm_conn.query_alarm_history(filter_expr=self.filter_expr,
|
self.alarm_conn.query_alarm_history(filter_expr=self.filter_expr,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user