Moved json field variable from AlertActionEntry to AlertActionManager and made AlertActionEntry not implement ToXContent.

Original commit: elastic/x-pack-elasticsearch@76731aee22
This commit is contained in:
Martijn van Groningen 2014-11-19 21:37:23 +01:00
parent 72e9d9e4b7
commit 43e6aa5b23
4 changed files with 17 additions and 31 deletions

View File

@ -30,7 +30,7 @@ public class AlertActionEntry implements ToXContent {
private DateTime scheduledTime;
private AlertTrigger trigger;
private List<AlertAction> actions;
private AlertActionState entryState;
private AlertActionState state;
private SearchRequest searchRequest;
/*Optional*/
@ -49,7 +49,7 @@ public class AlertActionEntry implements ToXContent {
this.scheduledTime = scheduledTime;
this.trigger = alert.trigger();
this.actions = alert.actions();
this.entryState = state;
this.state = state;
this.searchRequest = alert.getSearchRequest();
}
@ -155,12 +155,12 @@ public class AlertActionEntry implements ToXContent {
/**
* @return The current state of the alert event.
*/
public AlertActionState getEntryState() {
return entryState;
public AlertActionState getState() {
return state;
}
public void setEntryState(AlertActionState entryState) {
this.entryState = entryState;
public void setState(AlertActionState state) {
this.state = state;
}
public long getVersion() {
@ -203,7 +203,7 @@ public class AlertActionEntry implements ToXContent {
action.toXContent(historyEntry, params);
}
historyEntry.endObject();
historyEntry.field(AlertActionState.FIELD_NAME, entryState.toString());
historyEntry.field(AlertActionManager.STATE, state.toString());
if (errorMsg != null) {
historyEntry.field("error_msg", errorMsg);

View File

@ -51,6 +51,7 @@ public class AlertActionManager extends AbstractComponent {
public static final String REQUEST = "request";
public static final String RESPONSE = "response";
public static final String ACTIONS_FIELD = "actions";
public static final String STATE = "state";
public static final String ALERT_HISTORY_INDEX = ".alert_history";
public static final String ALERT_HISTORY_TYPE = "alerthistory";
@ -142,7 +143,7 @@ public class AlertActionManager extends AbstractComponent {
client.admin().indices().refresh(new RefreshRequest(ALERT_HISTORY_INDEX)).actionGet();
SearchResponse response = client.prepareSearch(ALERT_HISTORY_INDEX)
.setQuery(QueryBuilders.termQuery(AlertActionState.FIELD_NAME, AlertActionState.SEARCH_NEEDED.toString()))
.setQuery(QueryBuilders.termQuery(STATE, AlertActionState.SEARCH_NEEDED.toString()))
.setSearchType(SearchType.SCAN)
.setScroll(scrollTimeout)
.setSize(scrollSize)
@ -155,7 +156,7 @@ public class AlertActionManager extends AbstractComponent {
for (SearchHit sh : response.getHits()) {
String historyId = sh.getId();
AlertActionEntry historyEntry = parseHistory(historyId, sh.getSourceRef(), sh.version(), actionRegistry);
assert historyEntry.getEntryState() == AlertActionState.SEARCH_NEEDED;
assert historyEntry.getState() == AlertActionState.SEARCH_NEEDED;
actionsToBeProcessed.add(historyEntry);
}
response = client.prepareSearchScroll(response.getScrollId()).setScroll(scrollTimeout).get();
@ -214,8 +215,8 @@ public class AlertActionManager extends AbstractComponent {
case ERROR_MESSAGE:
entry.setErrorMsg(parser.textOrNull());
break;
case AlertActionState.FIELD_NAME:
entry.setEntryState(AlertActionState.fromString(parser.text()));
case STATE:
entry.setState(AlertActionState.fromString(parser.text()));
break;
default:
throw new ElasticsearchIllegalArgumentException("Unexpected field [" + currentFieldName + "]");
@ -257,7 +258,7 @@ public class AlertActionManager extends AbstractComponent {
}
private void updateHistoryEntry(AlertActionEntry entry, AlertActionState actionPerformed) throws IOException {
entry.setEntryState(actionPerformed);
entry.setState(actionPerformed);
IndexResponse response = client.prepareIndex(ALERT_HISTORY_INDEX, ALERT_HISTORY_TYPE, entry.getId())
.setSource(XContentFactory.jsonBuilder().value(entry))
.get();

View File

@ -6,23 +6,17 @@
package org.elasticsearch.alerts.actions;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
/**
*/
public enum AlertActionState implements ToXContent {
public enum AlertActionState {
SEARCH_NEEDED,
SEARCH_UNDERWAY,
NO_ACTION_NEEDED,
ACTION_PERFORMED,
ERROR;
public static final String FIELD_NAME = "state";
@Override
public String toString(){
switch (this) {
@ -57,13 +51,4 @@ public enum AlertActionState implements ToXContent {
throw new ElasticsearchIllegalArgumentException("Unknown value [" + s + "] for AlertHistoryState" );
}
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(FIELD_NAME);
builder.value(this.toString());
builder.endObject();
return builder;
}
}

View File

@ -109,7 +109,7 @@ public class AlertActionsTest extends ElasticsearchIntegrationTest {
builder.value(searchResponse);
builder.endObject();
builder.field(AlertActionManager.ACTIONS_FIELD, actionMap);
builder.field(AlertActionState.FIELD_NAME, AlertActionState.SEARCH_NEEDED.toString());
builder.field(AlertActionManager.STATE, AlertActionState.SEARCH_NEEDED.toString());
builder.endObject();
final AlertActionRegistry alertActionRegistry = internalCluster().getInstance(AlertActionRegistry.class, internalCluster().getMasterName());
final AlertActionManager alertManager = internalCluster().getInstance(AlertActionManager.class, internalCluster().getMasterName());
@ -120,7 +120,7 @@ public class AlertActionsTest extends ElasticsearchIntegrationTest {
assertEquals(actionEntry.isTriggered(), true);
assertEquals(actionEntry.getScheduledTime(), scheduledFireTime);
assertEquals(actionEntry.getFireTime(), fireTime);
assertEquals(actionEntry.getEntryState(), AlertActionState.SEARCH_NEEDED);
assertEquals(actionEntry.getState(), AlertActionState.SEARCH_NEEDED);
assertEquals(XContentMapValues.extractValue("hits.total", actionEntry.getSearchResponse()), 10);
}