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