Skip to content

Commit cb0b294

Browse files
author
Khanh Nguyen
committed
Refactored bitsensor enhancement
1 parent 33c94b9 commit cb0b294

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from elastalert.enhancements import BaseEnhancement
22
from util import convert_array_to_object
3+
from util import parse_detections
34

4-
5+
# For easier access to nested values in an array , this merges all items in array
6+
# within
57
class AlertTextEnhancement(BaseEnhancement):
68
# The enhancement is run against every match
79
# The match is passed to the process function where it can be modified in any way
810
# ElastAlert will do this for each enhancement linked to a rule
911
def process(self, match):
10-
match['detections_string'] = ''
11-
if 'detections' in match:
12-
match['detections_parsed'] = convert_array_to_object(match['detections'])
12+
parsed_match = parse_detections(match)
13+
match.update(parsed_match)

elastalert_modules/util.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1-
1+
from itertools import chain
22

33
def convert_array_to_object(array):
44
json = {}
55
for idx in range(len(array)):
6-
json[idx] = array[idx]
7-
return json
6+
json[str(idx)] = array[idx]
7+
return json
8+
9+
def parse_detections(match):
10+
key = 'detections'
11+
parsed = {key+'_parsed': {}}
12+
13+
if not isinstance(match[key], list):
14+
return parsed
15+
if len(match[key]) == 0:
16+
return parsed
17+
18+
# Converts array terms into objects
19+
# parsed[key + '_parsed'] = convert_array_to_object(match[key])
20+
21+
for sk, value in match[key][0].iteritems():
22+
value_array = []
23+
if isinstance(value, list):
24+
value_array = list(chain.from_iterable(sv for sv in (v[sk] for v in match[key]) if sv))
25+
else:
26+
value_array = [v[sk] for v in match[key]]
27+
unique_values = set(value_array)
28+
parsed[key + '_parsed'][sk] = ", ".join(str(va) for va in unique_values)
29+
30+
return parsed

0 commit comments

Comments
 (0)