Search in sources :

Example 6 with ModelProfile

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

the class EntityProfileRunnerTests method testModel.

public void testModel() throws InterruptedException {
    setUpExecuteEntityProfileAction(InittedEverResultStatus.INITTED);
    EntityProfile.Builder expectedProfile = new EntityProfile.Builder();
    ModelProfileOnNode modelProfile = new ModelProfileOnNode(nodeId, new ModelProfile(modelId, entity, modelSize));
    expectedProfile.modelProfile(modelProfile);
    final CountDownLatch inProgressLatch = new CountDownLatch(1);
    runner.profile(detectorId, entity, model, ActionListener.wrap(response -> {
        assertEquals(expectedProfile.build(), response);
        inProgressLatch.countDown();
    }, exception -> {
        assertTrue("Should not reach here", false);
        inProgressLatch.countDown();
    }));
    assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) ModelProfile(org.opensearch.ad.model.ModelProfile) DocValueFormat(org.opensearch.search.DocValueFormat) SearchHits(org.opensearch.search.SearchHits) EntityProfileName(org.opensearch.ad.model.EntityProfileName) ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) HashSet(java.util.HashSet) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) ANOMALY_DETECTOR_JOB_INDEX(org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX) EntityProfileResponse(org.opensearch.ad.transport.EntityProfileResponse) Mockito.doAnswer(org.mockito.Mockito.doAnswer) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) SearchRequest(org.opensearch.action.search.SearchRequest) ActionListener(org.opensearch.action.ActionListener) SearchResponse(org.opensearch.action.search.SearchResponse) GetResponse(org.opensearch.action.get.GetResponse) EntityProfile(org.opensearch.ad.model.EntityProfile) Collections.emptyMap(java.util.Collections.emptyMap) Client(org.opensearch.client.Client) CommonName(org.opensearch.ad.constant.CommonName) InitProgressProfile(org.opensearch.ad.model.InitProgressProfile) SearchHit(org.opensearch.search.SearchHit) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ANOMALY_DETECTORS_INDEX(org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX) GetRequest(org.opensearch.action.get.GetRequest) Set(java.util.Set) IOException(java.io.IOException) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) TimeUnit(java.util.concurrent.TimeUnit) TotalHits(org.apache.lucene.search.TotalHits) CountDownLatch(java.util.concurrent.CountDownLatch) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) ChronoUnit(java.time.temporal.ChronoUnit) Entity(org.opensearch.ad.model.Entity) SearchResponseSections(org.opensearch.action.search.SearchResponseSections) EntityState(org.opensearch.ad.model.EntityState) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) Collections(java.util.Collections) EntityProfileAction(org.opensearch.ad.transport.EntityProfileAction) Text(org.opensearch.common.text.Text) Mockito.mock(org.mockito.Mockito.mock) ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) EntityProfile(org.opensearch.ad.model.EntityProfile) CountDownLatch(java.util.concurrent.CountDownLatch) ModelProfile(org.opensearch.ad.model.ModelProfile)

Example 7 with ModelProfile

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

the class EntityProfileRunnerTests method setUpExecuteEntityProfileAction.

@SuppressWarnings("unchecked")
private void setUpExecuteEntityProfileAction(InittedEverResultStatus initted) {
    smallUpdates = 1;
    latestActiveTimestamp = 1603999189758L;
    isActive = Boolean.TRUE;
    modelId = "T4c3dXUBj-2IZN7itix__entity_" + entityValue;
    modelSize = 712480L;
    nodeId = "g6pmr547QR-CfpEvO67M4g";
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        ActionListener<EntityProfileResponse> listener = (ActionListener<EntityProfileResponse>) args[2];
        EntityProfileResponse.Builder profileResponseBuilder = new EntityProfileResponse.Builder();
        if (InittedEverResultStatus.UNKNOWN == initted) {
            profileResponseBuilder.setTotalUpdates(0L);
        } else if (InittedEverResultStatus.NOT_INITTED == initted) {
            profileResponseBuilder.setTotalUpdates(smallUpdates);
            profileResponseBuilder.setLastActiveMs(latestActiveTimestamp);
            profileResponseBuilder.setActive(isActive);
        } else {
            profileResponseBuilder.setTotalUpdates(requiredSamples + 1);
            ModelProfileOnNode model = new ModelProfileOnNode(nodeId, new ModelProfile(modelId, entity, modelSize));
            profileResponseBuilder.setModelProfile(model);
        }
        listener.onResponse(profileResponseBuilder.build());
        return null;
    }).when(client).execute(any(EntityProfileAction.class), any(), any());
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        SearchRequest request = (SearchRequest) args[0];
        String indexName = request.indices()[0];
        ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
        SearchResponse searchResponse = null;
        if (indexName.equals(CommonName.ANOMALY_RESULT_INDEX_ALIAS)) {
            InternalMax maxAgg = new InternalMax(CommonName.AGG_NAME_MAX_TIME, latestSampleTimestamp, DocValueFormat.RAW, emptyMap());
            InternalAggregations internalAggregations = InternalAggregations.from(Collections.singletonList(maxAgg));
            SearchHits hits = new SearchHits(new SearchHit[] {}, null, Float.NaN);
            SearchResponseSections searchSections = new SearchResponseSections(hits, internalAggregations, null, false, false, null, 1);
            searchResponse = new SearchResponse(searchSections, null, 1, 1, 0, 30, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
        } else {
            SearchHits collapsedHits = new SearchHits(new SearchHit[] { new SearchHit(2, "ID", new Text("type"), Collections.emptyMap(), Collections.emptyMap()), new SearchHit(3, "ID", new Text("type"), Collections.emptyMap(), Collections.emptyMap()) }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0F);
            InternalSearchResponse internalSearchResponse = new InternalSearchResponse(collapsedHits, null, null, null, false, null, 1);
            searchResponse = new SearchResponse(internalSearchResponse, null, 1, 1, 0, 0, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
        }
        listener.onResponse(searchResponse);
        return null;
    }).when(client).search(any(), any());
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) EntityProfileAction(org.opensearch.ad.transport.EntityProfileAction) SearchRequest(org.opensearch.action.search.SearchRequest) SearchResponseSections(org.opensearch.action.search.SearchResponseSections) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) SearchHit(org.opensearch.search.SearchHit) ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) Text(org.opensearch.common.text.Text) ModelProfile(org.opensearch.ad.model.ModelProfile) SearchResponse(org.opensearch.action.search.SearchResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) ActionListener(org.opensearch.action.ActionListener) EntityProfileResponse(org.opensearch.ad.transport.EntityProfileResponse) SearchHits(org.opensearch.search.SearchHits) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 8 with ModelProfile

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

the class BwcTests method setUpEntityProfileResponse.

private void setUpEntityProfileResponse() {
    long lastActiveTimestamp = 10L;
    EntityProfileResponse.Builder builder = new EntityProfileResponse.Builder();
    builder.setLastActiveMs(lastActiveTimestamp).build();
    ModelProfile modelProfile = new ModelProfile(modelId, entity, modelSize);
    ModelProfileOnNode model = new ModelProfileOnNode(nodeId, modelProfile);
    builder.setModelProfile(model);
    entityProfileResponse1_1 = builder.build();
    EntityProfileResponse1_0.Builder builder1_0 = new EntityProfileResponse1_0.Builder();
    builder1_0.setLastActiveMs(lastActiveTimestamp).build();
    ModelProfile1_0 modelProfile1_0 = new ModelProfile1_0(modelId, modelSize, nodeId);
    builder1_0.setModelProfile(modelProfile1_0);
    entityProfileResponse1_0 = builder1_0.build();
    ModelProfile convertedModelProfile = new ModelProfile(modelId, null, modelSize);
    convertedModelProfileOnNode = new ModelProfileOnNode(CommonName.EMPTY_FIELD, convertedModelProfile);
}
Also used : ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) EntityProfileResponse(org.opensearch.ad.transport.EntityProfileResponse) ModelProfile(org.opensearch.ad.model.ModelProfile)

Example 9 with ModelProfile

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

the class BwcTests method setUpProfileResponse.

@SuppressWarnings("serial")
private void setUpProfileResponse() {
    String node1 = "node1";
    String nodeName1 = "nodename1";
    DiscoveryNode discoveryNode1_1 = new DiscoveryNode(nodeName1, node1, new TransportAddress(TransportAddress.META_ADDRESS, 9300), emptyMap(), emptySet(), V_1_1_0);
    String node2 = "node2";
    String nodeName2 = "nodename2";
    DiscoveryNode discoveryNode2 = new DiscoveryNode(nodeName2, node2, new TransportAddress(TransportAddress.META_ADDRESS, 9301), emptyMap(), emptySet(), V_1_1_0);
    String model1Id = "model1";
    String model2Id = "model2";
    Map<String, Long> modelSizeMap1 = new HashMap<String, Long>() {

        {
            put(model1Id, modelSize);
            put(model2Id, modelSize2);
        }
    };
    Map<String, Long> modelSizeMap2 = new HashMap<String, Long>();
    int shingleSize = 8;
    ModelProfile modelProfile = new ModelProfile(model1Id, entity, modelSize);
    ModelProfile modelProfile2 = new ModelProfile(model2Id, entity2, modelSize2);
    ProfileNodeResponse profileNodeResponse1 = new ProfileNodeResponse(discoveryNode1_1, modelSizeMap1, shingleSize, 0, 0, Arrays.asList(modelProfile, modelProfile2), modelSizeMap1.size());
    ProfileNodeResponse profileNodeResponse2 = new ProfileNodeResponse(discoveryNode2, modelSizeMap2, -1, 0, 0, new ArrayList<>(), modelSizeMap2.size());
    List<ProfileNodeResponse> profileNodeResponses = Arrays.asList(profileNodeResponse1, profileNodeResponse2);
    List<FailedNodeException> failures = Collections.emptyList();
    ClusterName clusterName = new ClusterName("test-cluster-name");
    profileResponse1_1 = new ProfileResponse(clusterName, profileNodeResponses, failures);
    ProfileNodeResponse1_0 profileNodeResponse1_1_0 = new ProfileNodeResponse1_0(discoveryNode1_1, modelSizeMap1, shingleSize, 0, 0);
    ProfileNodeResponse1_0 profileNodeResponse2_1_0 = new ProfileNodeResponse1_0(discoveryNode2, modelSizeMap2, -1, 0, 0);
    List<ProfileNodeResponse1_0> profileNodeResponses1_0 = Arrays.asList(profileNodeResponse1_1_0, profileNodeResponse2_1_0);
    profileResponse1_0 = new ProfileResponse1_0(clusterName, profileNodeResponses1_0, failures);
    convertedModelProfileOnNodeArray = new ModelProfileOnNode[2];
    ModelProfile convertedModelProfile1 = new ModelProfile(model1Id, null, modelSize);
    convertedModelProfileOnNodeArray[0] = new ModelProfileOnNode(CommonName.EMPTY_FIELD, convertedModelProfile1);
    ModelProfile convertedModelProfile2 = new ModelProfile(model2Id, null, modelSize2);
    convertedModelProfileOnNodeArray[1] = new ModelProfileOnNode(CommonName.EMPTY_FIELD, convertedModelProfile2);
    convertedModelProfile = new ModelProfile1_0[2];
    convertedModelProfile[0] = new ModelProfile1_0(model1Id, modelSize, CommonName.EMPTY_FIELD);
    convertedModelProfile[1] = new ModelProfile1_0(model2Id, modelSize2, CommonName.EMPTY_FIELD);
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) TransportAddress(org.opensearch.common.transport.TransportAddress) ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) ProfileNodeResponse(org.opensearch.ad.transport.ProfileNodeResponse) EntityProfileResponse(org.opensearch.ad.transport.EntityProfileResponse) ProfileResponse(org.opensearch.ad.transport.ProfileResponse) ModelProfile(org.opensearch.ad.model.ModelProfile) ClusterName(org.opensearch.cluster.ClusterName) FailedNodeException(org.opensearch.action.FailedNodeException)

Example 10 with ModelProfile

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

the class EntityProfileTests method testResponseHashCodeEquals.

public void testResponseHashCodeEquals() {
    EntityProfileResponse.Builder builder = new EntityProfileResponse.Builder();
    builder.setLastActiveMs(lastActiveTimestamp).build();
    ModelProfileOnNode model = new ModelProfileOnNode(nodeId, new ModelProfile(modelId, entity, modelSize));
    builder.setModelProfile(model);
    EntityProfileResponse response = builder.build();
    HashSet<EntityProfileResponse> set = new HashSet<>();
    assertTrue(false == set.contains(response));
    set.add(response);
    assertTrue(set.contains(response));
}
Also used : ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) ModelProfile(org.opensearch.ad.model.ModelProfile) HashSet(java.util.HashSet)

Aggregations

ModelProfile (org.opensearch.ad.model.ModelProfile)13 ModelProfileOnNode (org.opensearch.ad.model.ModelProfileOnNode)8 Entity (org.opensearch.ad.model.Entity)5 EntityProfileResponse (org.opensearch.ad.transport.EntityProfileResponse)4 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 TotalHits (org.apache.lucene.search.TotalHits)2 ActionListener (org.opensearch.action.ActionListener)2 SearchRequest (org.opensearch.action.search.SearchRequest)2 SearchResponse (org.opensearch.action.search.SearchResponse)2 SearchResponseSections (org.opensearch.action.search.SearchResponseSections)2 EntityCache (org.opensearch.ad.caching.EntityCache)2 EntityModel (org.opensearch.ad.ml.EntityModel)2 EntityProfileName (org.opensearch.ad.model.EntityProfileName)2 EntityProfileAction (org.opensearch.ad.transport.EntityProfileAction)2 Text (org.opensearch.common.text.Text)2 SearchHit (org.opensearch.search.SearchHit)2 SearchHits (org.opensearch.search.SearchHits)2