Skip to content

Commit

Permalink
Moved AlertContext data model from common utils to alerting plugin.
Browse files Browse the repository at this point in the history
Signed-off-by: AWSHurneyt <[email protected]>
  • Loading branch information
AWSHurneyt committed Mar 12, 2024
1 parent 8cb0168 commit 85cf9f1
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.opensearch.action.search.SearchRequest
import org.opensearch.action.search.SearchResponse
import org.opensearch.action.support.WriteRequest
import org.opensearch.alerting.model.ActionRunResult
import org.opensearch.alerting.model.AlertContext
import org.opensearch.alerting.model.BucketLevelTriggerRunResult
import org.opensearch.alerting.model.InputRunResults
import org.opensearch.alerting.model.MonitorRunResult
Expand All @@ -31,7 +32,6 @@ import org.opensearch.alerting.workflow.WorkflowRunContext
import org.opensearch.common.xcontent.LoggingDeprecationHandler
import org.opensearch.common.xcontent.XContentType
import org.opensearch.commons.alerting.model.Alert
import org.opensearch.commons.alerting.model.AlertContext
import org.opensearch.commons.alerting.model.BucketLevelTrigger
import org.opensearch.commons.alerting.model.Finding
import org.opensearch.commons.alerting.model.Monitor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.opensearch.action.index.IndexRequest
import org.opensearch.action.search.SearchAction
import org.opensearch.action.search.SearchRequest
import org.opensearch.action.search.SearchResponse
import org.opensearch.alerting.model.AlertContext
import org.opensearch.alerting.model.DocumentLevelTriggerRunResult
import org.opensearch.alerting.model.IndexExecutionContext
import org.opensearch.alerting.model.InputRunResults
Expand Down Expand Up @@ -47,7 +48,6 @@ import org.opensearch.commons.alerting.action.PublishFindingsRequest
import org.opensearch.commons.alerting.action.SubscribeFindingsResponse
import org.opensearch.commons.alerting.model.ActionExecutionResult
import org.opensearch.commons.alerting.model.Alert
import org.opensearch.commons.alerting.model.AlertContext
import org.opensearch.commons.alerting.model.DocLevelMonitorInput
import org.opensearch.commons.alerting.model.DocLevelQuery
import org.opensearch.commons.alerting.model.DocumentLevelTrigger
Expand Down
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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
package org.opensearch.alerting.script

import org.apache.logging.log4j.LogManager
import org.opensearch.alerting.model.AlertContext
import org.opensearch.alerting.model.BucketLevelTriggerRunResult
import org.opensearch.alerting.model.MonitorRunResult
import org.opensearch.commons.alerting.model.Alert
import org.opensearch.commons.alerting.model.AlertContext
import org.opensearch.commons.alerting.model.BucketLevelTrigger
import org.opensearch.commons.alerting.model.Monitor
import java.time.Instant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package org.opensearch.alerting.script

import org.opensearch.commons.alerting.model.AlertContext
import org.opensearch.alerting.model.AlertContext
import org.opensearch.commons.alerting.model.DocumentLevelTrigger
import org.opensearch.commons.alerting.model.Monitor
import java.time.Instant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.opensearch.alerting.util

import org.apache.logging.log4j.LogManager
import org.opensearch.alerting.model.AlertContext
import org.opensearch.alerting.model.BucketLevelTriggerRunResult
import org.opensearch.alerting.model.destination.Destination
import org.opensearch.alerting.script.BucketLevelTriggerExecutionContext
Expand All @@ -15,7 +16,6 @@ import org.opensearch.cluster.service.ClusterService
import org.opensearch.common.settings.Settings
import org.opensearch.common.util.concurrent.ThreadContext
import org.opensearch.commons.alerting.model.AggregationResultBucket
import org.opensearch.commons.alerting.model.AlertContext
import org.opensearch.commons.alerting.model.BucketLevelTrigger
import org.opensearch.commons.alerting.model.DocumentLevelTrigger
import org.opensearch.commons.alerting.model.Monitor
Expand Down
20 changes: 20 additions & 0 deletions alerting/src/test/kotlin/org/opensearch/alerting/TestHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import junit.framework.TestCase.assertNull
import org.apache.hc.core5.http.Header
import org.apache.hc.core5.http.HttpEntity
import org.opensearch.alerting.model.ActionRunResult
import org.opensearch.alerting.model.AlertContext
import org.opensearch.alerting.model.BucketLevelTriggerRunResult
import org.opensearch.alerting.model.DocumentLevelTriggerRunResult
import org.opensearch.alerting.model.InputRunResults
Expand Down Expand Up @@ -782,3 +783,22 @@ fun randomChainedAlertTrigger(
} else actions
)
}

fun randomAlertContext(
alert: Alert = randomAlert(),
associatedQueries: List<DocLevelQuery>? = (-1..2).random().takeIf { it != -1 }?.let {
(0..it).map { randomDocLevelQuery() }
},
sampleDocs: List<Map<String, Any?>>? = (-1..2).random().takeIf { it != -1 }?.let {
(0..it).map {
// Using 'randomFinding' to mimic documents in an index.
randomFinding().asTemplateArg()
}
}
): AlertContext {
return AlertContext(
alert = alert,
associatedQueries = associatedQueries,
sampleDocs = sampleDocs
)
}
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.opensearch.alerting.util

import org.opensearch.alerting.model.AlertContext
import org.opensearch.alerting.randomAction
import org.opensearch.alerting.randomBucketLevelTrigger
import org.opensearch.alerting.randomChainedAlertTrigger
Expand All @@ -13,7 +14,6 @@ import org.opensearch.alerting.randomQueryLevelTrigger
import org.opensearch.alerting.randomTemplateScript
import org.opensearch.alerting.script.BucketLevelTriggerExecutionContext
import org.opensearch.alerting.script.DocumentLevelTriggerExecutionContext
import org.opensearch.commons.alerting.model.AlertContext
import org.opensearch.test.OpenSearchTestCase

class AlertingUtilsTests : OpenSearchTestCase() {
Expand Down

0 comments on commit 85cf9f1

Please sign in to comment.