Search in sources :

Example 6 with EntityProfileName

use of org.opensearch.ad.model.EntityProfileName in project anomaly-detection by opensearch-project.

the class EntityProfileRunnerTests method setUp.

@SuppressWarnings("unchecked")
@Override
public void setUp() throws Exception {
    super.setUp();
    detectorIntervalMin = 3;
    state = new HashSet<EntityProfileName>();
    state.add(EntityProfileName.STATE);
    initNInfo = new HashSet<EntityProfileName>();
    initNInfo.add(EntityProfileName.INIT_PROGRESS);
    initNInfo.add(EntityProfileName.ENTITY_INFO);
    model = new HashSet<EntityProfileName>();
    model.add(EntityProfileName.MODELS);
    detectorId = "A69pa3UBHuCbh-emo9oR";
    entityValue = "app-0";
    requiredSamples = 128;
    client = mock(Client.class);
    runner = new EntityProfileRunner(client, xContentRegistry(), requiredSamples);
    categoryField = "a";
    detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, Arrays.asList(categoryField));
    job = TestHelpers.randomAnomalyDetectorJob(true);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        GetRequest request = (GetRequest) args[0];
        ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
        String indexName = request.index();
        if (indexName.equals(ANOMALY_DETECTORS_INDEX)) {
            listener.onResponse(TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX));
        } else if (indexName.equals(ANOMALY_DETECTOR_JOB_INDEX)) {
            listener.onResponse(TestHelpers.createGetResponse(job, detector.getDetectorId(), AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX));
        }
        return null;
    }).when(client).get(any(), any());
    entity = Entity.createSingleAttributeEntity(categoryField, entityValue);
}
Also used : ActionListener(org.opensearch.action.ActionListener) GetRequest(org.opensearch.action.get.GetRequest) Client(org.opensearch.client.Client) GetResponse(org.opensearch.action.get.GetResponse) EntityProfileName(org.opensearch.ad.model.EntityProfileName)

Example 7 with EntityProfileName

use of org.opensearch.ad.model.EntityProfileName in project anomaly-detection by opensearch-project.

the class EntityProfileRequest1_0 method writeTo.

@Override
public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    out.writeString(adID);
    out.writeString(entityValue);
    out.writeVInt(profilesToCollect.size());
    for (EntityProfileName profile : profilesToCollect) {
        out.writeEnum(profile);
    }
}
Also used : EntityProfileName(org.opensearch.ad.model.EntityProfileName)

Example 8 with EntityProfileName

use of org.opensearch.ad.model.EntityProfileName in project anomaly-detection by opensearch-project.

the class BwcTests method setUpEntityProfileRequest.

private void setUpEntityProfileRequest() {
    profilesToCollect = new HashSet<EntityProfileName>();
    profilesToCollect.add(EntityProfileName.STATE);
    entityProfileRequest1_1 = new EntityProfileRequest(detectorId, entity, profilesToCollect);
    entityProfileRequest1_0 = new EntityProfileRequest1_0(detectorId, categoryValue, profilesToCollect);
}
Also used : EntityProfileRequest(org.opensearch.ad.transport.EntityProfileRequest) EntityProfileName(org.opensearch.ad.model.EntityProfileName)

Example 9 with EntityProfileName

use of org.opensearch.ad.model.EntityProfileName in project anomaly-detection by opensearch-project.

the class EntityProfileTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    state = new HashSet<EntityProfileName>();
    state.add(EntityProfileName.STATE);
    all = new HashSet<EntityProfileName>();
    all.add(EntityProfileName.INIT_PROGRESS);
    all.add(EntityProfileName.ENTITY_INFO);
    all.add(EntityProfileName.MODELS);
    model = new HashSet<EntityProfileName>();
    model.add(EntityProfileName.MODELS);
    hashRing = mock(HashRing.class);
    actionFilters = mock(ActionFilters.class);
    transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
    settings = Settings.EMPTY;
    modelId = "yecrdnUBqurvo9uKU_d8_entity_app_0";
    clusterService = mock(ClusterService.class);
    cacheProvider = mock(CacheProvider.class);
    EntityCache cache = mock(EntityCache.class);
    updates = 1L;
    when(cache.getTotalUpdates(anyString(), anyString())).thenReturn(updates);
    when(cache.isActive(anyString(), anyString())).thenReturn(isActive);
    when(cache.getLastActiveMs(anyString(), anyString())).thenReturn(lastActiveTimestamp);
    Map<String, Long> modelSizeMap = new HashMap<>();
    modelSizeMap.put(modelId, modelSize);
    when(cache.getModelSize(anyString())).thenReturn(modelSizeMap);
    when(cacheProvider.get()).thenReturn(cache);
    action = new EntityProfileTransportAction(actionFilters, transportService, settings, hashRing, clusterService, cacheProvider);
    future = new PlainActionFuture<>();
    transportAddress1 = new TransportAddress(new InetSocketAddress(InetAddress.getByName("1.2.3.4"), 9300));
    entity = Entity.createSingleAttributeEntity(categoryName, entityValue);
    request = new EntityProfileRequest(detectorId, entity, state);
    normalTransportInterceptor = new TransportInterceptor() {

        @Override
        public AsyncSender interceptSender(AsyncSender sender) {
            return new AsyncSender() {

                @Override
                public <T extends TransportResponse> void sendRequest(Transport.Connection connection, String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler) {
                    if (EntityProfileAction.NAME.equals(action)) {
                        sender.sendRequest(connection, action, request, options, entityProfileHandler(handler));
                    } else {
                        sender.sendRequest(connection, action, request, options, handler);
                    }
                }
            };
        }
    };
    failureTransportInterceptor = new TransportInterceptor() {

        @Override
        public AsyncSender interceptSender(AsyncSender sender) {
            return new AsyncSender() {

                @Override
                public <T extends TransportResponse> void sendRequest(Transport.Connection connection, String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler) {
                    if (EntityProfileAction.NAME.equals(action)) {
                        sender.sendRequest(connection, action, request, options, entityFailureProfileandler(handler));
                    } else {
                        sender.sendRequest(connection, action, request, options, handler);
                    }
                }
            };
        }
    };
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CacheProvider(org.opensearch.ad.caching.CacheProvider) JsonDeserializer(test.org.opensearch.ad.util.JsonDeserializer) BeforeClass(org.junit.BeforeClass) ModelProfile(org.opensearch.ad.model.ModelProfile) HashRing(org.opensearch.ad.cluster.HashRing) ToXContent(org.opensearch.common.xcontent.ToXContent) TransportInterceptor(org.opensearch.transport.TransportInterceptor) AbstractADTest(org.opensearch.ad.AbstractADTest) HashMap(java.util.HashMap) Version(org.opensearch.Version) JsonPathNotFoundException(org.opensearch.ad.common.exception.JsonPathNotFoundException) EntityProfileName(org.opensearch.ad.model.EntityProfileName) ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet) Transport(org.opensearch.transport.Transport) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Map(java.util.Map) EntityCache(org.opensearch.ad.caching.EntityCache) Mockito.anyString(org.mockito.Mockito.anyString) StreamInput(org.opensearch.common.io.stream.StreamInput) AfterClass(org.junit.AfterClass) CommonName(org.opensearch.ad.constant.CommonName) TransportRequest(org.opensearch.transport.TransportRequest) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) Set(java.util.Set) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) IOException(java.io.IOException) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) Task(org.opensearch.tasks.Task) TransportResponse(org.opensearch.transport.TransportResponse) InetSocketAddress(java.net.InetSocketAddress) TransportService(org.opensearch.transport.TransportService) TransportAddress(org.opensearch.common.transport.TransportAddress) ActionFilters(org.opensearch.action.support.ActionFilters) Entity(org.opensearch.ad.model.Entity) TestHelpers(org.opensearch.ad.TestHelpers) FakeNode(test.org.opensearch.ad.util.FakeNode) ClusterService(org.opensearch.cluster.service.ClusterService) Optional(java.util.Optional) ConnectTransportException(org.opensearch.transport.ConnectTransportException) Collections(java.util.Collections) TransportException(org.opensearch.transport.TransportException) Mockito.mock(org.mockito.Mockito.mock) TransportInterceptor(org.opensearch.transport.TransportInterceptor) EntityCache(org.opensearch.ad.caching.EntityCache) HashMap(java.util.HashMap) TransportAddress(org.opensearch.common.transport.TransportAddress) InetSocketAddress(java.net.InetSocketAddress) Mockito.anyString(org.mockito.Mockito.anyString) HashRing(org.opensearch.ad.cluster.HashRing) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) TransportRequest(org.opensearch.transport.TransportRequest) ActionFilters(org.opensearch.action.support.ActionFilters) CacheProvider(org.opensearch.ad.caching.CacheProvider) ClusterService(org.opensearch.cluster.service.ClusterService) TransportService(org.opensearch.transport.TransportService) Transport(org.opensearch.transport.Transport) EntityProfileName(org.opensearch.ad.model.EntityProfileName)

Example 10 with EntityProfileName

use of org.opensearch.ad.model.EntityProfileName in project anomaly-detection by opensearch-project.

the class EntityProfileRunner method profile.

/**
 * Get profile info of specific entity.
 *
 * @param detectorId detector identifier
 * @param entityValue entity value
 * @param profilesToCollect profiles to collect
 * @param listener action listener to handle exception and process entity profile response
 */
public void profile(String detectorId, Entity entityValue, Set<EntityProfileName> profilesToCollect, ActionListener<EntityProfile> listener) {
    if (profilesToCollect == null || profilesToCollect.size() == 0) {
        listener.onFailure(new IllegalArgumentException(CommonErrorMessages.EMPTY_PROFILES_COLLECT));
        return;
    }
    GetRequest getDetectorRequest = new GetRequest(ANOMALY_DETECTORS_INDEX, detectorId);
    client.get(getDetectorRequest, ActionListener.wrap(getResponse -> {
        if (getResponse != null && getResponse.isExists()) {
            try (XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, getResponse.getSourceAsString())) {
                ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
                AnomalyDetector detector = AnomalyDetector.parse(parser, detectorId);
                List<String> categoryFields = detector.getCategoryField();
                int maxCategoryFields = NumericSetting.maxCategoricalFields();
                if (categoryFields == null || categoryFields.size() == 0) {
                    listener.onFailure(new IllegalArgumentException(NOT_HC_DETECTOR_ERR_MSG));
                } else if (categoryFields.size() > maxCategoryFields) {
                    listener.onFailure(new IllegalArgumentException(CommonErrorMessages.getTooManyCategoricalFieldErr(maxCategoryFields)));
                } else {
                    validateEntity(entityValue, categoryFields, detectorId, profilesToCollect, detector, listener);
                }
            } catch (Exception t) {
                listener.onFailure(t);
            }
        } else {
            listener.onFailure(new IllegalArgumentException(CommonErrorMessages.FAIL_TO_FIND_DETECTOR_MSG + detectorId));
        }
    }, listener::onFailure));
}
Also used : EntityProfileRequest(org.opensearch.ad.transport.EntityProfileRequest) EntityProfileName(org.opensearch.ad.model.EntityProfileName) XContentParser(org.opensearch.common.xcontent.XContentParser) ANOMALY_DETECTOR_JOB_INDEX(org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX) EntityProfileResponse(org.opensearch.ad.transport.EntityProfileResponse) Map(java.util.Map) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) SearchRequest(org.opensearch.action.search.SearchRequest) ActionListener(org.opensearch.action.ActionListener) EntityProfile(org.opensearch.ad.model.EntityProfile) NestedQueryBuilder(org.opensearch.index.query.NestedQueryBuilder) QueryBuilders(org.opensearch.index.query.QueryBuilders) Client(org.opensearch.client.Client) CommonName(org.opensearch.ad.constant.CommonName) NumericSetting(org.opensearch.ad.settings.NumericSetting) Preference(org.opensearch.cluster.routing.Preference) InitProgressProfile(org.opensearch.ad.model.InitProgressProfile) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ANOMALY_DETECTORS_INDEX(org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX) GetRequest(org.opensearch.action.get.GetRequest) LoggingDeprecationHandler(org.opensearch.common.xcontent.LoggingDeprecationHandler) Set(java.util.Set) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) XContentParserUtils.ensureExpectedToken(org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken) ScoreMode(org.apache.lucene.search.join.ScoreMode) MultiResponsesDelegateActionListener(org.opensearch.ad.util.MultiResponsesDelegateActionListener) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) List(java.util.List) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) Logger(org.apache.logging.log4j.Logger) AnomalyResult(org.opensearch.ad.model.AnomalyResult) Entity(org.opensearch.ad.model.Entity) EntityState(org.opensearch.ad.model.EntityState) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) Optional(java.util.Optional) XContentType(org.opensearch.common.xcontent.XContentType) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) LogManager(org.apache.logging.log4j.LogManager) EntityProfileAction(org.opensearch.ad.transport.EntityProfileAction) ParseUtils(org.opensearch.ad.util.ParseUtils) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) GetRequest(org.opensearch.action.get.GetRequest) List(java.util.List) XContentParser(org.opensearch.common.xcontent.XContentParser) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Aggregations

EntityProfileName (org.opensearch.ad.model.EntityProfileName)11 Entity (org.opensearch.ad.model.Entity)7 Map (java.util.Map)6 Optional (java.util.Optional)6 Set (java.util.Set)6 ActionListener (org.opensearch.action.ActionListener)6 Client (org.opensearch.client.Client)6 List (java.util.List)5 LogManager (org.apache.logging.log4j.LogManager)5 Logger (org.apache.logging.log4j.Logger)5 GetRequest (org.opensearch.action.get.GetRequest)5 CommonName (org.opensearch.ad.constant.CommonName)5 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)5 ANOMALY_DETECTORS_INDEX (org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX)5 AnomalyDetectorJob (org.opensearch.ad.model.AnomalyDetectorJob)5 ANOMALY_DETECTOR_JOB_INDEX (org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX)5 EntityProfileRequest (org.opensearch.ad.transport.EntityProfileRequest)5 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)5 XContentParser (org.opensearch.common.xcontent.XContentParser)5 XContentParserUtils.ensureExpectedToken (org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken)5