Rename EmailAlertAction

Original commit: elastic/x-pack-elasticsearch@35e819cdf2
This commit is contained in:
Brian Murphy 2014-12-04 14:02:53 +00:00
parent 6791841f42
commit d70164de4d
3 changed files with 15 additions and 13 deletions

View File

@ -73,15 +73,15 @@ public class SnptAlertActionFactory implements AlertActionFactory, ConfigurableC
throw new ElasticsearchIllegalArgumentException("Unexpected token [" + token + "]"); throw new ElasticsearchIllegalArgumentException("Unexpected token [" + token + "]");
} }
} }
return new EmailAlertAction(display, addresses.toArray(new String[addresses.size()])); return new SntpAlertAction(display, addresses.toArray(new String[addresses.size()]));
} }
@Override @Override
public boolean doAction(AlertAction action, Alert alert, TriggerResult result) { public boolean doAction(AlertAction action, Alert alert, TriggerResult result) {
if (!(action instanceof EmailAlertAction)) { if (!(action instanceof SntpAlertAction)) {
throw new ElasticsearchIllegalStateException("Bad action [" + action.getClass() + "] passed to EmailAlertActionFactory expected [" + EmailAlertAction.class + "]"); throw new ElasticsearchIllegalStateException("Bad action [" + action.getClass() + "] passed to EmailAlertActionFactory expected [" + SntpAlertAction.class + "]");
} }
EmailAlertAction emailAlertAction = (EmailAlertAction)action; SntpAlertAction sntpAlertAction = (SntpAlertAction)action;
if (settings == null) { if (settings == null) {
settings = configurationManager.getGlobalConfig(); settings = configurationManager.getGlobalConfig();
configurationManager.registerListener(this); configurationManager.registerListener(this);
@ -113,8 +113,9 @@ public class SnptAlertActionFactory implements AlertActionFactory, ConfigurableC
try { try {
message.setFrom(new InternetAddress(settings.get(FROM_SETTING))); message.setFrom(new InternetAddress(settings.get(FROM_SETTING)));
message.setRecipients(Message.RecipientType.TO, message.setRecipients(Message.RecipientType.TO,
emailAlertAction.getEmailAddresses().toArray(new Address[1])); sntpAlertAction.getEmailAddresses().toArray(new Address[1]));
message.setSubject("Elasticsearch Alert " + alert.getAlertName() + " triggered"); message.setSubject("Elasticsearch Alert " + alert.getAlertName() + " triggered");
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
output.append("The following query triggered because ").append(result.getTrigger().toString()).append("\n"); output.append("The following query triggered because ").append(result.getTrigger().toString()).append("\n");
Object totalHits = XContentMapValues.extractValue("hits.total", result.getTriggerResponse()); Object totalHits = XContentMapValues.extractValue("hits.total", result.getTriggerResponse());
@ -129,12 +130,13 @@ public class SnptAlertActionFactory implements AlertActionFactory, ConfigurableC
output.append("\n"); output.append("\n");
output.append("\n"); output.append("\n");
if (emailAlertAction.getDisplayField() != null) {
if (sntpAlertAction.getDisplayField() != null) {
List<Map<String, Object>> hits = (List<Map<String, Object>>) XContentMapValues.extractValue("hits.hits", result.getActionResponse()); List<Map<String, Object>> hits = (List<Map<String, Object>>) XContentMapValues.extractValue("hits.hits", result.getActionResponse());
for (Map<String, Object> hit : hits) { for (Map<String, Object> hit : hits) {
Map<String, Object> _source = (Map<String, Object>) hit.get("_source"); Map<String, Object> _source = (Map<String, Object>) hit.get("_source");
if (_source.containsKey(emailAlertAction.getDisplayField())) { if (_source.containsKey(sntpAlertAction.getDisplayField())) {
output.append(_source.get(emailAlertAction.getDisplayField()).toString()); output.append(_source.get(sntpAlertAction.getDisplayField()).toString());
} else { } else {
output.append(_source); output.append(_source);
} }

View File

@ -15,12 +15,12 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class EmailAlertAction implements AlertAction { public class SntpAlertAction implements AlertAction {
private final String displayField; private final String displayField;
private final List<Address> emailAddresses = new ArrayList<>(); private final List<Address> emailAddresses = new ArrayList<>();
public EmailAlertAction(String displayField, String ... addresses){ public SntpAlertAction(String displayField, String... addresses){
for (String address : addresses) { for (String address : addresses) {
addEmailAddress(address); addEmailAddress(address);
} }
@ -70,7 +70,7 @@ public class EmailAlertAction implements AlertAction {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
EmailAlertAction that = (EmailAlertAction) o; SntpAlertAction that = (SntpAlertAction) o;
if (displayField != null ? !displayField.equals(that.displayField) : that.displayField != null) return false; if (displayField != null ? !displayField.equals(that.displayField) : that.displayField != null) return false;
if (emailAddresses != null ? !emailAddresses.equals(that.emailAddresses) : that.emailAddresses != null) if (emailAddresses != null ? !emailAddresses.equals(that.emailAddresses) : that.emailAddresses != null)

View File

@ -7,7 +7,7 @@ package org.elasticsearch.alerts;
import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.alerts.actions.AlertAction; import org.elasticsearch.alerts.actions.AlertAction;
import org.elasticsearch.alerts.actions.EmailAlertAction; import org.elasticsearch.alerts.actions.SntpAlertAction;
import org.elasticsearch.alerts.triggers.ScriptedTrigger; import org.elasticsearch.alerts.triggers.ScriptedTrigger;
import org.elasticsearch.common.joda.time.DateTime; import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
@ -34,7 +34,7 @@ public class AlertSerializationTest extends AbstractAlertingTests {
SearchRequest payloadRequest = createTriggerSearchRequest("my-payload-index").source(searchSource().query(matchAllQuery())); SearchRequest payloadRequest = createTriggerSearchRequest("my-payload-index").source(searchSource().query(matchAllQuery()));
List<AlertAction> actions = new ArrayList<>(); List<AlertAction> actions = new ArrayList<>();
actions.add(new EmailAlertAction("message", "foo@bar.com")); actions.add(new SntpAlertAction("message", "foo@bar.com"));
Alert alert = new Alert("test-serialization", Alert alert = new Alert("test-serialization",
triggerRequest, triggerRequest,
new ScriptedTrigger("return true", ScriptService.ScriptType.INLINE, "groovy"), new ScriptedTrigger("return true", ScriptService.ScriptType.INLINE, "groovy"),