Changed visibility of setters and made naming of setters and getters consistent
Original commit: elastic/x-pack-elasticsearch@c9cc736012
This commit is contained in:
parent
8e8d360fd2
commit
d3dcecbd77
|
@ -94,22 +94,22 @@ public class Alert implements ToXContent {
|
|||
/**
|
||||
* @return The last time this alert ran.
|
||||
*/
|
||||
public DateTime lastExecuteTime() {
|
||||
public DateTime getLastExecuteTime() {
|
||||
return lastExecuteTime;
|
||||
}
|
||||
|
||||
public void lastExecuteTime(DateTime lastActionFire) {
|
||||
void lastExecuteTime(DateTime lastActionFire) {
|
||||
this.lastExecuteTime = lastActionFire;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The current version of the alert. (es document version)
|
||||
*/
|
||||
public long version() {
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void version(long version) {
|
||||
void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
|
@ -127,11 +127,11 @@ public class Alert implements ToXContent {
|
|||
/**
|
||||
* @return The unique name of this alert.
|
||||
*/
|
||||
public String alertName() {
|
||||
public String getAlertName() {
|
||||
return alertName;
|
||||
}
|
||||
|
||||
public void alertName(String alertName) {
|
||||
void setAlertName(String alertName) {
|
||||
this.alertName = alertName;
|
||||
}
|
||||
|
||||
|
@ -142,40 +142,40 @@ public class Alert implements ToXContent {
|
|||
return searchRequest;
|
||||
}
|
||||
|
||||
public void setSearchRequest(SearchRequest searchRequest) {
|
||||
void setSearchRequest(SearchRequest searchRequest) {
|
||||
this.searchRequest = searchRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The trigger that is going to evaluate if the alert is going to execute the alert actions.
|
||||
*/
|
||||
public AlertTrigger trigger() {
|
||||
public AlertTrigger getTrigger() {
|
||||
return trigger;
|
||||
}
|
||||
|
||||
public void trigger(AlertTrigger trigger) {
|
||||
void setTrigger(AlertTrigger trigger) {
|
||||
this.trigger = trigger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the actions to be executed if the alert matches the trigger
|
||||
*/
|
||||
public List<AlertAction> actions() {
|
||||
public List<AlertAction> getActions() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
public void actions(List<AlertAction> action) {
|
||||
void setActions(List<AlertAction> action) {
|
||||
this.actions = action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The cron schedule expression that expresses when to run the alert.
|
||||
*/
|
||||
public String schedule() {
|
||||
public String getSchedule() {
|
||||
return schedule;
|
||||
}
|
||||
|
||||
public void schedule(String schedule) {
|
||||
void setSchedule(String schedule) {
|
||||
this.schedule = schedule;
|
||||
}
|
||||
|
||||
|
@ -186,7 +186,7 @@ public class Alert implements ToXContent {
|
|||
return timeLastActionExecuted;
|
||||
}
|
||||
|
||||
public void setTimeLastActionExecuted(DateTime timeLastActionExecuted) {
|
||||
void setTimeLastActionExecuted(DateTime timeLastActionExecuted) {
|
||||
this.timeLastActionExecuted = timeLastActionExecuted;
|
||||
}
|
||||
|
||||
|
@ -197,7 +197,7 @@ public class Alert implements ToXContent {
|
|||
return throttlePeriod;
|
||||
}
|
||||
|
||||
public void setThrottlePeriod(TimeValue throttlePeriod) {
|
||||
void setThrottlePeriod(TimeValue throttlePeriod) {
|
||||
this.throttlePeriod = throttlePeriod;
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ public class Alert implements ToXContent {
|
|||
return ackState;
|
||||
}
|
||||
|
||||
public void setAckState(AlertAckState ackState) {
|
||||
void setAckState(AlertAckState ackState) {
|
||||
this.ackState = ackState;
|
||||
}
|
||||
|
||||
|
@ -219,7 +219,7 @@ public class Alert implements ToXContent {
|
|||
return metadata;
|
||||
}
|
||||
|
||||
public void setMetadata(Map<String, Object> metadata) {
|
||||
void setMetadata(Map<String, Object> metadata) {
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
|
@ -230,7 +230,7 @@ public class Alert implements ToXContent {
|
|||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Alert alert = (Alert) o;
|
||||
return alert.alertName().equals(alertName);
|
||||
return alert.getAlertName().equals(alertName);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -13,7 +13,6 @@ import org.elasticsearch.action.index.IndexResponse;
|
|||
import org.elasticsearch.alerts.actions.AlertActionEntry;
|
||||
import org.elasticsearch.alerts.actions.AlertActionManager;
|
||||
import org.elasticsearch.alerts.actions.AlertActionRegistry;
|
||||
import org.elasticsearch.alerts.actions.AlertActionState;
|
||||
import org.elasticsearch.alerts.scheduler.AlertScheduler;
|
||||
import org.elasticsearch.alerts.triggers.TriggerManager;
|
||||
import org.elasticsearch.alerts.triggers.TriggerResult;
|
||||
|
@ -98,9 +97,9 @@ public class AlertManager extends AbstractComponent {
|
|||
try {
|
||||
AlertsStore.AlertStoreModification result = alertsStore.putAlert(alertName, alertSource);
|
||||
if (result.getPrevious() == null) {
|
||||
scheduler.schedule(alertName, result.getCurrent().schedule());
|
||||
} else if (!result.getPrevious().schedule().equals(result.getCurrent().schedule())) {
|
||||
scheduler.schedule(alertName, result.getCurrent().schedule());
|
||||
scheduler.schedule(alertName, result.getCurrent().getSchedule());
|
||||
} else if (!result.getPrevious().getSchedule().equals(result.getCurrent().getSchedule())) {
|
||||
scheduler.schedule(alertName, result.getCurrent().getSchedule());
|
||||
}
|
||||
return result.getIndexResponse();
|
||||
} finally {
|
||||
|
@ -141,19 +140,15 @@ public class AlertManager extends AbstractComponent {
|
|||
throw new ElasticsearchException("Alert is not available");
|
||||
}
|
||||
TriggerResult triggerResult = triggerManager.isTriggered(alert, entry.getScheduledTime(), entry.getFireTime());
|
||||
entry.setSearchResponse(triggerResult.getResponse());
|
||||
if (triggerResult.isTriggered()) {
|
||||
entry.setTriggered(true);
|
||||
if (!isActionThrottled(alert)) {
|
||||
triggerResult.setThrottled(isActionThrottled(alert));
|
||||
if (!triggerResult.isThrottled()) {
|
||||
actionRegistry.doAction(alert, triggerResult);
|
||||
alert.setTimeLastActionExecuted(entry.getScheduledTime());
|
||||
if (alert.getAckState() == AlertAckState.NOT_TRIGGERED) {
|
||||
alert.setAckState(AlertAckState.NEEDS_ACK);
|
||||
}
|
||||
} else {
|
||||
entry.setState(AlertActionState.THROTTLED);
|
||||
}
|
||||
|
||||
} else if (alert.getAckState() == AlertAckState.ACKED) {
|
||||
alert.setAckState(AlertAckState.NOT_TRIGGERED);
|
||||
}
|
||||
|
@ -174,7 +169,7 @@ public class AlertManager extends AbstractComponent {
|
|||
try {
|
||||
Alert alert = alertsStore.getAlert(alertName);
|
||||
if (alert == null) {
|
||||
throw new ElasticsearchException("Alert is does not exist [" + alertName + "]");
|
||||
throw new ElasticsearchException("Alert does not exist [" + alertName + "]");
|
||||
}
|
||||
if (alert.getAckState() == AlertAckState.NEEDS_ACK) {
|
||||
alert.setAckState(AlertAckState.ACKED);
|
||||
|
|
|
@ -99,7 +99,7 @@ public class AlertsStore extends AbstractComponent {
|
|||
Alert alert = parseAlert(alertName, alertSource);
|
||||
IndexRequest indexRequest = createIndexRequest(alertName, alertSource);
|
||||
IndexResponse response = client.index(indexRequest).actionGet();
|
||||
alert.version(response.getVersion());
|
||||
alert.setVersion(response.getVersion());
|
||||
Alert previous = alertMap.put(alertName, alert);
|
||||
return new AlertStoreModification(previous, alert, response);
|
||||
}
|
||||
|
@ -110,14 +110,14 @@ public class AlertsStore extends AbstractComponent {
|
|||
public void updateAlert(Alert alert) throws IOException {
|
||||
ensureStarted();
|
||||
BytesReference source = XContentFactory.contentBuilder(alert.getContentType()).value(alert).bytes();
|
||||
IndexResponse response = client.index(createIndexRequest(alert.alertName(), source)).actionGet();
|
||||
alert.version(response.getVersion());
|
||||
IndexResponse response = client.index(createIndexRequest(alert.getAlertName(), source)).actionGet();
|
||||
alert.setVersion(response.getVersion());
|
||||
// Don't need to update the alertMap, since we are working on an instance from it.
|
||||
assert verifySameInstance(alert);
|
||||
}
|
||||
|
||||
private boolean verifySameInstance(Alert alert) {
|
||||
Alert found = alertMap.get(alert.alertName());
|
||||
Alert found = alertMap.get(alert.getAlertName());
|
||||
assert found == alert : "expected " + alert + " but got " + found;
|
||||
return true;
|
||||
}
|
||||
|
@ -133,7 +133,7 @@ public class AlertsStore extends AbstractComponent {
|
|||
}
|
||||
|
||||
DeleteRequest deleteRequest = new DeleteRequest(ALERT_INDEX, ALERT_TYPE, name);
|
||||
deleteRequest.version(alert.version());
|
||||
deleteRequest.version(alert.getVersion());
|
||||
DeleteResponse deleteResponse = client.delete(deleteRequest).actionGet();
|
||||
assert deleteResponse.isFound();
|
||||
return deleteResponse;
|
||||
|
@ -231,13 +231,13 @@ public class AlertsStore extends AbstractComponent {
|
|||
|
||||
private Alert parseAlert(String alertId, SearchHit sh) {
|
||||
Alert alert = parseAlert(alertId, sh.getSourceRef());
|
||||
alert.version(sh.version());
|
||||
alert.setVersion(sh.version());
|
||||
return alert;
|
||||
}
|
||||
|
||||
protected Alert parseAlert(String alertName, BytesReference source) {
|
||||
Alert alert = new Alert();
|
||||
alert.alertName(alertName);
|
||||
alert.setAlertName(alertName);
|
||||
try (XContentParser parser = XContentHelper.createParser(source)) {
|
||||
alert.setContentType(parser.contentType());
|
||||
|
||||
|
@ -249,10 +249,10 @@ public class AlertsStore extends AbstractComponent {
|
|||
currentFieldName = parser.currentName();
|
||||
} else if (token == XContentParser.Token.START_OBJECT) {
|
||||
if (TRIGGER_FIELD.match(currentFieldName)) {
|
||||
alert.trigger(triggerManager.instantiateAlertTrigger(parser));
|
||||
alert.setTrigger(triggerManager.instantiateAlertTrigger(parser));
|
||||
} else if (ACTION_FIELD.match(currentFieldName)) {
|
||||
List<AlertAction> actions = alertActionRegistry.instantiateAlertActions(parser);
|
||||
alert.actions(actions);
|
||||
alert.setActions(actions);
|
||||
} else if (REQUEST_FIELD.match(currentFieldName)) {
|
||||
alert.setSearchRequest(AlertUtils.readSearchRequest(parser));
|
||||
} else if (META_FIELD.match(currentFieldName)) {
|
||||
|
@ -262,7 +262,7 @@ public class AlertsStore extends AbstractComponent {
|
|||
}
|
||||
} else if (token.isValue()) {
|
||||
if (SCHEDULE_FIELD.match(currentFieldName)) {
|
||||
alert.schedule(parser.textOrNull());
|
||||
alert.setSchedule(parser.textOrNull());
|
||||
} else if (LAST_ACTION_FIRE.match(currentFieldName)) {
|
||||
alert.lastExecuteTime(DateTime.parse(parser.textOrNull()));
|
||||
} else if (LAST_ACTION_EXECUTED_FIELD.match(currentFieldName)) {
|
||||
|
@ -282,15 +282,15 @@ public class AlertsStore extends AbstractComponent {
|
|||
throw new ElasticsearchException("Error during parsing alert", e);
|
||||
}
|
||||
|
||||
if (alert.lastExecuteTime() == null) {
|
||||
if (alert.getLastExecuteTime() == null) {
|
||||
alert.lastExecuteTime(new DateTime(0));
|
||||
}
|
||||
|
||||
if (alert.schedule() == null) {
|
||||
if (alert.getSchedule() == null) {
|
||||
throw new ElasticsearchIllegalArgumentException("Schedule is a required field");
|
||||
}
|
||||
|
||||
if (alert.trigger() == null) {
|
||||
if (alert.getTrigger() == null) {
|
||||
throw new ElasticsearchIllegalArgumentException("Trigger is a required field");
|
||||
}
|
||||
|
||||
|
|
|
@ -46,12 +46,12 @@ public class AlertActionEntry implements ToXContent {
|
|||
}
|
||||
|
||||
public AlertActionEntry(Alert alert, DateTime scheduledTime, DateTime fireTime, AlertActionState state) throws IOException {
|
||||
this.id = alert.alertName() + "#" + scheduledTime.toDateTimeISO();
|
||||
this.alertName = alert.alertName();
|
||||
this.id = alert.getAlertName() + "#" + scheduledTime.toDateTimeISO();
|
||||
this.alertName = alert.getAlertName();
|
||||
this.fireTime = fireTime;
|
||||
this.scheduledTime = scheduledTime;
|
||||
this.trigger = alert.trigger();
|
||||
this.actions = alert.actions();
|
||||
this.trigger = alert.getTrigger();
|
||||
this.actions = alert.getActions();
|
||||
this.state = state;
|
||||
this.searchRequest = alert.getSearchRequest();
|
||||
this.metadata = alert.getMetadata();
|
||||
|
@ -67,7 +67,7 @@ public class AlertActionEntry implements ToXContent {
|
|||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
@ -78,7 +78,7 @@ public class AlertActionEntry implements ToXContent {
|
|||
return scheduledTime;
|
||||
}
|
||||
|
||||
public void setScheduledTime(DateTime scheduledTime) {
|
||||
void setScheduledTime(DateTime scheduledTime) {
|
||||
this.scheduledTime = scheduledTime;
|
||||
}
|
||||
|
||||
|
@ -89,7 +89,7 @@ public class AlertActionEntry implements ToXContent {
|
|||
return alertName;
|
||||
}
|
||||
|
||||
public void setAlertName(String alertName) {
|
||||
void setAlertName(String alertName) {
|
||||
this.alertName = alertName;
|
||||
}
|
||||
|
||||
|
@ -100,7 +100,7 @@ public class AlertActionEntry implements ToXContent {
|
|||
return triggered;
|
||||
}
|
||||
|
||||
public void setTriggered(boolean triggered) {
|
||||
void setTriggered(boolean triggered) {
|
||||
this.triggered = triggered;
|
||||
}
|
||||
|
||||
|
@ -111,7 +111,7 @@ public class AlertActionEntry implements ToXContent {
|
|||
return fireTime;
|
||||
}
|
||||
|
||||
public void setFireTime(DateTime fireTime) {
|
||||
void setFireTime(DateTime fireTime) {
|
||||
this.fireTime = fireTime;
|
||||
}
|
||||
|
||||
|
@ -122,7 +122,7 @@ public class AlertActionEntry implements ToXContent {
|
|||
return trigger;
|
||||
}
|
||||
|
||||
public void setTrigger(AlertTrigger trigger) {
|
||||
void setTrigger(AlertTrigger trigger) {
|
||||
this.trigger = trigger;
|
||||
}
|
||||
|
||||
|
@ -133,7 +133,7 @@ public class AlertActionEntry implements ToXContent {
|
|||
return searchRequest;
|
||||
}
|
||||
|
||||
public void setSearchRequest(SearchRequest searchRequest) {
|
||||
void setSearchRequest(SearchRequest searchRequest) {
|
||||
this.searchRequest = searchRequest;
|
||||
}
|
||||
|
||||
|
@ -144,7 +144,7 @@ public class AlertActionEntry implements ToXContent {
|
|||
return searchResponse;
|
||||
}
|
||||
|
||||
public void setSearchResponse(Map<String, Object> searchResponse) {
|
||||
void setSearchResponse(Map<String, Object> searchResponse) {
|
||||
this.searchResponse = searchResponse;
|
||||
}
|
||||
|
||||
|
@ -155,7 +155,7 @@ public class AlertActionEntry implements ToXContent {
|
|||
return actions;
|
||||
}
|
||||
|
||||
public void setActions(List<AlertAction> actions) {
|
||||
void setActions(List<AlertAction> actions) {
|
||||
this.actions = actions;
|
||||
}
|
||||
|
||||
|
@ -166,7 +166,7 @@ public class AlertActionEntry implements ToXContent {
|
|||
return state;
|
||||
}
|
||||
|
||||
public void setState(AlertActionState state) {
|
||||
void setState(AlertActionState state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
|
@ -174,7 +174,7 @@ public class AlertActionEntry implements ToXContent {
|
|||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
|
@ -190,13 +190,13 @@ public class AlertActionEntry implements ToXContent {
|
|||
}
|
||||
|
||||
/**
|
||||
* @return The error if an error occured otherwise null
|
||||
* @return The error if an error occurred otherwise null
|
||||
*/
|
||||
public String getErrorMsg(){
|
||||
return this.errorMsg;
|
||||
}
|
||||
|
||||
public void setErrorMsg(String errorMsg) {
|
||||
void setErrorMsg(String errorMsg) {
|
||||
this.errorMsg = errorMsg;
|
||||
}
|
||||
|
||||
|
@ -207,7 +207,7 @@ public class AlertActionEntry implements ToXContent {
|
|||
return metadata;
|
||||
}
|
||||
|
||||
public void setMetadata(Map<String, Object> metadata) {
|
||||
void setMetadata(Map<String, Object> metadata) {
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
|
|
|
@ -268,7 +268,7 @@ public class AlertActionManager extends AbstractComponent {
|
|||
|
||||
public void addAlertAction(Alert alert, DateTime scheduledFireTime, DateTime fireTime) throws IOException {
|
||||
ensureStarted();
|
||||
logger.debug("Adding alert action for alert [{}]", alert.alertName());
|
||||
logger.debug("Adding alert action for alert [{}]", alert.getAlertName());
|
||||
String alertHistoryIndex = getAlertHistoryIndexNameForTime(scheduledFireTime);
|
||||
AlertActionEntry entry = new AlertActionEntry(alert, scheduledFireTime, fireTime, AlertActionState.SEARCH_NEEDED);
|
||||
IndexResponse response = client.prepareIndex(alertHistoryIndex, ALERT_HISTORY_TYPE, entry.getId())
|
||||
|
@ -276,7 +276,7 @@ public class AlertActionManager extends AbstractComponent {
|
|||
.setOpType(IndexRequest.OpType.CREATE)
|
||||
.get();
|
||||
entry.setVersion(response.getVersion());
|
||||
logger.debug("Added alert action for alert [{}]", alert.alertName());
|
||||
logger.debug("Added alert action for alert [{}]", alert.getAlertName());
|
||||
|
||||
long currentSize = actionsToBeProcessed.size() + 1;
|
||||
actionsToBeProcessed.add(entry);
|
||||
|
@ -354,9 +354,15 @@ public class AlertActionManager extends AbstractComponent {
|
|||
}
|
||||
updateHistoryEntry(entry, AlertActionState.SEARCH_UNDERWAY);
|
||||
logger.debug("Running an alert action entry for [{}]", entry.getAlertName());
|
||||
TriggerResult trigger = alertManager.executeAlert(entry);
|
||||
if (trigger.isTriggered()) {
|
||||
if (entry.getState() != AlertActionState.THROTTLED) {
|
||||
TriggerResult result = alertManager.executeAlert(entry);
|
||||
entry.setSearchResponse(result.getResponse());
|
||||
if (result.isTriggered()) {
|
||||
entry.setTriggered(true);
|
||||
if (result.isThrottled()) {
|
||||
if (alert.getAckState() != AlertAckState.NOT_TRIGGERED) {
|
||||
entry.setState(AlertActionState.THROTTLED);
|
||||
}
|
||||
} else if (entry.getState() != AlertActionState.THROTTLED) {
|
||||
entry.setState(AlertActionState.ACTION_PERFORMED);
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -60,7 +60,7 @@ public class AlertActionRegistry extends AbstractComponent {
|
|||
}
|
||||
|
||||
public void doAction(Alert alert, TriggerResult actionEntry){
|
||||
for (AlertAction action : alert.actions()) {
|
||||
for (AlertAction action : alert.getActions()) {
|
||||
AlertActionFactory factory = actionImplemented.get(action.getActionName());
|
||||
if (factory != null) {
|
||||
factory.doAction(action, alert, actionEntry);
|
||||
|
|
|
@ -89,7 +89,7 @@ public class EmailAlertActionFactory implements AlertActionFactory {
|
|||
message.setFrom(new InternetAddress(from));
|
||||
message.setRecipients(Message.RecipientType.TO,
|
||||
emailAlertAction.getEmailAddresses().toArray(new Address[1]));
|
||||
message.setSubject("Elasticsearch Alert " + alert.alertName() + " triggered");
|
||||
message.setSubject("Elasticsearch Alert " + alert.getAlertName() + " triggered");
|
||||
StringBuilder output = new StringBuilder();
|
||||
output.append("The following query triggered because ").append(result.getTrigger().toString()).append("\n");
|
||||
Object totalHits = XContentMapValues.extractValue("hits.total", result.getResponse());
|
||||
|
|
|
@ -71,7 +71,7 @@ public class IndexAlertActionFactory implements AlertActionFactory {
|
|||
XContentBuilder resultBuilder = XContentFactory.jsonBuilder().prettyPrint();
|
||||
resultBuilder.startObject();
|
||||
resultBuilder.field("response", result.getResponse());
|
||||
resultBuilder.field("timestamp", alert.lastExecuteTime()); ///@TODO FIXME the firetime should be in the result ?
|
||||
resultBuilder.field("timestamp", alert.getLastExecuteTime()); ///@TODO FIXME the firetime should be in the result ?
|
||||
resultBuilder.endObject();
|
||||
indexRequest.source(resultBuilder);
|
||||
} catch (IOException ie) {
|
||||
|
|
|
@ -59,7 +59,7 @@ public class AlertScheduler extends AbstractComponent {
|
|||
scheduler.setJobFactory(new SimpleJobFactory());
|
||||
Map<JobDetail, Set<? extends Trigger>> jobs = new HashMap<>();
|
||||
for (Map.Entry<String, Alert> entry : alerts.entrySet()) {
|
||||
jobs.put(createJobDetail(entry.getKey()), createTrigger(entry.getValue().schedule()));
|
||||
jobs.put(createJobDetail(entry.getKey()), createTrigger(entry.getValue().getSchedule()));
|
||||
}
|
||||
scheduler.scheduleJobs(jobs, false);
|
||||
scheduler.start();
|
||||
|
|
|
@ -20,7 +20,9 @@ import org.elasticsearch.common.inject.Inject;
|
|||
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
|
||||
import org.elasticsearch.common.joda.time.DateTime;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.*;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentHelper;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.index.mapper.core.DateFieldMapper;
|
||||
import org.elasticsearch.script.ExecutableScript;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
|
@ -101,12 +103,12 @@ public class TriggerManager extends AbstractComponent {
|
|||
public TriggerResult isTriggered(Alert alert, DateTime scheduledFireTime, DateTime fireTime) throws IOException {
|
||||
SearchRequest request = prepareTriggerSearch(alert, scheduledFireTime, fireTime);
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("For alert [{}] running query for [{}]", alert.alertName(), XContentHelper.convertToJson(request.source(), false, true));
|
||||
logger.trace("For alert [{}] running query for [{}]", alert.getAlertName(), XContentHelper.convertToJson(request.source(), false, true));
|
||||
}
|
||||
|
||||
SearchResponse response = client.search(request).actionGet(); // actionGet deals properly with InterruptedException
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Ran alert [{}] and got hits : [{}]", alert.alertName(), response.getHits().getTotalHits());
|
||||
logger.debug("Ran alert [{}] and got hits : [{}]", alert.getAlertName(), response.getHits().getTotalHits());
|
||||
for (SearchHit hit : response.getHits()) {
|
||||
logger.debug("Hit: {}", XContentHelper.toString(hit));
|
||||
}
|
||||
|
@ -114,12 +116,11 @@ public class TriggerManager extends AbstractComponent {
|
|||
}
|
||||
XContentBuilder builder = jsonBuilder().startObject().value(response).endObject();
|
||||
Map<String, Object> responseMap = XContentHelper.convertToMap(builder.bytes(), false).v2();
|
||||
return isTriggered(alert.trigger(), request, responseMap);
|
||||
return isTriggered(alert.getTrigger(), request, responseMap);
|
||||
}
|
||||
|
||||
protected TriggerResult isTriggered(AlertTrigger trigger, SearchRequest request, Map<String, Object> response) {
|
||||
TriggerFactory factory = triggersImplemented.get(trigger.getTriggerName());
|
||||
|
||||
if (factory == null) {
|
||||
throw new ElasticsearchIllegalArgumentException("No trigger exists with the name [" + trigger.getTriggerName() + "]");
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ import java.util.Map;
|
|||
public class TriggerResult {
|
||||
|
||||
private final boolean triggered;
|
||||
private boolean throttled;
|
||||
private final SearchRequest request;
|
||||
private final Map<String, Object> response;
|
||||
private final AlertTrigger trigger;
|
||||
|
@ -29,6 +30,14 @@ public class TriggerResult {
|
|||
return triggered;
|
||||
}
|
||||
|
||||
public boolean isThrottled() {
|
||||
return throttled;
|
||||
}
|
||||
|
||||
public void setThrottled(boolean throttled) {
|
||||
this.throttled = throttled;
|
||||
}
|
||||
|
||||
public SearchRequest getRequest() {
|
||||
return request;
|
||||
}
|
||||
|
|
|
@ -54,12 +54,12 @@ public class AlertSerializationTest extends ElasticsearchIntegrationTest {
|
|||
internalCluster().getInstance(AlertsStore.class, internalCluster().getMasterName());
|
||||
|
||||
Alert parsedAlert = alertsStore.parseAlert("test-serialization", jsonBuilder.bytes());
|
||||
assertEquals(parsedAlert.version(), alert.version());
|
||||
assertEquals(parsedAlert.actions(), alert.actions());
|
||||
assertEquals(parsedAlert.lastExecuteTime().getMillis(), alert.lastExecuteTime().getMillis());
|
||||
assertEquals(parsedAlert.schedule(), alert.schedule());
|
||||
assertEquals(parsedAlert.getVersion(), alert.getVersion());
|
||||
assertEquals(parsedAlert.getActions(), alert.getActions());
|
||||
assertEquals(parsedAlert.getLastExecuteTime().getMillis(), alert.getLastExecuteTime().getMillis());
|
||||
assertEquals(parsedAlert.getSchedule(), alert.getSchedule());
|
||||
assertEquals(parsedAlert.getSearchRequest().source(), alert.getSearchRequest().source());
|
||||
assertEquals(parsedAlert.trigger(), alert.trigger());
|
||||
assertEquals(parsedAlert.getTrigger(), alert.getTrigger());
|
||||
assertEquals(parsedAlert.getThrottlePeriod(), alert.getThrottlePeriod());
|
||||
if (parsedAlert.getTimeLastActionExecuted() == null) {
|
||||
assertNull(alert.getTimeLastActionExecuted());
|
||||
|
|
|
@ -53,9 +53,9 @@ public class AlertThrottleTests extends AbstractAlertingTests {
|
|||
Alert alert = new Alert();
|
||||
alert.setAckState(AlertAckState.NOT_TRIGGERED);
|
||||
alert.setSearchRequest(createTriggerSearchRequest("test-index").source(searchSource().query(matchAllQuery())));
|
||||
alert.trigger(new ScriptedTrigger("hits.total > 0", ScriptService.ScriptType.INLINE, "groovy"));
|
||||
alert.actions().add(new IndexAlertAction("action-index", "action-type"));
|
||||
alert.schedule( "0/5 * * * * ? *");
|
||||
alert.setTrigger(new ScriptedTrigger("hits.total > 0", ScriptService.ScriptType.INLINE, "groovy"));
|
||||
alert.getActions().add(new IndexAlertAction("action-index", "action-type"));
|
||||
alert.setSchedule("0/5 * * * * ? *");
|
||||
|
||||
alert.lastExecuteTime(new DateTime());
|
||||
XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
|
||||
|
@ -125,9 +125,9 @@ public class AlertThrottleTests extends AbstractAlertingTests {
|
|||
Alert alert = new Alert();
|
||||
alert.setAckState(AlertAckState.NOT_ACKABLE);
|
||||
alert.setSearchRequest(createTriggerSearchRequest("test-index").source(searchSource().query(matchAllQuery())));
|
||||
alert.trigger(new ScriptedTrigger("hits.total > 0", ScriptService.ScriptType.INLINE, "groovy"));
|
||||
alert.actions().add(new IndexAlertAction("action-index", "action-type"));
|
||||
alert.schedule("0/5 * * * * ? *");
|
||||
alert.setTrigger(new ScriptedTrigger("hits.total > 0", ScriptService.ScriptType.INLINE, "groovy"));
|
||||
alert.getActions().add(new IndexAlertAction("action-index", "action-type"));
|
||||
alert.setSchedule("0/5 * * * * ? *");
|
||||
alert.lastExecuteTime(new DateTime());
|
||||
alert.setThrottlePeriod(new TimeValue(10, TimeUnit.SECONDS));
|
||||
|
||||
|
|
|
@ -143,7 +143,7 @@ public class AlertActionsTest extends AbstractAlertingTests {
|
|||
|
||||
@Override
|
||||
public boolean doAction(AlertAction action, Alert alert, TriggerResult actionEntry) {
|
||||
logger.info("Alert {} invoked: {}", alert.alertName(), actionEntry);
|
||||
logger.info("Alert {} invoked: {}", alert.getAlertName(), actionEntry);
|
||||
alertActionInvoked.set(true);
|
||||
return true;
|
||||
}
|
||||
|
@ -175,12 +175,12 @@ public class AlertActionsTest extends AbstractAlertingTests {
|
|||
assertNotNull(alertsResponse.indexResponse());
|
||||
assertTrue(alertsResponse.indexResponse().isCreated());
|
||||
|
||||
GetAlertRequest getAlertRequest = new GetAlertRequest(alert.alertName());
|
||||
GetAlertRequest getAlertRequest = new GetAlertRequest(alert.getAlertName());
|
||||
GetAlertResponse getAlertResponse = alertsClient.getAlert(getAlertRequest).actionGet();
|
||||
assertTrue(getAlertResponse.getResponse().isExists());
|
||||
assertEquals(getAlertResponse.getResponse().getSourceAsMap().get("schedule").toString(), "0/5 * * * * ? *");
|
||||
|
||||
DeleteAlertRequest deleteAlertRequest = new DeleteAlertRequest(alert.alertName());
|
||||
DeleteAlertRequest deleteAlertRequest = new DeleteAlertRequest(alert.getAlertName());
|
||||
DeleteAlertResponse deleteAlertResponse = alertsClient.deleteAlert(deleteAlertRequest).actionGet();
|
||||
assertNotNull(deleteAlertResponse.deleteResponse());
|
||||
assertTrue(deleteAlertResponse.deleteResponse().isFound());
|
||||
|
|
Loading…
Reference in New Issue