Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
return no data error message to preview (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
wnbts authored Jan 15, 2020
1 parent 1637128 commit 25cd3ae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ public Features getPreviewFeatures(AnomalyDetector detector, long startMilli, lo
* @param endMilli end of the range in epoch milliseconds
* @param listener onResponse is called with time ranges, unprocessed features,
* and processed features of the data points from the period
* onFailure is called with IllegalArgumentException when there is no data to preview
*/
public void getPreviewFeatures(AnomalyDetector detector, long startMilli, long endMilli, ActionListener<Features> listener) {
Entry<List<Entry<Long, Long>>, Integer> sampleRangeResults = getSampleRanges(detector, startMilli, endMilli);
Expand All @@ -291,6 +292,10 @@ public void getPreviewFeatures(AnomalyDetector detector, long startMilli, long e

getSamplesForRanges(detector, sampleRanges, ActionListener.wrap(samples -> {
List<Entry<Long, Long>> searchTimeRange = samples.getKey();
if (searchTimeRange.size() == 0) {
listener.onFailure(new IllegalArgumentException("No data to preview anomaly detection."));
return;
}
double[][] sampleFeatures = samples.getValue();

List<Entry<Long, Long>> previewRanges = getPreviewRanges(searchTimeRange, stride);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,13 @@ public void getPreviewFeatures_returnExpected() {
}

@SuppressWarnings("unchecked")
private void getPreviewFeaturesTemplate(boolean fail) {
private void getPreviewFeaturesTemplate(List<Optional<double[]>> samplesResults, boolean querySuccess, boolean previewSuccess) {
long start = 0L;
long end = 240_000L;
IntervalTimeConfiguration detectionInterval = new IntervalTimeConfiguration(1, ChronoUnit.MINUTES);
when(detector.getDetectionInterval()).thenReturn(detectionInterval);

List<Entry<Long, Long>> sampleRanges = Arrays.asList(new SimpleEntry<>(0L, 60_000L), new SimpleEntry<>(120_000L, 180_000L));
List<Optional<double[]>> samplesResults = Arrays.asList(Optional.of(new double[] { 1 }), Optional.of(new double[] { 3 }));
RuntimeException exception = new RuntimeException();
doAnswer(invocation -> {
Object[] args = invocation.getArguments();

Expand All @@ -416,10 +414,10 @@ private void getPreviewFeaturesTemplate(boolean fail) {
listener = (ActionListener<List<Optional<double[]>>>) args[2];
}

if (fail) {
listener.onFailure(exception);
} else {
if (querySuccess) {
listener.onResponse(samplesResults);
} else {
listener.onFailure(new RuntimeException());
}

return null;
Expand All @@ -435,26 +433,31 @@ private void getPreviewFeaturesTemplate(boolean fail) {
ActionListener<Features> listener = mock(ActionListener.class);
featureManager.getPreviewFeatures(detector, start, end, listener);

if (fail) {
verify(listener).onFailure(exception);
} else {
if (previewSuccess) {
Features expected = new Features(
asList(new SimpleEntry<>(120_000L, 180_000L)),
new double[][] { { 3 } },
new double[][] { { 1, 2, 3 } }
);
verify(listener).onResponse(expected);
} else {
verify(listener).onFailure(any(Exception.class));
}

}

@Test
public void getPreviewFeatures_returnExpectedToListener() {
getPreviewFeaturesTemplate(false);
getPreviewFeaturesTemplate(asList(Optional.of(new double[] { 1 }), Optional.of(new double[] { 3 })), true, true);
}

@Test
public void getPreviewFeatures_returnExceptionToListener_whenNoDataToPreview() {
getPreviewFeaturesTemplate(asList(), true, false);
}

@Test
public void getPreviewFeatures_returnExceptionToListener() {
getPreviewFeaturesTemplate(true);
public void getPreviewFeatures_returnExceptionToListener_whenQueryFail() {
getPreviewFeaturesTemplate(asList(Optional.of(new double[] { 1 }), Optional.of(new double[] { 3 })), false, false);
}
}

0 comments on commit 25cd3ae

Please sign in to comment.