Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alerting comments #673

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import java.io.IOException
*/
class IndexCommentRequest : ActionRequest {
val entityId: String
val entityType: String
val commentId: String
val seqNo: Long
val primaryTerm: Long
Expand All @@ -26,13 +27,15 @@ class IndexCommentRequest : ActionRequest {

constructor(
entityId: String,
entityType: String,
commentId: String,
seqNo: Long,
primaryTerm: Long,
method: RestRequest.Method,
content: String
) : super() {
this.entityId = entityId
this.entityType = entityType
this.commentId = commentId
this.seqNo = seqNo
this.primaryTerm = primaryTerm
Expand All @@ -43,6 +46,7 @@ class IndexCommentRequest : ActionRequest {
@Throws(IOException::class)
constructor(sin: StreamInput) : this(
entityId = sin.readString(),
entityType = sin.readString(),
commentId = sin.readString(),
seqNo = sin.readLong(),
primaryTerm = sin.readLong(),
Expand All @@ -64,6 +68,7 @@ class IndexCommentRequest : ActionRequest {
@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
out.writeString(entityId)
out.writeString(entityType)
out.writeString(commentId)
out.writeLong(seqNo)
out.writeLong(primaryTerm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ import java.time.Instant
data class Comment(
val id: String = NO_ID,
val entityId: String = NO_ID,
val entityType: String,
val content: String,
val createdTime: Instant,
val lastUpdatedTime: Instant?,
val user: User?
) : Writeable, ToXContent {

@Throws(IOException::class)
constructor(sin: StreamInput) : this(
id = sin.readString(),
entityId = sin.readString(),
entityType = sin.readString(),
content = sin.readString(),
createdTime = sin.readInstant(),
lastUpdatedTime = sin.readOptionalInstant(),
Expand All @@ -37,21 +38,32 @@ data class Comment(

constructor(
entityId: String,
entityType: String,
content: String,
createdTime: Instant,
user: User?
) : this (
entityId = entityId,
entityType = entityType,
content = content,
createdTime = createdTime,
lastUpdatedTime = null,
user = user
)

enum class EntityType(val value: String) {
ALERT("alert");

override fun toString(): String {
return value
}
}

@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
out.writeString(id)
out.writeString(entityId)
out.writeString(entityType)
out.writeString(content)
out.writeInstant(createdTime)
out.writeOptionalInstant(lastUpdatedTime)
Expand All @@ -63,6 +75,7 @@ data class Comment(
return mapOf<String, Any?>(
_ID to id,
ENTITY_ID_FIELD to entityId,
ENTITY_TYPE_FIELD to entityType,
COMMENT_CONTENT_FIELD to content,
COMMENT_CREATED_TIME_FIELD to createdTime,
COMMENT_LAST_UPDATED_TIME_FIELD to lastUpdatedTime,
Expand All @@ -83,6 +96,7 @@ data class Comment(
private fun createXContentBuilder(builder: XContentBuilder, includeFullUser: Boolean): XContentBuilder {
builder.startObject()
.field(ENTITY_ID_FIELD, entityId)
.field(ENTITY_TYPE_FIELD, entityType)
.field(COMMENT_CONTENT_FIELD, content)
.optionalTimeField(COMMENT_CREATED_TIME_FIELD, createdTime)
.optionalTimeField(COMMENT_LAST_UPDATED_TIME_FIELD, lastUpdatedTime)
Expand All @@ -101,6 +115,7 @@ data class Comment(

companion object {
const val ENTITY_ID_FIELD = "entity_id"
const val ENTITY_TYPE_FIELD = "entity_type"
const val COMMENT_CONTENT_FIELD = "content"
const val COMMENT_CREATED_TIME_FIELD = "created_time"
const val COMMENT_LAST_UPDATED_TIME_FIELD = "last_updated_time"
Expand All @@ -112,6 +127,7 @@ data class Comment(
@Throws(IOException::class)
fun parse(xcp: XContentParser, id: String = NO_ID): Comment {
lateinit var entityId: String
lateinit var entityType: String
var content = ""
lateinit var createdTime: Instant
var lastUpdatedTime: Instant? = null
Expand All @@ -124,6 +140,7 @@ data class Comment(

when (fieldName) {
ENTITY_ID_FIELD -> entityId = xcp.text()
ENTITY_TYPE_FIELD -> entityType = xcp.text()
COMMENT_CONTENT_FIELD -> content = xcp.text()
COMMENT_CREATED_TIME_FIELD -> createdTime = requireNotNull(xcp.instant())
COMMENT_LAST_UPDATED_TIME_FIELD -> lastUpdatedTime = xcp.instant()
Expand All @@ -139,6 +156,7 @@ data class Comment(
return Comment(
id = id,
entityId = entityId,
entityType = entityType,
content = content,
createdTime = createdTime,
lastUpdatedTime = lastUpdatedTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ data class DataSources(
val alertsHistoryIndexPattern: String? = "<.opendistro-alerting-alert-history-{now/d}-1>", // AlertIndices.ALERT_HISTORY_INDEX_PATTERN

/** Configures a custom index alias to store comments associated with alerts.*/
val commentsIndex: String = ".opensearch-alerting-comments-history-write", // AlertIndices.COMMENTS_HISTORY_WRITE_INDEX
val commentsIndex: String? = ".opensearch-alerting-comments-history-write", // AlertIndices.COMMENTS_HISTORY_WRITE_INDEX

/** Configures a custom index pattern for commentsIndex alias.*/
val commentsIndexPattern: String? = "<.opensearch-alerting-comments-history-{now/d}-1>", // AlertIndices.COMMENTS_HISTORY_INDEX_PATTERN
Expand All @@ -56,9 +56,6 @@ data class DataSources(
require(alertsIndex.isNotEmpty()) {
"Alerts index cannot be empty"
}
require(commentsIndex.isNotEmpty()) {
"Comments index cannot be empty"
}
if (queryIndexMappingsByType.isNotEmpty()) {
require(queryIndex != ScheduledJob.DOC_LEVEL_QUERIES_INDEX) {
"Custom query index mappings are configurable only if a custom query index is configured too."
Expand Down Expand Up @@ -89,6 +86,29 @@ data class DataSources(
findingsEnabled = sin.readOptionalBoolean()
)

@Throws(IOException::class)
constructor(
queryIndex: String,
findingsIndex: String,
findingsIndexPattern: String?,
alertsIndex: String,
alertsHistoryIndex: String?,
alertsHistoryIndexPattern: String?,
queryIndexMappingsByType: Map<String, Map<String, String>>,
findingsEnabled: Boolean?
) : this(
queryIndex = queryIndex,
findingsIndex = findingsIndex,
findingsIndexPattern = findingsIndexPattern,
alertsIndex = alertsIndex,
alertsHistoryIndex = alertsHistoryIndex,
alertsHistoryIndexPattern = alertsHistoryIndexPattern,
commentsIndex = null,
commentsIndexPattern = null,
queryIndexMappingsByType = queryIndexMappingsByType,
findingsEnabled = findingsEnabled
)

@Suppress("UNCHECKED_CAST")
fun asTemplateArg(): Map<String, Any?> {
return mapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Test
import org.opensearch.common.io.stream.BytesStreamOutput
import org.opensearch.commons.alerting.model.Comment
import org.opensearch.core.common.io.stream.StreamInput
import org.opensearch.rest.RestRequest

class IndexCommentRequestTests {
@Test
fun `test index comment post request`() {
val req = IndexCommentRequest("123", "456", 1L, 2L, RestRequest.Method.POST, "comment")
val req = IndexCommentRequest("123", Comment.EntityType.ALERT.value, "456", 1L, 2L, RestRequest.Method.POST, "comment")
assertNotNull(req)
val out = BytesStreamOutput()
req.writeTo(out)
val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
val newReq = IndexCommentRequest(sin)
assertEquals("123", newReq.entityId)
assertEquals(Comment.EntityType.ALERT.value, newReq.entityType)
assertEquals("456", newReq.commentId)
assertEquals(1L, newReq.seqNo)
assertEquals(2L, newReq.primaryTerm)
Expand All @@ -26,13 +28,14 @@ class IndexCommentRequestTests {

@Test
fun `test index comment put request`() {
val req = IndexCommentRequest("123", "456", 1L, 2L, RestRequest.Method.PUT, "comment")
val req = IndexCommentRequest("123", Comment.EntityType.ALERT.value, "456", 1L, 2L, RestRequest.Method.PUT, "comment")
assertNotNull(req)
val out = BytesStreamOutput()
req.writeTo(out)
val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
val newReq = IndexCommentRequest(sin)
assertEquals("123", newReq.entityId)
assertEquals(Comment.EntityType.ALERT.value, newReq.entityType)
assertEquals("456", newReq.commentId)
assertEquals(1L, newReq.seqNo)
assertEquals(2L, newReq.primaryTerm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class IndexCommentResponseTests {
fun `test index comment response with comment`() {
val comment = Comment(
"123",
Comment.EntityType.ALERT.value,
"456",
"comment",
Instant.now(),
Expand Down
Loading