11package de .gdata .mobilelab .alertmanagercallback ;
22
3- import com .floreysoft .jmte .Engine ;
4- import org .graylog2 .plugin .Message ;
5- import org .graylog2 .plugin .MessageSummary ;
63import org .graylog2 .plugin .alarms .AlertCondition ;
74import org .graylog2 .plugin .configuration .Configuration ;
85import org .graylog2 .plugin .streams .Stream ;
96import org .joda .time .DateTime ;
107
118import java .io .IOException ;
12- import java .util .Collections ;
139import java .util .HashMap ;
14- import java .util .List ;
1510import java .util .Map ;
16- import java .util .stream .Collectors ;
17-
18- import static com .google .common .base .MoreObjects .firstNonNull ;
1911
2012class AlertManagerPayloadBuilder {
2113
@@ -25,12 +17,10 @@ class AlertManagerPayloadBuilder {
2517 private AlertCondition .CheckResult checkResult ;
2618 private Configuration configuration ;
2719 private CustomPropertiesTextFieldParser customPropertiesTextFieldParser ;
28- private Engine templateEngine ;
2920
3021 private AlertManagerPayloadBuilder () {
3122 // Private constructor to hide the implicit one
3223 customPropertiesTextFieldParser = new CustomPropertiesTextFieldParser ();
33- templateEngine = new Engine ();
3424 }
3525
3626 static AlertManagerPayloadBuilder newInstance () {
@@ -39,43 +29,38 @@ static AlertManagerPayloadBuilder newInstance() {
3929
4030 AlertManagerPayloadBuilder withStream (Stream stream ) {
4131 this .stream = stream ;
42-
4332 return this ;
4433 }
4534
4635 AlertManagerPayloadBuilder withCheckResult (AlertCondition .CheckResult checkResult ) {
4736 this .checkResult = checkResult ;
48-
4937 return this ;
5038 }
5139
5240 AlertManagerPayloadBuilder withConfiguration (Configuration configuration ) {
5341 this .configuration = configuration ;
54-
5542 return this ;
5643 }
5744
58- // parts copied from org.graylog2.alerts.FormattedEmailAlertSender#getModel()
59- private Map <String , Object > createModel (Stream stream , AlertCondition .CheckResult checkResult , List <Message > backlog ) {
60- Map <String , Object > model = new HashMap <>();
61- model .put ("stream" , stream );
62- model .put ("check_result" , checkResult );
63- model .put ("stream_url" , extractStreamUrl ());
64- model .put ("alertCondition" , checkResult .getTriggeredCondition ());
45+ AlertManagerPayload build () {
46+ AlertManagerPayload alertManagerPayload = new AlertManagerPayload ();
6547
66- final List <Message > messages = firstNonNull (backlog , Collections .emptyList ());
67- model .put ("backlog" , messages );
68- model .put ("backlog_size" , messages .size ());
48+ Map <String , Object > annotations = extractAnnotations ();
49+ Map <String , Object > labels = extractLabels ();
6950
70- return model ;
71- }
51+ // Replace template values such as '${stream.description}'
52+ CustomPropertiesJMTEResolver jmteResolver = CustomPropertiesJMTEResolver .Builder .newBuilder ()
53+ .withCheckResult (checkResult )
54+ .withStream (stream )
55+ .withUrl (extractStreamUrl ())
56+ .build ();
7257
73- AlertManagerPayload build () {
58+ annotations = jmteResolver .transformTemplateValues (annotations );
59+ labels = jmteResolver .transformTemplateValues (labels );
7460
61+ alertManagerPayload .setAnnotations (annotations );
62+ alertManagerPayload .setLabels (labels );
7563
76- AlertManagerPayload alertManagerPayload = new AlertManagerPayload ();
77- alertManagerPayload .setAnnotations (extractAnnotations ());
78- alertManagerPayload .setLabels (extractLabels ());
7964 alertManagerPayload .setGeneratorURL (extractStreamUrl ());
8065 alertManagerPayload .setStartsAt (extractStartsAt ());
8166 alertManagerPayload .setEndsAt (extractEndsAt ());
@@ -99,35 +84,9 @@ private Map<String, Object> extractLabels() {
9984 // damaged configuration, so we'll not put any additional label into the map
10085 }
10186
102- transformTemplateValues (labels );
103-
10487 return labels ;
10588 }
10689
107- private void transformTemplateValues (Map <String , Object > customValueMap ) {
108- customValueMap .entrySet ().forEach (
109- entry -> {
110- try {
111- if (entry .getValue () != null ) {
112- String valueAsString = (String ) entry .getValue ();
113- entry .setValue (templateEngine .transform (
114- valueAsString ,
115- createModel (
116- stream ,
117- checkResult ,
118- checkResult .getMatchingMessages ().stream ()
119- .map (MessageSummary ::getRawMessage )
120- .collect (Collectors .toList ())
121- )
122- ));
123- }
124- } catch (Exception ex ) {
125- // Just catch exceptions for bad formatting or direct values which should not be formatted
126- }
127- }
128- );
129- }
130-
13190 private Map <String , Object > extractAnnotations () {
13291 Map <String , Object > annotations = new HashMap <>();
13392
@@ -151,8 +110,6 @@ private Map<String, Object> extractAnnotations() {
151110 // damaged configuration, so we'll not put any additional annotation into the map
152111 }
153112
154- transformTemplateValues (annotations );
155-
156113 return annotations ;
157114 }
158115
0 commit comments