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());
}
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);
}
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;
}
Aggregations