Skip to content

Commit 707b2a5

Browse files
committed
Handling non-compound query keys defined as an array
1 parent c3ac34a commit 707b2a5

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

elastalert/loaders.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,15 @@ def _dt_to_ts_with_format(dt):
324324
if 'include' in rule and type(rule['include']) != list:
325325
raise EAException('include option must be a list')
326326

327-
if isinstance(rule.get('query_key'), list):
328-
rule['compound_query_key'] = rule['query_key']
329-
rule['query_key'] = ','.join(rule['query_key'])
327+
raw_query_key = rule.get('query_key')
328+
if isinstance(raw_query_key, list):
329+
if len(raw_query_key) > 1:
330+
rule['compound_query_key'] = raw_query_key
331+
rule['query_key'] = ','.join(raw_query_key)
332+
elif len(raw_query_key) == 1:
333+
rule['query_key'] = raw_query_key[0]
334+
else:
335+
del(rule['query_key'])
330336

331337
if isinstance(rule.get('aggregation_key'), list):
332338
rule['compound_aggregation_key'] = rule['aggregation_key']

tests/loaders_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,29 @@ def test_compound_query_key():
358358
assert test_rule_copy['compound_query_key'] == ['field1', 'field2']
359359

360360

361+
def test_query_key_with_single_value():
362+
test_config_copy = copy.deepcopy(test_config)
363+
rules_loader = FileRulesLoader(test_config_copy)
364+
test_rule_copy = copy.deepcopy(test_rule)
365+
test_rule_copy.pop('use_count_query')
366+
test_rule_copy['query_key'] = ['field1']
367+
rules_loader.load_options(test_rule_copy, test_config, 'filename.yaml')
368+
assert 'field1' in test_rule_copy['include']
369+
assert test_rule_copy['query_key'] == 'field1'
370+
assert 'compound_query_key' not in test_rule_copy
371+
372+
373+
def test_query_key_with_no_values():
374+
test_config_copy = copy.deepcopy(test_config)
375+
rules_loader = FileRulesLoader(test_config_copy)
376+
test_rule_copy = copy.deepcopy(test_rule)
377+
test_rule_copy.pop('use_count_query')
378+
test_rule_copy['query_key'] = []
379+
rules_loader.load_options(test_rule_copy, test_config, 'filename.yaml')
380+
assert 'query_key' not in test_rule_copy
381+
assert 'compound_query_key' not in test_rule_copy
382+
383+
361384
def test_name_inference():
362385
test_config_copy = copy.deepcopy(test_config)
363386
rules_loader = FileRulesLoader(test_config_copy)

0 commit comments

Comments
 (0)