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 Jun 28, 2024
1 parent 889bdb8 commit 20f9ef6
Show file tree
Hide file tree
Showing 2 changed files with 48 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 @@ -29,6 +29,7 @@ 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 @@ -37,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 @@ -370,6 +372,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)
}

@Test
fun `test Comment object`() {
val user = randomUser()
Expand Down

0 comments on commit 20f9ef6

Please sign in to comment.