From d3dcecbd77f86a9a7766163f79ad947477764b4d Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Mon, 1 Dec 2014 18:52:02 +0100 Subject: [PATCH] Changed visibility of setters and made naming of setters and getters consistent Original commit: elastic/x-pack-elasticsearch@c9cc7360127a684c3384ec2aa0c0739c0298a10f --- .../java/org/elasticsearch/alerts/Alert.java | 36 +++++++++---------- .../elasticsearch/alerts/AlertManager.java | 17 ++++----- .../org/elasticsearch/alerts/AlertsStore.java | 26 +++++++------- .../alerts/actions/AlertActionEntry.java | 36 +++++++++---------- .../alerts/actions/AlertActionManager.java | 16 ++++++--- .../alerts/actions/AlertActionRegistry.java | 2 +- .../actions/EmailAlertActionFactory.java | 2 +- .../actions/IndexAlertActionFactory.java | 2 +- .../alerts/scheduler/AlertScheduler.java | 2 +- .../alerts/triggers/TriggerManager.java | 11 +++--- .../alerts/triggers/TriggerResult.java | 9 +++++ .../alerts/AlertSerializationTest.java | 10 +++--- .../alerts/AlertThrottleTests.java | 12 +++---- .../alerts/actions/AlertActionsTest.java | 6 ++-- 14 files changed, 99 insertions(+), 88 deletions(-) diff --git a/src/main/java/org/elasticsearch/alerts/Alert.java b/src/main/java/org/elasticsearch/alerts/Alert.java index 65767e38c09..e9a0dbc218d 100644 --- a/src/main/java/org/elasticsearch/alerts/Alert.java +++ b/src/main/java/org/elasticsearch/alerts/Alert.java @@ -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 actions() { + public List getActions() { return actions; } - public void actions(List action) { + void setActions(List 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 metadata) { + void setMetadata(Map 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 diff --git a/src/main/java/org/elasticsearch/alerts/AlertManager.java b/src/main/java/org/elasticsearch/alerts/AlertManager.java index dd0a77845d2..a2b4f94dcf3 100644 --- a/src/main/java/org/elasticsearch/alerts/AlertManager.java +++ b/src/main/java/org/elasticsearch/alerts/AlertManager.java @@ -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); diff --git a/src/main/java/org/elasticsearch/alerts/AlertsStore.java b/src/main/java/org/elasticsearch/alerts/AlertsStore.java index e8f75ad6f94..585fb3e9acd 100644 --- a/src/main/java/org/elasticsearch/alerts/AlertsStore.java +++ b/src/main/java/org/elasticsearch/alerts/AlertsStore.java @@ -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 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"); } diff --git a/src/main/java/org/elasticsearch/alerts/actions/AlertActionEntry.java b/src/main/java/org/elasticsearch/alerts/actions/AlertActionEntry.java index de9a3c39c0b..5b1eb7e0bdb 100644 --- a/src/main/java/org/elasticsearch/alerts/actions/AlertActionEntry.java +++ b/src/main/java/org/elasticsearch/alerts/actions/AlertActionEntry.java @@ -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 searchResponse) { + void setSearchResponse(Map searchResponse) { this.searchResponse = searchResponse; } @@ -155,7 +155,7 @@ public class AlertActionEntry implements ToXContent { return actions; } - public void setActions(List actions) { + void setActions(List 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 metadata) { + void setMetadata(Map metadata) { this.metadata = metadata; } diff --git a/src/main/java/org/elasticsearch/alerts/actions/AlertActionManager.java b/src/main/java/org/elasticsearch/alerts/actions/AlertActionManager.java index 2902f17934b..a4ce4dd9303 100644 --- a/src/main/java/org/elasticsearch/alerts/actions/AlertActionManager.java +++ b/src/main/java/org/elasticsearch/alerts/actions/AlertActionManager.java @@ -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 { diff --git a/src/main/java/org/elasticsearch/alerts/actions/AlertActionRegistry.java b/src/main/java/org/elasticsearch/alerts/actions/AlertActionRegistry.java index a73e476adda..da97a6f76aa 100644 --- a/src/main/java/org/elasticsearch/alerts/actions/AlertActionRegistry.java +++ b/src/main/java/org/elasticsearch/alerts/actions/AlertActionRegistry.java @@ -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); diff --git a/src/main/java/org/elasticsearch/alerts/actions/EmailAlertActionFactory.java b/src/main/java/org/elasticsearch/alerts/actions/EmailAlertActionFactory.java index f38af9534ab..4391d3ccbb8 100644 --- a/src/main/java/org/elasticsearch/alerts/actions/EmailAlertActionFactory.java +++ b/src/main/java/org/elasticsearch/alerts/actions/EmailAlertActionFactory.java @@ -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()); diff --git a/src/main/java/org/elasticsearch/alerts/actions/IndexAlertActionFactory.java b/src/main/java/org/elasticsearch/alerts/actions/IndexAlertActionFactory.java index 40633b638ce..9d12fd1d584 100644 --- a/src/main/java/org/elasticsearch/alerts/actions/IndexAlertActionFactory.java +++ b/src/main/java/org/elasticsearch/alerts/actions/IndexAlertActionFactory.java @@ -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) { diff --git a/src/main/java/org/elasticsearch/alerts/scheduler/AlertScheduler.java b/src/main/java/org/elasticsearch/alerts/scheduler/AlertScheduler.java index af89a210745..c8093f3ed48 100644 --- a/src/main/java/org/elasticsearch/alerts/scheduler/AlertScheduler.java +++ b/src/main/java/org/elasticsearch/alerts/scheduler/AlertScheduler.java @@ -59,7 +59,7 @@ public class AlertScheduler extends AbstractComponent { scheduler.setJobFactory(new SimpleJobFactory()); Map> jobs = new HashMap<>(); for (Map.Entry 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(); diff --git a/src/main/java/org/elasticsearch/alerts/triggers/TriggerManager.java b/src/main/java/org/elasticsearch/alerts/triggers/TriggerManager.java index 05cc97e7b78..d6af66f25ee 100644 --- a/src/main/java/org/elasticsearch/alerts/triggers/TriggerManager.java +++ b/src/main/java/org/elasticsearch/alerts/triggers/TriggerManager.java @@ -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 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 response) { TriggerFactory factory = triggersImplemented.get(trigger.getTriggerName()); - if (factory == null) { throw new ElasticsearchIllegalArgumentException("No trigger exists with the name [" + trigger.getTriggerName() + "]"); } diff --git a/src/main/java/org/elasticsearch/alerts/triggers/TriggerResult.java b/src/main/java/org/elasticsearch/alerts/triggers/TriggerResult.java index cc32d2080ee..3287ea96cf8 100644 --- a/src/main/java/org/elasticsearch/alerts/triggers/TriggerResult.java +++ b/src/main/java/org/elasticsearch/alerts/triggers/TriggerResult.java @@ -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 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; } diff --git a/src/test/java/org/elasticsearch/alerts/AlertSerializationTest.java b/src/test/java/org/elasticsearch/alerts/AlertSerializationTest.java index 326df063e32..59b43ea745f 100644 --- a/src/test/java/org/elasticsearch/alerts/AlertSerializationTest.java +++ b/src/test/java/org/elasticsearch/alerts/AlertSerializationTest.java @@ -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()); diff --git a/src/test/java/org/elasticsearch/alerts/AlertThrottleTests.java b/src/test/java/org/elasticsearch/alerts/AlertThrottleTests.java index 6621665bbe8..bb37a98d11b 100644 --- a/src/test/java/org/elasticsearch/alerts/AlertThrottleTests.java +++ b/src/test/java/org/elasticsearch/alerts/AlertThrottleTests.java @@ -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)); diff --git a/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java b/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java index b772d2aa48c..02745adc080 100644 --- a/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java +++ b/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java @@ -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());