Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #868 from studanshu/updatetriggername
Browse files Browse the repository at this point in the history
adding function to return trigger name instead of making changes in t…
  • Loading branch information
sundeepsf committed Aug 8, 2018
2 parents 34e8201 + 88d6110 commit 2b1a6f6
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ public static TriggerType fromString(String name) {
public String value() {
return this.toString();
}

}
}
/* Copyright (c) 2016, Salesforce.com, Inc. All rights reserved. */
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected String getAuditBody(NotificationContext context, NotificationStatus no
sb.append(context.getNotification().getCustomText()).append("<br/>");
}
sb.append(MessageFormat.format("<b>Notification: </b> {0}<br/>", notification.getName()));
sb.append(MessageFormat.format("<b>Triggered by: </b> {0}<br/>", trigger.getName()));
sb.append(MessageFormat.format("<b>Triggered by: </b> {0}<br/>", getDisplayTriggerName(context)));
sb.append(MessageFormat.format("<b>Notification is on cooldown until: </b> {0}<br/>",
DATE_FORMATTER.get().format(new Date(context.getCoolDownExpiration()))));
sb.append(MessageFormat.format("<b>Evaluated metric expression: </b> {0}<br/>", context.getAlert().getExpression()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public void sendNotification(NotificationContext notificationContext) {
Map<String, String> additionalFields = new HashMap<>();

additionalFields.put("Notification status", "Notification created.");
updateTriggerName(notificationContext);
_createAnnotation(notificationContext, additionalFields);
sendAdditionalNotification(notificationContext);
_dispose();
Expand All @@ -115,9 +114,9 @@ private Map<String, String> getLowerCaseTagMap(final Map<String, String> tags)

/*
* Finds all the templates like ${scope}, ${metric} and replaces it with the required fields.
* If no matches are found, nothing is done.
* If no matches are found, nothing is done. Should be a protected function, making public for unit testing.
* */
protected void updateTriggerName(NotificationContext context) {
public String getDisplayTriggerName(NotificationContext context) {
String newTriggerName = context.getTrigger().getName();
Metric triggeredMetric = context.getTriggeredMetric();
newTriggerName = newTriggerName.replaceAll("(?i)\\$\\{scope\\}", triggeredMetric.getScope());
Expand All @@ -130,7 +129,7 @@ protected void updateTriggerName(NotificationContext context) {
if (lowerCaseTagMap.containsKey(currentTagKey))
newTriggerName = newTriggerName.replace(currentRegex, lowerCaseTagMap.get(currentTagKey));
}
context.getTrigger().setName(newTriggerName);
return newTriggerName;
}

private void _createAnnotation(NotificationContext notificationContext, Map<String, String> additionalFields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private String getEmailSubject(NotificationContext context) {
if (currentAlert.getNotifications().size() > 1)
currentSubject += " Notification: "+ context.getNotification().getName();
if (currentAlert.getTriggers().size() > 1)
currentSubject += " Trigger:" + context.getTrigger().getName();
currentSubject += " Trigger:" + getDisplayTriggerName(context);
return currentSubject;
}

Expand Down Expand Up @@ -169,7 +169,7 @@ protected String getEmailBody(NotificationContext context, NotificationStatus no
if(currentAlert.getNotifications().size() > 1)
sb.append(MessageFormat.format("<b>Notification: </b> {0}<br/>", notification.getName()));
if(currentAlert.getTriggers().size() > 1)
sb.append(MessageFormat.format("<b>Triggered by: </b> {0}<br/>", trigger.getName()));
sb.append(MessageFormat.format("<b>Triggered by: </b> {0}<br/>", getDisplayTriggerName(context)));
sb.append(MessageFormat.format("<b>Notification is on cooldown until: </b> {0}<br/>",
DATE_FORMATTER.get().format(new Date(context.getCoolDownExpiration()))));
sb.append(MessageFormat.format("<b>Evaluated metric expression: </b> {0}<br/>", context.getAlert().getExpression()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ protected void _sendAdditionalNotification(NotificationContext context, Notifica
String body = getGOCMessageBody(notification, trigger, context);
Severity sev = status == NotificationStatus.CLEARED ? Severity.OK : Severity.ERROR;

sendMessage(sev, context.getNotification().getName(), context.getAlert().getName(), context.getTrigger().getName(), body,
sendMessage(sev, context.getNotification().getName(), context.getAlert().getName(), getDisplayTriggerName(context), body,
context.getNotification().getSeverityLevel(),context.getNotification().getSRActionable(), context.getTriggerFiredTime(), context.getTriggeredMetric());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private String generateGusFeed(Notification notification, Trigger trigger, Notif
String notificationName = context.getNotification().getName();
String alertName = context.getAlert().getName();
String triggerFiredTime = DATE_FORMATTER.get().format(new Date(context.getTriggerFiredTime()));
String triggerName = trigger.getName();
String triggerName = getDisplayTriggerName(context);
String notificationCooldownExpiraton = DATE_FORMATTER.get().format(new Date(context.getCoolDownExpiration()));
String metricExpression = context.getAlert().getExpression();
String triggerDetails = getTriggerDetails(trigger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.util.HashMap;
import java.util.Map;

import com.salesforce.dva.argus.service.alert.DefaultAlertService;
import com.salesforce.dva.argus.service.alert.notifier.DefaultNotifier;
import org.junit.Test;

import com.salesforce.dva.argus.AbstractTest;
Expand Down Expand Up @@ -91,16 +93,19 @@ public void testUpdatingTriggerName() {
alert.setNotifications(Arrays.asList(new Notification[] { notification }));
alert.setTriggers(Arrays.asList(new Trigger[] { trigger }));
alert = system.getServiceFactory().getAlertService().updateAlert(alert);

Metric m = new Metric("scope", "metric");
Map<String, String> tags = new HashMap<>();
tags.put("tag1","val1");
tags.put("tag2", "val2");

Metric m = new Metric("scope", "metric");
m.setTags(tags);
NotificationContext context = new NotificationContext(alert, alert.getTriggers().get(0), notification, 1418319600000L, 0.0, m);
Notifier notifier = system.getServiceFactory().getAlertService().getNotifier(SupportedNotifier.GOC);
notifier.sendNotification(context);
assertEquals("scope-trigger_name-metric-trigger_metric-val1-trigger_tag1-val2-trigger_tag2-${tag3}-val2", context.getTrigger().getName());
assertEquals("${sCopE}-trigger_name-${MEtriC}-trigger_metric-${tag1}-trigger_tag1-${tag2}-trigger_tag2-${tag3}-${tAg2}", context.getTrigger().getName());
assertEquals("scope-trigger_name-metric-trigger_metric-val1-trigger_tag1-val2-trigger_tag2-${tag3}-val2", system.getNotifierFactory().getGOCNotifier().getDisplayTriggerName(context));


}
}
/* Copyright (c) 2016, Salesforce.com, Inc. All rights reserved. */

0 comments on commit 2b1a6f6

Please sign in to comment.