use of com.linkedin.thirdeye.datalayer.dto.RawAnomalyResultDTO in project pinot by linkedin.
the class AnomalyResource method viewRawAnomaliesInRange.
//View raw anomalies for collection
@GET
@Path("/raw-anomalies/view")
@Produces(MediaType.APPLICATION_JSON)
public String viewRawAnomaliesInRange(@QueryParam("functionId") String functionId, @QueryParam("dataset") String dataset, @QueryParam("startTimeIso") String startTimeIso, @QueryParam("endTimeIso") String endTimeIso, @QueryParam("metric") String metric) throws JsonProcessingException {
if (StringUtils.isBlank(functionId) && StringUtils.isBlank(dataset)) {
throw new IllegalArgumentException("must provide dataset or functionId");
}
DateTime endTime = DateTime.now();
if (StringUtils.isNotEmpty(endTimeIso)) {
endTime = ISODateTimeFormat.dateTimeParser().parseDateTime(endTimeIso);
}
DateTime startTime = endTime.minusDays(7);
if (StringUtils.isNotEmpty(startTimeIso)) {
startTime = ISODateTimeFormat.dateTimeParser().parseDateTime(startTimeIso);
}
List<RawAnomalyResultDTO> rawAnomalyResults = new ArrayList<>();
if (StringUtils.isNotBlank(functionId)) {
rawAnomalyResults = rawAnomalyResultDAO.findAllByTimeAndFunctionId(startTime.getMillis(), endTime.getMillis(), Long.valueOf(functionId));
} else if (StringUtils.isNotBlank(dataset)) {
List<AnomalyFunctionDTO> anomalyFunctions = anomalyFunctionDAO.findAllByCollection(dataset);
List<Long> functionIds = new ArrayList<>();
for (AnomalyFunctionDTO anomalyFunction : anomalyFunctions) {
if (StringUtils.isNotBlank(metric) && !anomalyFunction.getTopicMetric().equals(metric)) {
continue;
}
functionIds.add(anomalyFunction.getId());
}
for (Long id : functionIds) {
rawAnomalyResults.addAll(rawAnomalyResultDAO.findAllByTimeAndFunctionId(startTime.getMillis(), endTime.getMillis(), id));
}
}
String response = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(rawAnomalyResults);
return response;
}
use of com.linkedin.thirdeye.datalayer.dto.RawAnomalyResultDTO in project pinot by linkedin.
the class OnboardResource method deleteRawResults.
// Delete raw anomaly results from rawResultDAO
private int deleteRawResults(List<RawAnomalyResultDTO> rawResults) {
LOG.info("Deleting raw anomaly results...");
int rawAnomaliesDeleted = 0;
for (RawAnomalyResultDTO rawResult : rawResults) {
LOG.info("...Deleting raw anomaly result id {} for functionId {}", rawResult.getId(), rawResult.getFunctionId());
rawAnomalyResultDAO.delete(rawResult);
rawAnomaliesDeleted++;
}
return rawAnomaliesDeleted;
}
use of com.linkedin.thirdeye.datalayer.dto.RawAnomalyResultDTO in project pinot by linkedin.
the class MergedAnomalyResultManagerImpl method convertMergeAnomalyDTO2Bean.
protected MergedAnomalyResultBean convertMergeAnomalyDTO2Bean(MergedAnomalyResultDTO entity) {
MergedAnomalyResultBean bean = convertDTO2Bean(entity, MergedAnomalyResultBean.class);
if (entity.getFeedback() != null && entity.getFeedback().getId() != null) {
bean.setAnomalyFeedbackId(entity.getFeedback().getId());
}
if (entity.getFunction() != null) {
bean.setFunctionId(entity.getFunction().getId());
}
if (entity.getAnomalyResults() != null && !entity.getAnomalyResults().isEmpty()) {
List<Long> rawAnomalyIds = new ArrayList<>();
for (RawAnomalyResultDTO rawAnomalyDTO : entity.getAnomalyResults()) {
rawAnomalyIds.add(rawAnomalyDTO.getId());
}
bean.setRawAnomalyIdList(rawAnomalyIds);
}
return bean;
}
use of com.linkedin.thirdeye.datalayer.dto.RawAnomalyResultDTO in project pinot by linkedin.
the class TestWeekOverWeekRuleFunction method analyzeWoW.
@Test(dataProvider = "timeSeriesDataProvider")
public void analyzeWoW(Properties properties, TimeSeriesKey timeSeriesKey, long bucketSizeInMs, TimeSeries observedTimeSeries, List<TimeSeries> baselines) throws Exception {
AnomalyDetectionContext anomalyDetectionContext = new AnomalyDetectionContext();
anomalyDetectionContext.setBucketSizeInMS(bucketSizeInMs);
// Append properties for anomaly function specific setting
properties.put(WeekOverWeekRuleFunction.BASELINE, "w/w");
properties.put(SimpleThresholdDetectionModel.CHANGE_THRESHOLD, "-0.2");
// Create anomaly function spec
AnomalyFunctionDTO functionSpec = new AnomalyFunctionDTO();
functionSpec.setMetric(mainMetric);
functionSpec.setProperties(toString(properties));
WeekOverWeekRuleFunction function = new WeekOverWeekRuleFunction();
function.init(functionSpec);
anomalyDetectionContext.setAnomalyDetectionFunction(function);
anomalyDetectionContext.setCurrent(mainMetric, observedTimeSeries);
List<TimeSeries> singleBaseline = new ArrayList<>();
singleBaseline.add(baselines.get(0));
anomalyDetectionContext.setBaselines(mainMetric, singleBaseline);
anomalyDetectionContext.setTimeSeriesKey(timeSeriesKey);
List<RawAnomalyResultDTO> rawAnomalyResults = function.analyze(anomalyDetectionContext);
compareWoWRawAnomalies(rawAnomalyResults);
// Test data model
List<Interval> expectedDataRanges = new ArrayList<>();
expectedDataRanges.add(new Interval(observedStartTime, observedStartTime + bucketMillis * 5));
expectedDataRanges.add(new Interval(observedStartTime - oneWeekInMillis, observedStartTime + bucketMillis * 5 - oneWeekInMillis));
List<Interval> actualDataRanges = function.getDataModel().getAllDataIntervals(observedStartTime, observedStartTime + bucketMillis * 5);
Assert.assertEquals(actualDataRanges, expectedDataRanges);
}
use of com.linkedin.thirdeye.datalayer.dto.RawAnomalyResultDTO in project pinot by linkedin.
the class TestWeekOverWeekRuleFunction method recomputeMergedAnomalyWeight.
@Test(dataProvider = "timeSeriesDataProvider")
public void recomputeMergedAnomalyWeight(Properties properties, TimeSeriesKey timeSeriesKey, long bucketSizeInMs, TimeSeries observedTimeSeries, List<TimeSeries> baselines) throws Exception {
// Expected RawAnomalies without smoothing
List<RawAnomalyResultDTO> expectedRawAnomalies = new ArrayList<>();
RawAnomalyResultDTO rawAnomaly1 = new RawAnomalyResultDTO();
rawAnomaly1.setStartTime(observedStartTime + bucketMillis * 2);
rawAnomaly1.setEndTime(observedStartTime + bucketMillis * 3);
rawAnomaly1.setWeight(0.3d);
rawAnomaly1.setScore(15d);
expectedRawAnomalies.add(rawAnomaly1);
RawAnomalyResultDTO rawAnomaly2 = new RawAnomalyResultDTO();
rawAnomaly2.setStartTime(observedStartTime + bucketMillis * 3);
rawAnomaly2.setEndTime(observedStartTime + bucketMillis * 4);
rawAnomaly2.setWeight(0.22727272727272727);
rawAnomaly2.setScore(15d);
expectedRawAnomalies.add(rawAnomaly2);
AnomalyDetectionContext anomalyDetectionContext = new AnomalyDetectionContext();
anomalyDetectionContext.setBucketSizeInMS(bucketSizeInMs);
// Append properties for anomaly function specific setting
properties.put(WeekOverWeekRuleFunction.BASELINE, "w/2wAvg");
properties.put(SimpleThresholdDetectionModel.CHANGE_THRESHOLD, "0.2");
// Create anomaly function spec
AnomalyFunctionDTO functionSpec = new AnomalyFunctionDTO();
functionSpec.setMetric(mainMetric);
functionSpec.setProperties(toString(properties));
WeekOverWeekRuleFunction function = new WeekOverWeekRuleFunction();
function.init(functionSpec);
anomalyDetectionContext.setAnomalyDetectionFunction(function);
anomalyDetectionContext.setCurrent(mainMetric, observedTimeSeries);
anomalyDetectionContext.setBaselines(mainMetric, baselines);
anomalyDetectionContext.setTimeSeriesKey(timeSeriesKey);
MergedAnomalyResultDTO mergedAnomaly = new MergedAnomalyResultDTO();
mergedAnomaly.setStartTime(expectedRawAnomalies.get(0).getStartTime());
mergedAnomaly.setEndTime(expectedRawAnomalies.get(1).getEndTime());
mergedAnomaly.setAnomalyResults(expectedRawAnomalies);
function.updateMergedAnomalyInfo(anomalyDetectionContext, mergedAnomaly);
// Test weight; weight is the percentage change between the sums of observed values and
// expected values, respectively. Note that expected values are generated by the trained model,
// which takes as input one or many baseline time series.
final long oneWeekInMillis = TimeUnit.DAYS.toMillis(7);
double observedTotal = 0d;
double baselineTotal = 0d;
int bucketCount = 0;
Interval interval = new Interval(mergedAnomaly.getStartTime(), mergedAnomaly.getEndTime());
TimeSeries observedTS = anomalyDetectionContext.getTransformedCurrent(mainMetric);
List<TimeSeries> baselineTSs = anomalyDetectionContext.getTransformedBaselines(mainMetric);
for (long timestamp : observedTS.timestampSet()) {
if (interval.contains(timestamp)) {
++bucketCount;
observedTotal += observedTS.get(timestamp);
for (int i = 0; i < baselineTSs.size(); ++i) {
TimeSeries baselineTS = baselineTSs.get(i);
long baseTimeStamp = timestamp - oneWeekInMillis * (i + 1);
baselineTotal += baselineTS.get(baseTimeStamp);
}
}
}
baselineTotal /= baselineTSs.size();
// Compare anomaly weight, avg. current, avg. baseline, score, etc
double expectedWeight = (observedTotal - baselineTotal) / baselineTotal;
Assert.assertEquals(mergedAnomaly.getWeight(), expectedWeight, EPSILON);
double avgCurrent = observedTotal / bucketCount;
Assert.assertEquals(mergedAnomaly.getAvgCurrentVal(), avgCurrent, EPSILON);
double avgBaseline = baselineTotal / bucketCount;
Assert.assertEquals(mergedAnomaly.getAvgBaselineVal(), avgBaseline, EPSILON);
// Test Score; score is the average of all raw anomalies' score
double expectedScore = 0d;
for (RawAnomalyResultDTO rawAnomaly : expectedRawAnomalies) {
expectedScore += rawAnomaly.getScore();
}
expectedScore /= expectedRawAnomalies.size();
Assert.assertEquals(mergedAnomaly.getScore(), expectedScore, EPSILON);
}
Aggregations