forked from opensearch-project/common-utils
-
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.
Added support for printing sample documents for each bucket in bucket…
… level monitor notification messages. Added support for printing query/rule info in doc level monitor notification messages. Signed-off-by: AWSHurneyt <hurneyt@amazon.com>
- Loading branch information
1 parent
0a87637
commit 5d25a24
Showing
5 changed files
with
240 additions
and
15 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
75 changes: 75 additions & 0 deletions
75
src/main/kotlin/org/opensearch/commons/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,75 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.commons.alerting.model | ||
|
||
/** | ||
* 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 | ||
) : Alert( | ||
id = alert.id, | ||
version = alert.version, | ||
schemaVersion = alert.schemaVersion, | ||
monitorId = alert.monitorId, | ||
monitorName = alert.monitorName, | ||
monitorVersion = alert.monitorVersion, | ||
monitorUser = alert.monitorUser, | ||
triggerId = alert.triggerId, | ||
triggerName = alert.triggerName, | ||
state = alert.state, | ||
startTime = alert.startTime, | ||
endTime = alert.endTime, | ||
lastNotificationTime = alert.lastNotificationTime, | ||
acknowledgedTime = alert.acknowledgedTime, | ||
errorMessage = alert.errorMessage, | ||
errorHistory = alert.errorHistory, | ||
severity = alert.severity, | ||
actionExecutionResults = alert.actionExecutionResults, | ||
aggregationResultBucket = alert.aggregationResultBucket, | ||
findingIds = alert.findingIds, | ||
relatedDocIds = alert.relatedDocIds, | ||
executionId = alert.executionId, | ||
workflowId = alert.workflowId, | ||
workflowName = alert.workflowName, | ||
associatedAlertIds = alert.associatedAlertIds, | ||
clusters = alert.clusters | ||
) { | ||
|
||
override 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 = super.asTemplateArg().toMutableMap() | ||
|
||
// Add the non-null custom context fields to the alert templateArgs. | ||
customContextFields.forEach { (key, value) -> | ||
if (value !== null) templateArgs[key] = value | ||
} | ||
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
45 changes: 45 additions & 0 deletions
45
src/test/kotlin/org/opensearch/commons/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,45 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.commons.alerting.model | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.opensearch.commons.alerting.randomAlertContext | ||
|
||
class AlertContextTests { | ||
private var alertContext: AlertContext = randomAlertContext() | ||
|
||
@BeforeEach | ||
fun generateRandomData() { | ||
alertContext = randomAlertContext() | ||
} | ||
|
||
@Test | ||
fun `test AlertContext asTemplateArg`() { | ||
val templateArgs = alertContext.asTemplateArg() | ||
|
||
assertEquals(templateArgs[Alert.ALERT_ID_FIELD], alertContext.id, "Template args id does not match") | ||
assertEquals(templateArgs[Alert.ALERT_VERSION_FIELD], alertContext.version, "Template args version does not match") | ||
assertEquals(templateArgs[Alert.STATE_FIELD], alertContext.state.toString(), "Template args state does not match") | ||
assertEquals(templateArgs[Alert.ERROR_MESSAGE_FIELD], alertContext.errorMessage, "Template args error message does not match") | ||
assertEquals(templateArgs[Alert.ACKNOWLEDGED_TIME_FIELD], null, "Template args acknowledged time does not match") | ||
assertEquals(templateArgs[Alert.END_TIME_FIELD], alertContext.endTime?.toEpochMilli(), "Template args end time does not") | ||
assertEquals(templateArgs[Alert.START_TIME_FIELD], alertContext.startTime.toEpochMilli(), "Template args start time does not") | ||
assertEquals(templateArgs[Alert.LAST_NOTIFICATION_TIME_FIELD], null, "Template args last notification time does not match") | ||
assertEquals(templateArgs[Alert.SEVERITY_FIELD], alertContext.severity, "Template args severity does not match") | ||
assertEquals(templateArgs[Alert.CLUSTERS_FIELD], alertContext.clusters?.joinToString(","), "Template args clusters does not match") | ||
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(templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD], formattedQueries, "Template associated queries do not match") | ||
assertEquals(templateArgs[AlertContext.SAMPLE_DOCS_FIELD], alertContext.sampleDocs, "Template args sample docs do not match") | ||
} | ||
} |