Search in sources :

Example 1 with JsonPathNotFoundException

use of org.opensearch.ad.common.exception.JsonPathNotFoundException in project anomaly-detection by opensearch-project.

the class JsonDeserializer method getListValue.

public static <T> List<T> getListValue(String jsonString, Function<JsonElement, T> function, String... paths) throws JsonPathNotFoundException, IOException {
    JsonElement jsonNode = getChildNode(jsonString, paths);
    if (jsonNode != null && jsonNode.isJsonArray()) {
        JsonArray array = jsonNode.getAsJsonArray();
        List<T> values = new ArrayList<>(array.size());
        for (int i = 0; i < array.size(); i++) {
            values.add(function.apply(array.get(i)));
        }
        return values;
    }
    throw new JsonPathNotFoundException();
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPathNotFoundException(org.opensearch.ad.common.exception.JsonPathNotFoundException) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList)

Example 2 with JsonPathNotFoundException

use of org.opensearch.ad.common.exception.JsonPathNotFoundException in project anomaly-detection by opensearch-project.

the class CronTransportActionTests method testNormal.

public void testNormal() throws IOException, JsonPathNotFoundException {
    CronRequest request = new CronRequest();
    CronNodeRequest nodeRequest = new CronNodeRequest();
    BytesStreamOutput nodeRequestOut = new BytesStreamOutput();
    nodeRequestOut.setVersion(Version.V_1_0_0);
    nodeRequest.writeTo(nodeRequestOut);
    StreamInput siNode = nodeRequestOut.bytes().streamInput();
    siNode.setVersion(Version.V_1_0_0);
    CronNodeRequest nodeResponseRead = new CronNodeRequest(siNode);
    CronNodeResponse nodeResponse1 = action.nodeOperation(nodeResponseRead);
    CronNodeResponse nodeResponse2 = action.nodeOperation(new CronNodeRequest());
    CronResponse response = action.newResponse(request, Arrays.asList(nodeResponse1, nodeResponse2), Collections.emptyList());
    assertEquals(2, response.getNodes().size());
    assertTrue(!response.hasFailures());
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    response.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    String json = Strings.toString(builder);
    Function<JsonElement, String> function = (s) -> {
        try {
            return JsonDeserializer.getTextValue(s, CronNodeResponse.NODE_ID);
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
        return null;
    };
    assertArrayEquals(JsonDeserializer.getArrayValue(json, function, CronResponse.NODES_JSON_KEY), new String[] { localNodeID, localNodeID });
}
Also used : Arrays(java.util.Arrays) CacheProvider(org.opensearch.ad.caching.CacheProvider) JsonDeserializer(test.org.opensearch.ad.util.JsonDeserializer) BeforeClass(org.junit.BeforeClass) ModelManager(org.opensearch.ad.ml.ModelManager) ToXContent(org.opensearch.common.xcontent.ToXContent) ThreadPool(org.opensearch.threadpool.ThreadPool) AbstractADTest(org.opensearch.ad.AbstractADTest) Version(org.opensearch.Version) JsonPathNotFoundException(org.opensearch.ad.common.exception.JsonPathNotFoundException) Function(java.util.function.Function) Strings(org.opensearch.common.Strings) JsonElement(com.google.gson.JsonElement) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) EntityCache(org.opensearch.ad.caching.EntityCache) XContentFactory(org.opensearch.common.xcontent.XContentFactory) Bwc(org.opensearch.ad.util.Bwc) Before(org.junit.Before) StreamInput(org.opensearch.common.io.stream.StreamInput) FeatureManager(org.opensearch.ad.feature.FeatureManager) IOException(java.io.IOException) ADTaskManager(org.opensearch.ad.task.ADTaskManager) Mockito.when(org.mockito.Mockito.when) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) TransportService(org.opensearch.transport.TransportService) EntityColdStarter(org.opensearch.ad.ml.EntityColdStarter) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) ActionFilters(org.opensearch.action.support.ActionFilters) NodeStateManager(org.opensearch.ad.NodeStateManager) ClusterService(org.opensearch.cluster.service.ClusterService) ClusterName(org.opensearch.cluster.ClusterName) Assert(org.junit.Assert) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) JsonElement(com.google.gson.JsonElement) StreamInput(org.opensearch.common.io.stream.StreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) JsonPathNotFoundException(org.opensearch.ad.common.exception.JsonPathNotFoundException) IOException(java.io.IOException)

Example 3 with JsonPathNotFoundException

use of org.opensearch.ad.common.exception.JsonPathNotFoundException in project anomaly-detection by opensearch-project.

the class JsonDeserializer method getDoubleArrayValue.

/**
 * Search an int number inside a JSON string matching the input path expression
 *
 * @param jsonString an encoded JSON string
 * @param paths      path fragments
 * @return list of double
 * @throws JsonPathNotFoundException if json path is invalid
 * @throws IOException               if the underlying input source has problems
 *                                   during parsing
 */
public static double[] getDoubleArrayValue(String jsonString, String... paths) throws JsonPathNotFoundException, IOException {
    JsonElement jsonNode = getChildNode(jsonString, paths);
    if (jsonNode != null && jsonNode.isJsonArray()) {
        JsonArray array = jsonNode.getAsJsonArray();
        List<Double> values = new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
            values.add(array.get(i).getAsDouble());
        }
        return values.stream().mapToDouble(i -> i).toArray();
    }
    throw new JsonPathNotFoundException();
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) JsonParseException(com.google.gson.JsonParseException) JsonObject(com.google.gson.JsonObject) List(java.util.List) StringUtils(org.apache.commons.lang.StringUtils) JsonArray(com.google.gson.JsonArray) IOException(java.io.IOException) JsonPathNotFoundException(org.opensearch.ad.common.exception.JsonPathNotFoundException) JsonParser(com.google.gson.JsonParser) Function(java.util.function.Function) ArrayList(java.util.ArrayList) JsonPathNotFoundException(org.opensearch.ad.common.exception.JsonPathNotFoundException) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList)

Example 4 with JsonPathNotFoundException

use of org.opensearch.ad.common.exception.JsonPathNotFoundException in project anomaly-detection by opensearch-project.

the class JsonDeserializer method getArrayValue.

/**
 * Search an array inside a JSON string matching the input path expression and convert each element using a function
 *
 * @param jsonString an encoded JSON string
 * @param function function to parse each element
 * @param paths path fragments
 * @return an array of values
 * @throws JsonPathNotFoundException if json path is invalid
 * @throws IOException               if the underlying input source has problems
 *                                   during parsing
 */
@SuppressWarnings("unchecked")
public static <T> T[] getArrayValue(String jsonString, Function<JsonElement, T> function, String... paths) throws JsonPathNotFoundException, IOException {
    JsonElement jsonNode = getChildNode(jsonString, paths);
    if (jsonNode != null && jsonNode.isJsonArray()) {
        JsonArray array = jsonNode.getAsJsonArray();
        Object[] values = new Object[array.size()];
        for (int i = 0; i < array.size(); i++) {
            values[i] = function.apply(array.get(i));
        }
        return (T[]) values;
    }
    throw new JsonPathNotFoundException();
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPathNotFoundException(org.opensearch.ad.common.exception.JsonPathNotFoundException) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

Example 5 with JsonPathNotFoundException

use of org.opensearch.ad.common.exception.JsonPathNotFoundException in project anomaly-detection by opensearch-project.

the class AnomalyResultTests method testJsonResponse.

public void testJsonResponse() throws IOException, JsonPathNotFoundException {
    AnomalyResultResponse response = new AnomalyResultResponse(4d, 0.993, 1.01, Collections.singletonList(new FeatureData(featureId, featureName, 0d)), randomAlphaOfLength(4), randomLong(), randomLong(), randomBoolean(), randomInt(), new double[] { randomDoubleBetween(0, 1.0, true), randomDoubleBetween(0, 1.0, true) }, new double[] { randomDouble(), randomDouble() }, new double[][] { new double[] { randomDouble(), randomDouble() } }, new double[] { randomDouble() }, randomDoubleBetween(1.1, 10.0, true));
    XContentBuilder builder = jsonBuilder();
    response.toXContent(builder, ToXContent.EMPTY_PARAMS);
    String json = Strings.toString(builder);
    Function<JsonElement, FeatureData> function = (s) -> {
        try {
            String featureId = JsonDeserializer.getTextValue(s, FeatureData.FEATURE_ID_FIELD);
            String featureName = JsonDeserializer.getTextValue(s, FeatureData.FEATURE_NAME_FIELD);
            double featureValue = JsonDeserializer.getDoubleValue(s, FeatureData.DATA_FIELD);
            return new FeatureData(featureId, featureName, featureValue);
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
        return null;
    };
    AnomalyResultResponse readResponse = new AnomalyResultResponse(JsonDeserializer.getDoubleValue(json, AnomalyResultResponse.ANOMALY_GRADE_JSON_KEY), JsonDeserializer.getDoubleValue(json, AnomalyResultResponse.CONFIDENCE_JSON_KEY), JsonDeserializer.getDoubleValue(json, AnomalyResultResponse.ANOMALY_SCORE_JSON_KEY), JsonDeserializer.getListValue(json, function, AnomalyResultResponse.FEATURES_JSON_KEY), randomAlphaOfLength(4), randomLong(), randomLong(), randomBoolean(), randomInt(), new double[] { randomDoubleBetween(0, 1.0, true), randomDoubleBetween(0, 1.0, true) }, new double[] { randomDouble(), randomDouble() }, new double[][] { new double[] { randomDouble(), randomDouble() } }, new double[] { randomDouble() }, randomDoubleBetween(1.1, 10.0, true));
    assertAnomalyResultResponse(readResponse, readResponse.getAnomalyGrade(), readResponse.getConfidence(), 0d);
}
Also used : Arrays(java.util.Arrays) ModelManager(org.opensearch.ad.ml.ModelManager) IndexResponse(org.opensearch.action.index.IndexResponse) HashRing(org.opensearch.ad.cluster.HashRing) ThresholdingResult(org.opensearch.ad.ml.ThresholdingResult) TransportInterceptor(org.opensearch.transport.TransportInterceptor) AbstractADTest(org.opensearch.ad.AbstractADTest) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) Version(org.opensearch.Version) LimitExceededException(org.opensearch.ad.common.exception.LimitExceededException) Strings(org.opensearch.common.Strings) Transport(org.opensearch.transport.Transport) Mockito.doThrow(org.mockito.Mockito.doThrow) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) ActionListener(org.opensearch.action.ActionListener) Mockito.doReturn(org.mockito.Mockito.doReturn) NodeNotConnectedException(org.opensearch.transport.NodeNotConnectedException) AfterClass(org.junit.AfterClass) Client(org.opensearch.client.Client) Index(org.opensearch.index.Index) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) ADTaskManager(org.opensearch.ad.task.ADTaskManager) Settings(org.opensearch.common.settings.Settings) TransportService(org.opensearch.transport.TransportService) ActionFilters(org.opensearch.action.support.ActionFilters) Mockito.anyDouble(org.mockito.Mockito.anyDouble) NodeStateManager(org.opensearch.ad.NodeStateManager) TestHelpers(org.opensearch.ad.TestHelpers) DetectorInternalState(org.opensearch.ad.model.DetectorInternalState) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) SingleStreamModelIdMapper(org.opensearch.ad.ml.SingleStreamModelIdMapper) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) TransportException(org.opensearch.transport.TransportException) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) ADCircuitBreakerService(org.opensearch.ad.breaker.ADCircuitBreakerService) XContentFactory.jsonBuilder(org.opensearch.common.xcontent.XContentFactory.jsonBuilder) ThreadPool(org.opensearch.threadpool.ThreadPool) Mockito.spy(org.mockito.Mockito.spy) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) ArrayList(java.util.ArrayList) ClusterState(org.opensearch.cluster.ClusterState) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) Mockito.anyLong(org.mockito.Mockito.anyLong) Mockito.anyString(org.mockito.Mockito.anyString) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) Before(org.junit.Before) SinglePointFeatures(org.opensearch.ad.feature.SinglePointFeatures) InternalFailure(org.opensearch.ad.common.exception.InternalFailure) CommonName(org.opensearch.ad.constant.CommonName) CounterSupplier(org.opensearch.ad.stats.suppliers.CounterSupplier) FeatureManager(org.opensearch.ad.feature.FeatureManager) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) ADStat(org.opensearch.ad.stats.ADStat) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Mockito.never(org.mockito.Mockito.never) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) Matchers.hasItem(org.hamcrest.Matchers.hasItem) OpenSearchTestCase.randomDoubleBetween(org.opensearch.test.OpenSearchTestCase.randomDoubleBetween) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) Assert(org.junit.Assert) StatNames(org.opensearch.ad.stats.StatNames) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ResourceNotFoundException(org.opensearch.ad.common.exception.ResourceNotFoundException) OpenSearchTestCase.randomBoolean(org.opensearch.test.OpenSearchTestCase.randomBoolean) ADStats(org.opensearch.ad.stats.ADStats) ToXContent(org.opensearch.common.xcontent.ToXContent) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) OpenSearchTestCase.randomDouble(org.opensearch.test.OpenSearchTestCase.randomDouble) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) After(org.junit.After) OpenSearchTestCase.randomIntBetween(org.opensearch.test.OpenSearchTestCase.randomIntBetween) GetResponse(org.opensearch.action.get.GetResponse) RemoteTransportException(org.opensearch.transport.RemoteTransportException) TransportResponse(org.opensearch.transport.TransportResponse) Instant(java.time.Instant) TransportAddress(org.opensearch.common.transport.TransportAddress) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) NotSerializableExceptionWrapper(org.opensearch.common.io.stream.NotSerializableExceptionWrapper) JsonDeserializer(test.org.opensearch.ad.util.JsonDeserializer) BeforeClass(org.junit.BeforeClass) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) FeatureData(org.opensearch.ad.model.FeatureData) HashMap(java.util.HashMap) JsonPathNotFoundException(org.opensearch.ad.common.exception.JsonPathNotFoundException) Function(java.util.function.Function) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) JsonElement(com.google.gson.JsonElement) ArgumentCaptor(org.mockito.ArgumentCaptor) TestHelpers.createIndexBlockedState(org.opensearch.ad.TestHelpers.createIndexBlockedState) NoSuchElementException(java.util.NoSuchElementException) Mockito.same(org.mockito.Mockito.same) EndRunException(org.opensearch.ad.common.exception.EndRunException) StreamInput(org.opensearch.common.io.stream.StreamInput) Matchers.empty(org.hamcrest.Matchers.empty) TransportRequest(org.opensearch.transport.TransportRequest) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) GetRequest(org.opensearch.action.get.GetRequest) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ShardId(org.opensearch.index.shard.ShardId) ClusterName(org.opensearch.cluster.ClusterName) IndexRequest(org.opensearch.action.index.IndexRequest) Collections(java.util.Collections) JsonElement(com.google.gson.JsonElement) FeatureData(org.opensearch.ad.model.FeatureData) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.anyString(org.mockito.Mockito.anyString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) LimitExceededException(org.opensearch.ad.common.exception.LimitExceededException) NodeNotConnectedException(org.opensearch.transport.NodeNotConnectedException) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) TransportException(org.opensearch.transport.TransportException) IOException(java.io.IOException) ResourceNotFoundException(org.opensearch.ad.common.exception.ResourceNotFoundException) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) RemoteTransportException(org.opensearch.transport.RemoteTransportException) JsonPathNotFoundException(org.opensearch.ad.common.exception.JsonPathNotFoundException) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) NoSuchElementException(java.util.NoSuchElementException) EndRunException(org.opensearch.ad.common.exception.EndRunException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Aggregations

JsonElement (com.google.gson.JsonElement)6 JsonPathNotFoundException (org.opensearch.ad.common.exception.JsonPathNotFoundException)6 IOException (java.io.IOException)4 Function (java.util.function.Function)4 JsonArray (com.google.gson.JsonArray)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 Assert (org.junit.Assert)3 Before (org.junit.Before)3 Mockito.mock (org.mockito.Mockito.mock)3 Mockito.when (org.mockito.Mockito.when)3 Version (org.opensearch.Version)3 ActionFilters (org.opensearch.action.support.ActionFilters)3 AbstractADTest (org.opensearch.ad.AbstractADTest)3 NodeStateManager (org.opensearch.ad.NodeStateManager)3 FeatureManager (org.opensearch.ad.feature.FeatureManager)3 ModelManager (org.opensearch.ad.ml.ModelManager)3 ClusterName (org.opensearch.cluster.ClusterName)3 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)3