ForecastingDetector extends AbstractOutlierDetector { @Override public DetectorResult detect(MetricData metricData) { notNull(metricData, "metricData can't be null"); val pointForecast = pointForecaster.forecast(metricData); if (pointForecast == null) { return new OutlierDetectorResult(AnomalyLevel.UNKNOWN); } else if (pointForecast.isWarmup()) { return new OutlierDetectorResult(true, AnomalyLevel.UNKNOWN); } val intervalForecast = intervalForecaster.forecast(metricData, pointForecast.getValue()); val thresholds = toAnomalyThresholds(intervalForecast); val observed = metricData.getValue(); val level = classifier.classify(thresholds, observed); val trusted = isTrusted(); return new OutlierDetectorResult(level) .setPredicted(pointForecast.getValue()) .setThresholds(thresholds) .setTrusted(trusted); } ForecastingDetector(
            UUID uuid,
            PointForecaster pointForecaster,
            IntervalForecaster intervalForecaster,
            AnomalyType anomalyType,
            boolean trusted,
            String name); @Override DetectorResult detect(MetricData metricData); @Override String getName();  }
@Test public void testDetect() { initDependencies(false); val metricDef = TestObjectMother.metricDefinition(); val metricData = new MetricData(metricDef, 100.0, Instant.now().getEpochSecond()); val result = (OutlierDetectorResult) detectorUnderTest.detect(metricData); assertFalse(result.isWarmup()); assertNotNull(result); }
@Test public void testDetect_nullForecast() { initDependencies(false); val metricDef = new MetricDefinition("some-key"); val metricData = new MetricData(metricDef, 100.0, Instant.now().getEpochSecond()); when(pointForecaster.forecast(metricData)).thenReturn(null); val result = (OutlierDetectorResult) detectorUnderTest.detect(metricData); assertNotNull(result); assertFalse(result.isWarmup()); assertEquals(AnomalyLevel.UNKNOWN, result.getAnomalyLevel()); }
@Test(expected = IllegalArgumentException.class) public void testDetect_nullMetricData() { detectorUnderTest.detect(null); }
@Test public void testDetect_warmingUpForecast() { initDependencies(true); val metricDef = TestObjectMother.metricDefinition(); val metricData = new MetricData(metricDef, 100.0, Instant.now().getEpochSecond()); val result = (OutlierDetectorResult) detectorUnderTest.detect(metricData); assertTrue(result.isWarmup()); assertNotNull(result); }