Skip to content

Commit 767bd75

Browse files
committed
Populate results and triggered docs fields in document level trigger execution context
Signed-off-by: Surya Sashank Nistala <[email protected]>
1 parent fea6b4a commit 767bd75

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

alerting/src/main/kotlin/org/opensearch/alerting/DocumentLevelMonitorRunner.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ object DocumentLevelMonitorRunner : MonitorRunner() {
222222
queryToDocIds: Map<DocLevelQuery, Set<String>>,
223223
dryrun: Boolean
224224
): DocumentLevelTriggerRunResult {
225-
val triggerCtx = DocumentLevelTriggerExecutionContext(monitor, trigger)
225+
val triggerCtx = DocumentLevelTriggerExecutionContext(monitor, trigger, monitorResult)
226226
val triggerResult = monitorCtx.triggerService!!.runDocLevelTrigger(monitor, trigger, queryToDocIds)
227227

228228
val findings = mutableListOf<String>()

alerting/src/main/kotlin/org/opensearch/alerting/script/DocumentLevelTriggerExecutionContext.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ package org.opensearch.alerting.script
77

88
import org.opensearch.alerting.model.Alert
99
import org.opensearch.alerting.model.DocumentLevelTrigger
10+
import org.opensearch.alerting.model.DocumentLevelTriggerRunResult
1011
import org.opensearch.alerting.model.Monitor
12+
import org.opensearch.alerting.model.MonitorRunResult
1113
import java.time.Instant
1214

1315
data class DocumentLevelTriggerExecutionContext(
@@ -25,10 +27,11 @@ data class DocumentLevelTriggerExecutionContext(
2527
constructor(
2628
monitor: Monitor,
2729
trigger: DocumentLevelTrigger,
30+
monitorRunResult: MonitorRunResult<DocumentLevelTriggerRunResult>,
2831
alerts: List<Alert> = listOf()
2932
) : this(
30-
monitor, trigger, emptyList(), Instant.now(), Instant.now(),
31-
alerts, emptyList(), emptyList(), null
33+
monitor, trigger, monitorRunResult.inputResults.results, monitorRunResult.periodStart, monitorRunResult.periodEnd,
34+
alerts, emptyList(), emptyList(), monitorRunResult.scriptContextError(trigger)
3235
)
3336

3437
/**

alerting/src/test/kotlin/org/opensearch/alerting/DocumentMonitorRunnerIT.kt

+90
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,96 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() {
9999
assertTrue("Incorrect search result", matchingDocsToQuery.contains("5|$testIndex"))
100100
}
101101

102+
fun `test execute monitor yields results in alert action context`() {
103+
104+
val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now().truncatedTo(MILLIS))
105+
val testDoc = """{
106+
"message" : "This is an error from IAD region",
107+
"test_strict_date_time" : "$testTime",
108+
"test_field" : "us-west-2"
109+
}"""
110+
111+
val index = createTestIndex()
112+
113+
val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3")
114+
val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery))
115+
116+
val action = randomAction(template = randomTemplateScript("{{ctx.results.0}}"), destinationId = createDestination().id)
117+
val monitor = randomDocumentLevelMonitor(
118+
inputs = listOf(docLevelInput),
119+
triggers = listOf(randomDocumentLevelTrigger(condition = ALWAYS_RUN, actions = listOf(action)))
120+
)
121+
122+
indexDoc(index, "1", testDoc)
123+
124+
val response = executeMonitor(monitor, params = DRYRUN_MONITOR)
125+
126+
val output = entityAsMap(response)
127+
assertEquals(monitor.name, output["monitor_name"])
128+
129+
assertEquals(1, output.objectMap("trigger_results").values.size)
130+
131+
for (triggerResult in output.objectMap("trigger_results").values) {
132+
assertEquals(1, triggerResult.objectMap("action_results").values.size)
133+
for (alertActionResult in triggerResult.objectMap("action_results").values) {
134+
for (actionResult in alertActionResult.values) {
135+
@Suppress("UNCHECKED_CAST") val actionOutput = (actionResult as Map<String, Map<String, String>>)["output"]
136+
as Map<String, String>
137+
assertTrue(actionOutput["subject"].toString().contains(docQuery.id))
138+
assertTrue(actionOutput["message"].toString().contains(docQuery.id))
139+
}
140+
}
141+
}
142+
val alerts = searchAlerts(monitor)
143+
assertEquals("Alert saved for test monitor", 0, alerts.size)
144+
}
145+
146+
fun `test execute monitor yields triggeredDocs in alert action context`() {
147+
148+
val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now().truncatedTo(MILLIS))
149+
val testDoc = """{
150+
"message" : "This is an error from IAD region",
151+
"test_strict_date_time" : "$testTime",
152+
"test_field" : "us-west-2"
153+
}"""
154+
155+
val index = createTestIndex()
156+
157+
val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3")
158+
val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery))
159+
160+
val action = randomAction(template = randomTemplateScript("{{ctx.triggeredDocs}}"), destinationId = createDestination().id)
161+
val monitor = randomDocumentLevelMonitor(
162+
inputs = listOf(docLevelInput),
163+
triggers = listOf(randomDocumentLevelTrigger(condition = ALWAYS_RUN, actions = listOf(action)))
164+
)
165+
166+
val id = "1"
167+
indexDoc(index, id, testDoc)
168+
169+
val response = executeMonitor(monitor, params = DRYRUN_MONITOR)
170+
171+
val output = entityAsMap(response)
172+
assertEquals(monitor.name, output["monitor_name"])
173+
174+
assertEquals(1, output.objectMap("trigger_results").values.size)
175+
176+
for (triggerResult in output.objectMap("trigger_results").values) {
177+
assertEquals(1, triggerResult.objectMap("action_results").values.size)
178+
for (alertActionResult in triggerResult.objectMap("action_results").values) {
179+
assertEquals(alertActionResult.values.size, 1)
180+
for (actionResult in alertActionResult.values) {
181+
@Suppress("UNCHECKED_CAST") val actionOutput = (actionResult as Map<String, Map<String, String>>)["output"]
182+
as Map<String, String>
183+
assertTrue(actionOutput["subject"].toString().contains("0=$id|$index"))
184+
assertTrue(actionOutput["message"].toString().contains("0=$id|$index"))
185+
}
186+
}
187+
}
188+
val alerts = searchAlerts(monitor)
189+
assertEquals("Alert saved for test monitor", 0, alerts.size)
190+
}
191+
102192
fun `test execute monitor generates alerts and findings`() {
103193
val testIndex = createTestIndex()
104194
val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now().truncatedTo(MILLIS))

0 commit comments

Comments
 (0)