Skip to content

Commit

Permalink
add workflow null or empty check only when empty workflow id passed. (#…
Browse files Browse the repository at this point in the history
…1139)

* add workflow null or empty check only when empty workflow id passed. change Rest get alert handler to add empty string singleton list for workflow ids when none is passed

Signed-off-by: Surya Sashank Nistala <[email protected]>

* extract check into method

Signed-off-by: Surya Sashank Nistala <[email protected]>

---------

Signed-off-by: Surya Sashank Nistala <[email protected]>
(cherry picked from commit d6fd871)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Sep 13, 2023
1 parent 9651808 commit bca659a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class RestGetAlertsAction : BaseRestHandler() {
val workflowIds = mutableListOf<String>()
if (workflowId.isNullOrEmpty() == false) {
workflowIds.add(workflowId)
} else {
workflowIds.add("")
}
val table = Table(
sortOrder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import org.opensearch.core.action.ActionListener
import org.opensearch.core.xcontent.NamedXContentRegistry
import org.opensearch.core.xcontent.XContentParser
import org.opensearch.core.xcontent.XContentParserUtils
import org.opensearch.index.query.BoolQueryBuilder
import org.opensearch.index.query.Operator
import org.opensearch.index.query.QueryBuilders
import org.opensearch.search.builder.SearchSourceBuilder
Expand Down Expand Up @@ -114,22 +115,15 @@ class TransportGetAlertsAction @Inject constructor(

if (getAlertsRequest.monitorId != null) {
queryBuilder.filter(QueryBuilders.termQuery("monitor_id", getAlertsRequest.monitorId))
if (getAlertsRequest.workflowIds.isNullOrEmpty()) {
val noWorkflowIdQuery = QueryBuilders.boolQuery()
.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(Alert.WORKFLOW_ID_FIELD)))
.should(QueryBuilders.termsQuery(Alert.WORKFLOW_ID_FIELD, ""))
queryBuilder.must(noWorkflowIdQuery)
}
addWorkflowIdNullOrEmptyCheck(getAlertsRequest, queryBuilder)
} else if (getAlertsRequest.monitorIds.isNullOrEmpty() == false) {
queryBuilder.filter(QueryBuilders.termsQuery("monitor_id", getAlertsRequest.monitorIds))
if (getAlertsRequest.workflowIds.isNullOrEmpty()) {
val noWorkflowIdQuery = QueryBuilders.boolQuery()
.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(Alert.WORKFLOW_ID_FIELD)))
.should(QueryBuilders.termsQuery(Alert.WORKFLOW_ID_FIELD, ""))
queryBuilder.must(noWorkflowIdQuery)
}
addWorkflowIdNullOrEmptyCheck(getAlertsRequest, queryBuilder)
}
if (getAlertsRequest.workflowIds.isNullOrEmpty() == false) {
if (
getAlertsRequest.workflowIds.isNullOrEmpty() == false &&
!(getAlertsRequest.workflowIds!!.size == 1 && getAlertsRequest.workflowIds!![0] == "")
) {
queryBuilder.must(QueryBuilders.termsQuery("workflow_id", getAlertsRequest.workflowIds))
}
if (!tableProp.searchString.isNullOrBlank()) {
Expand Down Expand Up @@ -167,6 +161,21 @@ class TransportGetAlertsAction @Inject constructor(
}
}

// we add this check when we want to fetch alerts for monitors not generated as part of a workflow i.e. non-delegate monitor alerts
private fun addWorkflowIdNullOrEmptyCheck(
getAlertsRequest: GetAlertsRequest,
queryBuilder: BoolQueryBuilder,
) {
if (
getAlertsRequest.workflowIds != null && getAlertsRequest.workflowIds!!.size == 1 && getAlertsRequest.workflowIds!![0] == ""
) {
val noWorkflowIdQuery = QueryBuilders.boolQuery()
.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(Alert.WORKFLOW_ID_FIELD)))
.should(QueryBuilders.termsQuery(Alert.WORKFLOW_ID_FIELD, ""))
queryBuilder.must(noWorkflowIdQuery)
}
}

/** Precedence order for resolving alert index to be queried:
1. alertIndex param.
2. alert index mentioned in monitor data sources.
Expand Down

0 comments on commit bca659a

Please sign in to comment.