Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing ctx variables #318

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ data class BucketLevelTrigger(
NAME_FIELD to name,
SEVERITY_FIELD to severity,
ACTIONS_FIELD to actions.map { it.asTemplateArg() },
PARENT_BUCKET_PATH to getParentBucketPath()
PARENT_BUCKET_PATH to getParentBucketPath(),
CONDITION_FIELD to mapOf(
SCRIPT_FIELD to mapOf(
SOURCE_FIELD to bucketSelector.script.idOrCode,
LANG_FIELD to bucketSelector.script.lang
)
)
)
}

Expand All @@ -81,6 +87,9 @@ data class BucketLevelTrigger(
const val BUCKET_LEVEL_TRIGGER_FIELD = "bucket_level_trigger"
const val CONDITION_FIELD = "condition"
const val PARENT_BUCKET_PATH = "parentBucketPath"
const val SCRIPT_FIELD = "script"
const val SOURCE_FIELD = "source"
const val LANG_FIELD = "lang"

val XCONTENT_REGISTRY = NamedXContentRegistry.Entry(
Trigger::class.java, ParseField(BUCKET_LEVEL_TRIGGER_FIELD),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ data class DocLevelMonitorInput(
sin.readList(::DocLevelQuery) // docLevelQueries
)

fun asTemplateArg(): Map<String, Any?> {
override fun asTemplateArg(): Map<String, Any> {
return mapOf(
DESCRIPTION_FIELD to description,
INDICES_FIELD to indices,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ data class DocumentLevelTrigger(
ID_FIELD to id,
NAME_FIELD to name,
SEVERITY_FIELD to severity,
ACTIONS_FIELD to actions.map { it.asTemplateArg() }
ACTIONS_FIELD to actions.map { it.asTemplateArg() },
CONDITION_FIELD to mapOf(
SCRIPT_FIELD to mapOf(
SOURCE_FIELD to condition.idOrCode,
LANG_FIELD to condition.lang
)
)
)
}

Expand All @@ -78,6 +84,8 @@ data class DocumentLevelTrigger(
const val CONDITION_FIELD = "condition"
const val SCRIPT_FIELD = "script"
const val QUERY_IDS_FIELD = "query_ids"
const val SOURCE_FIELD = "source"
const val LANG_FIELD = "lang"

val XCONTENT_REGISTRY = NamedXContentRegistry.Entry(
Trigger::class.java, ParseField(DOCUMENT_LEVEL_TRIGGER_FIELD),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ interface Input : BaseModel {
}

fun name(): String

/** Returns a representation of the schedule suitable for passing into painless and mustache scripts. */
fun asTemplateArg(): Map<String, Any> = emptyMap()
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,18 @@ data class Monitor(
}

/** Returns a representation of the monitor suitable for passing into painless and mustache scripts. */
fun asTemplateArg(): Map<String, Any> {
return mapOf(_ID to id, _VERSION to version, NAME_FIELD to name, ENABLED_FIELD to enabled)
fun asTemplateArg(): Map<String, Any?> {
return mapOf(
_ID to id,
_VERSION to version,
NAME_FIELD to name,
ENABLED_FIELD to enabled,
MONITOR_TYPE_FIELD to monitorType.toString(),
ENABLED_TIME_FIELD to enabledTime?.toEpochMilli(),
LAST_UPDATE_TIME_FIELD to lastUpdateTime.toEpochMilli(),
SCHEDULE_FIELD to schedule.asTemplateArg(),
INPUTS_FIELD to inputs.map { it.asTemplateArg() }
)
}

fun toXContentWithUser(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ data class QueryLevelTrigger(
fun asTemplateArg(): Map<String, Any> {
return mapOf(
ID_FIELD to id, NAME_FIELD to name, SEVERITY_FIELD to severity,
ACTIONS_FIELD to actions.map { it.asTemplateArg() }
ACTIONS_FIELD to actions.map { it.asTemplateArg() },
CONDITION_FIELD to mapOf(
SCRIPT_FIELD to mapOf(
SOURCE_FIELD to condition.idOrCode,
LANG_FIELD to condition.lang
)
)
)
}

Expand All @@ -75,6 +81,8 @@ data class QueryLevelTrigger(
const val QUERY_LEVEL_TRIGGER_FIELD = "query_level_trigger"
const val CONDITION_FIELD = "condition"
const val SCRIPT_FIELD = "script"
const val SOURCE_FIELD = "source"
const val LANG_FIELD = "lang"

val XCONTENT_REGISTRY = NamedXContentRegistry.Entry(
Trigger::class.java, ParseField(QUERY_LEVEL_TRIGGER_FIELD),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ sealed class Schedule : BaseModel {
abstract fun getPeriodEndingAt(endTime: Instant?): Pair<Instant, Instant>

abstract fun runningOnTime(lastExecutionTime: Instant?): Boolean

/** Returns a representation of the schedule suitable for passing into painless and mustache scripts. */
abstract fun asTemplateArg(): Map<String, Any>
}

/**
Expand Down Expand Up @@ -254,6 +257,14 @@ data class CronSchedule(
out.writeString(expression)
out.writeZoneId(timezone)
}

override fun asTemplateArg(): Map<String, Any> =
mapOf(
CRON_FIELD to mapOf(
EXPRESSION_FIELD to expression,
TIMEZONE_FIELD to timezone.toString()
)
)
}

data class IntervalSchedule(
Expand Down Expand Up @@ -351,4 +362,12 @@ data class IntervalSchedule(
out.writeInt(interval)
out.writeEnum(unit)
}

override fun asTemplateArg(): Map<String, Any> =
mapOf(
PERIOD_FIELD to mapOf(
INTERVAL_FIELD to interval,
UNIT_FIELD to unit.toString()
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ data class SearchInput(val indices: List<String>, val query: SearchSourceBuilder
return SearchInput(sin)
}
}

override fun asTemplateArg(): Map<String, Any> =
mapOf(
SEARCH_FIELD to mapOf(
INDICES_FIELD to indices,
QUERY_FIELD to query.toString()
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ data class Action(
}

fun asTemplateArg(): Map<String, Any> {
return mapOf(NAME_FIELD to name)
return mapOf(
ID_FIELD to id,
NAME_FIELD to name,
DESTINATION_ID_FIELD to destinationId,
THROTTLE_ENABLED_FIELD to throttleEnabled
)
}

@Throws(IOException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@ fun randomDocLevelMonitorInput(
return DocLevelMonitorInput(description = description, indices = indices, queries = queries)
}

fun randomSearchInput(
indices: List<String> = listOf(1..RandomNumbers.randomIntBetween(Random(), 0, 10)).map { RandomStrings.randomAsciiLettersOfLength(Random(), 10) },
query: SearchSourceBuilder = SearchSourceBuilder().query(QueryBuilders.matchAllQuery())
): SearchInput {
return SearchInput(indices, query)
}

fun randomClusterMetricsInput(
path: String = ClusterMetricsInput.ClusterMetricType.CLUSTER_HEALTH.defaultPath,
pathParams: String = "",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.opensearch.commons.alerting.model

import org.junit.jupiter.api.Test
import org.opensearch.commons.alerting.model.BucketLevelTrigger.Companion.CONDITION_FIELD
import org.opensearch.commons.alerting.model.BucketLevelTrigger.Companion.LANG_FIELD
import org.opensearch.commons.alerting.model.BucketLevelTrigger.Companion.PARENT_BUCKET_PATH
import org.opensearch.commons.alerting.model.BucketLevelTrigger.Companion.SCRIPT_FIELD
import org.opensearch.commons.alerting.model.BucketLevelTrigger.Companion.SOURCE_FIELD
import org.opensearch.commons.alerting.model.Trigger.Companion.ACTIONS_FIELD
import org.opensearch.commons.alerting.model.Trigger.Companion.ID_FIELD
import org.opensearch.commons.alerting.model.Trigger.Companion.NAME_FIELD
import org.opensearch.commons.alerting.model.Trigger.Companion.SEVERITY_FIELD
import org.opensearch.commons.alerting.randomBucketLevelTrigger
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class BucketLevelTriggerTest {

@Test
fun `test BucketLevelTrigger asTemplateArgs`() {
val trigger = randomBucketLevelTrigger()

val templateArgs = trigger.asTemplateArg()

assertEquals(trigger.id, templateArgs[ID_FIELD], "Template arg field 'id' doesn't match")
assertEquals(trigger.name, templateArgs[NAME_FIELD], "Template arg field 'name' doesn't match")
assertEquals(trigger.severity, templateArgs[SEVERITY_FIELD], "Template arg field 'severity' doesn't match")
val actions = templateArgs[ACTIONS_FIELD] as List<*>
assertEquals(
trigger.actions.size,
actions.size,
"Template arg field 'actions' doesn't match"
)
assertEquals(
trigger.getParentBucketPath(),
templateArgs[PARENT_BUCKET_PATH],
"Template arg field 'parentBucketPath' doesn't match"
)
val condition = templateArgs[CONDITION_FIELD] as? Map<*, *>
assertNotNull(condition, "Template arg field 'condition' is empty")
val script = condition[SCRIPT_FIELD] as? Map<*, *>
assertNotNull(script, "Template arg field 'condition.script' is empty")
assertEquals(
trigger.bucketSelector.script.idOrCode,
script[SOURCE_FIELD],
"Template arg field 'script.source' doesn't match"
)
assertEquals(
trigger.bucketSelector.script.lang,
script[LANG_FIELD],
"Template arg field 'script.lang' doesn't match"
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.opensearch.commons.alerting.model

import org.junit.jupiter.api.Test
import org.opensearch.commons.alerting.model.DocumentLevelTrigger.Companion.CONDITION_FIELD
import org.opensearch.commons.alerting.model.DocumentLevelTrigger.Companion.LANG_FIELD
import org.opensearch.commons.alerting.model.DocumentLevelTrigger.Companion.SCRIPT_FIELD
import org.opensearch.commons.alerting.model.DocumentLevelTrigger.Companion.SOURCE_FIELD
import org.opensearch.commons.alerting.model.Trigger.Companion.ACTIONS_FIELD
import org.opensearch.commons.alerting.model.Trigger.Companion.ID_FIELD
import org.opensearch.commons.alerting.model.Trigger.Companion.NAME_FIELD
import org.opensearch.commons.alerting.model.Trigger.Companion.SEVERITY_FIELD
import org.opensearch.commons.alerting.randomDocumentLevelTrigger
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class DocumentLevelTriggerTest {

@Test
fun `test DocumentLevelTrigger asTemplateArgs`() {
val trigger = randomDocumentLevelTrigger()

val templateArgs = trigger.asTemplateArg()

assertEquals(trigger.id, templateArgs[ID_FIELD], "Template arg field 'id' doesn't match")
assertEquals(trigger.name, templateArgs[NAME_FIELD], "Template arg field 'name' doesn't match")
assertEquals(trigger.severity, templateArgs[SEVERITY_FIELD], "Template arg field 'severity' doesn't match")
val actions = templateArgs[ACTIONS_FIELD] as List<*>
assertEquals(
trigger.actions.size,
actions.size,
"Template arg field 'actions' doesn't match"
)
val condition = templateArgs[CONDITION_FIELD] as? Map<*, *>
assertNotNull(condition, "Template arg field 'condition' is empty")
val script = condition[SCRIPT_FIELD] as? Map<*, *>
assertNotNull(script, "Template arg field 'condition.script' is empty")
assertEquals(
trigger.condition.idOrCode,
script[SOURCE_FIELD],
"Template arg field 'script.source' doesn't match"
)
assertEquals(
trigger.condition.lang,
script[LANG_FIELD],
"Template arg field 'script.lang' doesn't match"
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.opensearch.commons.alerting.model

import org.junit.jupiter.api.Test
import org.opensearch.commons.alerting.randomQueryLevelMonitor
import org.opensearch.commons.alerting.util.IndexUtils
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class MonitorsTest {

@Test
fun `test monitor asTemplateArgs`() {
val monitor = randomQueryLevelMonitor(enabled = true)

val templateArgs = monitor.asTemplateArg()

assertEquals(monitor.id, templateArgs[IndexUtils._ID], "Template arg field 'id' doesn't match")
assertEquals(
monitor.version, templateArgs[IndexUtils._VERSION], "Template arg field 'version' doesn't match"
)
assertEquals(monitor.name, templateArgs[Monitor.NAME_FIELD], "Template arg field 'name' doesn't match")
assertEquals(
monitor.enabled, templateArgs[Monitor.ENABLED_FIELD], "Template arg field 'enabled' doesn't match"
)
assertEquals(
monitor.monitorType.toString(), templateArgs[Monitor.MONITOR_TYPE_FIELD],
"Template arg field 'monitoryType' doesn't match"
)
assertEquals(
monitor.enabledTime?.toEpochMilli(), templateArgs[Monitor.ENABLED_TIME_FIELD],
"Template arg field 'enabledTime' doesn't match"
)
assertEquals(
monitor.lastUpdateTime.toEpochMilli(), templateArgs[Monitor.LAST_UPDATE_TIME_FIELD],
"Template arg field 'lastUpdateTime' doesn't match"
)
assertNotNull(templateArgs[Monitor.SCHEDULE_FIELD], "Template arg field 'schedule' not set")
val inputs = templateArgs[Monitor.INPUTS_FIELD] as? List<*>
assertNotNull(inputs, "Template arg field 'inputs' not set")
assertEquals(1, inputs.size, "Template arg field 'inputs' is not populated")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.opensearch.commons.alerting.model

import org.junit.jupiter.api.Test
import org.opensearch.commons.alerting.model.QueryLevelTrigger.Companion.CONDITION_FIELD
import org.opensearch.commons.alerting.model.QueryLevelTrigger.Companion.LANG_FIELD
import org.opensearch.commons.alerting.model.QueryLevelTrigger.Companion.SCRIPT_FIELD
import org.opensearch.commons.alerting.model.QueryLevelTrigger.Companion.SOURCE_FIELD
import org.opensearch.commons.alerting.model.Trigger.Companion.ACTIONS_FIELD
import org.opensearch.commons.alerting.model.Trigger.Companion.ID_FIELD
import org.opensearch.commons.alerting.model.Trigger.Companion.NAME_FIELD
import org.opensearch.commons.alerting.model.Trigger.Companion.SEVERITY_FIELD
import org.opensearch.commons.alerting.randomQueryLevelTrigger
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class QueryLevelTriggerTest {

@Test
fun `test QueryLevelTrigger asTemplateArgs`() {
val trigger = randomQueryLevelTrigger()

val templateArgs = trigger.asTemplateArg()

assertEquals(trigger.id, templateArgs[ID_FIELD], "Template arg field 'id' doesn't match")
assertEquals(trigger.name, templateArgs[NAME_FIELD], "Template arg field 'name' doesn't match")
assertEquals(trigger.severity, templateArgs[SEVERITY_FIELD], "Template arg field 'severity' doesn't match")
val actions = templateArgs[ACTIONS_FIELD] as List<*>
assertEquals(
trigger.actions.size,
actions.size,
"Template arg field 'actions' doesn't match"
)
val condition = templateArgs[CONDITION_FIELD] as? Map<*, *>
assertNotNull(condition, "Template arg field 'condition' is empty")
val script = condition[SCRIPT_FIELD] as? Map<*, *>
assertNotNull(script, "Template arg field 'condition.script' is empty")
assertEquals(
trigger.condition.idOrCode,
script[SOURCE_FIELD],
"Template arg field 'script.source' doesn't match"
)
assertEquals(
trigger.condition.lang,
script[LANG_FIELD],
"Template arg field 'script.lang' doesn't match"
)
}
}
Loading
Loading