Skip to content

Commit

Permalink
Fixed ktlint errors.
Browse files Browse the repository at this point in the history
Signed-off-by: AWSHurneyt <[email protected]>
  • Loading branch information
AWSHurneyt committed Mar 13, 2024
1 parent fddb116 commit 4bd853c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,12 @@ class InputService(

// Deep copying query before passing it to rewriteQuery since otherwise, the monitor.input is modified directly
// which causes a strange bug where the rewritten query persists on the Monitor across executions
val rewrittenQuery = AggregationQueryRewriter.rewriteQuery(deepCopyQuery(searchInput.query), prevResult, monitor.triggers, returnSampleDocs)
val rewrittenQuery = AggregationQueryRewriter.rewriteQuery(
deepCopyQuery(searchInput.query),
prevResult,
monitor.triggers,
returnSampleDocs
)

// Rewrite query to consider the doc ids per given index
if (chainedFindingExist(matchingDocIdsPerIndex) && rewrittenQuery.query() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ class AggregationQueryRewriter {
/**
* Optionally adds support for returning sample documents for each bucket of data returned for a bucket level monitor.
*/
fun rewriteQuery(query: SearchSourceBuilder, prevResult: InputRunResults?, triggers: List<Trigger>, returnSampleDocs: Boolean = false): SearchSourceBuilder {
fun rewriteQuery(
query: SearchSourceBuilder,
prevResult: InputRunResults?,
triggers: List<Trigger>,
returnSampleDocs: Boolean = false
): SearchSourceBuilder {
triggers.forEach { trigger ->
if (trigger is BucketLevelTrigger) {
// add bucket selector pipeline aggregation for each trigger in query
Expand Down Expand Up @@ -69,7 +74,13 @@ class AggregationQueryRewriter {
val docFieldTags = parseSampleDocTags(listOf(trigger))
val sampleDocsAgg = getSampleDocAggs(factory)
sampleDocsAgg.forEach { agg ->
if (docFieldTags.isNotEmpty()) agg.fetchSource(FetchSourceContext(true, docFieldTags.toTypedArray(), emptyArray()))
if (docFieldTags.isNotEmpty()) agg.fetchSource(
FetchSourceContext(
true,
docFieldTags.toTypedArray(),
emptyArray()
)
)
if (!factory.subAggregations.contains(agg)) factory.subAggregation(agg)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2447,7 +2447,9 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() {
val actionExecutionPolicy = ActionExecutionPolicy(actionExecutionScope)
val actions = (0..randomInt(10)).map {
randomActionWithPolicy(
template = randomTemplateScript("{{#ctx.alerts}}\n{{#associated_queries}}\n(name={{name}})\n{{/associated_queries}}\n{{/ctx.alerts}}"),
template = randomTemplateScript(
"{{#ctx.alerts}}\n{{#associated_queries}}\n(name={{name}})\n{{/associated_queries}}\n{{/ctx.alerts}}"
),
destinationId = createDestination().id,
actionExecutionPolicy = actionExecutionPolicy
)
Expand Down Expand Up @@ -2477,7 +2479,8 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() {
for (alertActionResult in triggerResult.objectMap("action_results").values) {
assertEquals(actions.size, alertActionResult.values.size)
for (actionResult in alertActionResult.values) {
@Suppress("UNCHECKED_CAST") val actionOutput = (actionResult as Map<String, Map<String, String>>)["output"] as Map<String, String>
@Suppress("UNCHECKED_CAST")
val actionOutput = (actionResult as Map<String, Map<String, String>>)["output"] as Map<String, String>
assertTrue(
"The notification message is missing the query name.",
actionOutput["message"]!!.contains("(name=${docQuery.name})")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ class AlertContextTests : OpenSearchTestCase() {
associatedQueries.size,
(templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD] as List<DocLevelQuery>).size
)
assertEquals("Template associated queries do not match", formatAssociatedQueries(alertContext), templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD])
assertEquals(
"Template associated queries do not match",
formatAssociatedQueries(alertContext),
templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD]
)
assertNull("Template sample docs should be null", templateArgs[AlertContext.SAMPLE_DOCS_FIELD])
}

Expand All @@ -126,7 +130,11 @@ class AlertContextTests : OpenSearchTestCase() {
associatedQueries.size,
(templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD] as List<DocLevelQuery>).size
)
assertEquals("Template associated queries do not match", formatAssociatedQueries(alertContext), templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD])
assertEquals(
"Template associated queries do not match",
formatAssociatedQueries(alertContext),
templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD]
)
assertNull("Template sample docs should be null", templateArgs[AlertContext.SAMPLE_DOCS_FIELD])
}

Expand All @@ -146,7 +154,11 @@ class AlertContextTests : OpenSearchTestCase() {
associatedQueries.size,
(templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD] as List<DocLevelQuery>).size
)
assertEquals("Template associated queries do not match", formatAssociatedQueries(alertContext), templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD])
assertEquals(
"Template associated queries do not match",
formatAssociatedQueries(alertContext),
templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD]
)
assertNull("Template sample docs should be null", templateArgs[AlertContext.SAMPLE_DOCS_FIELD])
}

Expand All @@ -166,7 +178,11 @@ class AlertContextTests : OpenSearchTestCase() {
associatedQueries.size,
(templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD] as List<DocLevelQuery>).size
)
assertEquals("Template associated queries do not match", formatAssociatedQueries(alertContext), templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD])
assertEquals(
"Template associated queries do not match",
formatAssociatedQueries(alertContext),
templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD]
)

assertEquals(
"Template args sample docs should have size ${sampleDocs.size}",
Expand All @@ -192,7 +208,11 @@ class AlertContextTests : OpenSearchTestCase() {
associatedQueries.size,
(templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD] as List<DocLevelQuery>).size
)
assertEquals("Template associated queries do not match", formatAssociatedQueries(alertContext), templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD])
assertEquals(
"Template associated queries do not match",
formatAssociatedQueries(alertContext),
templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD]
)

assertEquals(
"Template args sample docs should have size ${sampleDocs.size}",
Expand All @@ -218,7 +238,11 @@ class AlertContextTests : OpenSearchTestCase() {
associatedQueries.size,
(templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD] as List<DocLevelQuery>).size
)
assertEquals("Template associated queries do not match", formatAssociatedQueries(alertContext), templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD])
assertEquals(
"Template associated queries do not match",
formatAssociatedQueries(alertContext),
templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD]
)

assertEquals(
"Template args sample docs should have size ${sampleDocs.size}",
Expand All @@ -244,7 +268,11 @@ class AlertContextTests : OpenSearchTestCase() {
associatedQueries.size,
(templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD] as List<DocLevelQuery>).size
)
assertEquals("Template associated queries do not match", formatAssociatedQueries(alertContext), templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD])
assertEquals(
"Template associated queries do not match",
formatAssociatedQueries(alertContext),
templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD]
)

assertEquals(
"Template args sample docs should have size ${sampleDocs.size}",
Expand All @@ -270,7 +298,11 @@ class AlertContextTests : OpenSearchTestCase() {
associatedQueries.size,
(templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD] as List<DocLevelQuery>).size
)
assertEquals("Template associated queries do not match", formatAssociatedQueries(alertContext), templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD])
assertEquals(
"Template associated queries do not match",
formatAssociatedQueries(alertContext),
templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD]
)

assertEquals(
"Template args sample docs should have size ${sampleDocs.size}",
Expand All @@ -296,7 +328,11 @@ class AlertContextTests : OpenSearchTestCase() {
associatedQueries.size,
(templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD] as List<DocLevelQuery>).size
)
assertEquals("Template associated queries do not match", formatAssociatedQueries(alertContext), templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD])
assertEquals(
"Template associated queries do not match",
formatAssociatedQueries(alertContext),
templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD]
)

assertEquals(
"Template args sample docs should have size ${sampleDocs.size}",
Expand All @@ -322,7 +358,11 @@ class AlertContextTests : OpenSearchTestCase() {
associatedQueries.size,
(templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD] as List<DocLevelQuery>).size
)
assertEquals("Template associated queries do not match", formatAssociatedQueries(alertContext), templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD])
assertEquals(
"Template associated queries do not match",
formatAssociatedQueries(alertContext),
templateArgs[AlertContext.ASSOCIATED_QUERIES_FIELD]
)

assertEquals(
"Template args sample docs should have size ${sampleDocs.size}",
Expand All @@ -342,7 +382,11 @@ class AlertContextTests : OpenSearchTestCase() {
assertEquals("Template args start time does not", alertContext.alert.startTime.toEpochMilli(), templateArgs[Alert.START_TIME_FIELD])
assertEquals("Template args last notification time does not match", templateArgs[Alert.LAST_NOTIFICATION_TIME_FIELD], null)
assertEquals("Template args severity does not match", alertContext.alert.severity, templateArgs[Alert.SEVERITY_FIELD])
assertEquals("Template args clusters does not match", alertContext.alert.clusters?.joinToString(","), templateArgs[Alert.CLUSTERS_FIELD])
assertEquals(
"Template args clusters does not match",
alertContext.alert.clusters?.joinToString(","),
templateArgs[Alert.CLUSTERS_FIELD]
)
}

private fun formatAssociatedQueries(alertContext: AlertContext): List<Map<String, Any>>? {
Expand Down

0 comments on commit 4bd853c

Please sign in to comment.