Search in sources :

Example 11 with ModelProfile

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

the class EntityProfileTests method testResponseToXContent.

public void testResponseToXContent() throws IOException, JsonPathNotFoundException {
    long lastActiveTimestamp = 10L;
    EntityProfileResponse.Builder builder = new EntityProfileResponse.Builder();
    builder.setLastActiveMs(lastActiveTimestamp).build();
    builder.setModelProfile(new ModelProfileOnNode(nodeId, new ModelProfile(modelId, entity, modelSize)));
    EntityProfileResponse response = builder.build();
    String json = TestHelpers.xContentBuilderToString(response.toXContent(TestHelpers.builder(), ToXContent.EMPTY_PARAMS));
    assertEquals(lastActiveTimestamp, JsonDeserializer.getLongValue(json, EntityProfileResponse.LAST_ACTIVE_TS));
    assertEquals(modelSize, JsonDeserializer.getChildNode(json, CommonName.MODEL, CommonName.MODEL_SIZE_IN_BYTES).getAsLong());
}
Also used : ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) Mockito.anyString(org.mockito.Mockito.anyString) ModelProfile(org.opensearch.ad.model.ModelProfile)

Example 12 with ModelProfile

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

the class ProfileTransportActionTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    modelManager = mock(ModelManager.class);
    featureManager = mock(FeatureManager.class);
    when(featureManager.getShingleSize(any(String.class))).thenReturn(shingleSize);
    EntityCache cache = mock(EntityCache.class);
    cacheProvider = mock(CacheProvider.class);
    when(cacheProvider.get()).thenReturn(cache);
    when(cache.getActiveEntities(anyString())).thenReturn(activeEntities);
    when(cache.getTotalUpdates(anyString())).thenReturn(totalUpdates);
    Map<String, Long> multiEntityModelSizeMap = new HashMap<>();
    String modelId1 = "T4c3dXUBj-2IZN7itix__entity_app_3";
    String modelId2 = "T4c3dXUBj-2IZN7itix__entity_app_2";
    multiEntityModelSizeMap.put(modelId1, multiEntityModelSize);
    multiEntityModelSizeMap.put(modelId2, multiEntityModelSize);
    when(cache.getModelSize(anyString())).thenReturn(multiEntityModelSizeMap);
    List<ModelProfile> modelProfiles = new ArrayList<>();
    String field = "field";
    String fieldVal1 = "value1";
    String fieldVal2 = "value2";
    Entity entity1 = Entity.createSingleAttributeEntity(field, fieldVal1);
    Entity entity2 = Entity.createSingleAttributeEntity(field, fieldVal2);
    modelProfiles.add(new ModelProfile(modelId1, entity1, multiEntityModelSize));
    modelProfiles.add(new ModelProfile(modelId1, entity2, multiEntityModelSize));
    when(cache.getAllModelProfile(anyString())).thenReturn(modelProfiles);
    Map<String, Long> modelSizes = new HashMap<>();
    modelSizes.put(modelId, modelSize);
    when(modelManager.getModelSize(any(String.class))).thenReturn(modelSizes);
    Settings settings = Settings.builder().put("plugins.anomaly_detection.max_model_size_per_node", 100).build();
    action = new ProfileTransportAction(client().threadPool(), clusterService(), mock(TransportService.class), mock(ActionFilters.class), modelManager, featureManager, cacheProvider, settings);
    profilesToRetrieve = new HashSet<DetectorProfileName>();
    profilesToRetrieve.add(DetectorProfileName.COORDINATING_NODE);
}
Also used : Entity(org.opensearch.ad.model.Entity) EntityCache(org.opensearch.ad.caching.EntityCache) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ModelManager(org.opensearch.ad.ml.ModelManager) CacheProvider(org.opensearch.ad.caching.CacheProvider) ModelProfile(org.opensearch.ad.model.ModelProfile) DetectorProfileName(org.opensearch.ad.model.DetectorProfileName) FeatureManager(org.opensearch.ad.feature.FeatureManager) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) Settings(org.opensearch.common.settings.Settings) Before(org.junit.Before)

Example 13 with ModelProfile

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

the class PriorityCache method getAllModelProfile.

@Override
public List<ModelProfile> getAllModelProfile(String detectorId) {
    CacheBuffer cacheBuffer = activeEnities.get(detectorId);
    List<ModelProfile> res = new ArrayList<>();
    if (cacheBuffer != null) {
        long size = cacheBuffer.getMemoryConsumptionPerEntity();
        cacheBuffer.getAllModels().forEach(entry -> {
            EntityModel model = entry.getModel();
            Entity entity = null;
            if (model != null && model.getEntity().isPresent()) {
                entity = model.getEntity().get();
            }
            res.add(new ModelProfile(entry.getModelId(), entity, size));
        });
    }
    return res;
}
Also used : Entity(org.opensearch.ad.model.Entity) ArrayList(java.util.ArrayList) EntityModel(org.opensearch.ad.ml.EntityModel) ModelProfile(org.opensearch.ad.model.ModelProfile)

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