From 3f5421fc0f3d6b22bbccb8260962ef2ed22d4fdd Mon Sep 17 00:00:00 2001 From: Riya <69919272+riysaxen-amzn@users.noreply.github.com> Date: Tue, 12 Mar 2024 20:30:28 -0700 Subject: [PATCH] fix findings API enhancemnts (#611) * fix findings API enhancemnts Signed-off-by: Riya Saxena * fix klint errors Signed-off-by: Riya Saxena * fix klint errors Signed-off-by: Riya Saxena * fix klint errors Signed-off-by: Riya Saxena --------- Signed-off-by: Riya Saxena --- .../alerting/action/GetFindingsRequest.kt | 33 ++++--------------- .../action/GetFindingsRequestTests.kt | 13 +++----- 2 files changed, 11 insertions(+), 35 deletions(-) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/GetFindingsRequest.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/GetFindingsRequest.kt index 8ad2ff82..59dd3c39 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/GetFindingsRequest.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/GetFindingsRequest.kt @@ -5,8 +5,8 @@ import org.opensearch.action.ActionRequestValidationException import org.opensearch.commons.alerting.model.Table import org.opensearch.core.common.io.stream.StreamInput import org.opensearch.core.common.io.stream.StreamOutput +import org.opensearch.index.query.BoolQueryBuilder import java.io.IOException -import java.time.Instant class GetFindingsRequest : ActionRequest { val findingId: String? @@ -14,34 +14,21 @@ class GetFindingsRequest : ActionRequest { val monitorId: String? val monitorIds: List? val findingIndex: String? - val severity: String? - val detectionType: String? - val findingIds: List? - val startTime: Instant? - val endTime: Instant? - + val boolQueryBuilder: BoolQueryBuilder? constructor( findingId: String?, table: Table, monitorId: String? = null, findingIndexName: String? = null, monitorIds: List? = null, - severity: String? = null, - detectionType: String? = null, - findingIds: List? = null, - startTime: Instant? = null, - endTime: Instant? = null + boolQueryBuilder: BoolQueryBuilder? = null ) : super() { this.findingId = findingId this.table = table this.monitorId = monitorId this.findingIndex = findingIndexName this.monitorIds = monitorIds - this.severity = severity - this.detectionType = detectionType - this.findingIds = findingIds - this.startTime = startTime - this.endTime = endTime + this.boolQueryBuilder = boolQueryBuilder } @Throws(IOException::class) @@ -51,11 +38,7 @@ class GetFindingsRequest : ActionRequest { monitorId = sin.readOptionalString(), findingIndexName = sin.readOptionalString(), monitorIds = sin.readOptionalStringList(), - severity = sin.readOptionalString(), - detectionType = sin.readOptionalString(), - findingIds = sin.readOptionalStringList(), - startTime = sin.readOptionalInstant(), - endTime = sin.readOptionalInstant() + boolQueryBuilder = BoolQueryBuilder(sin) ) override fun validate(): ActionRequestValidationException? { @@ -69,10 +52,6 @@ class GetFindingsRequest : ActionRequest { out.writeOptionalString(monitorId) out.writeOptionalString(findingIndex) out.writeOptionalStringCollection(monitorIds) - out.writeOptionalString(severity) - out.writeOptionalString(detectionType) - out.writeOptionalStringCollection(findingIds) - out.writeOptionalInstant(startTime) - out.writeOptionalInstant(endTime) + boolQueryBuilder?.writeTo(out) } } diff --git a/src/test/kotlin/org/opensearch/commons/alerting/action/GetFindingsRequestTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/action/GetFindingsRequestTests.kt index 67014d1b..238837cc 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/action/GetFindingsRequestTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/action/GetFindingsRequestTests.kt @@ -8,14 +8,15 @@ import org.junit.jupiter.api.Test import org.opensearch.common.io.stream.BytesStreamOutput import org.opensearch.commons.alerting.model.Table import org.opensearch.core.common.io.stream.StreamInput -import java.time.Instant +import org.opensearch.index.query.QueryBuilders internal class GetFindingsRequestTests { @Test fun `test get findings request`() { val table = Table("asc", "sortString", null, 1, 0, "") - val req = GetFindingsRequest("2121", table, "1", "finding_index_name", listOf("1", "2"), "severity", "detectionType", listOf("id1", "id2"), Instant.now(), Instant.now().plusSeconds(30000)) + val boolQueryBuilder = QueryBuilders.boolQuery() + val req = GetFindingsRequest("2121", table, "1", "finding_index_name", listOf("1", "2"), boolQueryBuilder) assertNotNull(req) val out = BytesStreamOutput() @@ -26,20 +27,16 @@ internal class GetFindingsRequestTests { assertEquals("1", newReq.monitorId) assertEquals("2121", newReq.findingId) assertEquals("finding_index_name", newReq.findingIndex) - assertEquals("severity", newReq.severity) - assertEquals("detectionType", newReq.detectionType) assertEquals(table, newReq.table) assertTrue(newReq.monitorIds!!.contains("1")) assertTrue(newReq.monitorIds!!.contains("2")) - assertTrue(newReq.findingIds!!.contains("id1")) - assertTrue(newReq.findingIds!!.contains("id2")) - assertTrue(newReq.startTime!! < newReq.endTime, "startTime less than endTime") } @Test fun `test validate returns null`() { val table = Table("asc", "sortString", null, 1, 0, "") - val req = GetFindingsRequest("2121", table, "1", "active", listOf("1", "2"), "severity", "detectionType", listOf("id1", "id2"), Instant.now(), Instant.now().plusSeconds(30000)) + val boolQueryBuilder = QueryBuilders.boolQuery() + val req = GetFindingsRequest("2121", table, "1", "active", listOf("1", "2"), boolQueryBuilder) assertNotNull(req) assertNull(req.validate()) }