Skip to content

Commit fb5d525

Browse files
authored
add missing ctx variables (#710)
Signed-off-by: Joanne Wang <[email protected]>
1 parent 41a042b commit fb5d525

File tree

19 files changed

+403
-22
lines changed

19 files changed

+403
-22
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ data class BucketLevelTrigger(
7070
SEVERITY_FIELD to severity,
7171
ACTIONS_FIELD to actions.map { it.asTemplateArg() },
7272
PARENT_BUCKET_PATH to getParentBucketPath(),
73-
CONDITION_FIELD to getCondition()
73+
CONDITION_FIELD to mapOf(
74+
SCRIPT_FIELD to mapOf(
75+
SOURCE_FIELD to bucketSelector.script.idOrCode,
76+
LANG_FIELD to bucketSelector.script.lang
77+
)
78+
)
7479
)
7580
}
7681

@@ -86,6 +91,9 @@ data class BucketLevelTrigger(
8691
const val BUCKET_LEVEL_TRIGGER_FIELD = "bucket_level_trigger"
8792
const val CONDITION_FIELD = "condition"
8893
const val PARENT_BUCKET_PATH = "parentBucketPath"
94+
const val SCRIPT_FIELD = "script"
95+
const val SOURCE_FIELD = "source"
96+
const val LANG_FIELD = "lang"
8997

9098
val XCONTENT_REGISTRY = NamedXContentRegistry.Entry(
9199
Trigger::class.java,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ data class DocLevelMonitorInput(
2424
sin.readList(::DocLevelQuery) // docLevelQueries
2525
)
2626

27-
fun asTemplateArg(): Map<String, Any?> {
27+
override fun asTemplateArg(): Map<String, Any> {
2828
return mapOf(
2929
DESCRIPTION_FIELD to description,
3030
INDICES_FIELD to indices,

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ data class DocumentLevelTrigger(
6060
ID_FIELD to id,
6161
NAME_FIELD to name,
6262
SEVERITY_FIELD to severity,
63-
ACTIONS_FIELD to actions.map { it.asTemplateArg() }
63+
ACTIONS_FIELD to actions.map { it.asTemplateArg() },
64+
CONDITION_FIELD to mapOf(
65+
SCRIPT_FIELD to mapOf(
66+
SOURCE_FIELD to condition.idOrCode,
67+
LANG_FIELD to condition.lang
68+
)
69+
)
6470
)
6571
}
6672

@@ -78,6 +84,8 @@ data class DocumentLevelTrigger(
7884
const val CONDITION_FIELD = "condition"
7985
const val SCRIPT_FIELD = "script"
8086
const val QUERY_IDS_FIELD = "query_ids"
87+
const val SOURCE_FIELD = "source"
88+
const val LANG_FIELD = "lang"
8189

8290
val XCONTENT_REGISTRY = NamedXContentRegistry.Entry(
8391
Trigger::class.java,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,7 @@ interface Input : BaseModel {
6666
}
6767

6868
fun name(): String
69+
70+
/** Returns a representation of the schedule suitable for passing into painless and mustache scripts. */
71+
fun asTemplateArg(): Map<String, Any> = emptyMap()
6972
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,18 @@ data class Monitor(
127127
}
128128

129129
/** Returns a representation of the monitor suitable for passing into painless and mustache scripts. */
130-
fun asTemplateArg(): Map<String, Any> {
131-
return mapOf(_ID to id, _VERSION to version, NAME_FIELD to name, ENABLED_FIELD to enabled)
130+
fun asTemplateArg(): Map<String, Any?> {
131+
return mapOf(
132+
_ID to id,
133+
_VERSION to version,
134+
NAME_FIELD to name,
135+
ENABLED_FIELD to enabled,
136+
MONITOR_TYPE_FIELD to monitorType.toString(),
137+
ENABLED_TIME_FIELD to enabledTime?.toEpochMilli(),
138+
LAST_UPDATE_TIME_FIELD to lastUpdateTime.toEpochMilli(),
139+
SCHEDULE_FIELD to schedule.asTemplateArg(),
140+
INPUTS_FIELD to inputs.map { it.asTemplateArg() }
141+
)
132142
}
133143

134144
fun toXContentWithUser(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ data class QueryLevelTrigger(
6060
ID_FIELD to id,
6161
NAME_FIELD to name,
6262
SEVERITY_FIELD to severity,
63-
ACTIONS_FIELD to actions.map { it.asTemplateArg() }
63+
ACTIONS_FIELD to actions.map { it.asTemplateArg() },
64+
CONDITION_FIELD to mapOf(
65+
SCRIPT_FIELD to mapOf(
66+
SOURCE_FIELD to condition.idOrCode,
67+
LANG_FIELD to condition.lang
68+
)
69+
)
6470
)
6571
}
6672

@@ -77,6 +83,8 @@ data class QueryLevelTrigger(
7783
const val QUERY_LEVEL_TRIGGER_FIELD = "query_level_trigger"
7884
const val CONDITION_FIELD = "condition"
7985
const val SCRIPT_FIELD = "script"
86+
const val SOURCE_FIELD = "source"
87+
const val LANG_FIELD = "lang"
8088

8189
val XCONTENT_REGISTRY = NamedXContentRegistry.Entry(
8290
Trigger::class.java,

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ sealed class Schedule : BaseModel {
146146
abstract fun getPeriodEndingAt(endTime: Instant?): Pair<Instant, Instant>
147147

148148
abstract fun runningOnTime(lastExecutionTime: Instant?): Boolean
149+
150+
/** Returns a representation of the schedule suitable for passing into painless and mustache scripts. */
151+
abstract fun asTemplateArg(): Map<String, Any>
149152
}
150153

151154
/**
@@ -257,6 +260,14 @@ data class CronSchedule(
257260
out.writeString(expression)
258261
out.writeZoneId(timezone)
259262
}
263+
264+
override fun asTemplateArg(): Map<String, Any> =
265+
mapOf(
266+
CRON_FIELD to mapOf(
267+
EXPRESSION_FIELD to expression,
268+
TIMEZONE_FIELD to timezone.toString()
269+
)
270+
)
260271
}
261272

262273
data class IntervalSchedule(
@@ -354,4 +365,12 @@ data class IntervalSchedule(
354365
out.writeInt(interval)
355366
out.writeEnum(unit)
356367
}
368+
369+
override fun asTemplateArg(): Map<String, Any> =
370+
mapOf(
371+
PERIOD_FIELD to mapOf(
372+
INTERVAL_FIELD to interval,
373+
UNIT_FIELD to unit.toString()
374+
)
375+
)
357376
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,12 @@ data class SearchInput(val indices: List<String>, val query: SearchSourceBuilder
8585
return SearchInput(sin)
8686
}
8787
}
88+
89+
override fun asTemplateArg(): Map<String, Any> =
90+
mapOf(
91+
SEARCH_FIELD to mapOf(
92+
INDICES_FIELD to indices,
93+
QUERY_FIELD to query.toString()
94+
)
95+
)
8896
}

src/main/kotlin/org/opensearch/commons/alerting/model/action/Action.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ data class Action(
6565
}
6666

6767
fun asTemplateArg(): Map<String, Any> {
68-
return mapOf(NAME_FIELD to name)
68+
return mapOf(
69+
ID_FIELD to id,
70+
NAME_FIELD to name,
71+
DESTINATION_ID_FIELD to destinationId,
72+
THROTTLE_ENABLED_FIELD to throttleEnabled
73+
)
6974
}
7075

7176
@Throws(IOException::class)

src/main/kotlin/org/opensearch/commons/alerting/model/remote/monitors/RemoteDocLevelMonitorInput.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ data class RemoteDocLevelMonitorInput(val input: BytesReference, val docLevelMon
2121
DocLevelMonitorInput.readFrom(sin)
2222
)
2323

24-
fun asTemplateArg(): Map<String, Any?> {
24+
override fun asTemplateArg(): Map<String, Any> {
2525
val bytes = input.toBytesRef().bytes
2626
return mapOf(
2727
RemoteDocLevelMonitorInput.INPUT_SIZE to bytes.size,

0 commit comments

Comments
 (0)