-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Findings and Alerts action, request, response and models from al…
…erting to common-utils (#254) Signed-off-by: Surya Sashank Nistala <[email protected]>
- Loading branch information
Showing
23 changed files
with
1,678 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/kotlin/org/opensearch/commons/alerting/action/GetAlertsRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.opensearch.commons.alerting.action | ||
|
||
import org.opensearch.action.ActionRequest | ||
import org.opensearch.action.ActionRequestValidationException | ||
import org.opensearch.common.io.stream.StreamInput | ||
import org.opensearch.common.io.stream.StreamOutput | ||
import org.opensearch.commons.alerting.model.Table | ||
import java.io.IOException | ||
|
||
class GetAlertsRequest : ActionRequest { | ||
val table: Table | ||
val severityLevel: String | ||
val alertState: String | ||
val monitorId: String? | ||
val alertIndex: String? | ||
|
||
constructor( | ||
table: Table, | ||
severityLevel: String, | ||
alertState: String, | ||
monitorId: String?, | ||
alertIndex: String? | ||
) : super() { | ||
this.table = table | ||
this.severityLevel = severityLevel | ||
this.alertState = alertState | ||
this.monitorId = monitorId | ||
this.alertIndex = alertIndex | ||
} | ||
|
||
@Throws(IOException::class) | ||
constructor(sin: StreamInput) : this( | ||
table = Table.readFrom(sin), | ||
severityLevel = sin.readString(), | ||
alertState = sin.readString(), | ||
monitorId = sin.readOptionalString(), | ||
alertIndex = sin.readOptionalString() | ||
) | ||
|
||
override fun validate(): ActionRequestValidationException? { | ||
return null | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun writeTo(out: StreamOutput) { | ||
table.writeTo(out) | ||
out.writeString(severityLevel) | ||
out.writeString(alertState) | ||
out.writeOptionalString(monitorId) | ||
out.writeOptionalString(alertIndex) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/org/opensearch/commons/alerting/action/GetAlertsResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.opensearch.commons.alerting.action | ||
|
||
import org.opensearch.common.io.stream.StreamInput | ||
import org.opensearch.common.io.stream.StreamOutput | ||
import org.opensearch.common.xcontent.ToXContent | ||
import org.opensearch.common.xcontent.XContentBuilder | ||
import org.opensearch.commons.alerting.model.Alert | ||
import org.opensearch.commons.notifications.action.BaseResponse | ||
import java.io.IOException | ||
import java.util.Collections | ||
|
||
class GetAlertsResponse : BaseResponse { | ||
val alerts: List<Alert> | ||
|
||
// totalAlerts is not the same as the size of alerts because there can be 30 alerts from the request, but | ||
// the request only asked for 5 alerts, so totalAlerts will be 30, but alerts will only contain 5 alerts | ||
val totalAlerts: Int? | ||
|
||
constructor( | ||
alerts: List<Alert>, | ||
totalAlerts: Int? | ||
) : super() { | ||
this.alerts = alerts | ||
this.totalAlerts = totalAlerts | ||
} | ||
|
||
@Throws(IOException::class) | ||
constructor(sin: StreamInput) : this( | ||
alerts = Collections.unmodifiableList(sin.readList(::Alert)), | ||
totalAlerts = sin.readOptionalInt() | ||
) | ||
|
||
@Throws(IOException::class) | ||
override fun writeTo(out: StreamOutput) { | ||
out.writeCollection(alerts) | ||
out.writeOptionalInt(totalAlerts) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { | ||
builder.startObject() | ||
.field("alerts", alerts) | ||
.field("totalAlerts", totalAlerts) | ||
|
||
return builder.endObject() | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/org/opensearch/commons/alerting/action/GetFindingsRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.opensearch.commons.alerting.action | ||
|
||
import org.opensearch.action.ActionRequest | ||
import org.opensearch.action.ActionRequestValidationException | ||
import org.opensearch.common.io.stream.StreamInput | ||
import org.opensearch.common.io.stream.StreamOutput | ||
import org.opensearch.commons.alerting.model.Table | ||
import java.io.IOException | ||
|
||
class GetFindingsRequest : ActionRequest { | ||
val findingId: String? | ||
val table: Table | ||
val monitorId: String? | ||
val findingIndex: String? | ||
|
||
constructor( | ||
findingId: String?, | ||
table: Table, | ||
monitorId: String? = null, | ||
findingIndexName: String? = null | ||
) : super() { | ||
this.findingId = findingId | ||
this.table = table | ||
this.monitorId = monitorId | ||
this.findingIndex = findingIndexName | ||
} | ||
|
||
@Throws(IOException::class) | ||
constructor(sin: StreamInput) : this( | ||
findingId = sin.readOptionalString(), | ||
table = Table.readFrom(sin), | ||
monitorId = sin.readOptionalString(), | ||
findingIndexName = sin.readOptionalString() | ||
) | ||
|
||
override fun validate(): ActionRequestValidationException? { | ||
return null | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun writeTo(out: StreamOutput) { | ||
out.writeOptionalString(findingId) | ||
table.writeTo(out) | ||
out.writeOptionalString(monitorId) | ||
out.writeOptionalString(findingIndex) | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/kotlin/org/opensearch/commons/alerting/action/GetFindingsResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.opensearch.commons.alerting.action | ||
|
||
import org.opensearch.common.io.stream.StreamInput | ||
import org.opensearch.common.io.stream.StreamOutput | ||
import org.opensearch.common.xcontent.ToXContent | ||
import org.opensearch.common.xcontent.XContentBuilder | ||
import org.opensearch.commons.alerting.model.FindingWithDocs | ||
import org.opensearch.commons.notifications.action.BaseResponse | ||
import org.opensearch.rest.RestStatus | ||
import java.io.IOException | ||
|
||
class GetFindingsResponse : BaseResponse { | ||
private var status: RestStatus | ||
var totalFindings: Int? | ||
var findings: List<FindingWithDocs> | ||
|
||
constructor( | ||
status: RestStatus, | ||
totalFindings: Int?, | ||
findings: List<FindingWithDocs> | ||
) : super() { | ||
this.status = status | ||
this.totalFindings = totalFindings | ||
this.findings = findings | ||
} | ||
|
||
@Throws(IOException::class) | ||
constructor(sin: StreamInput) { | ||
this.status = sin.readEnum(RestStatus::class.java) | ||
val findings = mutableListOf<FindingWithDocs>() | ||
this.totalFindings = sin.readOptionalInt() | ||
var currentSize = sin.readInt() | ||
for (i in 0 until currentSize) { | ||
findings.add(FindingWithDocs.readFrom(sin)) | ||
} | ||
this.findings = findings | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun writeTo(out: StreamOutput) { | ||
out.writeEnum(status) | ||
out.writeOptionalInt(totalFindings) | ||
out.writeInt(findings.size) | ||
for (finding in findings) { | ||
finding.writeTo(out) | ||
} | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { | ||
builder.startObject() | ||
.field("total_findings", totalFindings) | ||
.field("findings", findings) | ||
|
||
return builder.endObject() | ||
} | ||
|
||
override fun getStatus(): RestStatus { | ||
return this.status | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/kotlin/org/opensearch/commons/alerting/alerts/AlertError.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.opensearch.commons.alerting.alerts | ||
|
||
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.ToXContent | ||
import org.opensearch.common.xcontent.XContentBuilder | ||
import org.opensearch.common.xcontent.XContentParser | ||
import org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken | ||
import org.opensearch.commons.alerting.util.instant | ||
import org.opensearch.commons.alerting.util.optionalTimeField | ||
import java.io.IOException | ||
import java.time.Instant | ||
|
||
data class AlertError(val timestamp: Instant, val message: String) : Writeable, ToXContent { | ||
|
||
@Throws(IOException::class) | ||
constructor(sin: StreamInput) : this( | ||
sin.readInstant(), // timestamp | ||
sin.readString() // message | ||
) | ||
|
||
@Throws(IOException::class) | ||
override fun writeTo(out: StreamOutput) { | ||
out.writeInstant(timestamp) | ||
out.writeString(message) | ||
} | ||
companion object { | ||
|
||
const val TIMESTAMP_FIELD = "timestamp" | ||
const val MESSAGE_FIELD = "message" | ||
|
||
@JvmStatic | ||
@Throws(IOException::class) | ||
fun parse(xcp: XContentParser): AlertError { | ||
|
||
lateinit var timestamp: Instant | ||
lateinit var message: String | ||
|
||
ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp) | ||
while (xcp.nextToken() != XContentParser.Token.END_OBJECT) { | ||
val fieldName = xcp.currentName() | ||
xcp.nextToken() | ||
|
||
when (fieldName) { | ||
TIMESTAMP_FIELD -> timestamp = requireNotNull(xcp.instant()) | ||
MESSAGE_FIELD -> message = xcp.text() | ||
} | ||
} | ||
return AlertError(timestamp = timestamp, message = message) | ||
} | ||
|
||
@JvmStatic | ||
@Throws(IOException::class) | ||
fun readFrom(sin: StreamInput): AlertError { | ||
return AlertError(sin) | ||
} | ||
} | ||
|
||
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { | ||
return builder.startObject() | ||
.optionalTimeField(TIMESTAMP_FIELD, timestamp) | ||
.field(MESSAGE_FIELD, message) | ||
.endObject() | ||
} | ||
} |
Oops, something went wrong.