Skip to content

Commit c507ac9

Browse files
authored
NoOpTrigger (#420)
* added noop trigger Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com>
1 parent 6af3110 commit c507ac9

File tree

6 files changed

+123
-2
lines changed

6 files changed

+123
-2
lines changed

src/main/kotlin/org/opensearch/commons/alerting/model/Alert.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,24 @@ data class Alert(
125125
aggregationResultBucket = null, findingIds = findingIds, relatedDocIds = relatedDocIds
126126
)
127127

128+
constructor(
129+
id: String = NO_ID,
130+
monitor: Monitor,
131+
trigger: NoOpTrigger,
132+
startTime: Instant,
133+
lastNotificationTime: Instant?,
134+
state: State = State.ERROR,
135+
errorMessage: String,
136+
errorHistory: List<AlertError> = mutableListOf(),
137+
schemaVersion: Int = NO_SCHEMA_VERSION
138+
) : this(
139+
id = id, monitorId = monitor.id, monitorName = monitor.name, monitorVersion = monitor.version, monitorUser = monitor.user,
140+
triggerId = trigger.id, triggerName = trigger.name, state = state, startTime = startTime,
141+
lastNotificationTime = lastNotificationTime, errorMessage = errorMessage, errorHistory = errorHistory,
142+
severity = trigger.severity, actionExecutionResults = listOf(), schemaVersion = schemaVersion,
143+
aggregationResultBucket = null, findingIds = listOf(), relatedDocIds = listOf()
144+
)
145+
128146
enum class State {
129147
ACTIVE, ACKNOWLEDGED, COMPLETED, ERROR, DELETED
130148
}

src/main/kotlin/org/opensearch/commons/alerting/model/Monitor.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ data class Monitor(
5050
// Ensure that trigger ids are unique within a monitor
5151
val triggerIds = mutableSetOf<String>()
5252
triggers.forEach { trigger ->
53+
// NoOpTrigger is only used in "Monitor Error Alerts" as a placeholder
54+
require(trigger !is NoOpTrigger)
55+
5356
require(triggerIds.add(trigger.id)) { "Duplicate trigger id: ${trigger.id}. Trigger ids must be unique." }
5457
// Verify Trigger type based on Monitor type
5558
when (monitorType) {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.opensearch.commons.alerting.model
2+
3+
import org.opensearch.common.CheckedFunction
4+
import org.opensearch.common.UUIDs
5+
import org.opensearch.common.io.stream.StreamInput
6+
import org.opensearch.common.io.stream.StreamOutput
7+
import org.opensearch.common.xcontent.XContentParserUtils
8+
import org.opensearch.commons.alerting.model.action.Action
9+
import org.opensearch.core.ParseField
10+
import org.opensearch.core.xcontent.NamedXContentRegistry
11+
import org.opensearch.core.xcontent.ToXContent
12+
import org.opensearch.core.xcontent.XContentBuilder
13+
import org.opensearch.core.xcontent.XContentParser
14+
import java.io.IOException
15+
16+
data class NoOpTrigger(
17+
override val id: String = UUIDs.base64UUID(),
18+
override val name: String = "NoOp trigger",
19+
override val severity: String = "",
20+
override val actions: List<Action> = listOf(),
21+
) : Trigger {
22+
23+
@Throws(IOException::class)
24+
constructor(sin: StreamInput) : this()
25+
26+
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {
27+
builder.startObject()
28+
.startObject(NOOP_TRIGGER_FIELD)
29+
.field(ID_FIELD, id)
30+
.endObject()
31+
.endObject()
32+
return builder
33+
}
34+
35+
override fun name(): String {
36+
return NOOP_TRIGGER_FIELD
37+
}
38+
39+
fun asTemplateArg(): Map<String, Any> {
40+
return mapOf()
41+
}
42+
43+
@Throws(IOException::class)
44+
override fun writeTo(out: StreamOutput) {
45+
}
46+
47+
companion object {
48+
const val ID_FIELD = "id"
49+
const val NOOP_TRIGGER_FIELD = "noop_trigger"
50+
val XCONTENT_REGISTRY = NamedXContentRegistry.Entry(
51+
Trigger::class.java, ParseField(NOOP_TRIGGER_FIELD),
52+
CheckedFunction { parseInner(it) }
53+
)
54+
55+
@JvmStatic @Throws(IOException::class)
56+
fun parseInner(xcp: XContentParser): NoOpTrigger {
57+
var id = UUIDs.base64UUID()
58+
if (xcp.currentToken() == XContentParser.Token.START_OBJECT) xcp.nextToken()
59+
if (xcp.currentName() == ID_FIELD) {
60+
xcp.nextToken()
61+
id = xcp.text()
62+
xcp.nextToken()
63+
}
64+
if (xcp.currentToken() != XContentParser.Token.END_OBJECT) {
65+
XContentParserUtils.throwUnknownToken(xcp.currentToken(), xcp.tokenLocation)
66+
}
67+
return NoOpTrigger(id = id)
68+
}
69+
70+
@JvmStatic
71+
@Throws(IOException::class)
72+
fun readFrom(sin: StreamInput): NoOpTrigger {
73+
return NoOpTrigger(sin)
74+
}
75+
}
76+
}

src/main/kotlin/org/opensearch/commons/alerting/model/Trigger.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ interface Trigger : BaseModel {
1212
enum class Type(val value: String) {
1313
DOCUMENT_LEVEL_TRIGGER(DocumentLevelTrigger.DOCUMENT_LEVEL_TRIGGER_FIELD),
1414
QUERY_LEVEL_TRIGGER(QueryLevelTrigger.QUERY_LEVEL_TRIGGER_FIELD),
15-
BUCKET_LEVEL_TRIGGER(BucketLevelTrigger.BUCKET_LEVEL_TRIGGER_FIELD);
15+
BUCKET_LEVEL_TRIGGER(BucketLevelTrigger.BUCKET_LEVEL_TRIGGER_FIELD),
16+
NOOP_TRIGGER(NoOpTrigger.NOOP_TRIGGER_FIELD);
1617

1718
override fun toString(): String {
1819
return value

src/test/kotlin/org/opensearch/commons/alerting/TestHelpers.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import org.opensearch.commons.alerting.model.Finding
2929
import org.opensearch.commons.alerting.model.Input
3030
import org.opensearch.commons.alerting.model.IntervalSchedule
3131
import org.opensearch.commons.alerting.model.Monitor
32+
import org.opensearch.commons.alerting.model.NoOpTrigger
3233
import org.opensearch.commons.alerting.model.QueryLevelTrigger
3334
import org.opensearch.commons.alerting.model.Schedule
3435
import org.opensearch.commons.alerting.model.SearchInput
@@ -395,7 +396,8 @@ fun xContentRegistry(): NamedXContentRegistry {
395396
DocLevelMonitorInput.XCONTENT_REGISTRY,
396397
QueryLevelTrigger.XCONTENT_REGISTRY,
397398
BucketLevelTrigger.XCONTENT_REGISTRY,
398-
DocumentLevelTrigger.XCONTENT_REGISTRY
399+
DocumentLevelTrigger.XCONTENT_REGISTRY,
400+
NoOpTrigger.XCONTENT_REGISTRY
399401
) + SearchModule(Settings.EMPTY, emptyList()).namedXContents
400402
)
401403
}

src/test/kotlin/org/opensearch/commons/alerting/model/XContentTests.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import org.opensearch.core.xcontent.ToXContent
3030
import org.opensearch.index.query.QueryBuilders
3131
import org.opensearch.search.builder.SearchSourceBuilder
3232
import org.opensearch.test.OpenSearchTestCase
33+
import java.time.Instant
3334
import java.time.temporal.ChronoUnit
3435
import kotlin.test.assertFailsWith
3536

@@ -179,6 +180,16 @@ class XContentTests {
179180
Assertions.assertEquals(trigger, parsedTrigger, "Round tripping BucketLevelTrigger doesn't work")
180181
}
181182

183+
@Test
184+
fun `test no-op trigger parsing`() {
185+
val trigger = NoOpTrigger()
186+
187+
val triggerString = trigger.toXContent(builder(), ToXContent.EMPTY_PARAMS).string()
188+
val parsedTrigger = Trigger.parse(parser(triggerString))
189+
190+
Assertions.assertEquals(trigger, parsedTrigger, "Round tripping NoOpTrigger doesn't work")
191+
}
192+
182193
@Test
183194
fun `test creating a monitor with duplicate trigger ids fails`() {
184195
try {
@@ -357,6 +368,16 @@ class XContentTests {
357368
assertEquals("Round tripping alert doesn't work", alert, parsedAlert)
358369
}
359370

371+
@Test
372+
fun `test alert parsing with noop trigger`() {
373+
val monitor = randomQueryLevelMonitor()
374+
val alert = Alert(
375+
monitor = monitor, trigger = NoOpTrigger(), startTime = Instant.now().truncatedTo(ChronoUnit.MILLIS),
376+
errorMessage = "some error", lastNotificationTime = Instant.now()
377+
)
378+
assertEquals("Round tripping alert doesn't work", alert.triggerName, "NoOp trigger")
379+
}
380+
360381
@Test
361382
fun `test alert parsing without user`() {
362383
val alertStr = "{\"id\":\"\",\"version\":-1,\"monitor_id\":\"\",\"schema_version\":0,\"monitor_version\":1," +

0 commit comments

Comments
 (0)