forked from opensearch-project/alerting
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved AlertContext data model from common utils to alerting plugin.
Signed-off-by: AWSHurneyt <[email protected]>
- Loading branch information
1 parent
8cb0168
commit 85cf9f1
Showing
9 changed files
with
114 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
alerting/src/main/kotlin/org/opensearch/alerting/model/AlertContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.alerting.model | ||
|
||
import org.opensearch.commons.alerting.model.Alert | ||
import org.opensearch.commons.alerting.model.DocLevelQuery | ||
|
||
/** | ||
* This model is a wrapper for [Alert] that should only be used to create a more | ||
* informative alert object to enrich mustache template notification messages. | ||
*/ | ||
data class AlertContext( | ||
val alert: Alert, | ||
val associatedQueries: List<DocLevelQuery>? = null, | ||
val sampleDocs: List<Map<String, Any?>>? = null | ||
) { | ||
fun asTemplateArg(): Map<String, Any?> { | ||
val queriesContext = associatedQueries?.map { | ||
mapOf( | ||
DocLevelQuery.QUERY_ID_FIELD to it.id, | ||
DocLevelQuery.NAME_FIELD to it.name, | ||
DocLevelQuery.TAGS_FIELD to it.tags | ||
) | ||
} | ||
|
||
// Compile the custom context fields. | ||
val customContextFields = mapOf( | ||
ASSOCIATED_QUERIES_FIELD to queriesContext, | ||
SAMPLE_DOCS_FIELD to sampleDocs | ||
) | ||
|
||
// Get the alert template args | ||
val templateArgs = alert.asTemplateArg().toMutableMap() | ||
|
||
// Add the non-null custom context fields to the alert templateArgs. | ||
customContextFields.forEach { (key, value) -> | ||
value?.let { templateArgs[key] = it } | ||
} | ||
return templateArgs | ||
} | ||
|
||
companion object { | ||
const val ASSOCIATED_QUERIES_FIELD = "associated_queries" | ||
const val SAMPLE_DOCS_FIELD = "sample_documents" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
alerting/src/test/kotlin/org/opensearch/alerting/model/AlertContextTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.alerting.model | ||
|
||
import org.opensearch.alerting.randomAlertContext | ||
import org.opensearch.commons.alerting.model.Alert | ||
import org.opensearch.commons.alerting.model.DocLevelQuery | ||
import org.opensearch.test.OpenSearchTestCase | ||
|
||
class AlertContextTests : OpenSearchTestCase() { | ||
|
||
fun `test AlertContext asTemplateArg`() { | ||
val alertContext: AlertContext = randomAlertContext() | ||
val templateArgs = alertContext.asTemplateArg() | ||
|
||
assertEquals("Template args id does not match", templateArgs[Alert.ALERT_ID_FIELD], alertContext.alert.id) | ||
assertEquals("Template args version does not match", templateArgs[Alert.ALERT_VERSION_FIELD], alertContext.alert.version) | ||
assertEquals("Template args state does not match", templateArgs[Alert.STATE_FIELD], alertContext.alert.state.toString()) | ||
assertEquals("Template args error message does not match", templateArgs[Alert.ERROR_MESSAGE_FIELD], alertContext.alert.errorMessage) | ||
assertEquals("Template args acknowledged time does not match", templateArgs[Alert.ACKNOWLEDGED_TIME_FIELD], null) | ||
assertEquals("Template args end time does not", templateArgs[Alert.END_TIME_FIELD], alertContext.alert.endTime?.toEpochMilli()) | ||
assertEquals("Template args start time does not", templateArgs[Alert.START_TIME_FIELD], alertContext.alert.startTime.toEpochMilli()) | ||
assertEquals("Template args last notification time does not match", templateArgs[Alert.LAST_NOTIFICATION_TIME_FIELD], null) | ||
assertEquals("Template args severity does not match", templateArgs[Alert.SEVERITY_FIELD], alertContext.alert.severity) | ||
assertEquals("Template args clusters does not match", templateArgs[Alert.CLUSTERS_FIELD], alertContext.alert.clusters?.joinToString(",")) | ||
val formattedQueries = alertContext.associatedQueries?.map { | ||
mapOf( | ||
DocLevelQuery.QUERY_ID_FIELD to it.id, | ||
DocLevelQuery.NAME_FIELD to it.name, | ||
DocLevelQuery.TAGS_FIELD to it.tags | ||
) | ||
} | ||
assertEquals("Template associated queries do not match", templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD], formattedQueries) | ||
assertEquals("Template args sample docs do not match", templateArgs[AlertContext.SAMPLE_DOCS_FIELD], alertContext.sampleDocs) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters