standardized script/template model variables
Now `Variables.createCtxModel` is responsible for creating the model for scripts & templates across the board. Accessing the payload, alert name, fired time and scheduled fire time is done via the `ctx.` prefix. Original commit: elastic/x-pack-elasticsearch@443ac17579
This commit is contained in:
parent
43f11cbb3d
commit
f9765fd393
|
@ -8,7 +8,6 @@ package org.elasticsearch.alerts.actions;
|
|||
import org.elasticsearch.alerts.ExecutionContext;
|
||||
import org.elasticsearch.alerts.Payload;
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.collect.ImmutableMap;
|
||||
import org.elasticsearch.common.logging.ESLogger;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
|
@ -20,9 +19,6 @@ import java.io.IOException;
|
|||
*/
|
||||
public abstract class Action<R extends Action.Result> implements ToXContent {
|
||||
|
||||
public static final String ALERT_NAME_VARIABLE = "alert_name";
|
||||
public static final String PAYLOAD_VARIABLE = "payload";
|
||||
|
||||
protected final ESLogger logger;
|
||||
|
||||
protected Action(ESLogger logger) {
|
||||
|
@ -39,12 +35,6 @@ public abstract class Action<R extends Action.Result> implements ToXContent {
|
|||
*/
|
||||
public abstract R execute(ExecutionContext context, Payload payload) throws IOException;
|
||||
|
||||
protected static ImmutableMap<String, Object> templateModel(ExecutionContext ctx, Payload payload) {
|
||||
return ImmutableMap.<String, Object>builder()
|
||||
.put(ALERT_NAME_VARIABLE, ctx.alert().name())
|
||||
.put(PAYLOAD_VARIABLE, payload.data())
|
||||
.build();
|
||||
}
|
||||
/**
|
||||
* Parses xcontent to a concrete action of the same type.
|
||||
*/
|
||||
|
|
|
@ -10,9 +10,9 @@ import org.elasticsearch.alerts.Payload;
|
|||
import org.elasticsearch.alerts.actions.Action;
|
||||
import org.elasticsearch.alerts.actions.ActionSettingsException;
|
||||
import org.elasticsearch.alerts.actions.email.service.*;
|
||||
import org.elasticsearch.alerts.support.Variables;
|
||||
import org.elasticsearch.alerts.support.template.Template;
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.collect.ImmutableMap;
|
||||
import org.elasticsearch.common.component.AbstractComponent;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.logging.ESLogger;
|
||||
|
@ -22,6 +22,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
|
|||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
|
@ -63,7 +64,7 @@ public class EmailAction extends Action<EmailAction.Result> {
|
|||
|
||||
@Override
|
||||
public Result execute(ExecutionContext ctx, Payload payload) throws IOException {
|
||||
ImmutableMap<String, Object> model = templateModel(ctx, payload);
|
||||
Map<String, Object> model = Variables.createCtxModel(ctx, payload);
|
||||
|
||||
Email.Builder email = Email.builder()
|
||||
.id(ctx.id())
|
||||
|
|
|
@ -12,11 +12,11 @@ import org.elasticsearch.alerts.actions.Action;
|
|||
import org.elasticsearch.alerts.actions.ActionException;
|
||||
import org.elasticsearch.alerts.actions.ActionSettingsException;
|
||||
import org.elasticsearch.alerts.support.Script;
|
||||
import org.elasticsearch.alerts.support.Variables;
|
||||
import org.elasticsearch.alerts.support.template.Template;
|
||||
import org.elasticsearch.alerts.support.template.XContentTemplate;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.collect.ImmutableMap;
|
||||
import org.elasticsearch.common.component.AbstractComponent;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.logging.ESLogger;
|
||||
|
@ -27,6 +27,7 @@ import org.elasticsearch.common.xcontent.XContentParser;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*/
|
||||
|
@ -55,10 +56,7 @@ public class WebhookAction extends Action<WebhookAction.Result> {
|
|||
|
||||
@Override
|
||||
public Result execute(ExecutionContext ctx, Payload payload) throws IOException {
|
||||
ImmutableMap<String, Object> model = ImmutableMap.<String, Object>builder()
|
||||
.put(ALERT_NAME_VARIABLE, ctx.alert().name())
|
||||
.put(PAYLOAD_VARIABLE, payload.data())
|
||||
.build();
|
||||
Map<String, Object> model = Variables.createCtxModel(ctx, payload);
|
||||
String urlText = url.render(model);
|
||||
String bodyText = body != null ? body.render(model) : XContentTemplate.YAML.render(model);
|
||||
try {
|
||||
|
|
|
@ -55,7 +55,7 @@ public class ScriptCondition extends Condition<ScriptCondition.Result> {
|
|||
public Result execute(ExecutionContext ctx) throws IOException {
|
||||
ImmutableMap<String, Object> model = ImmutableMap.<String, Object>builder()
|
||||
.putAll(script.params())
|
||||
.put(Variables.PAYLOAD, ctx.payload().data())
|
||||
.putAll(Variables.createCtxModel(ctx, ctx.payload()))
|
||||
.build();
|
||||
ExecutableScript executable = scriptService.executable(script.lang(), script.script(), script.type(), model);
|
||||
Object value = executable.run();
|
||||
|
|
|
@ -5,13 +5,37 @@
|
|||
*/
|
||||
package org.elasticsearch.alerts.support;
|
||||
|
||||
import org.elasticsearch.alerts.ExecutionContext;
|
||||
import org.elasticsearch.alerts.Payload;
|
||||
import org.elasticsearch.common.joda.time.DateTime;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public final class Variables {
|
||||
|
||||
public static final String PAYLOAD = "payload";
|
||||
public static final String CTX = "ctx";
|
||||
public static final String ALERT_NAME = "alert_name";
|
||||
public static final String FIRE_TIME = "fire_time";
|
||||
public static final String SCHEDULED_FIRE_TIME = "scheduled_fire_time";
|
||||
public static final String PAYLOAD = "payload";
|
||||
|
||||
public static Map<String, Object> createCtxModel(ExecutionContext ctx, Payload payload) {
|
||||
return createCtxModel(ctx.alert().name(), ctx.fireTime(), ctx.scheduledTime(), payload);
|
||||
}
|
||||
|
||||
public static Map<String, Object> createCtxModel(String alertName, DateTime fireTime, DateTime scheduledTime, Payload payload) {
|
||||
Map<String, Object> vars = new HashMap<>();
|
||||
vars.put(ALERT_NAME, alertName);
|
||||
vars.put(FIRE_TIME, fireTime);
|
||||
vars.put(SCHEDULED_FIRE_TIME, scheduledTime);
|
||||
vars.put(PAYLOAD, payload.data());
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put(CTX, vars);
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,6 +19,8 @@ import java.io.IOException;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.elasticsearch.alerts.support.Variables.createCtxModel;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
@ -47,7 +49,7 @@ public class ScriptTransform extends Transform {
|
|||
public Result apply(ExecutionContext ctx, Payload payload) throws IOException {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.putAll(script.params());
|
||||
model.putAll(createModel(ctx, payload));
|
||||
model.putAll(createCtxModel(ctx, payload));
|
||||
ExecutableScript executable = scriptService.executable(script.lang(), script.script(), script.type(), model);
|
||||
Object value = executable.run();
|
||||
if (!(value instanceof Map)) {
|
||||
|
|
|
@ -35,6 +35,7 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
|
||||
import static org.elasticsearch.alerts.support.AlertsDateUtils.formatDate;
|
||||
import static org.elasticsearch.alerts.support.Variables.createCtxModel;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -81,11 +82,11 @@ public class SearchTransform extends Transform {
|
|||
.indices(requestPrototype.indices());
|
||||
if (Strings.hasLength(requestPrototype.source())) {
|
||||
String requestSource = XContentHelper.convertToJson(requestPrototype.source(), false);
|
||||
ExecutableScript script = scriptService.executable("mustache", requestSource, ScriptService.ScriptType.INLINE, createModel(ctx, payload));
|
||||
ExecutableScript script = scriptService.executable("mustache", requestSource, ScriptService.ScriptType.INLINE, createCtxModel(ctx, payload));
|
||||
request.source((BytesReference) script.unwrap(script.run()), false);
|
||||
} else if (requestPrototype.templateName() != null) {
|
||||
MapBuilder<String, String> templateParams = MapBuilder.newMapBuilder(requestPrototype.templateParams())
|
||||
.putAll(flatten(createModel(ctx, payload)));
|
||||
.putAll(flatten(createCtxModel(ctx, payload)));
|
||||
request.templateParams(templateParams.map());
|
||||
request.templateName(requestPrototype.templateName());
|
||||
request.templateType(requestPrototype.templateType());
|
||||
|
|
|
@ -7,14 +7,11 @@ package org.elasticsearch.alerts.transform;
|
|||
|
||||
import org.elasticsearch.alerts.ExecutionContext;
|
||||
import org.elasticsearch.alerts.Payload;
|
||||
import org.elasticsearch.alerts.support.Variables;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -42,14 +39,6 @@ public abstract class Transform implements ToXContent {
|
|||
|
||||
public abstract Result apply(ExecutionContext ctx, Payload payload) throws IOException;
|
||||
|
||||
protected static Map<String, Object> createModel(ExecutionContext ctx, Payload payload) {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put(Variables.SCHEDULED_FIRE_TIME, ctx.scheduledTime());
|
||||
model.put(Variables.FIRE_TIME, ctx.fireTime());
|
||||
model.put(Variables.PAYLOAD, payload.data());
|
||||
return model;
|
||||
}
|
||||
|
||||
public static class Result {
|
||||
|
||||
private final String type;
|
||||
|
|
|
@ -17,6 +17,7 @@ import org.elasticsearch.alerts.support.template.Template;
|
|||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.collect.ImmutableMap;
|
||||
import org.elasticsearch.common.joda.time.DateTime;
|
||||
import org.elasticsearch.common.joda.time.DateTimeZone;
|
||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
|
@ -75,16 +76,23 @@ public class EmailActionTests extends ElasticsearchTestCase {
|
|||
}
|
||||
};
|
||||
|
||||
DateTime now = new DateTime(DateTimeZone.UTC);
|
||||
|
||||
String ctxId = randomAsciiOfLength(5);
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
when(ctx.id()).thenReturn(ctxId);
|
||||
Alert alert = mock(Alert.class);
|
||||
when(alert.name()).thenReturn("alert1");
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
when(ctx.fireTime()).thenReturn(now);
|
||||
when(ctx.scheduledTime()).thenReturn(now);
|
||||
|
||||
Map<String, Object> expectedModel = ImmutableMap.<String, Object>builder()
|
||||
.put("alert_name", "alert1")
|
||||
.put("payload", data)
|
||||
.put("ctx", ImmutableMap.<String, Object>builder()
|
||||
.put("alert_name", "alert1")
|
||||
.put("payload", data)
|
||||
.put("fire_time", now)
|
||||
.put("scheduled_fire_time", now).build())
|
||||
.build();
|
||||
|
||||
when(subject.render(expectedModel)).thenReturn("_subject");
|
||||
|
|
|
@ -7,6 +7,7 @@ package org.elasticsearch.alerts.condition.script;
|
|||
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.search.ShardSearchFailure;
|
||||
import org.elasticsearch.alerts.Alert;
|
||||
import org.elasticsearch.alerts.AlertsSettingsException;
|
||||
import org.elasticsearch.alerts.ExecutionContext;
|
||||
import org.elasticsearch.alerts.Payload;
|
||||
|
@ -14,6 +15,7 @@ import org.elasticsearch.alerts.condition.ConditionException;
|
|||
import org.elasticsearch.alerts.support.Script;
|
||||
import org.elasticsearch.alerts.support.init.proxy.ScriptServiceProxy;
|
||||
import org.elasticsearch.common.collect.ImmutableMap;
|
||||
import org.elasticsearch.common.joda.time.DateTime;
|
||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
|
@ -58,9 +60,14 @@ public class ScriptConditionTests extends ElasticsearchTestCase {
|
|||
@Test
|
||||
public void testExecute() throws Exception {
|
||||
ScriptServiceProxy scriptService = getScriptServiceProxy(tp);
|
||||
ScriptCondition condition = new ScriptCondition(logger, scriptService, new Script("payload.hits.total > 1"));
|
||||
ScriptCondition condition = new ScriptCondition(logger, scriptService, new Script("ctx.payload.hits.total > 1"));
|
||||
SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 500l, new ShardSearchFailure[0]);
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
Alert alert = mock(Alert.class);
|
||||
when(alert.name()).thenReturn("_name");
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
when(ctx.scheduledTime()).thenReturn(new DateTime());
|
||||
when(ctx.fireTime()).thenReturn(new DateTime());
|
||||
when(ctx.payload()).thenReturn(new Payload.ActionResponse(response));
|
||||
assertFalse(condition.execute(ctx).met());
|
||||
}
|
||||
|
@ -68,10 +75,15 @@ public class ScriptConditionTests extends ElasticsearchTestCase {
|
|||
@Test
|
||||
public void testExecute_MergedParams() throws Exception {
|
||||
ScriptServiceProxy scriptService = getScriptServiceProxy(tp);
|
||||
Script script = new Script("payload.hits.total > threshold", ScriptService.ScriptType.INLINE, ScriptService.DEFAULT_LANG, ImmutableMap.<String, Object>of("threshold", 1));
|
||||
Script script = new Script("ctx.payload.hits.total > threshold", ScriptService.ScriptType.INLINE, ScriptService.DEFAULT_LANG, ImmutableMap.<String, Object>of("threshold", 1));
|
||||
ScriptCondition condition = new ScriptCondition(logger, scriptService, script);
|
||||
SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 500l, new ShardSearchFailure[0]);
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
Alert alert = mock(Alert.class);
|
||||
when(alert.name()).thenReturn("_name");
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
when(ctx.scheduledTime()).thenReturn(new DateTime());
|
||||
when(ctx.fireTime()).thenReturn(new DateTime());
|
||||
when(ctx.payload()).thenReturn(new Payload.ActionResponse(response));
|
||||
assertFalse(condition.execute(ctx).met());
|
||||
}
|
||||
|
@ -80,13 +92,18 @@ public class ScriptConditionTests extends ElasticsearchTestCase {
|
|||
public void testParser_Valid() throws Exception {
|
||||
ScriptCondition.Parser conditionParser = new ScriptCondition.Parser(ImmutableSettings.settingsBuilder().build(), getScriptServiceProxy(tp));
|
||||
|
||||
XContentBuilder builder = createConditionContent("payload.hits.total > 1", null, null);
|
||||
XContentBuilder builder = createConditionContent("ctx.payload.hits.total > 1", null, null);
|
||||
XContentParser parser = XContentFactory.xContent(builder.bytes()).createParser(builder.bytes());
|
||||
parser.nextToken();
|
||||
ScriptCondition condition = conditionParser.parse(parser);
|
||||
|
||||
SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 500l, new ShardSearchFailure[0]);
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
Alert alert = mock(Alert.class);
|
||||
when(alert.name()).thenReturn("_name");
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
when(ctx.scheduledTime()).thenReturn(new DateTime());
|
||||
when(ctx.fireTime()).thenReturn(new DateTime());
|
||||
when(ctx.payload()).thenReturn(new Payload.ActionResponse(response));
|
||||
|
||||
assertFalse(condition.execute(ctx).met());
|
||||
|
@ -98,6 +115,9 @@ public class ScriptConditionTests extends ElasticsearchTestCase {
|
|||
condition = conditionParser.parse(parser);
|
||||
|
||||
reset(ctx);
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
when(ctx.scheduledTime()).thenReturn(new DateTime());
|
||||
when(ctx.fireTime()).thenReturn(new DateTime());
|
||||
when(ctx.payload()).thenReturn(new Payload.ActionResponse(response));
|
||||
|
||||
assertTrue(condition.execute(ctx).met());
|
||||
|
|
|
@ -116,8 +116,7 @@ public class SearchInputTests extends ElasticsearchIntegrationTest {
|
|||
data.put("baz", new ArrayList<String>() );
|
||||
|
||||
SearchSourceBuilder searchSourceBuilder = searchSource().query(
|
||||
filteredQuery(matchQuery("event_type", "a"), rangeFilter("_timestamp").from("{{" + Variables.SCHEDULED_FIRE_TIME + "}}||-30s").to("{{" + Variables.SCHEDULED_FIRE_TIME + "}}"))
|
||||
);
|
||||
filteredQuery(matchQuery("event_type", "a"), rangeFilter("_timestamp").from("{{" + Variables.SCHEDULED_FIRE_TIME + "}}||-30s").to("{{" + Variables.SCHEDULED_FIRE_TIME + "}}")));
|
||||
SearchRequest request = client()
|
||||
.prepareSearch()
|
||||
.setSearchType(SearchInput.DEFAULT_SEARCH_TYPE)
|
||||
|
|
|
@ -45,7 +45,7 @@ public class AlertMetadataTests extends AbstractAlertsIntegrationTests {
|
|||
.source(alertSourceBuilder()
|
||||
.schedule(cron("0/5 * * * * ? *"))
|
||||
.input(searchInput(AlertsTestUtils.newInputSearchRequest("my-index").source(searchSource().query(matchAllQuery()))))
|
||||
.condition(scriptCondition("payload.hits.total == 1"))
|
||||
.condition(scriptCondition("ctx.payload.hits.total == 1"))
|
||||
.metadata(metadata))
|
||||
.get();
|
||||
// Wait for a no action entry to be added. (the condition search request will not match, because there are no docs in my-index)
|
||||
|
|
|
@ -57,7 +57,7 @@ public class AlertStatsTests extends AbstractAlertsIntegrationTests {
|
|||
assertThat(response.getAlertManagerStarted(), equalTo(AlertsService.State.STARTED));
|
||||
|
||||
SearchRequest searchRequest = AlertsTestUtils.newInputSearchRequest("my-index").source(searchSource().query(termQuery("field", "value")));
|
||||
BytesReference alertSource = createAlertSource("* * * * * ? *", searchRequest, "payload.hits.total == 1");
|
||||
BytesReference alertSource = createAlertSource("* * * * * ? *", searchRequest, "ctx.payload.hits.total == 1");
|
||||
alertClient().preparePutAlert("testAlert")
|
||||
.source(alertSource)
|
||||
.get();
|
||||
|
|
|
@ -58,7 +58,7 @@ public class AlertThrottleTests extends AbstractAlertsIntegrationTests {
|
|||
.source(alertSourceBuilder()
|
||||
.schedule(cron("0/5 * * * * ? *"))
|
||||
.input(searchInput(matchAllRequest().indices("test-index")))
|
||||
.condition(scriptCondition("payload.hits.total > 0"))
|
||||
.condition(scriptCondition("ctx.payload.hits.total > 0"))
|
||||
.transform(searchTransform(matchAllRequest().indices("test-index")))
|
||||
.addAction(ActionBuilders.indexAction("action-index", "action-type"))
|
||||
.throttlePeriod(TimeValue.timeValueMillis(0)))
|
||||
|
@ -129,7 +129,7 @@ public class AlertThrottleTests extends AbstractAlertsIntegrationTests {
|
|||
.source(alertSourceBuilder()
|
||||
.schedule(cron("0/5 * * * * ? *"))
|
||||
.input(searchInput(matchAllRequest().indices("test-index")))
|
||||
.condition(scriptCondition("payload.hits.total > 0"))
|
||||
.condition(scriptCondition("ctx.payload.hits.total > 0"))
|
||||
.transform(searchTransform(matchAllRequest().indices("test-index")))
|
||||
.addAction(ActionBuilders.indexAction("action-index", "action-type"))
|
||||
.throttlePeriod(TimeValue.timeValueSeconds(10)))
|
||||
|
|
|
@ -64,7 +64,7 @@ public class BasicAlertsTests extends AbstractAlertsIntegrationTests {
|
|||
.source(alertSourceBuilder()
|
||||
.schedule(interval(5, IntervalSchedule.Interval.Unit.SECONDS))
|
||||
.input(searchInput(searchRequest))
|
||||
.condition(scriptCondition("payload.hits.total == 1")))
|
||||
.condition(scriptCondition("ctx.payload.hits.total == 1")))
|
||||
.get();
|
||||
assertAlertWithMinimumPerformedActionsCount("my-first-alert", 1);
|
||||
|
||||
|
@ -81,7 +81,7 @@ public class BasicAlertsTests extends AbstractAlertsIntegrationTests {
|
|||
.source(alertSourceBuilder()
|
||||
.schedule(interval(5, IntervalSchedule.Interval.Unit.SECONDS))
|
||||
.input(searchInput(searchRequest))
|
||||
.condition(scriptCondition("payload.hits.total == 1")))
|
||||
.condition(scriptCondition("ctx.payload.hits.total == 1")))
|
||||
.get();
|
||||
|
||||
// The alert's condition won't meet because there is no data that matches with the query
|
||||
|
@ -101,7 +101,7 @@ public class BasicAlertsTests extends AbstractAlertsIntegrationTests {
|
|||
.source(alertSourceBuilder()
|
||||
.schedule(interval(5, IntervalSchedule.Interval.Unit.SECONDS))
|
||||
.input(searchInput(searchRequest))
|
||||
.condition(scriptCondition("payload.hits.total == 1")))
|
||||
.condition(scriptCondition("ctx.payload.hits.total == 1")))
|
||||
.get();
|
||||
assertThat(indexResponse.indexResponse().isCreated(), is(true));
|
||||
|
||||
|
@ -175,7 +175,7 @@ public class BasicAlertsTests extends AbstractAlertsIntegrationTests {
|
|||
.source(alertSourceBuilder()
|
||||
.schedule(interval(5, IntervalSchedule.Interval.Unit.SECONDS))
|
||||
.input(searchInput(searchRequest))
|
||||
.condition(scriptCondition("payload.hits?.hits[0]._score == 1.0")))
|
||||
.condition(scriptCondition("ctx.payload.hits?.hits[0]._score == 1.0")))
|
||||
.get();
|
||||
assertThat(indexResponse.indexResponse().isCreated(), is(true));
|
||||
assertAlertWithMinimumPerformedActionsCount("my-first-alert", 1);
|
||||
|
@ -193,17 +193,17 @@ public class BasicAlertsTests extends AbstractAlertsIntegrationTests {
|
|||
|
||||
|
||||
alertClient().preparePutAlert("1")
|
||||
.source(source.condition(scriptCondition("payload.hits.total == 1")))
|
||||
.source(source.condition(scriptCondition("ctx.payload.hits.total == 1")))
|
||||
.get();
|
||||
assertAlertWithMinimumPerformedActionsCount("1", 0, false);
|
||||
|
||||
alertClient().preparePutAlert("1")
|
||||
.source(source.condition(scriptCondition("payload.hits.total == 0")))
|
||||
.source(source.condition(scriptCondition("ctx.payload.hits.total == 0")))
|
||||
.get();
|
||||
assertAlertWithMinimumPerformedActionsCount("1", 1, false);
|
||||
|
||||
alertClient().preparePutAlert("1")
|
||||
.source(source.schedule(cron("0/5 * * * * ? 2020")).condition(scriptCondition("payload.hits.total == 0")))
|
||||
.source(source.schedule(cron("0/5 * * * * ? 2020")).condition(scriptCondition("ctx.payload.hits.total == 0")))
|
||||
.get();
|
||||
|
||||
Thread.sleep(5000);
|
||||
|
@ -250,7 +250,7 @@ public class BasicAlertsTests extends AbstractAlertsIntegrationTests {
|
|||
.source(alertSourceBuilder()
|
||||
.schedule(cron("* 0/1 * * * ? *"))
|
||||
.input(searchInput(searchRequest))
|
||||
.condition(scriptCondition("payload.aggregations.rate.buckets[0]?.doc_count > 5"))
|
||||
.condition(scriptCondition("ctx.payload.aggregations.rate.buckets[0]?.doc_count > 5"))
|
||||
.addAction(indexAction("my-index", "trail")))
|
||||
.get();
|
||||
|
||||
|
@ -299,7 +299,7 @@ public class BasicAlertsTests extends AbstractAlertsIntegrationTests {
|
|||
|
||||
alertClient().prepareDeleteAlert(alertName).get();
|
||||
alertClient().preparePutAlert(alertName)
|
||||
.source(createAlertSource(String.format(Locale.ROOT, "0/%s * * * * ? *", (scheduleTimeInMs / 1000)), request, "return payload.hits.total >= 3"))
|
||||
.source(createAlertSource(String.format(Locale.ROOT, "0/%s * * * * ? *", (scheduleTimeInMs / 1000)), request, "return ctx.payload.hits.total >= 3"))
|
||||
.get();
|
||||
|
||||
long time1 = System.currentTimeMillis();
|
||||
|
|
|
@ -55,7 +55,7 @@ public class BootStrapTests extends AbstractAlertsIntegrationTests {
|
|||
ensureAlertingStarted();
|
||||
|
||||
SearchRequest searchRequest = AlertsTestUtils.newInputSearchRequest("my-index").source(searchSource().query(termQuery("field", "value")));
|
||||
BytesReference alertSource = createAlertSource("0 0/5 * * * ? *", searchRequest, "payload.hits.total == 1");
|
||||
BytesReference alertSource = createAlertSource("0 0/5 * * * ? *", searchRequest, "ctx.payload.hits.total == 1");
|
||||
client().prepareIndex(AlertsStore.ALERT_INDEX, AlertsStore.ALERT_TYPE, "my-first-alert")
|
||||
.setSource(alertSource)
|
||||
.setConsistencyLevel(WriteConsistencyLevel.ALL)
|
||||
|
|
|
@ -66,7 +66,7 @@ public class NoMasterNodeTests extends AbstractAlertsIntegrationTests {
|
|||
// Have a sample document in the index, the alert is going to evaluate
|
||||
client().prepareIndex("my-index", "my-type").setSource("field", "value").get();
|
||||
SearchRequest searchRequest = AlertsTestUtils.newInputSearchRequest("my-index").source(searchSource().query(termQuery("field", "value")));
|
||||
BytesReference alertSource = createAlertSource("0/5 * * * * ? *", searchRequest, "payload.hits.total == 1");
|
||||
BytesReference alertSource = createAlertSource("0/5 * * * * ? *", searchRequest, "ctx.payload.hits.total == 1");
|
||||
alertClient().preparePutAlert("my-first-alert")
|
||||
.source(alertSource)
|
||||
.get();
|
||||
|
@ -114,7 +114,7 @@ public class NoMasterNodeTests extends AbstractAlertsIntegrationTests {
|
|||
for (int i = 1; i <= numberOfAlerts; i++) {
|
||||
String alertName = "alert" + i;
|
||||
SearchRequest searchRequest = AlertsTestUtils.newInputSearchRequest("my-index").source(searchSource().query(termQuery("field", "value")));
|
||||
BytesReference alertSource = createAlertSource("0/5 * * * * ? *", searchRequest, "payload.hits.total == 1");
|
||||
BytesReference alertSource = createAlertSource("0/5 * * * * ? *", searchRequest, "ctx.payload.hits.total == 1");
|
||||
alertClient().preparePutAlert(alertName)
|
||||
.source(alertSource)
|
||||
.get();
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
package org.elasticsearch.alerts.transform;
|
||||
|
||||
import org.elasticsearch.alerts.Alert;
|
||||
import org.elasticsearch.alerts.ExecutionContext;
|
||||
import org.elasticsearch.alerts.Payload;
|
||||
import org.elasticsearch.alerts.support.Script;
|
||||
|
@ -45,14 +46,13 @@ public class ScriptTransformTests extends ElasticsearchTestCase {
|
|||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
when(ctx.scheduledTime()).thenReturn(now);
|
||||
when(ctx.fireTime()).thenReturn(now);
|
||||
Alert alert = mock(Alert.class);
|
||||
when(alert.name()).thenReturn("_name");
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
|
||||
Payload payload = new Payload.Simple(ImmutableMap.<String, Object>builder().put("key", "value").build());
|
||||
|
||||
Map<String, Object> model = ImmutableMap.<String, Object>builder()
|
||||
.put(Variables.PAYLOAD, payload.data())
|
||||
.put(Variables.FIRE_TIME, now)
|
||||
.put(Variables.SCHEDULED_FIRE_TIME, now)
|
||||
.build();
|
||||
Map<String, Object> model = Variables.createCtxModel(ctx, payload);
|
||||
|
||||
Map<String, Object> transformed = ImmutableMap.<String, Object>builder()
|
||||
.put("key", "value")
|
||||
|
|
|
@ -8,6 +8,7 @@ package org.elasticsearch.alerts.transform;
|
|||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.search.SearchType;
|
||||
import org.elasticsearch.alerts.Alert;
|
||||
import org.elasticsearch.alerts.ExecutionContext;
|
||||
import org.elasticsearch.alerts.Payload;
|
||||
import org.elasticsearch.alerts.support.Variables;
|
||||
|
@ -63,6 +64,10 @@ public class SearchTransformTests extends AbstractAlertsSingleNodeTests {
|
|||
DateTime now = new DateTime();
|
||||
when(ctx.scheduledTime()).thenReturn(now);
|
||||
when(ctx.fireTime()).thenReturn(now);
|
||||
Alert alert = mock(Alert.class);
|
||||
when(alert.name()).thenReturn("_name");
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
|
||||
|
||||
Payload payload = new Payload.Simple(new HashMap<String, Object>());
|
||||
|
||||
|
@ -115,15 +120,18 @@ public class SearchTransformTests extends AbstractAlertsSingleNodeTests {
|
|||
refresh();
|
||||
|
||||
SearchRequest request = Requests.searchRequest("idx").source(searchSource().query(filteredQuery(matchAllQuery(), boolFilter()
|
||||
.must(rangeFilter("date").gt("{{" + Variables.SCHEDULED_FIRE_TIME + "}}"))
|
||||
.must(rangeFilter("date").lt("{{" + Variables.FIRE_TIME + "}}"))
|
||||
.must(termFilter("value", "{{" + Variables.PAYLOAD + ".value}}")))));
|
||||
.must(rangeFilter("date").gt("{{" + Variables.CTX + "." + Variables.SCHEDULED_FIRE_TIME + "}}"))
|
||||
.must(rangeFilter("date").lt("{{" + Variables.CTX + "." + Variables.FIRE_TIME + "}}"))
|
||||
.must(termFilter("value", "{{" + Variables.CTX + "." + Variables.PAYLOAD + ".value}}")))));
|
||||
|
||||
SearchTransform transform = new SearchTransform(logger, scriptService(), ClientProxy.of(client()), request);
|
||||
|
||||
ExecutionContext ctx = mock(ExecutionContext.class);
|
||||
when(ctx.scheduledTime()).thenReturn(parseDate("2015-01-01T00:00:00"));
|
||||
when(ctx.fireTime()).thenReturn(parseDate("2015-01-04T00:00:00"));
|
||||
Alert alert = mock(Alert.class);
|
||||
when(alert.name()).thenReturn("_name");
|
||||
when(ctx.alert()).thenReturn(alert);
|
||||
|
||||
Payload payload = new Payload.Simple(ImmutableMap.<String, Object>builder()
|
||||
.put("value", "val_3")
|
||||
|
|
Loading…
Reference in New Issue