use of org.opensearch.ad.model.IntervalTimeConfiguration in project anomaly-detection by opensearch-project.
the class AnomalyDetectorJobRunner method indexAnomalyResult.
private void indexAnomalyResult(AnomalyDetectorJob jobParameter, LockService lockService, LockModel lock, Instant detectionStartTime, Instant executionStartTime, AnomalyResultResponse response) {
String detectorId = jobParameter.getName();
detectorEndRunExceptionCount.remove(detectorId);
try {
// We return 0 or Double.NaN rcf score if there is no error.
if ((response.getAnomalyScore() <= 0 || Double.isNaN(response.getAnomalyScore())) && response.getError() == null) {
updateRealtimeTask(response, detectorId);
return;
}
IntervalTimeConfiguration windowDelay = (IntervalTimeConfiguration) jobParameter.getWindowDelay();
Instant dataStartTime = detectionStartTime.minus(windowDelay.getInterval(), windowDelay.getUnit());
Instant dataEndTime = executionStartTime.minus(windowDelay.getInterval(), windowDelay.getUnit());
User user = jobParameter.getUser();
if (response.getError() != null) {
log.info("Anomaly result action run successfully for {} with error {}", detectorId, response.getError());
}
AnomalyResult anomalyResult = response.toAnomalyResult(detectorId, dataStartTime, dataEndTime, executionStartTime, Instant.now(), anomalyDetectionIndices.getSchemaVersion(ADIndex.RESULT), user, response.getError());
String resultIndex = jobParameter.getResultIndex();
anomalyResultHandler.index(anomalyResult, detectorId, resultIndex);
updateRealtimeTask(response, detectorId);
} catch (EndRunException e) {
handleAdException(jobParameter, lockService, lock, detectionStartTime, executionStartTime, e);
} catch (Exception e) {
log.error("Failed to index anomaly result for " + detectorId, e);
} finally {
releaseLock(jobParameter, lockService, lock);
}
}
use of org.opensearch.ad.model.IntervalTimeConfiguration in project anomaly-detection by opensearch-project.
the class AnomalyResultTransportAction method onGetDetector.
private ActionListener<Optional<AnomalyDetector>> onGetDetector(ActionListener<AnomalyResultResponse> listener, String adID, AnomalyResultRequest request) {
return ActionListener.wrap(detectorOptional -> {
if (!detectorOptional.isPresent()) {
listener.onFailure(new EndRunException(adID, "AnomalyDetector is not available.", true));
return;
}
AnomalyDetector anomalyDetector = detectorOptional.get();
if (anomalyDetector.isMultientityDetector()) {
hcDetectors.add(adID);
adStats.getStat(StatNames.AD_HC_EXECUTE_REQUEST_COUNT.getName()).increment();
}
long delayMillis = Optional.ofNullable((IntervalTimeConfiguration) anomalyDetector.getWindowDelay()).map(t -> t.toDuration().toMillis()).orElse(0L);
long dataStartTime = request.getStart() - delayMillis;
long dataEndTime = request.getEnd() - delayMillis;
adTaskManager.initRealtimeTaskCacheAndCleanupStaleCache(adID, anomalyDetector, transportService, ActionListener.runAfter(initRealtimeTaskCacheListener(adID), () -> executeAnomalyDetection(listener, adID, request, anomalyDetector, dataStartTime, dataEndTime)));
}, exception -> handleExecuteException(exception, listener, adID));
}
use of org.opensearch.ad.model.IntervalTimeConfiguration in project anomaly-detection by opensearch-project.
the class FeatureManagerTests method setup.
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
maxTrainSamples = 24;
maxSampleStride = 100;
trainSampleTimeRangeInHours = 1;
minTrainSamples = 4;
shingleSize = 3;
maxMissingPointsRate = 0.67;
maxNeighborDistance = 2;
previewSampleRate = 0.5;
maxPreviewSamples = 2;
featureBufferTtl = Duration.ofMillis(1_000L);
detectorId = "id";
when(detector.getDetectorId()).thenReturn(detectorId);
when(detector.getShingleSize()).thenReturn(shingleSize);
IntervalTimeConfiguration detectorIntervalTimeConfig = new IntervalTimeConfiguration(1, ChronoUnit.MINUTES);
intervalInMilliseconds = detectorIntervalTimeConfig.toDuration().toMillis();
when(detector.getDetectorIntervalInMilliseconds()).thenReturn(intervalInMilliseconds);
Interpolator interpolator = new LinearUniformInterpolator(new SingleFeatureLinearUniformInterpolator());
ExecutorService executorService = mock(ExecutorService.class);
when(threadPool.executor(AnomalyDetectorPlugin.AD_THREAD_POOL_NAME)).thenReturn(executorService);
doAnswer(invocation -> {
Runnable runnable = invocation.getArgument(0);
runnable.run();
return null;
}).when(executorService).execute(any(Runnable.class));
this.featureManager = spy(new FeatureManager(searchFeatureDao, interpolator, clock, maxTrainSamples, maxSampleStride, trainSampleTimeRangeInHours, minTrainSamples, maxMissingPointsRate, maxNeighborDistance, previewSampleRate, maxPreviewSamples, featureBufferTtl, threadPool, AnomalyDetectorPlugin.AD_THREAD_POOL_NAME));
}
use of org.opensearch.ad.model.IntervalTimeConfiguration in project anomaly-detection by opensearch-project.
the class FeatureManagerTests method getColdStartData_returnExpectedToListener.
@Test
@SuppressWarnings("unchecked")
@Parameters(method = "getTrainDataTestData")
public void getColdStartData_returnExpectedToListener(Long latestTime, List<Entry<Long, Long>> sampleRanges, List<Optional<double[]>> samples, double[][] expected) throws Exception {
long detectionInterval = (new IntervalTimeConfiguration(15, ChronoUnit.MINUTES)).toDuration().toMillis();
when(detector.getDetectorIntervalInMilliseconds()).thenReturn(detectionInterval);
when(detector.getShingleSize()).thenReturn(4);
doAnswer(invocation -> {
ActionListener<Optional<Long>> listener = invocation.getArgument(1);
listener.onResponse(Optional.ofNullable(latestTime));
return null;
}).when(searchFeatureDao).getLatestDataTime(eq(detector), any(ActionListener.class));
if (latestTime != null) {
doAnswer(invocation -> {
ActionListener<List<Optional<double[]>>> listener = invocation.getArgument(2);
listener.onResponse(samples);
return null;
}).when(searchFeatureDao).getFeatureSamplesForPeriods(eq(detector), eq(sampleRanges), any(ActionListener.class));
}
ActionListener<Optional<double[][]>> listener = mock(ActionListener.class);
featureManager = spy(new FeatureManager(searchFeatureDao, interpolator, clock, maxTrainSamples, maxSampleStride, trainSampleTimeRangeInHours, minTrainSamples, 0.5, /*maxMissingPointsRate*/
1, /*maxNeighborDistance*/
previewSampleRate, maxPreviewSamples, featureBufferTtl, threadPool, AnomalyDetectorPlugin.AD_THREAD_POOL_NAME));
featureManager.getColdStartData(detector, listener);
ArgumentCaptor<Optional<double[][]>> captor = ArgumentCaptor.forClass(Optional.class);
verify(listener).onResponse(captor.capture());
Optional<double[][]> result = captor.getValue();
assertTrue(Arrays.deepEquals(expected, result.orElse(null)));
}
use of org.opensearch.ad.model.IntervalTimeConfiguration in project anomaly-detection by opensearch-project.
the class NoPowermockSearchFeatureDaoTests method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
serviceField = "service";
hostField = "host";
detector = mock(AnomalyDetector.class);
when(detector.isMultientityDetector()).thenReturn(true);
when(detector.getCategoryField()).thenReturn(Arrays.asList(new String[] { serviceField, hostField }));
detectorId = "123";
when(detector.getDetectorId()).thenReturn(detectorId);
when(detector.getTimeField()).thenReturn("testTimeField");
when(detector.getIndices()).thenReturn(Arrays.asList("testIndices"));
IntervalTimeConfiguration detectionInterval = new IntervalTimeConfiguration(1, ChronoUnit.MINUTES);
when(detector.getDetectionInterval()).thenReturn(detectionInterval);
when(detector.getFilterQuery()).thenReturn(QueryBuilders.matchAllQuery());
client = mock(Client.class);
interpolator = new LinearUniformInterpolator(new SingleFeatureLinearUniformInterpolator());
clientUtil = mock(ClientUtil.class);
settings = Settings.EMPTY;
ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.MAX_ENTITIES_FOR_PREVIEW, AnomalyDetectorSettings.PAGE_SIZE))));
clusterService = mock(ClusterService.class);
when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
clock = mock(Clock.class);
searchFeatureDao = new SearchFeatureDao(client, // Important. Without this, ParseUtils cannot parse anything
xContentRegistry(), interpolator, clientUtil, settings, clusterService, AnomalyDetectorSettings.NUM_SAMPLES_PER_TREE, clock, 1, 1, 60_000L);
String app0 = "app_0";
String server1 = "server_1";
attrs1 = new HashMap<>();
attrs1.put(serviceField, app0);
attrs1.put(hostField, server1);
String server2 = "server_2";
attrs1 = new HashMap<>();
attrs1.put(serviceField, app0);
attrs1.put(hostField, server2);
}
Aggregations