From 89b35a34b98497600e5a53f1284a3dc32e85d574 Mon Sep 17 00:00:00 2001 From: AWSHurneyt Date: Mon, 10 Jul 2023 13:42:54 -0700 Subject: [PATCH 1/3] Fixed search monitor API to return alert counts. (#978) * Fixed a bug in the search monitor API when which prevent alert counts from being returned. Signed-off-by: AWSHurneyt * Implemented integration test for frontend use case. Signed-off-by: AWSHurneyt * Fixed ktlint errors. Signed-off-by: AWSHurneyt * Fixed ktlint errors. Signed-off-by: AWSHurneyt * Removed redundant code. Signed-off-by: AWSHurneyt * Fixed ktlint errors. Signed-off-by: AWSHurneyt * Added back check for null query. Signed-off-by: AWSHurneyt --------- Signed-off-by: AWSHurneyt (cherry picked from commit 641d17b2ee0ec5a5fda5b2a68fc01809e9e759c2) --- .../transport/TransportSearchMonitorAction.kt | 12 +- .../alerting/resthandler/MonitorRestApiIT.kt | 114 ++++++++++++++++++ 2 files changed, 125 insertions(+), 1 deletion(-) diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportSearchMonitorAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportSearchMonitorAction.kt index 61713b128..ac8a81a30 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportSearchMonitorAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportSearchMonitorAction.kt @@ -21,6 +21,7 @@ import org.opensearch.cluster.service.ClusterService import org.opensearch.common.inject.Inject import org.opensearch.common.settings.Settings import org.opensearch.commons.alerting.model.Monitor +import org.opensearch.commons.alerting.model.ScheduledJob import org.opensearch.commons.authuser.User import org.opensearch.index.query.BoolQueryBuilder import org.opensearch.index.query.ExistsQueryBuilder @@ -52,9 +53,18 @@ class TransportSearchMonitorAction @Inject constructor( override fun doExecute(task: Task, searchMonitorRequest: SearchMonitorRequest, actionListener: ActionListener) { val searchSourceBuilder = searchMonitorRequest.searchRequest.source() + .seqNoAndPrimaryTerm(true) + .version(true) val queryBuilder = if (searchSourceBuilder.query() == null) BoolQueryBuilder() else QueryBuilders.boolQuery().must(searchSourceBuilder.query()) - queryBuilder.filter(QueryBuilders.existsQuery(Monitor.MONITOR_TYPE)) + + // The SearchMonitor API supports one 'index' parameter of either the SCHEDULED_JOBS_INDEX or ALL_ALERT_INDEX_PATTERN. + // When querying the ALL_ALERT_INDEX_PATTERN, we don't want to check whether the MONITOR_TYPE field exists + // because we're querying alert indexes. + if (searchMonitorRequest.searchRequest.indices().contains(ScheduledJob.SCHEDULED_JOBS_INDEX)) { + queryBuilder.filter(QueryBuilders.existsQuery(Monitor.MONITOR_TYPE)) + } + searchSourceBuilder.query(queryBuilder) .seqNoAndPrimaryTerm(true) .version(true) diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt index 0359f8e03..4b0c74928 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt @@ -54,7 +54,9 @@ import org.opensearch.core.xcontent.XContentBuilder import org.opensearch.index.query.QueryBuilders import org.opensearch.rest.RestStatus import org.opensearch.script.Script +import org.opensearch.search.aggregations.AggregationBuilders import org.opensearch.search.builder.SearchSourceBuilder +import org.opensearch.search.sort.SortOrder import org.opensearch.test.OpenSearchTestCase import org.opensearch.test.junit.annotations.TestLogging import org.opensearch.test.rest.OpenSearchRestTestCase @@ -1264,6 +1266,118 @@ class MonitorRestApiIT : AlertingRestTestCase() { } } + /** + * This use case is needed by the frontend plugin for displaying alert counts on the Monitors list page. + * https://github.com/opensearch-project/alerting-dashboards-plugin/blob/main/server/services/MonitorService.js#L235 + */ + fun `test get acknowledged, active, error, and ignored alerts counts`() { + putAlertMappings() + val monitorAlertCounts = hashMapOf>() + val numMonitors = randomIntBetween(1, 10) + repeat(numMonitors) { + val monitor = createRandomMonitor(refresh = true) + + val numAcknowledgedAlerts = randomIntBetween(1, 10) + val numActiveAlerts = randomIntBetween(1, 10) + var numCompletedAlerts = randomIntBetween(1, 10) + val numErrorAlerts = randomIntBetween(1, 10) + val numIgnoredAlerts = randomIntBetween(1, numCompletedAlerts) + numCompletedAlerts -= numIgnoredAlerts + + val alertCounts = hashMapOf( + Alert.State.ACKNOWLEDGED.name to numAcknowledgedAlerts, + Alert.State.ACTIVE.name to numActiveAlerts, + Alert.State.COMPLETED.name to numCompletedAlerts, + Alert.State.ERROR.name to numErrorAlerts, + "IGNORED" to numIgnoredAlerts + ) + monitorAlertCounts[monitor.id] = alertCounts + + repeat(numAcknowledgedAlerts) { + createAlert(randomAlert(monitor).copy(acknowledgedTime = Instant.now(), state = Alert.State.ACKNOWLEDGED)) + } + repeat(numActiveAlerts) { + createAlert(randomAlert(monitor).copy(state = Alert.State.ACTIVE)) + } + repeat(numCompletedAlerts) { + createAlert(randomAlert(monitor).copy(acknowledgedTime = Instant.now(), state = Alert.State.COMPLETED)) + } + repeat(numErrorAlerts) { + createAlert(randomAlert(monitor).copy(state = Alert.State.ERROR)) + } + repeat(numIgnoredAlerts) { + createAlert(randomAlert(monitor).copy(acknowledgedTime = null, state = Alert.State.COMPLETED)) + } + } + + val sourceBuilder = SearchSourceBuilder() + .size(0) + .query(QueryBuilders.termsQuery("monitor_id", monitorAlertCounts.keys)) + .aggregation( + AggregationBuilders + .terms("uniq_monitor_ids").field("monitor_id") + .subAggregation(AggregationBuilders.filter("active", QueryBuilders.termQuery("state", "ACTIVE"))) + .subAggregation(AggregationBuilders.filter("acknowledged", QueryBuilders.termQuery("state", "ACKNOWLEDGED"))) + .subAggregation(AggregationBuilders.filter("errors", QueryBuilders.termQuery("state", "ERROR"))) + .subAggregation( + AggregationBuilders.filter( + "ignored", + QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery("state", "COMPLETED")) + .mustNot(QueryBuilders.existsQuery("acknowledged_time")) + ) + ) + .subAggregation(AggregationBuilders.max("last_notification_time").field("last_notification_time")) + .subAggregation( + AggregationBuilders.topHits("latest_alert") + .size(1) + .sort("start_time", SortOrder.DESC) + .fetchSource(arrayOf("last_notification_time", "trigger_name"), null) + ) + ) + + val searchResponse = client().makeRequest( + "GET", + "$ALERTING_BASE_URI/_search", + hashMapOf("index" to AlertIndices.ALL_ALERT_INDEX_PATTERN), + StringEntity(sourceBuilder.toString(), ContentType.APPLICATION_JSON) + ) + val xcp = createParser(XContentType.JSON.xContent(), searchResponse.entity.content).map() + val aggregations = (xcp["aggregations"]!! as Map>) + val uniqMonitorIds = aggregations["uniq_monitor_ids"]!! + val buckets = uniqMonitorIds["buckets"]!! as ArrayList> + + assertEquals("Incorrect number of monitors returned", monitorAlertCounts.keys.size, buckets.size) + buckets.forEach { bucket -> + val id = bucket["key"]!! + val monitorCounts = monitorAlertCounts[id]!! + + val acknowledged = (bucket["acknowledged"]!! as Map)["doc_count"]!! + assertEquals( + "Incorrect ${Alert.State.ACKNOWLEDGED} count returned for monitor $id", + monitorCounts[Alert.State.ACKNOWLEDGED.name], acknowledged + ) + + val active = (bucket["active"]!! as Map)["doc_count"]!! + assertEquals( + "Incorrect ${Alert.State.ACTIVE} count returned for monitor $id", + monitorCounts[Alert.State.ACTIVE.name], active + ) + + val errors = (bucket["errors"]!! as Map)["doc_count"]!! + assertEquals( + "Incorrect ${Alert.State.ERROR} count returned for monitor $id", + monitorCounts[Alert.State.ERROR.name], errors + ) + + val ignored = (bucket["ignored"]!! as Map)["doc_count"]!! + assertEquals( + "Incorrect IGNORED count returned for monitor $id", + monitorCounts["IGNORED"], ignored + ) + } + } + private fun validateAlertingStatsNodeResponse(nodesResponse: Map) { assertEquals("Incorrect number of nodes", numberOfNodes, nodesResponse["total"]) assertEquals("Failed nodes found during monitor stats call", 0, nodesResponse["failed"]) From 3dc47782ffcd488fb037c079918b151a769e7898 Mon Sep 17 00:00:00 2001 From: Ashish Agrawal Date: Mon, 10 Jul 2023 16:12:57 -0700 Subject: [PATCH 2/3] Resolve string issues from core (#987) Signed-off-by: Ashish Agrawal --- .../main/kotlin/org/opensearch/alerting/AlertService.kt | 2 +- .../main/kotlin/org/opensearch/alerting/MonitorRunner.kt | 2 +- .../kotlin/org/opensearch/alerting/alerts/AlertIndices.kt | 4 ++-- .../org/opensearch/alerting/model/destination/Chime.kt | 3 +-- .../alerting/model/destination/CustomWebhook.kt | 3 +-- .../org/opensearch/alerting/model/destination/Slack.kt | 3 +-- .../alerting/model/destination/email/EmailGroup.kt | 2 +- .../alerting/transport/TransportGetDestinationsAction.kt | 2 +- .../alerting/transport/TransportGetFindingsAction.kt | 2 +- .../org/opensearch/alerting/util/AlertingException.kt | 2 +- .../org/opensearch/alerting/util/AnomalyDetectionUtils.kt | 2 +- .../kotlin/org/opensearch/alerting/util/IndexUtils.kt | 4 ++-- .../org/opensearch/alerting/util/RestHandlerUtils.kt | 4 ++-- .../destinationmigration/DestinationConversionUtils.kt | 2 +- .../DestinationMigrationUtilService.kt | 2 +- .../alerting/transport/AlertingSingleNodeTestCase.kt | 8 ++++---- .../core/resthandler/RestScheduledJobStatsHandler.kt | 2 +- 17 files changed, 23 insertions(+), 26 deletions(-) diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/AlertService.kt b/alerting/src/main/kotlin/org/opensearch/alerting/AlertService.kt index a8a5601ff..47eed6010 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/AlertService.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/AlertService.kt @@ -219,7 +219,7 @@ class AlertService( return Alert( id = id, monitor = monitor, trigger = NoOpTrigger(), startTime = currentTime, lastNotificationTime = currentTime, state = Alert.State.ERROR, errorMessage = alertError?.message, - schemaVersion = IndexUtils.alertIndexSchemaVersion + schemaVersion = IndexUtils.alertIndexSchemaVersion, executionId = "" ) } diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/MonitorRunner.kt b/alerting/src/main/kotlin/org/opensearch/alerting/MonitorRunner.kt index 419962578..65901fc69 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/MonitorRunner.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/MonitorRunner.kt @@ -26,11 +26,11 @@ import org.opensearch.alerting.util.isAllowed import org.opensearch.alerting.util.isTestAction import org.opensearch.alerting.workflow.WorkflowRunContext import org.opensearch.client.node.NodeClient -import org.opensearch.common.Strings import org.opensearch.commons.alerting.model.Monitor import org.opensearch.commons.alerting.model.Table import org.opensearch.commons.alerting.model.action.Action import org.opensearch.commons.notifications.model.NotificationConfigInfo +import org.opensearch.core.common.Strings import java.time.Instant abstract class MonitorRunner { diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/alerts/AlertIndices.kt b/alerting/src/main/kotlin/org/opensearch/alerting/alerts/AlertIndices.kt index 25ef422e8..b49004e84 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/alerts/AlertIndices.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/alerts/AlertIndices.kt @@ -488,7 +488,7 @@ class AlertIndices( clusterStateRequest, object : ActionListener { override fun onResponse(clusterStateResponse: ClusterStateResponse) { - if (!clusterStateResponse.state.metadata.indices.isEmpty) { + if (clusterStateResponse.state.metadata.indices.isNotEmpty()) { val indicesToDelete = getIndicesToDelete(clusterStateResponse) logger.info("Deleting old $tag indices viz $indicesToDelete") deleteAllOldHistoryIndices(indicesToDelete) @@ -523,7 +523,7 @@ class AlertIndices( ): String? { val creationTime = indexMetadata.creationDate if ((Instant.now().toEpochMilli() - creationTime) > retentionPeriodMillis) { - val alias = indexMetadata.aliases.firstOrNull { writeIndex == it.value.alias } + val alias = indexMetadata.aliases.entries.firstOrNull { writeIndex == it.value.alias } if (alias != null) { if (historyEnabled) { // If the index has the write alias and history is enabled, don't delete the index diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/Chime.kt b/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/Chime.kt index 16a92cd8a..8ef48742a 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/Chime.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/Chime.kt @@ -5,15 +5,14 @@ package org.opensearch.alerting.model.destination -import org.opensearch.common.Strings import org.opensearch.common.io.stream.StreamInput import org.opensearch.common.io.stream.StreamOutput import org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken +import org.opensearch.core.common.Strings import org.opensearch.core.xcontent.ToXContent import org.opensearch.core.xcontent.XContentBuilder import org.opensearch.core.xcontent.XContentParser import java.io.IOException -import java.lang.IllegalStateException /** * A value object that represents a Chime message. Chime message will be diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/CustomWebhook.kt b/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/CustomWebhook.kt index e7eb6daa7..7adbf7526 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/CustomWebhook.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/CustomWebhook.kt @@ -5,15 +5,14 @@ package org.opensearch.alerting.model.destination -import org.opensearch.common.Strings import org.opensearch.common.io.stream.StreamInput import org.opensearch.common.io.stream.StreamOutput import org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken +import org.opensearch.core.common.Strings import org.opensearch.core.xcontent.ToXContent import org.opensearch.core.xcontent.XContentBuilder import org.opensearch.core.xcontent.XContentParser import java.io.IOException -import java.lang.IllegalStateException /** * A value object that represents a Custom webhook message. Webhook message will be diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/Slack.kt b/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/Slack.kt index 0f41fa41d..aecd61a73 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/Slack.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/Slack.kt @@ -5,15 +5,14 @@ package org.opensearch.alerting.model.destination -import org.opensearch.common.Strings import org.opensearch.common.io.stream.StreamInput import org.opensearch.common.io.stream.StreamOutput import org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken +import org.opensearch.core.common.Strings import org.opensearch.core.xcontent.ToXContent import org.opensearch.core.xcontent.XContentBuilder import org.opensearch.core.xcontent.XContentParser import java.io.IOException -import java.lang.IllegalStateException /** * A value object that represents a Slack message. Slack message will be diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/email/EmailGroup.kt b/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/email/EmailGroup.kt index 59c088e7d..fc7b16767 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/email/EmailGroup.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/model/destination/email/EmailGroup.kt @@ -6,12 +6,12 @@ package org.opensearch.alerting.model.destination.email import org.opensearch.alerting.util.isValidEmail -import org.opensearch.common.Strings import org.opensearch.common.io.stream.StreamInput import org.opensearch.common.io.stream.StreamOutput import org.opensearch.common.io.stream.Writeable import org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken import org.opensearch.commons.alerting.util.IndexUtils.Companion.NO_SCHEMA_VERSION +import org.opensearch.core.common.Strings import org.opensearch.core.xcontent.ToXContent import org.opensearch.core.xcontent.XContentBuilder import org.opensearch.core.xcontent.XContentParser diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetDestinationsAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetDestinationsAction.kt index 52283c296..353431b4b 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetDestinationsAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetDestinationsAction.kt @@ -20,7 +20,6 @@ import org.opensearch.alerting.settings.AlertingSettings import org.opensearch.alerting.util.AlertingException import org.opensearch.client.Client import org.opensearch.cluster.service.ClusterService -import org.opensearch.common.Strings import org.opensearch.common.inject.Inject import org.opensearch.common.settings.Settings import org.opensearch.common.xcontent.LoggingDeprecationHandler @@ -29,6 +28,7 @@ import org.opensearch.common.xcontent.XContentParserUtils import org.opensearch.common.xcontent.XContentType import org.opensearch.commons.alerting.model.ScheduledJob import org.opensearch.commons.authuser.User +import org.opensearch.core.common.Strings import org.opensearch.core.xcontent.NamedXContentRegistry import org.opensearch.core.xcontent.XContentParser import org.opensearch.index.query.Operator diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetFindingsAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetFindingsAction.kt index 0f8ff205c..532616fb6 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetFindingsAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportGetFindingsAction.kt @@ -28,7 +28,6 @@ import org.opensearch.alerting.settings.AlertingSettings import org.opensearch.alerting.util.AlertingException import org.opensearch.client.Client import org.opensearch.cluster.service.ClusterService -import org.opensearch.common.Strings import org.opensearch.common.inject.Inject import org.opensearch.common.settings.Settings import org.opensearch.common.xcontent.LoggingDeprecationHandler @@ -42,6 +41,7 @@ import org.opensearch.commons.alerting.model.Finding import org.opensearch.commons.alerting.model.FindingDocument import org.opensearch.commons.alerting.model.FindingWithDocs import org.opensearch.commons.utils.recreateObject +import org.opensearch.core.common.Strings import org.opensearch.core.xcontent.NamedXContentRegistry import org.opensearch.core.xcontent.XContentParser import org.opensearch.index.query.Operator diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/util/AlertingException.kt b/alerting/src/main/kotlin/org/opensearch/alerting/util/AlertingException.kt index 4df774b6d..a86f571d4 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/util/AlertingException.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/util/AlertingException.kt @@ -9,7 +9,7 @@ import org.apache.logging.log4j.LogManager import org.opensearch.OpenSearchException import org.opensearch.OpenSearchSecurityException import org.opensearch.OpenSearchStatusException -import org.opensearch.common.Strings +import org.opensearch.core.common.Strings import org.opensearch.index.IndexNotFoundException import org.opensearch.index.engine.VersionConflictEngineException import org.opensearch.indices.InvalidIndexNameException diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/util/AnomalyDetectionUtils.kt b/alerting/src/main/kotlin/org/opensearch/alerting/util/AnomalyDetectionUtils.kt index 1196c8f19..e83f45a15 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/util/AnomalyDetectionUtils.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/util/AnomalyDetectionUtils.kt @@ -6,10 +6,10 @@ package org.opensearch.alerting.util import org.apache.lucene.search.join.ScoreMode -import org.opensearch.common.Strings import org.opensearch.commons.alerting.model.Monitor import org.opensearch.commons.alerting.model.SearchInput import org.opensearch.commons.authuser.User +import org.opensearch.core.common.Strings import org.opensearch.index.query.BoolQueryBuilder import org.opensearch.index.query.NestedQueryBuilder import org.opensearch.index.query.QueryBuilders diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/util/IndexUtils.kt b/alerting/src/main/kotlin/org/opensearch/alerting/util/IndexUtils.kt index 9edf19f08..d0d7771f0 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/util/IndexUtils.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/util/IndexUtils.kt @@ -100,7 +100,7 @@ class IndexUtils { @JvmStatic fun getIndexNameWithAlias(clusterState: ClusterState, alias: String): String { - return clusterState.metadata.indices.first { it.value.aliases.containsKey(alias) }.key + return clusterState.metadata.indices.entries.first { it.value.aliases.containsKey(alias) }.key } @JvmStatic @@ -127,7 +127,7 @@ class IndexUtils { actionListener: ActionListener ) { if (clusterState.metadata.indices.containsKey(index)) { - if (shouldUpdateIndex(clusterState.metadata.indices[index], mapping)) { + if (shouldUpdateIndex(clusterState.metadata.indices[index]!!, mapping)) { val putMappingRequest: PutMappingRequest = PutMappingRequest(index).source(mapping, XContentType.JSON) client.putMapping(putMappingRequest, actionListener) } else { diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/util/RestHandlerUtils.kt b/alerting/src/main/kotlin/org/opensearch/alerting/util/RestHandlerUtils.kt index 7c72aae9a..b5aeaa542 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/util/RestHandlerUtils.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/util/RestHandlerUtils.kt @@ -6,7 +6,7 @@ package org.opensearch.alerting.util import org.opensearch.alerting.AlertingPlugin -import org.opensearch.common.Strings +import org.opensearch.core.common.Strings import org.opensearch.rest.RestRequest import org.opensearch.search.fetch.subphase.FetchSourceContext @@ -18,7 +18,7 @@ import org.opensearch.search.fetch.subphase.FetchSourceContext * @return FetchSourceContext */ fun context(request: RestRequest): FetchSourceContext? { - val userAgent = Strings.coalesceToEmpty(request.header("User-Agent")) + val userAgent = if (request.header("User-Agent") == null) "" else request.header("User-Agent") return if (!userAgent.contains(AlertingPlugin.OPEN_SEARCH_DASHBOARDS_USER_AGENT)) { FetchSourceContext(true, Strings.EMPTY_ARRAY, AlertingPlugin.UI_METADATA_EXCLUDE) } else null diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/util/destinationmigration/DestinationConversionUtils.kt b/alerting/src/main/kotlin/org/opensearch/alerting/util/destinationmigration/DestinationConversionUtils.kt index 0e5185885..667548c60 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/util/destinationmigration/DestinationConversionUtils.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/util/destinationmigration/DestinationConversionUtils.kt @@ -10,7 +10,6 @@ import org.opensearch.alerting.model.destination.Destination import org.opensearch.alerting.model.destination.email.EmailAccount import org.opensearch.alerting.model.destination.email.Recipient import org.opensearch.alerting.util.DestinationType -import org.opensearch.common.Strings import org.opensearch.commons.notifications.model.Chime import org.opensearch.commons.notifications.model.ConfigType import org.opensearch.commons.notifications.model.Email @@ -22,6 +21,7 @@ import org.opensearch.commons.notifications.model.NotificationConfig import org.opensearch.commons.notifications.model.Slack import org.opensearch.commons.notifications.model.SmtpAccount import org.opensearch.commons.notifications.model.Webhook +import org.opensearch.core.common.Strings import java.net.URI import java.net.URISyntaxException import java.util.Locale diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/util/destinationmigration/DestinationMigrationUtilService.kt b/alerting/src/main/kotlin/org/opensearch/alerting/util/destinationmigration/DestinationMigrationUtilService.kt index f71d413b3..1a87c8a67 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/util/destinationmigration/DestinationMigrationUtilService.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/util/destinationmigration/DestinationMigrationUtilService.kt @@ -21,7 +21,6 @@ import org.opensearch.alerting.util.destinationmigration.DestinationConversionUt import org.opensearch.alerting.util.destinationmigration.DestinationConversionUtils.Companion.convertEmailGroupToNotificationConfig import org.opensearch.alerting.util.destinationmigration.NotificationApiUtils.Companion.createNotificationConfig import org.opensearch.client.node.NodeClient -import org.opensearch.common.Strings import org.opensearch.common.xcontent.LoggingDeprecationHandler import org.opensearch.common.xcontent.XContentFactory import org.opensearch.common.xcontent.XContentParserUtils @@ -31,6 +30,7 @@ import org.opensearch.commons.alerting.model.ScheduledJob import org.opensearch.commons.notifications.action.CreateNotificationConfigRequest import org.opensearch.commons.notifications.model.NotificationConfig import org.opensearch.commons.notifications.model.NotificationConfigInfo +import org.opensearch.core.common.Strings import org.opensearch.core.xcontent.NamedXContentRegistry import org.opensearch.core.xcontent.XContentParser import org.opensearch.index.query.QueryBuilders diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/transport/AlertingSingleNodeTestCase.kt b/alerting/src/test/kotlin/org/opensearch/alerting/transport/AlertingSingleNodeTestCase.kt index 98a8abcff..f4e58f328 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/transport/AlertingSingleNodeTestCase.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/transport/AlertingSingleNodeTestCase.kt @@ -193,8 +193,8 @@ abstract class AlertingSingleNodeTestCase : OpenSearchSingleNodeTestCase() { protected fun assertAliasNotExists(alias: String) { val aliasesResponse = client().admin().indices().getAliases(GetAliasesRequest()).get() - val foundAlias = aliasesResponse.aliases.values().forEach { - it.value.forEach { + val foundAlias = aliasesResponse.aliases.values.forEach { + it.forEach { if (it.alias == alias) { fail("alias exists, but it shouldn't") } @@ -204,8 +204,8 @@ abstract class AlertingSingleNodeTestCase : OpenSearchSingleNodeTestCase() { protected fun assertAliasExists(alias: String) { val aliasesResponse = client().admin().indices().getAliases(GetAliasesRequest()).get() - val foundAlias = aliasesResponse.aliases.values().forEach { - it.value.forEach { + val foundAlias = aliasesResponse.aliases.values.forEach { + it.forEach { if (it.alias == alias) { return } diff --git a/core/src/main/kotlin/org/opensearch/alerting/core/resthandler/RestScheduledJobStatsHandler.kt b/core/src/main/kotlin/org/opensearch/alerting/core/resthandler/RestScheduledJobStatsHandler.kt index 9835cd4f1..c4f800ab3 100644 --- a/core/src/main/kotlin/org/opensearch/alerting/core/resthandler/RestScheduledJobStatsHandler.kt +++ b/core/src/main/kotlin/org/opensearch/alerting/core/resthandler/RestScheduledJobStatsHandler.kt @@ -8,7 +8,7 @@ package org.opensearch.alerting.core.resthandler import org.opensearch.alerting.core.action.node.ScheduledJobsStatsAction import org.opensearch.alerting.core.action.node.ScheduledJobsStatsRequest import org.opensearch.client.node.NodeClient -import org.opensearch.common.Strings +import org.opensearch.core.common.Strings import org.opensearch.rest.BaseRestHandler import org.opensearch.rest.RestHandler import org.opensearch.rest.RestHandler.Route From ba47726f104c513399a5b63b3623da91d3b3b4ed Mon Sep 17 00:00:00 2001 From: AWSHurneyt Date: Mon, 10 Jul 2023 16:57:31 -0700 Subject: [PATCH 3/3] Fixed string entity call. Signed-off-by: AWSHurneyt --- .../org/opensearch/alerting/resthandler/MonitorRestApiIT.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt index 4b0c74928..398d563b8 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt @@ -1340,7 +1340,7 @@ class MonitorRestApiIT : AlertingRestTestCase() { "GET", "$ALERTING_BASE_URI/_search", hashMapOf("index" to AlertIndices.ALL_ALERT_INDEX_PATTERN), - StringEntity(sourceBuilder.toString(), ContentType.APPLICATION_JSON) + NStringEntity(sourceBuilder.toString(), ContentType.APPLICATION_JSON) ) val xcp = createParser(XContentType.JSON.xContent(), searchResponse.entity.content).map() val aggregations = (xcp["aggregations"]!! as Map>)