Skip to content

Commit 08d6fd7

Browse files
committed
Made AlertContext a separate class from Alert instead of inheriting/extending it.
Signed-off-by: AWSHurneyt <[email protected]>
1 parent c36e4da commit 08d6fd7

File tree

4 files changed

+12
-263
lines changed

4 files changed

+12
-263
lines changed

src/main/kotlin/org/opensearch/commons/alerting/model/Alert.kt

+2-99
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken
1717
import java.io.IOException
1818
import java.time.Instant
1919

20-
open class Alert(
20+
data class Alert(
2121
val id: String = NO_ID,
2222
val version: Long = NO_VERSION,
2323
val schemaVersion: Int = NO_SCHEMA_VERSION,
@@ -591,7 +591,7 @@ open class Alert(
591591
return builder
592592
}
593593

594-
open fun asTemplateArg(): Map<String, Any?> {
594+
fun asTemplateArg(): Map<String, Any?> {
595595
return mapOf(
596596
ACKNOWLEDGED_TIME_FIELD to acknowledgedTime?.toEpochMilli(),
597597
ALERT_ID_FIELD to id,
@@ -614,101 +614,4 @@ open class Alert(
614614
CLUSTERS_FIELD to clusters?.joinToString(",")
615615
)
616616
}
617-
618-
/**
619-
* Creates a copy of an [Alert] with optionally modified properties
620-
*/
621-
fun copy(
622-
id: String = this.id,
623-
version: Long = this.version,
624-
schemaVersion: Int = this.schemaVersion,
625-
monitorId: String = this.monitorId,
626-
workflowId: String = this.workflowId,
627-
workflowName: String = this.workflowName,
628-
monitorName: String = this.monitorName,
629-
monitorVersion: Long = this.monitorVersion,
630-
monitorUser: User? = this.monitorUser,
631-
triggerId: String = this.triggerId,
632-
triggerName: String = this.triggerName,
633-
findingIds: List<String> = this.findingIds,
634-
relatedDocIds: List<String> = this.relatedDocIds,
635-
state: State = this.state,
636-
startTime: Instant = this.startTime,
637-
endTime: Instant? = this.endTime,
638-
lastNotificationTime: Instant? = this.lastNotificationTime,
639-
acknowledgedTime: Instant? = this.acknowledgedTime,
640-
errorMessage: String? = this.errorMessage,
641-
errorHistory: List<AlertError> = this.errorHistory,
642-
severity: String = this.severity,
643-
actionExecutionResults: List<ActionExecutionResult> = this.actionExecutionResults,
644-
aggregationResultBucket: AggregationResultBucket? = this.aggregationResultBucket,
645-
executionId: String? = this.executionId,
646-
associatedAlertIds: List<String> = this.associatedAlertIds,
647-
clusters: List<String>? = this.clusters
648-
): Alert {
649-
return Alert(
650-
id = id,
651-
version = version,
652-
schemaVersion = schemaVersion,
653-
monitorId = monitorId,
654-
workflowId = workflowId,
655-
workflowName = workflowName,
656-
monitorName = monitorName,
657-
monitorVersion = monitorVersion,
658-
monitorUser = monitorUser,
659-
triggerId = triggerId,
660-
triggerName = triggerName,
661-
findingIds = findingIds,
662-
relatedDocIds = relatedDocIds,
663-
state = state,
664-
startTime = startTime,
665-
endTime = endTime,
666-
lastNotificationTime = lastNotificationTime,
667-
acknowledgedTime = acknowledgedTime,
668-
errorMessage = errorMessage,
669-
errorHistory = errorHistory,
670-
severity = severity,
671-
actionExecutionResults = actionExecutionResults,
672-
aggregationResultBucket = aggregationResultBucket,
673-
executionId = executionId,
674-
associatedAlertIds = associatedAlertIds,
675-
clusters = clusters
676-
)
677-
}
678-
679-
override fun equals(other: Any?): Boolean {
680-
if (this === other) return true
681-
if (javaClass != other?.javaClass) return false
682-
683-
other as Alert
684-
685-
if (id != other.id) return false
686-
if (version != other.version) return false
687-
if (schemaVersion != other.schemaVersion) return false
688-
if (monitorId != other.monitorId) return false
689-
if (workflowId != other.workflowId) return false
690-
if (workflowName != other.workflowName) return false
691-
if (monitorName != other.monitorName) return false
692-
if (monitorVersion != other.monitorVersion) return false
693-
if (monitorUser != other.monitorUser) return false
694-
if (triggerId != other.triggerId) return false
695-
if (triggerName != other.triggerName) return false
696-
if (findingIds != other.findingIds) return false
697-
if (relatedDocIds != other.relatedDocIds) return false
698-
if (state != other.state) return false
699-
if (startTime != other.startTime) return false
700-
if (endTime != other.endTime) return false
701-
if (lastNotificationTime != other.lastNotificationTime) return false
702-
if (acknowledgedTime != other.acknowledgedTime) return false
703-
if (errorMessage != other.errorMessage) return false
704-
if (errorHistory != other.errorHistory) return false
705-
if (severity != other.severity) return false
706-
if (actionExecutionResults != other.actionExecutionResults) return false
707-
if (aggregationResultBucket != other.aggregationResultBucket) return false
708-
if (executionId != other.executionId) return false
709-
if (associatedAlertIds != other.associatedAlertIds) return false
710-
if (clusters != other.clusters) return false
711-
712-
return true
713-
}
714617
}

src/main/kotlin/org/opensearch/commons/alerting/model/AlertContext.kt

+2-30
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,8 @@ data class AlertContext(
1313
val alert: Alert,
1414
val associatedQueries: List<DocLevelQuery>? = null,
1515
val sampleDocs: List<Map<String, Any?>>? = null
16-
) : Alert(
17-
id = alert.id,
18-
version = alert.version,
19-
schemaVersion = alert.schemaVersion,
20-
monitorId = alert.monitorId,
21-
monitorName = alert.monitorName,
22-
monitorVersion = alert.monitorVersion,
23-
monitorUser = alert.monitorUser,
24-
triggerId = alert.triggerId,
25-
triggerName = alert.triggerName,
26-
state = alert.state,
27-
startTime = alert.startTime,
28-
endTime = alert.endTime,
29-
lastNotificationTime = alert.lastNotificationTime,
30-
acknowledgedTime = alert.acknowledgedTime,
31-
errorMessage = alert.errorMessage,
32-
errorHistory = alert.errorHistory,
33-
severity = alert.severity,
34-
actionExecutionResults = alert.actionExecutionResults,
35-
aggregationResultBucket = alert.aggregationResultBucket,
36-
findingIds = alert.findingIds,
37-
relatedDocIds = alert.relatedDocIds,
38-
executionId = alert.executionId,
39-
workflowId = alert.workflowId,
40-
workflowName = alert.workflowName,
41-
associatedAlertIds = alert.associatedAlertIds,
42-
clusters = alert.clusters
4316
) {
44-
45-
override fun asTemplateArg(): Map<String, Any?> {
17+
fun asTemplateArg(): Map<String, Any?> {
4618
val queriesContext = associatedQueries?.map {
4719
mapOf(
4820
DocLevelQuery.QUERY_ID_FIELD to it.id,
@@ -58,7 +30,7 @@ data class AlertContext(
5830
)
5931

6032
// Get the alert template args
61-
val templateArgs = super.asTemplateArg().toMutableMap()
33+
val templateArgs = alert.asTemplateArg().toMutableMap()
6234

6335
// Add the non-null custom context fields to the alert templateArgs.
6436
customContextFields.forEach { (key, value) ->

src/test/kotlin/org/opensearch/commons/alerting/AlertTests.kt

-126
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package org.opensearch.commons.alerting
22

33
import org.junit.jupiter.api.Assertions
44
import org.junit.jupiter.api.Assertions.assertEquals
5-
import org.junit.jupiter.api.Assertions.assertFalse
6-
import org.junit.jupiter.api.Assertions.assertTrue
75
import org.junit.jupiter.api.Test
86
import org.opensearch.commons.alerting.model.Alert
97
import java.time.Instant
@@ -86,128 +84,4 @@ class AlertTests {
8684
assertEquals(alert.id, "")
8785
assertEquals(workflow.id, alert.workflowId)
8886
}
89-
90-
@Test
91-
fun `test alert copy`() {
92-
val alert = randomAlert()
93-
94-
val copiedAlert = alert.copy()
95-
96-
assertEquals(alert.id, copiedAlert.id)
97-
assertEquals(alert.version, copiedAlert.version)
98-
assertEquals(alert.schemaVersion, copiedAlert.schemaVersion)
99-
assertEquals(alert.monitorId, copiedAlert.monitorId)
100-
assertEquals(alert.workflowId, copiedAlert.workflowId)
101-
assertEquals(alert.workflowName, copiedAlert.workflowName)
102-
assertEquals(alert.monitorName, copiedAlert.monitorName)
103-
assertEquals(alert.monitorVersion, copiedAlert.monitorVersion)
104-
assertEquals(alert.monitorUser, copiedAlert.monitorUser)
105-
assertEquals(alert.triggerId, copiedAlert.triggerId)
106-
assertEquals(alert.triggerName, copiedAlert.triggerName)
107-
assertEquals(alert.findingIds, copiedAlert.findingIds)
108-
assertEquals(alert.relatedDocIds, copiedAlert.relatedDocIds)
109-
assertEquals(alert.state, copiedAlert.state)
110-
assertEquals(alert.startTime, copiedAlert.startTime)
111-
assertEquals(alert.endTime, copiedAlert.endTime)
112-
assertEquals(alert.lastNotificationTime, copiedAlert.lastNotificationTime)
113-
assertEquals(alert.acknowledgedTime, copiedAlert.acknowledgedTime)
114-
assertEquals(alert.errorMessage, copiedAlert.errorMessage)
115-
assertEquals(alert.errorHistory, copiedAlert.errorHistory)
116-
assertEquals(alert.severity, copiedAlert.severity)
117-
assertEquals(alert.actionExecutionResults, copiedAlert.actionExecutionResults)
118-
assertEquals(alert.aggregationResultBucket, copiedAlert.aggregationResultBucket)
119-
assertEquals(alert.executionId, copiedAlert.executionId)
120-
assertEquals(alert.associatedAlertIds, copiedAlert.associatedAlertIds)
121-
assertEquals(alert.clusters, copiedAlert.clusters)
122-
}
123-
124-
@Test
125-
fun `test alert copy with modified properties`() {
126-
val alert = randomAlert()
127-
val newAlertValues = randomAlert()
128-
129-
val alertCopy = alert.copy(
130-
id = newAlertValues.id,
131-
triggerId = newAlertValues.triggerId,
132-
triggerName = newAlertValues.triggerName,
133-
actionExecutionResults = newAlertValues.actionExecutionResults,
134-
clusters = newAlertValues.clusters
135-
)
136-
137-
// Modified properties; compare to newAlertValues
138-
assertEquals(newAlertValues.id, alertCopy.id)
139-
assertEquals(newAlertValues.triggerId, alertCopy.triggerId)
140-
assertEquals(newAlertValues.triggerName, alertCopy.triggerName)
141-
assertEquals(newAlertValues.actionExecutionResults, alertCopy.actionExecutionResults)
142-
assertEquals(newAlertValues.clusters, alertCopy.clusters)
143-
144-
// Retained values; compare to original alert
145-
assertEquals(alert.version, alertCopy.version)
146-
assertEquals(alert.schemaVersion, alertCopy.schemaVersion)
147-
assertEquals(alert.monitorId, alertCopy.monitorId)
148-
assertEquals(alert.workflowId, alertCopy.workflowId)
149-
assertEquals(alert.workflowName, alertCopy.workflowName)
150-
assertEquals(alert.monitorName, alertCopy.monitorName)
151-
assertEquals(alert.monitorVersion, alertCopy.monitorVersion)
152-
assertEquals(alert.monitorUser, alertCopy.monitorUser)
153-
assertEquals(alert.findingIds, alertCopy.findingIds)
154-
assertEquals(alert.relatedDocIds, alertCopy.relatedDocIds)
155-
assertEquals(alert.state, alertCopy.state)
156-
assertEquals(alert.startTime, alertCopy.startTime)
157-
assertEquals(alert.endTime, alertCopy.endTime)
158-
assertEquals(alert.lastNotificationTime, alertCopy.lastNotificationTime)
159-
assertEquals(alert.acknowledgedTime, alertCopy.acknowledgedTime)
160-
assertEquals(alert.errorMessage, alertCopy.errorMessage)
161-
assertEquals(alert.errorHistory, alertCopy.errorHistory)
162-
assertEquals(alert.severity, alertCopy.severity)
163-
assertEquals(alert.aggregationResultBucket, alertCopy.aggregationResultBucket)
164-
assertEquals(alert.executionId, alertCopy.executionId)
165-
assertEquals(alert.associatedAlertIds, alertCopy.associatedAlertIds)
166-
}
167-
168-
@Test
169-
fun `test alert equals with duplicate alerts`() {
170-
val alert = randomAlert()
171-
val alertCopy = alert.copy()
172-
173-
val alertsMatch = alert.equals(alertCopy)
174-
175-
assertTrue(alertsMatch)
176-
}
177-
178-
@Test
179-
fun `test alert equals with different alerts`() {
180-
val alert = randomAlert()
181-
val newAlertValues = randomAlert()
182-
val alertCopy = alert.copy(
183-
id = newAlertValues.id,
184-
triggerId = newAlertValues.triggerId,
185-
triggerName = newAlertValues.triggerName,
186-
actionExecutionResults = newAlertValues.actionExecutionResults,
187-
clusters = newAlertValues.clusters
188-
)
189-
190-
val alertsMatch = alert.equals(alertCopy)
191-
192-
assertFalse(alertsMatch)
193-
}
194-
195-
@Test
196-
fun `test alert equals with null alert`() {
197-
val alert = randomAlert()
198-
199-
val alertsMatch = alert.equals(null)
200-
201-
assertFalse(alertsMatch)
202-
}
203-
204-
@Test
205-
fun `test alert equals with alertContext`() {
206-
val alert = randomAlert()
207-
val alertContext = randomAlertContext(alert = alert)
208-
209-
val alertsMatch = alert.equals(alertContext)
210-
211-
assertFalse(alertsMatch)
212-
}
21387
}

src/test/kotlin/org/opensearch/commons/alerting/model/AlertContextTests.kt

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ class AlertContextTests {
2222
fun `test AlertContext asTemplateArg`() {
2323
val templateArgs = alertContext.asTemplateArg()
2424

25-
assertEquals(templateArgs[Alert.ALERT_ID_FIELD], alertContext.id, "Template args id does not match")
26-
assertEquals(templateArgs[Alert.ALERT_VERSION_FIELD], alertContext.version, "Template args version does not match")
27-
assertEquals(templateArgs[Alert.STATE_FIELD], alertContext.state.toString(), "Template args state does not match")
28-
assertEquals(templateArgs[Alert.ERROR_MESSAGE_FIELD], alertContext.errorMessage, "Template args error message does not match")
25+
assertEquals(templateArgs[Alert.ALERT_ID_FIELD], alertContext.alert.id, "Template args id does not match")
26+
assertEquals(templateArgs[Alert.ALERT_VERSION_FIELD], alertContext.alert.version, "Template args version does not match")
27+
assertEquals(templateArgs[Alert.STATE_FIELD], alertContext.alert.state.toString(), "Template args state does not match")
28+
assertEquals(templateArgs[Alert.ERROR_MESSAGE_FIELD], alertContext.alert.errorMessage, "Template args error message does not match")
2929
assertEquals(templateArgs[Alert.ACKNOWLEDGED_TIME_FIELD], null, "Template args acknowledged time does not match")
30-
assertEquals(templateArgs[Alert.END_TIME_FIELD], alertContext.endTime?.toEpochMilli(), "Template args end time does not")
31-
assertEquals(templateArgs[Alert.START_TIME_FIELD], alertContext.startTime.toEpochMilli(), "Template args start time does not")
30+
assertEquals(templateArgs[Alert.END_TIME_FIELD], alertContext.alert.endTime?.toEpochMilli(), "Template args end time does not")
31+
assertEquals(templateArgs[Alert.START_TIME_FIELD], alertContext.alert.startTime.toEpochMilli(), "Template args start time does not")
3232
assertEquals(templateArgs[Alert.LAST_NOTIFICATION_TIME_FIELD], null, "Template args last notification time does not match")
33-
assertEquals(templateArgs[Alert.SEVERITY_FIELD], alertContext.severity, "Template args severity does not match")
34-
assertEquals(templateArgs[Alert.CLUSTERS_FIELD], alertContext.clusters?.joinToString(","), "Template args clusters does not match")
33+
assertEquals(templateArgs[Alert.SEVERITY_FIELD], alertContext.alert.severity, "Template args severity does not match")
34+
assertEquals(templateArgs[Alert.CLUSTERS_FIELD], alertContext.alert.clusters?.joinToString(","), "Template args clusters does not match")
3535
val formattedQueries = alertContext.associatedQueries?.map {
3636
mapOf(
3737
DocLevelQuery.QUERY_ID_FIELD to it.id,

0 commit comments

Comments
 (0)