Skip to content

Commit

Permalink
Merge pull request #1 from jowg-amazon/fix_serd
Browse files Browse the repository at this point in the history
fix serde for monitor (opensearch-project#692)
  • Loading branch information
AWSHurneyt authored Jul 2, 2024
2 parents 4c4e62d + 95734a9 commit b3bf4a2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.opensearch.commons.alerting.model

import org.opensearch.common.CheckedFunction
import org.opensearch.commons.alerting.model.remote.monitors.RemoteMonitorTrigger
import org.opensearch.commons.alerting.util.IndexUtils.Companion.MONITOR_MAX_INPUTS
import org.opensearch.commons.alerting.util.IndexUtils.Companion.MONITOR_MAX_TRIGGERS
import org.opensearch.commons.alerting.util.IndexUtils.Companion.NO_SCHEMA_VERSION
Expand Down Expand Up @@ -188,8 +189,10 @@ data class Monitor(
inputs.forEach {
if (it is SearchInput) {
out.writeEnum(Input.Type.SEARCH_INPUT)
} else {
} else if (it is DocLevelMonitorInput) {
out.writeEnum(Input.Type.DOCUMENT_LEVEL_INPUT)
} else {
out.writeEnum(Input.Type.REMOTE_DOC_LEVEL_MONITOR_INPUT)
}
it.writeTo(out)
}
Expand All @@ -199,6 +202,7 @@ data class Monitor(
when (it) {
is BucketLevelTrigger -> out.writeEnum(Trigger.Type.BUCKET_LEVEL_TRIGGER)
is DocumentLevelTrigger -> out.writeEnum(Trigger.Type.DOCUMENT_LEVEL_TRIGGER)
is RemoteMonitorTrigger -> out.writeEnum(Trigger.Type.REMOTE_MONITOR_TRIGGER)
else -> out.writeEnum(Trigger.Type.QUERY_LEVEL_TRIGGER)
}
it.writeTo(out)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import org.opensearch.commons.alerting.randomQueryLevelTriggerRunResult
import org.opensearch.commons.alerting.randomThrottle
import org.opensearch.commons.alerting.randomUser
import org.opensearch.commons.alerting.randomUserEmpty
import org.opensearch.commons.alerting.randomWorkflow
import org.opensearch.commons.alerting.util.IndexUtils
import org.opensearch.commons.authuser.User
import org.opensearch.core.common.io.stream.StreamInput
import org.opensearch.core.common.io.stream.StreamOutput
Expand All @@ -36,6 +38,7 @@ import org.opensearch.search.builder.SearchSourceBuilder
import org.opensearch.test.OpenSearchTestCase
import java.io.IOException
import java.time.Instant
import java.time.temporal.ChronoUnit
import kotlin.test.assertTrue

class WriteableTests {
Expand Down Expand Up @@ -359,6 +362,47 @@ class WriteableTests {
Assert.assertEquals("Round tripping DocLevelMonitorInput failed", newDocLevelMonitorInput, docLevelMonitorInput)
}

@Test
fun `test RemoteMonitor as stream`() {
val myMonitorInput = MyMonitorInput(1, "hello", MyMonitorInput(2, "world", null))
var myObjOut = BytesStreamOutput()
myMonitorInput.writeTo(myObjOut)
val docLevelMonitorInput = DocLevelMonitorInput(
"test",
listOf("test"),
listOf(randomDocLevelQuery())
)
val remoteDocLevelMonitorInput = RemoteDocLevelMonitorInput(myObjOut.bytes(), docLevelMonitorInput)

val myMonitorTrigger = MyMonitorTrigger(1, "hello", MyMonitorTrigger(2, "world", null))
myObjOut = BytesStreamOutput()
myMonitorTrigger.writeTo(myObjOut)
val remoteMonitorTrigger = RemoteMonitorTrigger("id", "name", "1", listOf(), myObjOut.bytes())

val monitor = Monitor(
Monitor.NO_ID,
Monitor.NO_VERSION,
"hello",
true,
IntervalSchedule(1, ChronoUnit.MINUTES),
Instant.now(),
Instant.now(),
"remote_doc_level_monitor",
null,
IndexUtils.NO_SCHEMA_VERSION,
listOf(remoteDocLevelMonitorInput),
listOf(remoteMonitorTrigger),
mapOf()
)

val out = BytesStreamOutput()
monitor.writeTo(out)

val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
val newMonitor = Monitor(sin)
Assert.assertEquals("Round tripping RemoteMonitor failed", monitor, newMonitor)
}

fun randomDocumentLevelTriggerRunResult(): DocumentLevelTriggerRunResult {
val map = mutableMapOf<String, ActionRunResult>()
map.plus(Pair("key1", randomActionRunResult()))
Expand Down

0 comments on commit b3bf4a2

Please sign in to comment.