Skip to content

Commit

Permalink
fix execution id field in alerts (#429)
Browse files Browse the repository at this point in the history
Signed-off-by: Surya Sashank Nistala <[email protected]>
  • Loading branch information
eirsep authored May 16, 2023
1 parent c4e5825 commit 1cd59fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
38 changes: 19 additions & 19 deletions src/main/kotlin/org/opensearch/commons/alerting/model/Alert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ data class Alert(
val severity: String,
val actionExecutionResults: List<ActionExecutionResult>,
val aggregationResultBucket: AggregationResultBucket? = null,
val workflowExecutionId: String? = null,
val executionId: String? = null,
) : Writeable, ToXContent {

init {
Expand All @@ -54,7 +54,7 @@ data class Alert(
state: State = State.ACTIVE,
errorMessage: String? = null,
schemaVersion: Int = NO_SCHEMA_VERSION,
workflowExecutionId: String,
executionId: String,
chainedAlertTrigger: ChainedAlertTrigger,
user: User
) : this(
Expand All @@ -63,7 +63,7 @@ data class Alert(
lastNotificationTime = lastNotificationTime, errorMessage = errorMessage, errorHistory = emptyList(),
severity = chainedAlertTrigger.severity, actionExecutionResults = emptyList(), schemaVersion = schemaVersion,
aggregationResultBucket = null, findingIds = emptyList(), relatedDocIds = emptyList(),
workflowExecutionId = workflowExecutionId
executionId = executionId
)

constructor(
Expand All @@ -76,14 +76,14 @@ data class Alert(
errorHistory: List<AlertError> = mutableListOf(),
actionExecutionResults: List<ActionExecutionResult> = mutableListOf(),
schemaVersion: Int = NO_SCHEMA_VERSION,
workflowExecutionId: String? = null
executionId: String? = null
) : this(
monitorId = monitor.id, monitorName = monitor.name, monitorVersion = monitor.version, monitorUser = monitor.user,
triggerId = trigger.id, triggerName = trigger.name, state = state, startTime = startTime,
lastNotificationTime = lastNotificationTime, errorMessage = errorMessage, errorHistory = errorHistory,
severity = trigger.severity, actionExecutionResults = actionExecutionResults, schemaVersion = schemaVersion,
aggregationResultBucket = null, findingIds = emptyList(), relatedDocIds = emptyList(),
workflowExecutionId = workflowExecutionId
executionId = executionId
)

constructor(
Expand All @@ -97,14 +97,14 @@ data class Alert(
actionExecutionResults: List<ActionExecutionResult> = mutableListOf(),
schemaVersion: Int = NO_SCHEMA_VERSION,
findingIds: List<String> = emptyList(),
workflowExecutionId: String? = null
executionId: String? = null
) : this(
monitorId = monitor.id, monitorName = monitor.name, monitorVersion = monitor.version, monitorUser = monitor.user,
triggerId = trigger.id, triggerName = trigger.name, state = state, startTime = startTime,
lastNotificationTime = lastNotificationTime, errorMessage = errorMessage, errorHistory = errorHistory,
severity = trigger.severity, actionExecutionResults = actionExecutionResults, schemaVersion = schemaVersion,
aggregationResultBucket = null, findingIds = findingIds, relatedDocIds = emptyList(),
workflowExecutionId = workflowExecutionId
executionId = executionId
)

constructor(
Expand All @@ -119,14 +119,14 @@ data class Alert(
schemaVersion: Int = NO_SCHEMA_VERSION,
aggregationResultBucket: AggregationResultBucket,
findingIds: List<String> = emptyList(),
workflowExecutionId: String? = null
executionId: String? = null
) : this(
monitorId = monitor.id, monitorName = monitor.name, monitorVersion = monitor.version, monitorUser = monitor.user,
triggerId = trigger.id, triggerName = trigger.name, state = state, startTime = startTime,
lastNotificationTime = lastNotificationTime, errorMessage = errorMessage, errorHistory = errorHistory,
severity = trigger.severity, actionExecutionResults = actionExecutionResults, schemaVersion = schemaVersion,
aggregationResultBucket = aggregationResultBucket, findingIds = findingIds, relatedDocIds = emptyList(),
workflowExecutionId = workflowExecutionId
executionId = executionId
)

constructor(
Expand All @@ -142,14 +142,14 @@ data class Alert(
errorHistory: List<AlertError> = mutableListOf(),
actionExecutionResults: List<ActionExecutionResult> = mutableListOf(),
schemaVersion: Int = NO_SCHEMA_VERSION,
workflowExecutionId: String? = null
executionId: String? = null
) : this(
id = id, monitorId = monitor.id, monitorName = monitor.name, monitorVersion = monitor.version, monitorUser = monitor.user,
triggerId = trigger.id, triggerName = trigger.name, state = state, startTime = startTime,
lastNotificationTime = lastNotificationTime, errorMessage = errorMessage, errorHistory = errorHistory,
severity = trigger.severity, actionExecutionResults = actionExecutionResults, schemaVersion = schemaVersion,
aggregationResultBucket = null, findingIds = findingIds, relatedDocIds = relatedDocIds,
workflowExecutionId = workflowExecutionId
executionId = executionId
)

enum class State {
Expand Down Expand Up @@ -181,7 +181,7 @@ data class Alert(
severity = sin.readString(),
actionExecutionResults = sin.readList(::ActionExecutionResult),
aggregationResultBucket = if (sin.readBoolean()) AggregationResultBucket(sin) else null,
workflowExecutionId = sin.readOptionalString()
executionId = sin.readOptionalString()
)

fun isAcknowledged(): Boolean = (state == State.ACKNOWLEDGED)
Expand Down Expand Up @@ -215,7 +215,7 @@ data class Alert(
} else {
out.writeBoolean(false)
}
out.writeOptionalString(workflowExecutionId)
out.writeOptionalString(executionId)
}

companion object {
Expand All @@ -240,7 +240,7 @@ data class Alert(
const val ALERT_HISTORY_FIELD = "alert_history"
const val SEVERITY_FIELD = "severity"
const val ACTION_EXECUTION_RESULTS_FIELD = "action_execution_results"
const val WORKFLOW_EXECUTION_ID_FIELD = "workflow_execution_id"
const val EXECUTION_ID_FIELD = "execution_id"
const val BUCKET_KEYS = AggregationResultBucket.BUCKET_KEYS
const val PARENTS_BUCKET_PATH = AggregationResultBucket.PARENTS_BUCKET_PATH
const val NO_ID = ""
Expand All @@ -266,7 +266,7 @@ data class Alert(
var lastNotificationTime: Instant? = null
var acknowledgedTime: Instant? = null
var errorMessage: String? = null
var workflowExecutionId: String? = null
var executionId: String? = null
val errorHistory: MutableList<AlertError> = mutableListOf()
val actionExecutionResults: MutableList<ActionExecutionResult> = mutableListOf()
var aggAlertBucket: AggregationResultBucket? = null
Expand Down Expand Up @@ -301,7 +301,7 @@ data class Alert(
LAST_NOTIFICATION_TIME_FIELD -> lastNotificationTime = xcp.instant()
ACKNOWLEDGED_TIME_FIELD -> acknowledgedTime = xcp.instant()
ERROR_MESSAGE_FIELD -> errorMessage = xcp.textOrNull()
WORKFLOW_EXECUTION_ID_FIELD -> workflowExecutionId = xcp.textOrNull()
EXECUTION_ID_FIELD -> executionId = xcp.textOrNull()
ALERT_HISTORY_FIELD -> {
ensureExpectedToken(XContentParser.Token.START_ARRAY, xcp.currentToken(), xcp)
while (xcp.nextToken() != XContentParser.Token.END_ARRAY) {
Expand Down Expand Up @@ -337,7 +337,7 @@ data class Alert(
lastNotificationTime = lastNotificationTime, acknowledgedTime = acknowledgedTime,
errorMessage = errorMessage, errorHistory = errorHistory, severity = severity,
actionExecutionResults = actionExecutionResults, aggregationResultBucket = aggAlertBucket, findingIds = findingIds,
relatedDocIds = relatedDocIds, workflowExecutionId = workflowExecutionId
relatedDocIds = relatedDocIds, executionId = executionId
)
}

Expand All @@ -363,7 +363,7 @@ data class Alert(
.field(SCHEMA_VERSION_FIELD, schemaVersion)
.field(MONITOR_VERSION_FIELD, monitorVersion)
.field(MONITOR_NAME_FIELD, monitorName)
.field(WORKFLOW_EXECUTION_ID_FIELD, workflowExecutionId)
.field(EXECUTION_ID_FIELD, executionId)

if (!secure) {
builder.optionalUserField(MONITOR_USER_FIELD, monitorUser)
Expand Down Expand Up @@ -394,7 +394,7 @@ data class Alert(
ALERT_VERSION_FIELD to version,
END_TIME_FIELD to endTime?.toEpochMilli(),
ERROR_MESSAGE_FIELD to errorMessage,
WORKFLOW_EXECUTION_ID_FIELD to workflowExecutionId,
EXECUTION_ID_FIELD to executionId,
LAST_NOTIFICATION_TIME_FIELD to lastNotificationTime?.toEpochMilli(),
SEVERITY_FIELD to severity,
START_TIME_FIELD to startTime.toEpochMilli(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ fun xContentRegistry(): NamedXContentRegistry {
DocLevelMonitorInput.XCONTENT_REGISTRY,
QueryLevelTrigger.XCONTENT_REGISTRY,
BucketLevelTrigger.XCONTENT_REGISTRY,
ChainedAlertTrigger.XCONTENT_REGISTRY,
DocumentLevelTrigger.XCONTENT_REGISTRY
) + SearchModule(Settings.EMPTY, emptyList()).namedXContents
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class GetAlertsResponseTests {
var actualXContentString = req.toXContent(builder(), ToXContent.EMPTY_PARAMS).string()
val expectedXContentString = "{\"alerts\":[{\"id\":\"id\",\"version\":0,\"monitor_id\":\"monitorId\"," +
"\"schema_version\":0,\"monitor_version\":0,\"monitor_name\":\"monitorName\"," +
"\"trigger_id\":\"triggerId\",\"trigger_name\":\"triggerName\"," +
"\"execution_id\":null,\"trigger_id\":\"triggerId\",\"trigger_name\":\"triggerName\"," +
"\"finding_ids\":[],\"related_doc_ids\":[],\"state\":\"ACKNOWLEDGED\",\"error_message\":null,\"alert_history\":[]," +
"\"severity\":\"severity\",\"action_execution_results\":[],\"start_time\":" + now.toEpochMilli() +
",\"last_notification_time\":null,\"end_time\":null,\"acknowledged_time\":null}],\"totalAlerts\":1}"
Expand Down

0 comments on commit 1cd59fa

Please sign in to comment.