use of org.opensearch.ad.model.DetectorProfileName in project anomaly-detection by opensearch-project.
the class ProfileTransportActionTests method testMultiEntityNodeOperation.
@Test
public void testMultiEntityNodeOperation() {
setUpModelSize(100);
DiscoveryNode nodeId = clusterService().localNode();
profilesToRetrieve = new HashSet<DetectorProfileName>();
profilesToRetrieve.add(DetectorProfileName.ACTIVE_ENTITIES);
ProfileRequest profileRequest = new ProfileRequest(detectorId, profilesToRetrieve, true, nodeId);
ProfileNodeResponse response = action.nodeOperation(new ProfileNodeRequest(profileRequest));
assertEquals(activeEntities, response.getActiveEntities());
assertEquals(null, response.getModelSize());
profilesToRetrieve.add(DetectorProfileName.INIT_PROGRESS);
profileRequest = new ProfileRequest(detectorId, profilesToRetrieve, true, nodeId);
response = action.nodeOperation(new ProfileNodeRequest(profileRequest));
assertEquals(activeEntities, response.getActiveEntities());
assertEquals(null, response.getModelSize());
assertEquals(totalUpdates, response.getTotalUpdates());
profilesToRetrieve.add(DetectorProfileName.MODELS);
profileRequest = new ProfileRequest(detectorId, profilesToRetrieve, true, nodeId);
response = action.nodeOperation(new ProfileNodeRequest(profileRequest));
assertEquals(activeEntities, response.getActiveEntities());
assertEquals(null, response.getModelSize());
assertEquals(2, response.getModelProfiles().size());
assertEquals(totalUpdates, response.getTotalUpdates());
assertEquals(2, response.getModelCount());
}
use of org.opensearch.ad.model.DetectorProfileName in project anomaly-detection by opensearch-project.
the class GetAnomalyDetectorTransportAction method getExecute.
protected void getExecute(GetAnomalyDetectorRequest request, ActionListener<GetAnomalyDetectorResponse> listener) {
String detectorID = request.getDetectorID();
String typesStr = request.getTypeStr();
String rawPath = request.getRawPath();
Entity entity = request.getEntity();
boolean all = request.isAll();
boolean returnJob = request.isReturnJob();
boolean returnTask = request.isReturnTask();
try {
if (!Strings.isEmpty(typesStr) || rawPath.endsWith(PROFILE) || rawPath.endsWith(PROFILE + "/")) {
if (entity != null) {
Set<EntityProfileName> entityProfilesToCollect = getEntityProfilesToCollect(typesStr, all);
EntityProfileRunner profileRunner = new EntityProfileRunner(client, xContentRegistry, AnomalyDetectorSettings.NUM_MIN_SAMPLES);
profileRunner.profile(detectorID, entity, entityProfilesToCollect, ActionListener.wrap(profile -> {
listener.onResponse(new GetAnomalyDetectorResponse(0, null, 0, 0, null, null, false, null, null, false, null, null, profile, true));
}, e -> listener.onFailure(e)));
} else {
Set<DetectorProfileName> profilesToCollect = getProfilesToCollect(typesStr, all);
AnomalyDetectorProfileRunner profileRunner = new AnomalyDetectorProfileRunner(client, xContentRegistry, nodeFilter, AnomalyDetectorSettings.NUM_MIN_SAMPLES, transportService, adTaskManager);
profileRunner.profile(detectorID, getProfileActionListener(listener), profilesToCollect);
}
} else {
if (returnTask) {
adTaskManager.getAndExecuteOnLatestADTasks(detectorID, null, null, ALL_DETECTOR_TASK_TYPES, (taskList) -> {
Optional<ADTask> realtimeAdTask = Optional.empty();
Optional<ADTask> historicalAdTask = Optional.empty();
if (taskList != null && taskList.size() > 0) {
Map<String, ADTask> adTasks = new HashMap<>();
List<ADTask> duplicateAdTasks = new ArrayList<>();
for (ADTask task : taskList) {
if (adTasks.containsKey(task.getTaskType())) {
LOG.info("Found duplicate latest task of detector {}, task id: {}, task type: {}", detectorID, task.getTaskType(), task.getTaskId());
duplicateAdTasks.add(task);
continue;
}
adTasks.put(task.getTaskType(), task);
}
if (duplicateAdTasks.size() > 0) {
adTaskManager.resetLatestFlagAsFalse(duplicateAdTasks);
}
if (adTasks.containsKey(ADTaskType.REALTIME_HC_DETECTOR.name())) {
realtimeAdTask = Optional.ofNullable(adTasks.get(ADTaskType.REALTIME_HC_DETECTOR.name()));
} else if (adTasks.containsKey(ADTaskType.REALTIME_SINGLE_ENTITY.name())) {
realtimeAdTask = Optional.ofNullable(adTasks.get(ADTaskType.REALTIME_SINGLE_ENTITY.name()));
}
if (adTasks.containsKey(ADTaskType.HISTORICAL_HC_DETECTOR.name())) {
historicalAdTask = Optional.ofNullable(adTasks.get(ADTaskType.HISTORICAL_HC_DETECTOR.name()));
} else if (adTasks.containsKey(ADTaskType.HISTORICAL_SINGLE_ENTITY.name())) {
historicalAdTask = Optional.ofNullable(adTasks.get(ADTaskType.HISTORICAL_SINGLE_ENTITY.name()));
} else if (adTasks.containsKey(ADTaskType.HISTORICAL.name())) {
historicalAdTask = Optional.ofNullable(adTasks.get(ADTaskType.HISTORICAL.name()));
}
}
getDetectorAndJob(detectorID, returnJob, returnTask, realtimeAdTask, historicalAdTask, listener);
}, transportService, true, 2, listener);
} else {
getDetectorAndJob(detectorID, returnJob, returnTask, Optional.empty(), Optional.empty(), listener);
}
}
} catch (Exception e) {
LOG.error(e);
listener.onFailure(e);
}
}
use of org.opensearch.ad.model.DetectorProfileName in project anomaly-detection by opensearch-project.
the class ProfileRequest method writeTo.
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeVInt(profilesToBeRetrieved.size());
for (DetectorProfileName profile : profilesToBeRetrieved) {
out.writeEnum(profile);
}
out.writeString(detectorId);
out.writeBoolean(forMultiEntityDetector);
}
use of org.opensearch.ad.model.DetectorProfileName in project anomaly-detection by opensearch-project.
the class AnomalyDetectorProfileRunnerTests method testErrorStateTemplate.
@SuppressWarnings("unchecked")
public void testErrorStateTemplate(RCFPollingStatus initStatus, ErrorResultStatus status, DetectorState state, String error, JobStatus jobStatus, Set<DetectorProfileName> profilesToCollect) throws IOException, InterruptedException {
ADTask adTask = TestHelpers.randomAdTask();
adTask.setError(getError(status));
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
Consumer<Optional<ADTask>> function = (Consumer<Optional<ADTask>>) args[2];
function.accept(Optional.of(adTask));
return null;
}).when(adTaskManager).getAndExecuteOnLatestDetectorLevelTask(any(), any(), any(), any(), anyBoolean(), any());
setUpClientExecuteRCFPollingAction(initStatus);
setUpClientGet(DetectorStatus.EXIST, jobStatus, initStatus, status);
DetectorProfile.Builder builder = new DetectorProfile.Builder();
if (profilesToCollect.contains(DetectorProfileName.STATE)) {
builder.state(state);
}
if (profilesToCollect.contains(DetectorProfileName.ERROR)) {
builder.error(error);
}
DetectorProfile expectedProfile = builder.build();
final CountDownLatch inProgressLatch = new CountDownLatch(1);
runner.profile(detector.getDetectorId(), ActionListener.wrap(response -> {
assertEquals(expectedProfile, response);
inProgressLatch.countDown();
}, exception -> {
logger.info(exception);
for (StackTraceElement ste : exception.getStackTrace()) {
logger.info(ste);
}
assertTrue("Should not reach here", false);
inProgressLatch.countDown();
}), profilesToCollect);
assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
use of org.opensearch.ad.model.DetectorProfileName in project anomaly-detection by opensearch-project.
the class ProfileTransportActionTests method testNodeOperation.
@Test
public void testNodeOperation() {
setUpModelSize(100);
DiscoveryNode nodeId = clusterService().localNode();
ProfileRequest profileRequest = new ProfileRequest(detectorId, profilesToRetrieve, false, nodeId);
ProfileNodeResponse response = action.nodeOperation(new ProfileNodeRequest(profileRequest));
assertEquals(shingleSize, response.getShingleSize());
assertEquals(null, response.getModelSize());
profilesToRetrieve = new HashSet<DetectorProfileName>();
profilesToRetrieve.add(DetectorProfileName.TOTAL_SIZE_IN_BYTES);
profileRequest = new ProfileRequest(detectorId, profilesToRetrieve, false, nodeId);
response = action.nodeOperation(new ProfileNodeRequest(profileRequest));
assertEquals(-1, response.getShingleSize());
assertEquals(1, response.getModelSize().size());
assertEquals(modelSize, response.getModelSize().get(modelId).longValue());
}
Aggregations