Skip to content

Commit

Permalink
fix serde for monitor (#692)
Browse files Browse the repository at this point in the history
Signed-off-by: Subhobrata Dey <[email protected]>
  • Loading branch information
sbcd90 authored and jowg-amazon committed Jul 2, 2024
1 parent 4c4e62d commit 95734a9
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 95734a9

Please sign in to comment.