Skip to content

Commit 4d6843f

Browse files
author
Khanh Nguyen
committed
Merges errors to errors_parsed
1 parent 5e461ea commit 4d6843f

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from elastalert.enhancements import BaseEnhancement
22
from util import convert_array_to_object
3-
from util import parse_detections
3+
from util import parse_array
44

5-
# For easier access to nested values in an array , this merges all items in array
5+
# For easier access to nested values in an array , this merges all items in array
66
# within
77
class AlertTextEnhancement(BaseEnhancement):
88
# The enhancement is run against every match
99
# The match is passed to the process function where it can be modified in any way
1010
# ElastAlert will do this for each enhancement linked to a rule
1111
def process(self, match):
12-
parsed_match = parse_detections(match)
12+
parsed_match = parse_array(match, 'detections')
13+
match.update(parsed_match)
14+
parsed_match = parse_array(match, 'errors')
1315
match.update(parsed_match)

elastalert_modules/util.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ def convert_array_to_object(array):
66
json[str(idx)] = array[idx]
77
return json
88

9-
def parse_detections(match):
10-
key = 'detections'
9+
def parse_array(match, key) :
10+
o = match[key] if key in match else {}
1111
parsed = {key+'_parsed': {}}
1212

13-
if not isinstance(match[key], list):
13+
if not isinstance(o, list):
1414
return parsed
15-
if len(match[key]) == 0:
15+
if len(o) == 0:
1616
return parsed
1717

1818
# Converts array terms into objects
19-
# parsed[key + '_parsed'] = convert_array_to_object(match[key])
19+
# parsed[key + '_parsed'] = convert_array_to_object(o)
2020

21-
for sk, value in match[key][0].iteritems():
21+
for sk, value in o[0].iteritems():
2222
value_array = []
2323
if isinstance(value, list):
24-
value_array = list(chain.from_iterable(sv for sv in (v[sk] for v in match[key]) if sv))
24+
value_array = list(chain.from_iterable(sv for sv in (v[sk] for v in o) if sv))
2525
else:
26-
value_array = [v[sk] for v in match[key]]
26+
value_array = [v[sk] for v in o]
2727
unique_values = set(value_array)
2828
parsed[key + '_parsed'][sk] = ", ".join(str(va) for va in unique_values)
2929

0 commit comments

Comments
 (0)