diff --git a/build.gradle b/build.gradle index 4a30ac88..1555fe0b 100644 --- a/build.gradle +++ b/build.gradle @@ -72,6 +72,7 @@ apply plugin: 'opensearch.pluginzip' def sqlJarDirectory = "$buildDir/dependencies/opensearch-sql-plugin" def jsJarDirectory = "$buildDir/dependencies/opensearch-job-scheduler" def adJarDirectory = "$buildDir/dependencies/opensearch-time-series-analytics" +// def alertingJarDirectory = "$buildDir/dependencies/alerting" configurations { zipArchive @@ -101,6 +102,11 @@ task addJarsToClasspath(type: Copy) { include "opensearch-time-series-analytics-${version}.jar" } into("$buildDir/classes") + + // from(fileTree(dir: alertingJarDirectory)) { + // include "alerting-${version}.jar" + // } + // into("$buildDir/classes") } dependencies { @@ -116,10 +122,12 @@ dependencies { compileOnly group: 'org.opensearch', name:'opensearch-ml-client', version: "${version}" implementation fileTree(dir: jsJarDirectory, include: ["opensearch-job-scheduler-${version}.jar"]) implementation fileTree(dir: adJarDirectory, include: ["opensearch-time-series-analytics-${version}.jar"]) + // implementation fileTree(dir: alertingJarDirectory, include: ["alerting-${version}.jar"]) implementation fileTree(dir: sqlJarDirectory, include: ["opensearch-sql-${version}.jar", "ppl-${version}.jar", "protocol-${version}.jar"]) compileOnly "org.opensearch:common-utils:${version}" compileOnly "org.jetbrains.kotlin:kotlin-stdlib:${kotlin_version}" compileOnly "org.opensearch:opensearch-job-scheduler-spi:${version}" + // compileOnly "org.opensearch.plugin:alerting:${version}" // ZipArchive dependencies used for integration tests @@ -129,6 +137,7 @@ dependencies { zipArchive group: 'org.opensearch.plugin', name:'opensearch-sql-plugin', version: "${version}" zipArchive group: 'org.opensearch.plugin', name:'opensearch-knn', version: "${version}" zipArchive group: 'org.opensearch.plugin', name:'neural-search', version: "${version}" + zipArchive group: 'org.opensearch.plugin', name:'alerting', version: "${version}" // Test dependencies testImplementation "org.opensearch.test:framework:${opensearch_version}" @@ -164,9 +173,16 @@ task extractAdJar(type: Copy) { into adJarDirectory } +// task extractAlertingJar(type: Copy) { +// mustRunAfter() +// from(zipTree(configurations.zipArchive.find { it.name.startsWith("alerting")})) +// into alertingJarDirectory +// } + tasks.addJarsToClasspath.dependsOn(extractSqlJar) tasks.addJarsToClasspath.dependsOn(extractJsJar) tasks.addJarsToClasspath.dependsOn(extractAdJar) +// tasks.addJarsToClasspath.dependsOn(extractAlertingJar) project.tasks.delombok.dependsOn(addJarsToClasspath) tasks.publishNebulaPublicationToMavenLocal.dependsOn ':generatePomFileForPluginZipPublication' tasks.validateNebulaPom.dependsOn ':generatePomFileForPluginZipPublication' @@ -212,6 +228,7 @@ compileJava { dependsOn extractSqlJar dependsOn extractJsJar dependsOn extractAdJar + // dependsOn extractAlertingJar dependsOn delombok options.compilerArgs.addAll(["-processor", 'lombok.launch.AnnotationProcessorHider$AnnotationProcessor']) } diff --git a/src/main/java/org/opensearch/agent/tools/SearchMonitorsTool.java b/src/main/java/org/opensearch/agent/tools/SearchMonitorsTool.java index 91f25182..162a3daf 100644 --- a/src/main/java/org/opensearch/agent/tools/SearchMonitorsTool.java +++ b/src/main/java/org/opensearch/agent/tools/SearchMonitorsTool.java @@ -20,6 +20,7 @@ import org.opensearch.commons.alerting.action.GetMonitorResponse; import org.opensearch.commons.alerting.action.SearchMonitorRequest; import org.opensearch.commons.alerting.model.Monitor; +import org.opensearch.commons.alerting.model.ScheduledJob; import org.opensearch.core.action.ActionListener; import org.opensearch.index.query.BoolQueryBuilder; import org.opensearch.index.query.ExistsQueryBuilder; @@ -167,7 +168,8 @@ public void run(Map parameters, ActionListener listener) .from(startIndex) .sort(sortString, sortOrder); - SearchMonitorRequest searchMonitorRequest = new SearchMonitorRequest(new SearchRequest().source(searchSourceBuilder)); + SearchRequest searchRequest = new SearchRequest().source(searchSourceBuilder).indices(ScheduledJob.SCHEDULED_JOBS_INDEX); + SearchMonitorRequest searchMonitorRequest = new SearchMonitorRequest(searchRequest); ActionListener searchMonitorListener = ActionListener.wrap(response -> { StringBuilder sb = new StringBuilder(); diff --git a/src/test/java/org/opensearch/integTest/SearchMonitorsToolIT.java b/src/test/java/org/opensearch/integTest/SearchMonitorsToolIT.java index 69e42128..6720dd35 100644 --- a/src/test/java/org/opensearch/integTest/SearchMonitorsToolIT.java +++ b/src/test/java/org/opensearch/integTest/SearchMonitorsToolIT.java @@ -3,94 +3,92 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.integTest; +package org.opensearch.integTest; - import java.nio.file.Files; - import java.nio.file.Path; - import java.util.List; - import java.util.Locale; - - import org.junit.After; - import org.junit.Before; - import org.opensearch.agent.tools.utils.ToolConstants; - - import lombok.SneakyThrows; - - public class SearchMonitorsToolIT extends BaseAgentToolsIT { - private String registerAgentRequestBody; - private static final String monitorId = "foo-id"; - private static final String monitorName = "foo-name"; - - @Before - @SneakyThrows - public void setUp() { - super.setUp(); - registerAgentRequestBody = Files - .readString( - Path - .of( - this - .getClass() - .getClassLoader() - .getResource("org/opensearch/agent/tools/register_flow_agent_of_search_monitors_tool_request_body.json") - .toURI() - ) - ); - createMonitorsSystemIndex(monitorId, monitorName); - } - - @After - @SneakyThrows - public void tearDown() { - super.tearDown(); - deleteExternalIndices(); - deleteSystemIndices(); - } - - @SneakyThrows - public void testSearchMonitorsToolInFlowAgent_withNoSystemIndex() { - deleteSystemIndices(); - String agentId = createAgent(registerAgentRequestBody); - String agentInput = "{\"parameters\":{\"monitorName\": \"" + monitorName + "\"}}"; - String result = executeAgent(agentId, agentInput); - assertEquals("Monitors=[]TotalMonitors=0", result); - } - - // @SneakyThrows - // public void testSearchAnomalyDetectorsToolInFlowAgent_noMatching() { - // String agentId = createAgent(registerAgentRequestBody); - // String agentInput = "{\"parameters\":{\"detectorName\": \"" + detectorName + "foo" + "\"}}"; - // String result = executeAgent(agentId, agentInput); - // assertEquals("AnomalyDetectors=[]TotalAnomalyDetectors=0", result); - // } - - // @SneakyThrows - // public void testSearchAnomalyDetectorsToolInFlowAgent_matching() { - // String agentId = createAgent(registerAgentRequestBody); - // String agentInput = "{\"parameters\":{\"detectorName\": \"" + detectorName + "\"}}"; - // String result = executeAgent(agentId, agentInput); - // assertEquals( - // String.format(Locale.ROOT, "AnomalyDetectors=[{id=%s,name=%s}]TotalAnomalyDetectors=%d", detectorId, detectorName, 1), - // result - // ); - // } - - @SneakyThrows - private void createMonitorsSystemIndex(String monitorId, String monitorName) { - createIndexWithConfiguration( - ToolConstants.ALERTING_CONFIG_INDEX, - "{\n" - + " \"mappings\": {\n" - + " \"properties\": {\n" - + " \"name\": {\n" - + " \"type\": \"text\",\n" - + " \"fields\": { \"keyword\": { \"type\": \"keyword\", \"ignore_above\": 256 }}" - + " }\n" - + " }\n" - + " }\n" - + "}" - ); - addDocToIndex(ToolConstants.ALERTING_CONFIG_INDEX, monitorId, List.of("name"), List.of(monitorName)); - } - } - \ No newline at end of file +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import org.junit.After; +import org.junit.Before; +import org.opensearch.agent.tools.utils.ToolConstants; + +import lombok.SneakyThrows; + +public class SearchMonitorsToolIT extends BaseAgentToolsIT { + private String registerAgentRequestBody; + private static final String monitorId = "foo-id"; + private static final String monitorName = "foo-name"; + + @Before + @SneakyThrows + public void setUp() { + super.setUp(); + registerAgentRequestBody = Files + .readString( + Path + .of( + this + .getClass() + .getClassLoader() + .getResource("org/opensearch/agent/tools/register_flow_agent_of_search_monitors_tool_request_body.json") + .toURI() + ) + ); + createMonitorsSystemIndex(monitorId, monitorName); + } + + @After + @SneakyThrows + public void tearDown() { + super.tearDown(); + deleteExternalIndices(); + deleteSystemIndices(); + } + + @SneakyThrows + public void testSearchMonitorsToolInFlowAgent_withNoSystemIndex() { + deleteSystemIndices(); + String agentId = createAgent(registerAgentRequestBody); + String agentInput = "{\"parameters\":{\"monitorName\": \"" + monitorName + "\"}}"; + String result = executeAgent(agentId, agentInput); + assertEquals("Monitors=[]TotalMonitors=0", result); + } + + // @SneakyThrows + // public void testSearchAnomalyDetectorsToolInFlowAgent_noMatching() { + // String agentId = createAgent(registerAgentRequestBody); + // String agentInput = "{\"parameters\":{\"detectorName\": \"" + detectorName + "foo" + "\"}}"; + // String result = executeAgent(agentId, agentInput); + // assertEquals("AnomalyDetectors=[]TotalAnomalyDetectors=0", result); + // } + + // @SneakyThrows + // public void testSearchAnomalyDetectorsToolInFlowAgent_matching() { + // String agentId = createAgent(registerAgentRequestBody); + // String agentInput = "{\"parameters\":{\"detectorName\": \"" + detectorName + "\"}}"; + // String result = executeAgent(agentId, agentInput); + // assertEquals( + // String.format(Locale.ROOT, "AnomalyDetectors=[{id=%s,name=%s}]TotalAnomalyDetectors=%d", detectorId, detectorName, 1), + // result + // ); + // } + + @SneakyThrows + private void createMonitorsSystemIndex(String monitorId, String monitorName) { + createIndexWithConfiguration( + ToolConstants.ALERTING_CONFIG_INDEX, + "{\n" + + " \"mappings\": {\n" + + " \"properties\": {\n" + + " \"name\": {\n" + + " \"type\": \"text\",\n" + + " \"fields\": { \"keyword\": { \"type\": \"keyword\", \"ignore_above\": 256 }}" + + " }\n" + + " }\n" + + " }\n" + + "}" + ); + addDocToIndex(ToolConstants.ALERTING_CONFIG_INDEX, monitorId, List.of("name"), List.of(monitorName)); + } +}