use of org.joda.time.Interval in project pinot by linkedin.
the class MinMaxThresholdDetectionModel method detect.
@Override
public List<RawAnomalyResultDTO> detect(String metricName, AnomalyDetectionContext anomalyDetectionContext) {
List<RawAnomalyResultDTO> anomalyResults = new ArrayList<>();
// Get min / max props
Double min = null;
if (properties.containsKey(MIN_VAL)) {
min = Double.valueOf(properties.getProperty(MIN_VAL));
}
Double max = null;
if (properties.containsKey(MAX_VAL)) {
max = Double.valueOf(properties.getProperty(MAX_VAL));
}
TimeSeries timeSeries = anomalyDetectionContext.getTransformedCurrent(metricName);
// Compute the weight of this time series (average across whole)
double averageValue = 0;
for (long time : timeSeries.timestampSet()) {
averageValue += timeSeries.get(time);
}
// Compute the bucket size, so we can iterate in those steps
long bucketMillis = anomalyDetectionContext.getBucketSizeInMS();
Interval timeSeriesInterval = timeSeries.getTimeSeriesInterval();
long numBuckets = Math.abs(timeSeriesInterval.getEndMillis() - timeSeriesInterval.getStartMillis()) / bucketMillis;
// avg value of this time series
averageValue /= numBuckets;
DimensionMap dimensionMap = anomalyDetectionContext.getTimeSeriesKey().getDimensionMap();
for (long timeBucket : timeSeries.timestampSet()) {
double value = timeSeries.get(timeBucket);
double deviationFromThreshold = getDeviationFromThreshold(value, min, max);
if (deviationFromThreshold != 0) {
RawAnomalyResultDTO anomalyResult = new RawAnomalyResultDTO();
anomalyResult.setProperties(properties.toString());
anomalyResult.setStartTime(timeBucket);
// point-in-time
anomalyResult.setEndTime(timeBucket + bucketMillis);
anomalyResult.setDimensions(dimensionMap);
anomalyResult.setScore(averageValue);
// higher change, higher the severity
anomalyResult.setWeight(deviationFromThreshold);
anomalyResult.setAvgCurrentVal(value);
String message = String.format(DEFAULT_MESSAGE_TEMPLATE, deviationFromThreshold, value, min, max);
anomalyResult.setMessage(message);
if (value == 0.0) {
anomalyResult.setDataMissing(true);
}
anomalyResults.add(anomalyResult);
}
}
return anomalyResults;
}
use of org.joda.time.Interval in project pinot by linkedin.
the class SimpleThresholdDetectionModel method detect.
@Override
public List<RawAnomalyResultDTO> detect(String metricName, AnomalyDetectionContext anomalyDetectionContext) {
List<RawAnomalyResultDTO> anomalyResults = new ArrayList<>();
// Get thresholds
double changeThreshold = Double.valueOf(getProperties().getProperty(CHANGE_THRESHOLD));
double volumeThreshold = 0d;
if (getProperties().containsKey(AVERAGE_VOLUME_THRESHOLD)) {
volumeThreshold = Double.valueOf(getProperties().getProperty(AVERAGE_VOLUME_THRESHOLD));
}
long bucketSizeInMillis = anomalyDetectionContext.getBucketSizeInMS();
// Compute the weight of this time series (average across whole)
TimeSeries currentTimeSeries = anomalyDetectionContext.getTransformedCurrent(metricName);
double averageValue = 0;
for (long time : currentTimeSeries.timestampSet()) {
averageValue += currentTimeSeries.get(time);
}
Interval currentInterval = currentTimeSeries.getTimeSeriesInterval();
long currentStart = currentInterval.getStartMillis();
long currentEnd = currentInterval.getEndMillis();
long numBuckets = (currentEnd - currentStart) / bucketSizeInMillis;
if (numBuckets != 0) {
averageValue /= numBuckets;
}
// Check if this time series even meets our volume threshold
DimensionMap dimensionMap = anomalyDetectionContext.getTimeSeriesKey().getDimensionMap();
if (averageValue < volumeThreshold) {
LOGGER.info("{} does not meet volume threshold {}: {}", dimensionMap, volumeThreshold, averageValue);
// empty list
return anomalyResults;
}
PredictionModel predictionModel = anomalyDetectionContext.getTrainedPredictionModel(metricName);
if (!(predictionModel instanceof ExpectedTimeSeriesPredictionModel)) {
LOGGER.info("SimpleThresholdDetectionModel detection model expects an ExpectedTimeSeriesPredictionModel but the trained prediction model in anomaly detection context is not.");
// empty list
return anomalyResults;
}
ExpectedTimeSeriesPredictionModel expectedTimeSeriesPredictionModel = (ExpectedTimeSeriesPredictionModel) predictionModel;
TimeSeries expectedTimeSeries = expectedTimeSeriesPredictionModel.getExpectedTimeSeries();
Interval expectedTSInterval = expectedTimeSeries.getTimeSeriesInterval();
long expectedStart = expectedTSInterval.getStartMillis();
long seasonalOffset = currentStart - expectedStart;
for (long currentTimestamp : currentTimeSeries.timestampSet()) {
long expectedTimestamp = currentTimestamp - seasonalOffset;
if (!expectedTimeSeries.hasTimestamp(expectedTimestamp)) {
continue;
}
double baselineValue = expectedTimeSeries.get(expectedTimestamp);
double currentValue = currentTimeSeries.get(currentTimestamp);
if (isAnomaly(currentValue, baselineValue, changeThreshold)) {
RawAnomalyResultDTO anomalyResult = new RawAnomalyResultDTO();
anomalyResult.setDimensions(dimensionMap);
anomalyResult.setProperties(getProperties().toString());
anomalyResult.setStartTime(currentTimestamp);
// point-in-time
anomalyResult.setEndTime(currentTimestamp + bucketSizeInMillis);
anomalyResult.setScore(averageValue);
anomalyResult.setWeight(calculateChange(currentValue, baselineValue));
anomalyResult.setAvgCurrentVal(currentValue);
anomalyResult.setAvgBaselineVal(baselineValue);
String message = getAnomalyResultMessage(changeThreshold, currentValue, baselineValue);
anomalyResult.setMessage(message);
anomalyResults.add(anomalyResult);
if (currentValue == 0.0 || baselineValue == 0.0) {
anomalyResult.setDataMissing(true);
}
}
}
return anomalyResults;
}
use of org.joda.time.Interval in project pinot by linkedin.
the class SimplePercentageMergeModel method update.
/**
* The weight of the merged anomaly is calculated by this equation:
* weight = (avg. observed value) / (avg. expected value) - 1;
*
* Note that the values of the holes in the time series are not included in the computation.
* Considering the observed and expected time series:
* observed: 1 2 x 4 x 6
* expected: 1 x x 4 5 6
* The values that are included in the computation are those at slots 1, 4, and 6.
*
* @param anomalyDetectionContext the context that provided a trained
* ExpectedTimeSeriesPredictionModel for computing the weight.
* Moreover, the data range of the time series should equals the
* range of anomaly to be updated.
*
* @param anomalyToUpdated the anomaly of which the information is updated.
*/
@Override
public void update(AnomalyDetectionContext anomalyDetectionContext, MergedAnomalyResultDTO anomalyToUpdated) {
String mainMetric = anomalyDetectionContext.getAnomalyDetectionFunction().getSpec().getTopicMetric();
PredictionModel predictionModel = anomalyDetectionContext.getTrainedPredictionModel(mainMetric);
if (!(predictionModel instanceof ExpectedTimeSeriesPredictionModel)) {
LOGGER.error("SimplePercentageMergeModel expects an ExpectedTimeSeriesPredictionModel but the trained model is not one.");
return;
}
ExpectedTimeSeriesPredictionModel expectedTimeSeriesPredictionModel = (ExpectedTimeSeriesPredictionModel) predictionModel;
TimeSeries expectedTimeSeries = expectedTimeSeriesPredictionModel.getExpectedTimeSeries();
long expectedStartTime = expectedTimeSeries.getTimeSeriesInterval().getStartMillis();
TimeSeries observedTimeSeries = anomalyDetectionContext.getTransformedCurrent(mainMetric);
long observedStartTime = observedTimeSeries.getTimeSeriesInterval().getStartMillis();
double avgCurrent = 0d;
double avgBaseline = 0d;
int count = 0;
Interval anomalyInterval = new Interval(anomalyToUpdated.getStartTime(), anomalyToUpdated.getEndTime());
for (long observedTimestamp : observedTimeSeries.timestampSet()) {
if (anomalyInterval.contains(observedTimestamp)) {
long offset = observedTimestamp - observedStartTime;
long expectedTimestamp = expectedStartTime + offset;
if (expectedTimeSeries.hasTimestamp(expectedTimestamp)) {
avgCurrent += observedTimeSeries.get(observedTimestamp);
avgBaseline += expectedTimeSeries.get(expectedTimestamp);
++count;
}
}
}
double weight = 0d;
if (count != 0 && avgBaseline != 0d) {
weight = (avgCurrent - avgBaseline) / avgBaseline;
avgCurrent /= count;
avgBaseline /= count;
} else {
weight = 0d;
}
// Average score of raw anomalies
List<RawAnomalyResultDTO> rawAnomalyResultDTOs = anomalyToUpdated.getAnomalyResults();
double score = 0d;
if (CollectionUtils.isNotEmpty(rawAnomalyResultDTOs)) {
for (RawAnomalyResultDTO rawAnomaly : rawAnomalyResultDTOs) {
score += rawAnomaly.getScore();
}
score /= rawAnomalyResultDTOs.size();
} else {
score = anomalyToUpdated.getScore();
}
anomalyToUpdated.setWeight(weight);
anomalyToUpdated.setScore(score);
anomalyToUpdated.setAvgCurrentVal(avgCurrent);
anomalyToUpdated.setAvgBaselineVal(avgBaseline);
anomalyToUpdated.setMessage(String.format(DEFAULT_MESSAGE_TEMPLATE, weight * 100, avgCurrent, avgBaseline, score));
}
use of org.joda.time.Interval in project pinot by linkedin.
the class SeasonalAveragePredictionModel method getLatestTimeSeries.
/**
* Returns the time series, which has the largest start millis, from a set of time series.
*
* @param baselineTimeSeries the set of baselines
* @return the time series, which has the largest start millis, from a set of time series.
*/
private TimeSeries getLatestTimeSeries(List<TimeSeries> baselineTimeSeries) {
if (CollectionUtils.isNotEmpty(baselineTimeSeries)) {
if (baselineTimeSeries.size() > 1) {
TimeSeries latestTimeSeries = baselineTimeSeries.get(0);
Interval latestInterval = latestTimeSeries.getTimeSeriesInterval();
for (TimeSeries ts : baselineTimeSeries) {
Interval currentInterval = ts.getTimeSeriesInterval();
if (latestInterval.getStartMillis() < currentInterval.getStartMillis()) {
latestTimeSeries = ts;
latestInterval = currentInterval;
}
}
return latestTimeSeries;
} else {
return baselineTimeSeries.get(0);
}
} else {
return null;
}
}
use of org.joda.time.Interval in project pinot by linkedin.
the class MovingAverageSmoothingFunction method transform.
/**
* Smooths the given time series using moving average.
*
* If the input time series is shorter than the moving average window size, then this method
* does not apply smoothing on the time series, i.e., it returns the original time series.
*
* The transformed time series is shorten by the size of the moving average window in
* comparison to the original time series. For instance, if there are 10 consecutive data points
* the a time series and the window size for moving average is 2, then the transformed time series
* contains only 9 consecutive data points; The first data points has no other data point to
* average and thus it is discarded.
*
* @param timeSeries the time series that provides the data points to be transformed.
* @param anomalyDetectionContext the anomaly detection context that could provide additional
* information for the transformation.
* @return a time series that is smoothed using moving average.
*/
@Override
public TimeSeries transform(TimeSeries timeSeries, AnomalyDetectionContext anomalyDetectionContext) {
Interval timeSeriesInterval = timeSeries.getTimeSeriesInterval();
long startTime = timeSeriesInterval.getStartMillis();
long endTime = timeSeriesInterval.getEndMillis();
long bucketSizeInMillis = anomalyDetectionContext.getBucketSizeInMS();
int movingAverageWindowSize = Integer.valueOf(getProperties().getProperty(MOVING_AVERAGE_SMOOTHING_WINDOW_SIZE));
// Check if the moving average window size is larger than the time series itself
long transformedStartTime = startTime + bucketSizeInMillis * (movingAverageWindowSize - 1);
if (transformedStartTime > endTime) {
String metricName = anomalyDetectionContext.getAnomalyDetectionFunction().getSpec().getTopicMetric();
DimensionMap dimensionMap = anomalyDetectionContext.getTimeSeriesKey().getDimensionMap();
LOGGER.warn("Input time series (Metric:{}, Dimension:{}) is shorter than the moving average " + "smoothing window; therefore, smoothing is not applied on this time series.", metricName, dimensionMap);
return timeSeries;
}
TimeSeries transformedTimeSeries = new TimeSeries();
Interval transformedInterval = new Interval(transformedStartTime, endTime);
transformedTimeSeries.setTimeSeriesInterval(transformedInterval);
for (long timeKeyToTransform : timeSeries.timestampSet()) {
if (!transformedInterval.contains(timeKeyToTransform)) {
continue;
}
double sum = 0d;
int count = 0;
for (int i = 0; i < movingAverageWindowSize; ++i) {
long timeKey = timeKeyToTransform - bucketSizeInMillis * i;
if (timeSeries.hasTimestamp(timeKey)) {
sum += timeSeries.get(timeKey);
++count;
}
}
// count is at least one due to the existence of timeKeyToTransform
double average = sum / count;
transformedTimeSeries.set(timeKeyToTransform, average);
}
return transformedTimeSeries;
}
Aggregations