http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/TrendADSystem.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/TrendADSystem.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/TrendADSystem.java deleted file mode 100644 index df36a4a..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/TrendADSystem.java +++ /dev/null @@ -1,317 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.prototype; - -import org.apache.ambari.metrics.alertservice.prototype.common.DataSeries; -import org.apache.ambari.metrics.alertservice.prototype.methods.MetricAnomaly; -import org.apache.ambari.metrics.alertservice.prototype.methods.hsdev.HsdevTechnique; -import org.apache.ambari.metrics.alertservice.prototype.methods.kstest.KSTechnique; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.hadoop.metrics2.sink.timeline.TimelineMetric; -import org.apache.hadoop.metrics2.sink.timeline.TimelineMetrics; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; - -public class TrendADSystem implements Serializable { - - private MetricsCollectorInterface metricsCollectorInterface; - private List<TrendMetric> trendMetrics; - - private long ksTestIntervalMillis = 10 * 60 * 1000; - private long ksTrainIntervalMillis = 10 * 60 * 1000; - private KSTechnique ksTechnique; - - private HsdevTechnique hsdevTechnique; - private int hsdevNumHistoricalPeriods = 3; - - private Map<KsSingleRunKey, MetricAnomaly> trackedKsAnomalies = new HashMap<>(); - private static final Log LOG = LogFactory.getLog(TrendADSystem.class); - private String inputFile = ""; - - public TrendADSystem(MetricsCollectorInterface metricsCollectorInterface, - long ksTestIntervalMillis, - long ksTrainIntervalMillis, - int hsdevNumHistoricalPeriods) { - - this.metricsCollectorInterface = metricsCollectorInterface; - this.ksTestIntervalMillis = ksTestIntervalMillis; - this.ksTrainIntervalMillis = ksTrainIntervalMillis; - this.hsdevNumHistoricalPeriods = hsdevNumHistoricalPeriods; - - this.ksTechnique = new KSTechnique(); - this.hsdevTechnique = new HsdevTechnique(); - - trendMetrics = new ArrayList<>(); - } - - public void runKSTest(long currentEndTime, Set<TrendMetric> trendMetrics) { - readInputFile(inputFile); - - long ksTestIntervalStartTime = currentEndTime - ksTestIntervalMillis; - LOG.info("Running KS Test for test data interval [" + new Date(ksTestIntervalStartTime) + " : " + - new Date(currentEndTime) + "], with train data period [" + new Date(ksTestIntervalStartTime - ksTrainIntervalMillis) - + " : " + new Date(ksTestIntervalStartTime) + "]"); - - for (TrendMetric metric : trendMetrics) { - String metricName = metric.metricName; - String appId = metric.appId; - String hostname = metric.hostname; - String key = metricName + ":" + appId + ":" + hostname; - - TimelineMetrics ksData = metricsCollectorInterface.fetchMetrics(metricName, appId, hostname, ksTestIntervalStartTime - ksTrainIntervalMillis, - currentEndTime); - - if (ksData.getMetrics().isEmpty()) { - LOG.info("No metrics fetched for KS, metricKey = " + key); - continue; - } - - List<Double> trainTsList = new ArrayList<>(); - List<Double> trainDataList = new ArrayList<>(); - List<Double> testTsList = new ArrayList<>(); - List<Double> testDataList = new ArrayList<>(); - - for (TimelineMetric timelineMetric : ksData.getMetrics()) { - for (Long timestamp : timelineMetric.getMetricValues().keySet()) { - if (timestamp <= ksTestIntervalStartTime) { - trainDataList.add(timelineMetric.getMetricValues().get(timestamp)); - trainTsList.add((double) timestamp); - } else { - testDataList.add(timelineMetric.getMetricValues().get(timestamp)); - testTsList.add((double) timestamp); - } - } - } - - LOG.info("Train Data size : " + trainDataList.size() + ", Test Data Size : " + testDataList.size()); - if (trainDataList.isEmpty() || testDataList.isEmpty() || trainDataList.size() < testDataList.size()) { - LOG.info("Not enough train/test data to perform KS analysis."); - continue; - } - - String ksTrainSeries = "KSTrainSeries"; - double[] trainTs = new double[trainTsList.size()]; - double[] trainData = new double[trainTsList.size()]; - for (int i = 0; i < trainTs.length; i++) { - trainTs[i] = trainTsList.get(i); - trainData[i] = trainDataList.get(i); - } - - String ksTestSeries = "KSTestSeries"; - double[] testTs = new double[testTsList.size()]; - double[] testData = new double[testTsList.size()]; - for (int i = 0; i < testTs.length; i++) { - testTs[i] = testTsList.get(i); - testData[i] = testDataList.get(i); - } - - LOG.info("Train Size = " + trainTs.length + ", Test Size = " + testTs.length); - - DataSeries ksTrainData = new DataSeries(ksTrainSeries, trainTs, trainData); - DataSeries ksTestData = new DataSeries(ksTestSeries, testTs, testData); - - MetricAnomaly metricAnomaly = ksTechnique.runKsTest(key, ksTrainData, ksTestData); - if (metricAnomaly == null) { - LOG.info("No anomaly from KS test."); - } else { - LOG.info("Found Anomaly in KS Test. Publishing KS Anomaly metric...."); - TimelineMetric timelineMetric = getAsTimelineMetric(metricAnomaly, - ksTestIntervalStartTime, currentEndTime, ksTestIntervalStartTime - ksTrainIntervalMillis, ksTestIntervalStartTime); - TimelineMetrics timelineMetrics = new TimelineMetrics(); - timelineMetrics.addOrMergeTimelineMetric(timelineMetric); - metricsCollectorInterface.emitMetrics(timelineMetrics); - - trackedKsAnomalies.put(new KsSingleRunKey(ksTestIntervalStartTime, currentEndTime, metricName, appId, hostname), metricAnomaly); - } - } - - if (trendMetrics.isEmpty()) { - LOG.info("No Trend metrics tracked!!!!"); - } - - } - - private TimelineMetric getAsTimelineMetric(MetricAnomaly metricAnomaly, - long testStart, - long testEnd, - long trainStart, - long trainEnd) { - - TimelineMetric timelineMetric = new TimelineMetric(); - timelineMetric.setMetricName(metricAnomaly.getMetricKey()); - timelineMetric.setAppId(MetricsCollectorInterface.serviceName + "-" + metricAnomaly.getMethodType()); - timelineMetric.setInstanceId(null); - timelineMetric.setHostName(MetricsCollectorInterface.getDefaultLocalHostName()); - timelineMetric.setStartTime(testEnd); - HashMap<String, String> metadata = new HashMap<>(); - metadata.put("method", metricAnomaly.getMethodType()); - metadata.put("anomaly-score", String.valueOf(metricAnomaly.getAnomalyScore())); - metadata.put("test-start-time", String.valueOf(testStart)); - metadata.put("train-start-time", String.valueOf(trainStart)); - metadata.put("train-end-time", String.valueOf(trainEnd)); - timelineMetric.setMetadata(metadata); - TreeMap<Long,Double> metricValues = new TreeMap<>(); - metricValues.put(testEnd, metricAnomaly.getMetricValue()); - timelineMetric.setMetricValues(metricValues); - return timelineMetric; - - } - - public void runHsdevMethod() { - - List<TimelineMetric> hsdevMetricAnomalies = new ArrayList<>(); - - for (KsSingleRunKey ksSingleRunKey : trackedKsAnomalies.keySet()) { - - long hsdevTestEnd = ksSingleRunKey.endTime; - long hsdevTestStart = ksSingleRunKey.startTime; - - long period = hsdevTestEnd - hsdevTestStart; - - long hsdevTrainStart = hsdevTestStart - (hsdevNumHistoricalPeriods) * period; - long hsdevTrainEnd = hsdevTestStart; - - LOG.info("Running HSdev Test for test data interval [" + new Date(hsdevTestStart) + " : " + - new Date(hsdevTestEnd) + "], with train data period [" + new Date(hsdevTrainStart) - + " : " + new Date(hsdevTrainEnd) + "]"); - - String metricName = ksSingleRunKey.metricName; - String appId = ksSingleRunKey.appId; - String hostname = ksSingleRunKey.hostname; - String key = metricName + "_" + appId + "_" + hostname; - - TimelineMetrics hsdevData = metricsCollectorInterface.fetchMetrics( - metricName, - appId, - hostname, - hsdevTrainStart, - hsdevTestEnd); - - if (hsdevData.getMetrics().isEmpty()) { - LOG.info("No metrics fetched for HSDev, metricKey = " + key); - continue; - } - - List<Double> trainTsList = new ArrayList<>(); - List<Double> trainDataList = new ArrayList<>(); - List<Double> testTsList = new ArrayList<>(); - List<Double> testDataList = new ArrayList<>(); - - for (TimelineMetric timelineMetric : hsdevData.getMetrics()) { - for (Long timestamp : timelineMetric.getMetricValues().keySet()) { - if (timestamp <= hsdevTestStart) { - trainDataList.add(timelineMetric.getMetricValues().get(timestamp)); - trainTsList.add((double) timestamp); - } else { - testDataList.add(timelineMetric.getMetricValues().get(timestamp)); - testTsList.add((double) timestamp); - } - } - } - - if (trainDataList.isEmpty() || testDataList.isEmpty() || trainDataList.size() < testDataList.size()) { - LOG.info("Not enough train/test data to perform Hsdev analysis."); - continue; - } - - String hsdevTrainSeries = "HsdevTrainSeries"; - double[] trainTs = new double[trainTsList.size()]; - double[] trainData = new double[trainTsList.size()]; - for (int i = 0; i < trainTs.length; i++) { - trainTs[i] = trainTsList.get(i); - trainData[i] = trainDataList.get(i); - } - - String hsdevTestSeries = "HsdevTestSeries"; - double[] testTs = new double[testTsList.size()]; - double[] testData = new double[testTsList.size()]; - for (int i = 0; i < testTs.length; i++) { - testTs[i] = testTsList.get(i); - testData[i] = testDataList.get(i); - } - - LOG.info("Train Size = " + trainTs.length + ", Test Size = " + testTs.length); - - DataSeries hsdevTrainData = new DataSeries(hsdevTrainSeries, trainTs, trainData); - DataSeries hsdevTestData = new DataSeries(hsdevTestSeries, testTs, testData); - - MetricAnomaly metricAnomaly = hsdevTechnique.runHsdevTest(key, hsdevTrainData, hsdevTestData); - if (metricAnomaly == null) { - LOG.info("No anomaly from Hsdev test. Mismatch between KS and HSDev. "); - ksTechnique.updateModel(key, false, 10); - } else { - LOG.info("Found Anomaly in Hsdev Test. This confirms KS anomaly."); - hsdevMetricAnomalies.add(getAsTimelineMetric(metricAnomaly, - hsdevTestStart, hsdevTestEnd, hsdevTrainStart, hsdevTrainEnd)); - } - } - clearTrackedKsRunKeys(); - - if (!hsdevMetricAnomalies.isEmpty()) { - LOG.info("Publishing Hsdev Anomalies...."); - TimelineMetrics timelineMetrics = new TimelineMetrics(); - timelineMetrics.setMetrics(hsdevMetricAnomalies); - metricsCollectorInterface.emitMetrics(timelineMetrics); - } - } - - private void clearTrackedKsRunKeys() { - trackedKsAnomalies.clear(); - } - - private void readInputFile(String fileName) { - trendMetrics.clear(); - try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { - for (String line; (line = br.readLine()) != null; ) { - String[] splits = line.split(","); - LOG.info("Adding a new metric to track in Trend AD system : " + splits[0]); - trendMetrics.add(new TrendMetric(splits[0], splits[1], splits[2])); - } - } catch (IOException e) { - LOG.error("Error reading input file : " + e); - } - } - - class KsSingleRunKey implements Serializable{ - - long startTime; - long endTime; - String metricName; - String appId; - String hostname; - - public KsSingleRunKey(long startTime, long endTime, String metricName, String appId, String hostname) { - this.startTime = startTime; - this.endTime = endTime; - this.metricName = metricName; - this.appId = appId; - this.hostname = hostname; - } - } -}
http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/TrendMetric.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/TrendMetric.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/TrendMetric.java deleted file mode 100644 index 3bead8b..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/TrendMetric.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.prototype; - -import java.io.Serializable; - -public class TrendMetric implements Serializable { - - String metricName; - String appId; - String hostname; - - public TrendMetric(String metricName, String appId, String hostname) { - this.metricName = metricName; - this.appId = appId; - this.hostname = hostname; - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/common/DataSeries.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/common/DataSeries.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/common/DataSeries.java deleted file mode 100644 index eb19857..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/common/DataSeries.java +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.prototype.common; - -import java.util.Arrays; - -public class DataSeries { - - public String seriesName; - public double[] ts; - public double[] values; - - public DataSeries(String seriesName, double[] ts, double[] values) { - this.seriesName = seriesName; - this.ts = ts; - this.values = values; - } - - @Override - public String toString() { - return seriesName + Arrays.toString(ts) + Arrays.toString(values); - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/common/ResultSet.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/common/ResultSet.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/common/ResultSet.java deleted file mode 100644 index 101b0e9..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/common/ResultSet.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.prototype.common; - - -import java.util.ArrayList; -import java.util.List; - -public class ResultSet { - - public List<double[]> resultset = new ArrayList<>(); - - public ResultSet(List<double[]> resultset) { - this.resultset = resultset; - } - - public void print() { - System.out.println("Result : "); - if (!resultset.isEmpty()) { - for (int i = 0; i<resultset.get(0).length;i++) { - for (double[] entity : resultset) { - System.out.print(entity[i] + " "); - } - System.out.println(); - } - } - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/common/StatisticUtils.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/common/StatisticUtils.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/common/StatisticUtils.java deleted file mode 100644 index 4ea4ac5..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/common/StatisticUtils.java +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.prototype.common; - - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; - -public class StatisticUtils { - - public static double mean(double[] values) { - double sum = 0; - for (double d : values) { - sum += d; - } - return sum / values.length; - } - - public static double variance(double[] values) { - double avg = mean(values); - double variance = 0; - for (double d : values) { - variance += Math.pow(d - avg, 2.0); - } - return variance; - } - - public static double sdev(double[] values, boolean useBesselsCorrection) { - double variance = variance(values); - int n = (useBesselsCorrection) ? values.length - 1 : values.length; - return Math.sqrt(variance / n); - } - - public static double median(double[] values) { - double[] clonedValues = Arrays.copyOf(values, values.length); - Arrays.sort(clonedValues); - int n = values.length; - - if (n % 2 != 0) { - return clonedValues[(n-1)/2]; - } else { - return ( clonedValues[(n-1)/2] + clonedValues[n/2] ) / 2; - } - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/AnomalyDetectionTechnique.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/AnomalyDetectionTechnique.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/AnomalyDetectionTechnique.java deleted file mode 100644 index 0b10b4b..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/AnomalyDetectionTechnique.java +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.prototype.methods; - -import org.apache.hadoop.metrics2.sink.timeline.TimelineMetric; - -import java.sql.Time; -import java.util.List; -import java.util.Map; - -public abstract class AnomalyDetectionTechnique { - - protected String methodType; - - public abstract List<MetricAnomaly> test(TimelineMetric metric); - -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/MetricAnomaly.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/MetricAnomaly.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/MetricAnomaly.java deleted file mode 100644 index da4f030..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/MetricAnomaly.java +++ /dev/null @@ -1,86 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.prototype.methods; - -import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; - -public class MetricAnomaly implements Serializable{ - - private String methodType; - private double anomalyScore; - private String metricKey; - private long timestamp; - private double metricValue; - - - public MetricAnomaly(String metricKey, long timestamp, double metricValue, String methodType, double anomalyScore) { - this.metricKey = metricKey; - this.timestamp = timestamp; - this.metricValue = metricValue; - this.methodType = methodType; - this.anomalyScore = anomalyScore; - - } - - public String getMethodType() { - return methodType; - } - - public void setMethodType(String methodType) { - this.methodType = methodType; - } - - public double getAnomalyScore() { - return anomalyScore; - } - - public void setAnomalyScore(double anomalyScore) { - this.anomalyScore = anomalyScore; - } - - public void setMetricKey(String metricKey) { - this.metricKey = metricKey; - } - - public String getMetricKey() { - return metricKey; - } - - public void setMetricName(String metricName) { - this.metricKey = metricName; - } - - public long getTimestamp() { - return timestamp; - } - - public void setTimestamp(long timestamp) { - this.timestamp = timestamp; - } - - public double getMetricValue() { - return metricValue; - } - - public void setMetricValue(double metricValue) { - this.metricValue = metricValue; - } - -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/ema/EmaModel.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/ema/EmaModel.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/ema/EmaModel.java deleted file mode 100644 index a31410d..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/ema/EmaModel.java +++ /dev/null @@ -1,131 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.prototype.methods.ema; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import javax.xml.bind.annotation.XmlRootElement; -import java.io.Serializable; - -import static org.apache.ambari.metrics.alertservice.prototype.methods.ema.EmaTechnique.suppressAnomaliesTheshold; - -@XmlRootElement -public class EmaModel implements Serializable { - - private String metricName; - private String hostname; - private String appId; - private double ema; - private double ems; - private double weight; - private double timessdev; - - private int ctr = 0; - - private static final Log LOG = LogFactory.getLog(EmaModel.class); - - public EmaModel(String name, String hostname, String appId, double weight, double timessdev) { - this.metricName = name; - this.hostname = hostname; - this.appId = appId; - this.weight = weight; - this.timessdev = timessdev; - this.ema = 0.0; - this.ems = 0.0; - } - - public String getMetricName() { - return metricName; - } - - public String getHostname() { - return hostname; - } - - public String getAppId() { - return appId; - } - - public double testAndUpdate(double metricValue) { - - double anomalyScore = 0.0; - LOG.info("Before Update ->" + metricName + ":" + appId + ":" + hostname + " - " + "ema = " + ema + ", ems = " + ems + ", timessdev = " + timessdev); - update(metricValue); - if (ctr > suppressAnomaliesTheshold) { - anomalyScore = test(metricValue); - if (anomalyScore > 0.0) { - LOG.info("Anomaly ->" + metricName + ":" + appId + ":" + hostname + " - " + "ema = " + ema + ", ems = " + ems + - ", timessdev = " + timessdev + ", metricValue = " + metricValue); - } else { - LOG.info("Not an Anomaly ->" + metricName + ":" + appId + ":" + hostname + " - " + "ema = " + ema + ", ems = " + ems + - ", timessdev = " + timessdev + ", metricValue = " + metricValue); - } - } else { - ctr++; - if (ctr > suppressAnomaliesTheshold) { - LOG.info("Ema Model for " + metricName + ":" + appId + ":" + hostname + " is ready for testing data."); - } - } - return anomalyScore; - } - - public void update(double metricValue) { - ema = weight * ema + (1 - weight) * metricValue; - ems = Math.sqrt(weight * Math.pow(ems, 2.0) + (1 - weight) * Math.pow(metricValue - ema, 2.0)); - LOG.debug("In update : ema = " + ema + ", ems = " + ems); - } - - public double test(double metricValue) { - LOG.debug("In test : ema = " + ema + ", ems = " + ems); - double diff = Math.abs(ema - metricValue) - (timessdev * ems); - LOG.debug("diff = " + diff); - if (diff > 0) { - return Math.abs((metricValue - ema) / ems); //Z score - } else { - return 0.0; - } - } - - public void updateModel(boolean increaseSensitivity, double percent) { - LOG.info("Updating model for " + metricName + " with increaseSensitivity = " + increaseSensitivity + ", percent = " + percent); - double delta = percent / 100; - if (increaseSensitivity) { - delta = delta * -1; - } - this.timessdev = timessdev + delta * timessdev; - //this.weight = Math.min(1.0, weight + delta * weight); - LOG.info("New model parameters " + metricName + " : timessdev = " + timessdev + ", weight = " + weight); - } - - public double getWeight() { - return weight; - } - - public void setWeight(double weight) { - this.weight = weight; - } - - public double getTimessdev() { - return timessdev; - } - - public void setTimessdev(double timessdev) { - this.timessdev = timessdev; - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/ema/EmaModelLoader.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/ema/EmaModelLoader.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/ema/EmaModelLoader.java deleted file mode 100644 index 62749c1..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/ema/EmaModelLoader.java +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.prototype.methods.ema; - -import com.google.gson.Gson; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.spark.SparkContext; -import org.apache.spark.mllib.util.Loader; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; - -public class EmaModelLoader implements Loader<EmaTechnique> { - private static final Log LOG = LogFactory.getLog(EmaModelLoader.class); - - @Override - public EmaTechnique load(SparkContext sc, String path) { - return new EmaTechnique(0.5,3); -// Gson gson = new Gson(); -// try { -// String fileString = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8); -// return gson.fromJson(fileString, EmaTechnique.class); -// } catch (IOException e) { -// LOG.error(e); -// } -// return null; - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/ema/EmaTechnique.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/ema/EmaTechnique.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/ema/EmaTechnique.java deleted file mode 100644 index 52c6cf3..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/ema/EmaTechnique.java +++ /dev/null @@ -1,151 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.prototype.methods.ema; - -import com.google.gson.Gson; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.ambari.metrics.alertservice.prototype.methods.MetricAnomaly; -import org.apache.ambari.metrics.alertservice.prototype.methods.AnomalyDetectionTechnique; -import org.apache.hadoop.metrics2.sink.timeline.TimelineMetric; -import org.apache.spark.SparkContext; -import org.apache.spark.mllib.util.Saveable; - -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import java.io.BufferedWriter; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.Serializable; -import java.io.Writer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -@XmlRootElement -public class EmaTechnique extends AnomalyDetectionTechnique implements Serializable, Saveable { - - @XmlElement(name = "trackedEmas") - private Map<String, EmaModel> trackedEmas; - private static final Log LOG = LogFactory.getLog(EmaTechnique.class); - - private double startingWeight = 0.5; - private double startTimesSdev = 3.0; - private String methodType = "ema"; - public static int suppressAnomaliesTheshold = 100; - - public EmaTechnique(double startingWeight, double startTimesSdev, int suppressAnomaliesTheshold) { - trackedEmas = new HashMap<>(); - this.startingWeight = startingWeight; - this.startTimesSdev = startTimesSdev; - EmaTechnique.suppressAnomaliesTheshold = suppressAnomaliesTheshold; - LOG.info("New EmaTechnique......"); - } - - public EmaTechnique(double startingWeight, double startTimesSdev) { - trackedEmas = new HashMap<>(); - this.startingWeight = startingWeight; - this.startTimesSdev = startTimesSdev; - LOG.info("New EmaTechnique......"); - } - - public List<MetricAnomaly> test(TimelineMetric metric) { - String metricName = metric.getMetricName(); - String appId = metric.getAppId(); - String hostname = metric.getHostName(); - String key = metricName + ":" + appId + ":" + hostname; - - EmaModel emaModel = trackedEmas.get(key); - if (emaModel == null) { - LOG.debug("EmaModel not present for " + key); - LOG.debug("Number of tracked Emas : " + trackedEmas.size()); - emaModel = new EmaModel(metricName, hostname, appId, startingWeight, startTimesSdev); - trackedEmas.put(key, emaModel); - } else { - LOG.debug("EmaModel already present for " + key); - } - - List<MetricAnomaly> anomalies = new ArrayList<>(); - - for (Long timestamp : metric.getMetricValues().keySet()) { - double metricValue = metric.getMetricValues().get(timestamp); - double anomalyScore = emaModel.testAndUpdate(metricValue); - if (anomalyScore > 0.0) { - LOG.info("Found anomaly for : " + key + ", anomalyScore = " + anomalyScore); - MetricAnomaly metricAnomaly = new MetricAnomaly(key, timestamp, metricValue, methodType, anomalyScore); - anomalies.add(metricAnomaly); - } else { - LOG.debug("Discarding non-anomaly for : " + key); - } - } - return anomalies; - } - - public boolean updateModel(TimelineMetric timelineMetric, boolean increaseSensitivity, double percent) { - String metricName = timelineMetric.getMetricName(); - String appId = timelineMetric.getAppId(); - String hostname = timelineMetric.getHostName(); - String key = metricName + "_" + appId + "_" + hostname; - - - EmaModel emaModel = trackedEmas.get(key); - - if (emaModel == null) { - LOG.warn("EMA Model for " + key + " not found"); - return false; - } - emaModel.updateModel(increaseSensitivity, percent); - - return true; - } - - @Override - public void save(SparkContext sc, String path) { - Gson gson = new Gson(); - try { - String json = gson.toJson(this); - try (Writer writer = new BufferedWriter(new OutputStreamWriter( - new FileOutputStream(path), "utf-8"))) { - writer.write(json); - } - } catch (IOException e) { - LOG.error(e); - } - } - - @Override - public String formatVersion() { - return "1.0"; - } - - public Map<String, EmaModel> getTrackedEmas() { - return trackedEmas; - } - - public double getStartingWeight() { - return startingWeight; - } - - public double getStartTimesSdev() { - return startTimesSdev; - } - -} - http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/hsdev/HsdevTechnique.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/hsdev/HsdevTechnique.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/hsdev/HsdevTechnique.java deleted file mode 100644 index 04f4a73..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/hsdev/HsdevTechnique.java +++ /dev/null @@ -1,81 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.prototype.methods.hsdev; - -import org.apache.ambari.metrics.alertservice.prototype.common.DataSeries; -import org.apache.ambari.metrics.alertservice.prototype.methods.MetricAnomaly; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import static org.apache.ambari.metrics.alertservice.prototype.common.StatisticUtils.median; -import static org.apache.ambari.metrics.alertservice.prototype.common.StatisticUtils.sdev; - -import java.io.Serializable; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -public class HsdevTechnique implements Serializable { - - private Map<String, Double> hsdevMap; - private String methodType = "hsdev"; - private static final Log LOG = LogFactory.getLog(HsdevTechnique.class); - - public HsdevTechnique() { - hsdevMap = new HashMap<>(); - } - - public MetricAnomaly runHsdevTest(String key, DataSeries trainData, DataSeries testData) { - int testLength = testData.values.length; - int trainLength = trainData.values.length; - - if (trainLength < testLength) { - LOG.info("Not enough train data."); - return null; - } - - if (!hsdevMap.containsKey(key)) { - hsdevMap.put(key, 3.0); - } - - double n = hsdevMap.get(key); - - double historicSd = sdev(trainData.values, false); - double historicMedian = median(trainData.values); - double currentMedian = median(testData.values); - - - if (historicSd > 0) { - double diff = Math.abs(currentMedian - historicMedian); - LOG.info("Found anomaly for metric : " + key + " in the period ending " + new Date((long)testData.ts[testLength - 1])); - LOG.info("Current median = " + currentMedian + ", Historic Median = " + historicMedian + ", HistoricSd = " + historicSd); - - if (diff > n * historicSd) { - double zScore = diff / historicSd; - LOG.info("Z Score of current series : " + zScore); - return new MetricAnomaly(key, - (long) testData.ts[testLength - 1], - testData.values[testLength - 1], - methodType, - zScore); - } - } - - return null; - } - -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/kstest/KSTechnique.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/kstest/KSTechnique.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/kstest/KSTechnique.java deleted file mode 100644 index ff8dbcf..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/prototype/methods/kstest/KSTechnique.java +++ /dev/null @@ -1,101 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ambari.metrics.alertservice.prototype.methods.kstest; - -import org.apache.ambari.metrics.alertservice.prototype.RFunctionInvoker; -import org.apache.ambari.metrics.alertservice.prototype.common.DataSeries; -import org.apache.ambari.metrics.alertservice.prototype.common.ResultSet; -import org.apache.ambari.metrics.alertservice.prototype.methods.MetricAnomaly; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.io.Serializable; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public class KSTechnique implements Serializable { - - private String methodType = "ks"; - private Map<String, Double> pValueMap; - private static final Log LOG = LogFactory.getLog(KSTechnique.class); - - public KSTechnique() { - pValueMap = new HashMap(); - } - - public MetricAnomaly runKsTest(String key, DataSeries trainData, DataSeries testData) { - - int testLength = testData.values.length; - int trainLength = trainData.values.length; - - if (trainLength < testLength) { - LOG.info("Not enough train data."); - return null; - } - - if (!pValueMap.containsKey(key)) { - pValueMap.put(key, 0.05); - } - double pValue = pValueMap.get(key); - - ResultSet result = RFunctionInvoker.ksTest(trainData, testData, Collections.singletonMap("ks.p_value", String.valueOf(pValue))); - if (result == null) { - LOG.error("Resultset is null when invoking KS R function..."); - return null; - } - - if (result.resultset.size() > 0) { - - LOG.info("Is size 1 ? result size = " + result.resultset.get(0).length); - LOG.info("p_value = " + result.resultset.get(3)[0]); - double dValue = result.resultset.get(2)[0]; - - return new MetricAnomaly(key, - (long) testData.ts[testLength - 1], - testData.values[testLength - 1], - methodType, - dValue); - } - - return null; - } - - public void updateModel(String metricKey, boolean increaseSensitivity, double percent) { - - LOG.info("Updating KS model for " + metricKey + " with increaseSensitivity = " + increaseSensitivity + ", percent = " + percent); - - if (!pValueMap.containsKey(metricKey)) { - LOG.error("Unknown metric key : " + metricKey); - LOG.info("pValueMap :" + pValueMap.toString()); - return; - } - - double delta = percent / 100; - if (!increaseSensitivity) { - delta = delta * -1; - } - - double pValue = pValueMap.get(metricKey); - double newPValue = Math.min(1.0, pValue + delta * pValue); - pValueMap.put(metricKey, newPValue); - LOG.info("New pValue = " + newPValue); - } - -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/AbstractMetricSeries.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/AbstractMetricSeries.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/AbstractMetricSeries.java deleted file mode 100644 index a8e31bf..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/AbstractMetricSeries.java +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.seriesgenerator; - -public interface AbstractMetricSeries { - - public double nextValue(); - public double[] getSeries(int n); - -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/DualBandMetricSeries.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/DualBandMetricSeries.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/DualBandMetricSeries.java deleted file mode 100644 index 4158ff4..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/DualBandMetricSeries.java +++ /dev/null @@ -1,88 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.seriesgenerator; - -import java.util.Random; - -public class DualBandMetricSeries implements AbstractMetricSeries { - - double lowBandValue = 0.0; - double lowBandDeviationPercentage = 0.0; - int lowBandPeriodSize = 10; - double highBandValue = 1.0; - double highBandDeviationPercentage = 0.0; - int highBandPeriodSize = 10; - - Random random = new Random(); - double lowBandValueLowerLimit, lowBandValueHigherLimit; - double highBandLowerLimit, highBandUpperLimit; - int l = 0, h = 0; - - public DualBandMetricSeries(double lowBandValue, - double lowBandDeviationPercentage, - int lowBandPeriodSize, - double highBandValue, - double highBandDeviationPercentage, - int highBandPeriodSize) { - this.lowBandValue = lowBandValue; - this.lowBandDeviationPercentage = lowBandDeviationPercentage; - this.lowBandPeriodSize = lowBandPeriodSize; - this.highBandValue = highBandValue; - this.highBandDeviationPercentage = highBandDeviationPercentage; - this.highBandPeriodSize = highBandPeriodSize; - init(); - } - - private void init() { - lowBandValueLowerLimit = lowBandValue - lowBandDeviationPercentage * lowBandValue; - lowBandValueHigherLimit = lowBandValue + lowBandDeviationPercentage * lowBandValue; - highBandLowerLimit = highBandValue - highBandDeviationPercentage * highBandValue; - highBandUpperLimit = highBandValue + highBandDeviationPercentage * highBandValue; - } - - @Override - public double nextValue() { - - double value = 0.0; - - if (l < lowBandPeriodSize) { - value = lowBandValueLowerLimit + (lowBandValueHigherLimit - lowBandValueLowerLimit) * random.nextDouble(); - l++; - } else if (h < highBandPeriodSize) { - value = highBandLowerLimit + (highBandUpperLimit - highBandLowerLimit) * random.nextDouble(); - h++; - } - - if (l == lowBandPeriodSize && h == highBandPeriodSize) { - l = 0; - h = 0; - } - - return value; - } - - @Override - public double[] getSeries(int n) { - double[] series = new double[n]; - for (int i = 0; i < n; i++) { - series[i] = nextValue(); - } - return series; - } - -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/MetricSeriesGeneratorFactory.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/MetricSeriesGeneratorFactory.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/MetricSeriesGeneratorFactory.java deleted file mode 100644 index 1e37ff3..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/MetricSeriesGeneratorFactory.java +++ /dev/null @@ -1,379 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ambari.metrics.alertservice.seriesgenerator; - -import java.util.Arrays; -import java.util.Map; -import java.util.Random; - -public class MetricSeriesGeneratorFactory { - - /** - * Return a normally distributed data series with some deviation % and outliers. - * - * @param n size of the data series - * @param value The value around which the uniform data series is centered on. - * @param deviationPercentage The allowed deviation % on either side of the uniform value. For example, if value = 10, and deviation % is 0.1, the series values lie between 0.9 to 1.1. - * @param outlierProbability The probability of finding an outlier in the series. - * @param outlierDeviationLowerPercentage min percentage outlier should be away from the uniform value in % terms. if value = 10 and outlierDeviationPercentage = 30%, the outlier is 7 and 13. - * @param outlierDeviationHigherPercentage max percentage outlier should be away from the uniform value in % terms. if value = 10 and outlierDeviationPercentage = 60%, the outlier is 4 and 16. - * @param outliersAboveValue Outlier should be greater or smaller than the value. - * @return uniform series - */ - public static double[] createUniformSeries(int n, - double value, - double deviationPercentage, - double outlierProbability, - double outlierDeviationLowerPercentage, - double outlierDeviationHigherPercentage, - boolean outliersAboveValue) { - - UniformMetricSeries metricSeries = new UniformMetricSeries(value, - deviationPercentage, - outlierProbability, - outlierDeviationLowerPercentage, - outlierDeviationHigherPercentage, - outliersAboveValue); - - return metricSeries.getSeries(n); - } - - - /** - * /** - * Returns a normally distributed series. - * - * @param n size of the data series - * @param mean mean of the distribution - * @param sd sd of the distribution - * @param outlierProbability sd of the distribution - * @param outlierDeviationSDTimesLower Lower Limit of the outlier with respect to times sdev from the mean. - * @param outlierDeviationSDTimesHigher Higher Limit of the outlier with respect to times sdev from the mean. - * @param outlierOnRightEnd Outlier should be on the right end or the left end. - * @return normal series - */ - public static double[] createNormalSeries(int n, - double mean, - double sd, - double outlierProbability, - double outlierDeviationSDTimesLower, - double outlierDeviationSDTimesHigher, - boolean outlierOnRightEnd) { - - - NormalMetricSeries metricSeries = new NormalMetricSeries(mean, - sd, - outlierProbability, - outlierDeviationSDTimesLower, - outlierDeviationSDTimesHigher, - outlierOnRightEnd); - - return metricSeries.getSeries(n); - } - - - /** - * Returns a monotonically increasing / decreasing series - * - * @param n size of the data series - * @param startValue Start value of the monotonic sequence - * @param slope direction of monotonicity m > 0 for increasing and m < 0 for decreasing. - * @param deviationPercentage The allowed deviation % on either side of the current 'y' value. For example, if current value = 10 according to slope, and deviation % is 0.1, the series values lie between 0.9 to 1.1. - * @param outlierProbability The probability of finding an outlier in the series. - * @param outlierDeviationLowerPercentage min percentage outlier should be away from the current 'y' value in % terms. if value = 10 and outlierDeviationPercentage = 30%, the outlier is 7 and 13. - * @param outlierDeviationHigherPercentage max percentage outlier should be away from the current 'y' value in % terms. if value = 10 and outlierDeviationPercentage = 60%, the outlier is 4 and 16. - * @param outliersAboveValue Outlier should be greater or smaller than the 'y' value. - * @return - */ - public static double[] createMonotonicSeries(int n, - double startValue, - double slope, - double deviationPercentage, - double outlierProbability, - double outlierDeviationLowerPercentage, - double outlierDeviationHigherPercentage, - boolean outliersAboveValue) { - - MonotonicMetricSeries metricSeries = new MonotonicMetricSeries(startValue, - slope, - deviationPercentage, - outlierProbability, - outlierDeviationLowerPercentage, - outlierDeviationHigherPercentage, - outliersAboveValue); - - return metricSeries.getSeries(n); - } - - - /** - * Returns a dual band series (lower and higher) - * - * @param n size of the data series - * @param lowBandValue lower band value - * @param lowBandDeviationPercentage lower band deviation - * @param lowBandPeriodSize lower band - * @param highBandValue high band centre value - * @param highBandDeviationPercentage high band deviation. - * @param highBandPeriodSize high band size - * @return - */ - public static double[] getDualBandSeries(int n, - double lowBandValue, - double lowBandDeviationPercentage, - int lowBandPeriodSize, - double highBandValue, - double highBandDeviationPercentage, - int highBandPeriodSize) { - - DualBandMetricSeries metricSeries = new DualBandMetricSeries(lowBandValue, - lowBandDeviationPercentage, - lowBandPeriodSize, - highBandValue, - highBandDeviationPercentage, - highBandPeriodSize); - - return metricSeries.getSeries(n); - } - - /** - * Returns a step function series. - * - * @param n size of the data series - * @param startValue start steady value - * @param steadyValueDeviationPercentage required devation in the steady state value - * @param steadyPeriodSlope direction of monotonicity m > 0 for increasing and m < 0 for decreasing, m = 0 no increase or decrease. - * @param steadyPeriodMinSize min size for step period - * @param steadyPeriodMaxSize max size for step period. - * @param stepChangePercentage Increase / decrease in steady state to denote a step in terms of deviation percentage from the last value. - * @param upwardStep upward or downward step. - * @return - */ - public static double[] getStepFunctionSeries(int n, - double startValue, - double steadyValueDeviationPercentage, - double steadyPeriodSlope, - int steadyPeriodMinSize, - int steadyPeriodMaxSize, - double stepChangePercentage, - boolean upwardStep) { - - StepFunctionMetricSeries metricSeries = new StepFunctionMetricSeries(startValue, - steadyValueDeviationPercentage, - steadyPeriodSlope, - steadyPeriodMinSize, - steadyPeriodMaxSize, - stepChangePercentage, - upwardStep); - - return metricSeries.getSeries(n); - } - - /** - * Series with small period of turbulence and then back to steady. - * - * @param n size of the data series - * @param steadyStateValue steady state center value - * @param steadyStateDeviationPercentage steady state deviation in percentage - * @param turbulentPeriodDeviationLowerPercentage turbulent state lower limit in terms of percentage from centre value. - * @param turbulentPeriodDeviationHigherPercentage turbulent state higher limit in terms of percentage from centre value. - * @param turbulentPeriodLength turbulent period length (number of points) - * @param turbulentStatePosition Where the turbulent state should be 0 - at the beginning, 1 - in the middle (25% - 50% of the series), 2 - at the end of the series. - * @return - */ - public static double[] getSteadySeriesWithTurbulentPeriod(int n, - double steadyStateValue, - double steadyStateDeviationPercentage, - double turbulentPeriodDeviationLowerPercentage, - double turbulentPeriodDeviationHigherPercentage, - int turbulentPeriodLength, - int turbulentStatePosition - ) { - - - SteadyWithTurbulenceMetricSeries metricSeries = new SteadyWithTurbulenceMetricSeries(n, - steadyStateValue, - steadyStateDeviationPercentage, - turbulentPeriodDeviationLowerPercentage, - turbulentPeriodDeviationHigherPercentage, - turbulentPeriodLength, - turbulentStatePosition); - - return metricSeries.getSeries(n); - } - - - public static double[] generateSeries(String type, int n, Map<String, String> configs) { - - double[] series; - switch (type) { - - case "normal": - series = createNormalSeries(n, - Double.parseDouble(configs.getOrDefault("mean", "0")), - Double.parseDouble(configs.getOrDefault("sd", "1")), - Double.parseDouble(configs.getOrDefault("outlierProbability", "0")), - Double.parseDouble(configs.getOrDefault("outlierDeviationSDTimesLower", "0")), - Double.parseDouble(configs.getOrDefault("outlierDeviationSDTimesHigher", "0")), - Boolean.parseBoolean(configs.getOrDefault("outlierOnRightEnd", "true"))); - break; - - case "uniform": - series = createUniformSeries(n, - Double.parseDouble(configs.getOrDefault("value", "10")), - Double.parseDouble(configs.getOrDefault("deviationPercentage", "0")), - Double.parseDouble(configs.getOrDefault("outlierProbability", "0")), - Double.parseDouble(configs.getOrDefault("outlierDeviationLowerPercentage", "0")), - Double.parseDouble(configs.getOrDefault("outlierDeviationHigherPercentage", "0")), - Boolean.parseBoolean(configs.getOrDefault("outliersAboveValue", "true"))); - break; - - case "monotonic": - series = createMonotonicSeries(n, - Double.parseDouble(configs.getOrDefault("startValue", "10")), - Double.parseDouble(configs.getOrDefault("slope", "0")), - Double.parseDouble(configs.getOrDefault("deviationPercentage", "0")), - Double.parseDouble(configs.getOrDefault("outlierProbability", "0")), - Double.parseDouble(configs.getOrDefault("outlierDeviationLowerPercentage", "0")), - Double.parseDouble(configs.getOrDefault("outlierDeviationHigherPercentage", "0")), - Boolean.parseBoolean(configs.getOrDefault("outliersAboveValue", "true"))); - break; - - case "dualband": - series = getDualBandSeries(n, - Double.parseDouble(configs.getOrDefault("lowBandValue", "10")), - Double.parseDouble(configs.getOrDefault("lowBandDeviationPercentage", "0")), - Integer.parseInt(configs.getOrDefault("lowBandPeriodSize", "0")), - Double.parseDouble(configs.getOrDefault("highBandValue", "10")), - Double.parseDouble(configs.getOrDefault("highBandDeviationPercentage", "0")), - Integer.parseInt(configs.getOrDefault("highBandPeriodSize", "0"))); - break; - - case "step": - series = getStepFunctionSeries(n, - Double.parseDouble(configs.getOrDefault("startValue", "10")), - Double.parseDouble(configs.getOrDefault("steadyValueDeviationPercentage", "0")), - Double.parseDouble(configs.getOrDefault("steadyPeriodSlope", "0")), - Integer.parseInt(configs.getOrDefault("steadyPeriodMinSize", "0")), - Integer.parseInt(configs.getOrDefault("steadyPeriodMaxSize", "0")), - Double.parseDouble(configs.getOrDefault("stepChangePercentage", "0")), - Boolean.parseBoolean(configs.getOrDefault("upwardStep", "true"))); - break; - - case "turbulence": - series = getSteadySeriesWithTurbulentPeriod(n, - Double.parseDouble(configs.getOrDefault("steadyStateValue", "10")), - Double.parseDouble(configs.getOrDefault("steadyStateDeviationPercentage", "0")), - Double.parseDouble(configs.getOrDefault("turbulentPeriodDeviationLowerPercentage", "0")), - Double.parseDouble(configs.getOrDefault("turbulentPeriodDeviationHigherPercentage", "10")), - Integer.parseInt(configs.getOrDefault("turbulentPeriodLength", "0")), - Integer.parseInt(configs.getOrDefault("turbulentStatePosition", "0"))); - break; - - default: - series = createNormalSeries(n, - 0, - 1, - 0, - 0, - 0, - true); - } - return series; - } - - public static AbstractMetricSeries generateSeries(String type, Map<String, String> configs) { - - AbstractMetricSeries series; - switch (type) { - - case "normal": - series = new NormalMetricSeries(Double.parseDouble(configs.getOrDefault("mean", "0")), - Double.parseDouble(configs.getOrDefault("sd", "1")), - Double.parseDouble(configs.getOrDefault("outlierProbability", "0")), - Double.parseDouble(configs.getOrDefault("outlierDeviationSDTimesLower", "0")), - Double.parseDouble(configs.getOrDefault("outlierDeviationSDTimesHigher", "0")), - Boolean.parseBoolean(configs.getOrDefault("outlierOnRightEnd", "true"))); - break; - - case "uniform": - series = new UniformMetricSeries( - Double.parseDouble(configs.getOrDefault("value", "10")), - Double.parseDouble(configs.getOrDefault("deviationPercentage", "0")), - Double.parseDouble(configs.getOrDefault("outlierProbability", "0")), - Double.parseDouble(configs.getOrDefault("outlierDeviationLowerPercentage", "0")), - Double.parseDouble(configs.getOrDefault("outlierDeviationHigherPercentage", "0")), - Boolean.parseBoolean(configs.getOrDefault("outliersAboveValue", "true"))); - break; - - case "monotonic": - series = new MonotonicMetricSeries( - Double.parseDouble(configs.getOrDefault("startValue", "10")), - Double.parseDouble(configs.getOrDefault("slope", "0")), - Double.parseDouble(configs.getOrDefault("deviationPercentage", "0")), - Double.parseDouble(configs.getOrDefault("outlierProbability", "0")), - Double.parseDouble(configs.getOrDefault("outlierDeviationLowerPercentage", "0")), - Double.parseDouble(configs.getOrDefault("outlierDeviationHigherPercentage", "0")), - Boolean.parseBoolean(configs.getOrDefault("outliersAboveValue", "true"))); - break; - - case "dualband": - series = new DualBandMetricSeries( - Double.parseDouble(configs.getOrDefault("lowBandValue", "10")), - Double.parseDouble(configs.getOrDefault("lowBandDeviationPercentage", "0")), - Integer.parseInt(configs.getOrDefault("lowBandPeriodSize", "0")), - Double.parseDouble(configs.getOrDefault("highBandValue", "10")), - Double.parseDouble(configs.getOrDefault("highBandDeviationPercentage", "0")), - Integer.parseInt(configs.getOrDefault("highBandPeriodSize", "0"))); - break; - - case "step": - series = new StepFunctionMetricSeries( - Double.parseDouble(configs.getOrDefault("startValue", "10")), - Double.parseDouble(configs.getOrDefault("steadyValueDeviationPercentage", "0")), - Double.parseDouble(configs.getOrDefault("steadyPeriodSlope", "0")), - Integer.parseInt(configs.getOrDefault("steadyPeriodMinSize", "0")), - Integer.parseInt(configs.getOrDefault("steadyPeriodMaxSize", "0")), - Double.parseDouble(configs.getOrDefault("stepChangePercentage", "0")), - Boolean.parseBoolean(configs.getOrDefault("upwardStep", "true"))); - break; - - case "turbulence": - series = new SteadyWithTurbulenceMetricSeries( - Integer.parseInt(configs.getOrDefault("approxSeriesLength", "100")), - Double.parseDouble(configs.getOrDefault("steadyStateValue", "10")), - Double.parseDouble(configs.getOrDefault("steadyStateDeviationPercentage", "0")), - Double.parseDouble(configs.getOrDefault("turbulentPeriodDeviationLowerPercentage", "0")), - Double.parseDouble(configs.getOrDefault("turbulentPeriodDeviationHigherPercentage", "10")), - Integer.parseInt(configs.getOrDefault("turbulentPeriodLength", "0")), - Integer.parseInt(configs.getOrDefault("turbulentStatePosition", "0"))); - break; - - default: - series = new NormalMetricSeries(0, - 1, - 0, - 0, - 0, - true); - } - return series; - } - -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/MonotonicMetricSeries.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/MonotonicMetricSeries.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/MonotonicMetricSeries.java deleted file mode 100644 index a883d08..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/MonotonicMetricSeries.java +++ /dev/null @@ -1,101 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.seriesgenerator; - -import java.util.Random; - -public class MonotonicMetricSeries implements AbstractMetricSeries { - - double startValue = 0.0; - double slope = 0.5; - double deviationPercentage = 0.0; - double outlierProbability = 0.0; - double outlierDeviationLowerPercentage = 0.0; - double outlierDeviationHigherPercentage = 0.0; - boolean outliersAboveValue = true; - - Random random = new Random(); - double nonOutlierProbability; - - // y = mx + c - double y; - double m; - double x; - double c; - - public MonotonicMetricSeries(double startValue, - double slope, - double deviationPercentage, - double outlierProbability, - double outlierDeviationLowerPercentage, - double outlierDeviationHigherPercentage, - boolean outliersAboveValue) { - this.startValue = startValue; - this.slope = slope; - this.deviationPercentage = deviationPercentage; - this.outlierProbability = outlierProbability; - this.outlierDeviationLowerPercentage = outlierDeviationLowerPercentage; - this.outlierDeviationHigherPercentage = outlierDeviationHigherPercentage; - this.outliersAboveValue = outliersAboveValue; - init(); - } - - private void init() { - y = startValue; - m = slope; - x = 1; - c = y - (m * x); - nonOutlierProbability = 1.0 - outlierProbability; - } - - @Override - public double nextValue() { - - double value; - double probability = random.nextDouble(); - - y = m * x + c; - if (probability <= nonOutlierProbability) { - double valueDeviationLowerLimit = y - deviationPercentage * y; - double valueDeviationHigherLimit = y + deviationPercentage * y; - value = valueDeviationLowerLimit + (valueDeviationHigherLimit - valueDeviationLowerLimit) * random.nextDouble(); - } else { - if (outliersAboveValue) { - double outlierLowerLimit = y + outlierDeviationLowerPercentage * y; - double outlierUpperLimit = y + outlierDeviationHigherPercentage * y; - value = outlierLowerLimit + (outlierUpperLimit - outlierLowerLimit) * random.nextDouble(); - } else { - double outlierLowerLimit = y - outlierDeviationLowerPercentage * y; - double outlierUpperLimit = y - outlierDeviationHigherPercentage * y; - value = outlierUpperLimit + (outlierLowerLimit - outlierUpperLimit) * random.nextDouble(); - } - } - x++; - return value; - } - - @Override - public double[] getSeries(int n) { - double[] series = new double[n]; - for (int i = 0; i < n; i++) { - series[i] = nextValue(); - } - return series; - } - -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/NormalMetricSeries.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/NormalMetricSeries.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/NormalMetricSeries.java deleted file mode 100644 index cc83d2c..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/NormalMetricSeries.java +++ /dev/null @@ -1,81 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.seriesgenerator; - -import java.util.Random; - -public class NormalMetricSeries implements AbstractMetricSeries { - - double mean = 0.0; - double sd = 1.0; - double outlierProbability = 0.0; - double outlierDeviationSDTimesLower = 0.0; - double outlierDeviationSDTimesHigher = 0.0; - boolean outlierOnRightEnd = true; - - Random random = new Random(); - double nonOutlierProbability; - - - public NormalMetricSeries(double mean, - double sd, - double outlierProbability, - double outlierDeviationSDTimesLower, - double outlierDeviationSDTimesHigher, - boolean outlierOnRightEnd) { - this.mean = mean; - this.sd = sd; - this.outlierProbability = outlierProbability; - this.outlierDeviationSDTimesLower = outlierDeviationSDTimesLower; - this.outlierDeviationSDTimesHigher = outlierDeviationSDTimesHigher; - this.outlierOnRightEnd = outlierOnRightEnd; - init(); - } - - private void init() { - nonOutlierProbability = 1.0 - outlierProbability; - } - - @Override - public double nextValue() { - - double value; - double probability = random.nextDouble(); - - if (probability <= nonOutlierProbability) { - value = random.nextGaussian() * sd + mean; - } else { - if (outlierOnRightEnd) { - value = mean + (outlierDeviationSDTimesLower + (outlierDeviationSDTimesHigher - outlierDeviationSDTimesLower) * random.nextDouble()) * sd; - } else { - value = mean - (outlierDeviationSDTimesLower + (outlierDeviationSDTimesHigher - outlierDeviationSDTimesLower) * random.nextDouble()) * sd; - } - } - return value; - } - - @Override - public double[] getSeries(int n) { - double[] series = new double[n]; - for (int i = 0; i < n; i++) { - series[i] = nextValue(); - } - return series; - } - -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e33b5455/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/SteadyWithTurbulenceMetricSeries.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/SteadyWithTurbulenceMetricSeries.java b/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/SteadyWithTurbulenceMetricSeries.java deleted file mode 100644 index c4ed3ba..0000000 --- a/ambari-metrics/ambari-metrics-alertservice/src/main/java/org/apache/ambari/metrics/alertservice/seriesgenerator/SteadyWithTurbulenceMetricSeries.java +++ /dev/null @@ -1,115 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.metrics.alertservice.seriesgenerator; - -import java.util.Random; - -public class SteadyWithTurbulenceMetricSeries implements AbstractMetricSeries { - - double steadyStateValue = 0.0; - double steadyStateDeviationPercentage = 0.0; - double turbulentPeriodDeviationLowerPercentage = 0.3; - double turbulentPeriodDeviationHigherPercentage = 0.5; - int turbulentPeriodLength = 5; - int turbulentStatePosition = 1; - int approximateSeriesLength = 10; - - Random random = new Random(); - double valueDeviationLowerLimit; - double valueDeviationHigherLimit; - double tPeriodLowerLimit; - double tPeriodUpperLimit; - int tPeriodStartIndex = 0; - int index = 0; - - public SteadyWithTurbulenceMetricSeries(int approximateSeriesLength, - double steadyStateValue, - double steadyStateDeviationPercentage, - double turbulentPeriodDeviationLowerPercentage, - double turbulentPeriodDeviationHigherPercentage, - int turbulentPeriodLength, - int turbulentStatePosition) { - this.approximateSeriesLength = approximateSeriesLength; - this.steadyStateValue = steadyStateValue; - this.steadyStateDeviationPercentage = steadyStateDeviationPercentage; - this.turbulentPeriodDeviationLowerPercentage = turbulentPeriodDeviationLowerPercentage; - this.turbulentPeriodDeviationHigherPercentage = turbulentPeriodDeviationHigherPercentage; - this.turbulentPeriodLength = turbulentPeriodLength; - this.turbulentStatePosition = turbulentStatePosition; - init(); - } - - private void init() { - - if (turbulentStatePosition == 1) { - tPeriodStartIndex = (int) (0.25 * approximateSeriesLength + (0.25 * approximateSeriesLength * random.nextDouble())); - } else if (turbulentStatePosition == 2) { - tPeriodStartIndex = approximateSeriesLength - turbulentPeriodLength; - } - - valueDeviationLowerLimit = steadyStateValue - steadyStateDeviationPercentage * steadyStateValue; - valueDeviationHigherLimit = steadyStateValue + steadyStateDeviationPercentage * steadyStateValue; - - tPeriodLowerLimit = steadyStateValue + turbulentPeriodDeviationLowerPercentage * steadyStateValue; - tPeriodUpperLimit = steadyStateValue + turbulentPeriodDeviationHigherPercentage * steadyStateValue; - } - - @Override - public double nextValue() { - - double value; - - if (index >= tPeriodStartIndex && index <= (tPeriodStartIndex + turbulentPeriodLength)) { - value = tPeriodLowerLimit + (tPeriodUpperLimit - tPeriodLowerLimit) * random.nextDouble(); - } else { - value = valueDeviationLowerLimit + (valueDeviationHigherLimit - valueDeviationLowerLimit) * random.nextDouble(); - } - index++; - return value; - } - - @Override - public double[] getSeries(int n) { - - double[] series = new double[n]; - int turbulentPeriodStartIndex = 0; - - if (turbulentStatePosition == 1) { - turbulentPeriodStartIndex = (int) (0.25 * n + (0.25 * n * random.nextDouble())); - } else if (turbulentStatePosition == 2) { - turbulentPeriodStartIndex = n - turbulentPeriodLength; - } - - double valueDevLowerLimit = steadyStateValue - steadyStateDeviationPercentage * steadyStateValue; - double valueDevHigherLimit = steadyStateValue + steadyStateDeviationPercentage * steadyStateValue; - - double turbulentPeriodLowerLimit = steadyStateValue + turbulentPeriodDeviationLowerPercentage * steadyStateValue; - double turbulentPeriodUpperLimit = steadyStateValue + turbulentPeriodDeviationHigherPercentage * steadyStateValue; - - for (int i = 0; i < n; i++) { - if (i >= turbulentPeriodStartIndex && i < (turbulentPeriodStartIndex + turbulentPeriodLength)) { - series[i] = turbulentPeriodLowerLimit + (turbulentPeriodUpperLimit - turbulentPeriodLowerLimit) * random.nextDouble(); - } else { - series[i] = valueDevLowerLimit + (valueDevHigherLimit - valueDevLowerLimit) * random.nextDouble(); - } - } - - return series; - } - -}
