Search in sources :

Example 1 with AlertFilter

use of com.linkedin.thirdeye.detector.email.filter.AlertFilter in project pinot by linkedin.

the class DetectionJobResource method tuneAlertFilter.

/**
   *
   * @param id anomaly function id
   * @param startTime start time of anomalies to tune alert filter
   * @param endTime end time of anomalies to tune alert filter
   * @param autoTuneType the type of auto tune to invoke (default is "AUTOTUNE")
   * @return HTTP response of request: string of alert filter
   */
@POST
@Path("/autotune/filter/{functionId}")
public Response tuneAlertFilter(@PathParam("functionId") long id, @QueryParam("startTime") long startTime, @QueryParam("endTime") long endTime, @QueryParam("autoTuneType") String autoTuneType) {
    // get anomalies by function id, start time and end time
    AnomalyFunctionDTO anomalyFunctionSpec = DAO_REGISTRY.getAnomalyFunctionDAO().findById(id);
    AnomalyFunctionManager anomalyFunctionDAO = DAO_REGISTRY.getAnomalyFunctionDAO();
    MergedAnomalyResultManager anomalyMergedResultDAO = DAO_REGISTRY.getMergedAnomalyResultDAO();
    List<MergedAnomalyResultDTO> anomalyResultDTOS = anomalyMergedResultDAO.findByStartTimeInRangeAndFunctionId(startTime, endTime, id);
    // create alert filter and evaluator
    AlertFilter alertFilter = alertFilterFactory.fromSpec(anomalyFunctionSpec.getAlertFilter());
    AlertFilterEvaluationUtil evaluator = new AlertFilterEvaluationUtil(alertFilter);
    // create alert filter auto tune
    AlertFilterAutoTune alertFilterAutotune = alertFilterAutotuneFactory.fromSpec(autoTuneType);
    LOG.info("initiated alertFilterAutoTune of Type {}", alertFilterAutotune.getClass().toString());
    try {
        //evaluate current alert filter (calculate current precision and recall)
        evaluator.updatePrecisionAndRecall(anomalyResultDTOS);
        LOG.info("AlertFilter of Type {}, has been evaluated with precision: {}, recall: {}", alertFilter.getClass().toString(), evaluator.getPrecision(), evaluator.getRecall());
        // get tuned alert filter
        Map<String, String> tunedAlertFilter = alertFilterAutotune.tuneAlertFilter(anomalyResultDTOS, evaluator.getPrecision(), evaluator.getRecall());
        LOG.info("tuned AlertFilter");
        // otherwise do nothing and return alert filter
        if (alertFilterAutotune.isUpdated()) {
            anomalyFunctionSpec.setAlertFilter(tunedAlertFilter);
            anomalyFunctionDAO.update(anomalyFunctionSpec);
            LOG.info("Model has been updated");
        } else {
            LOG.info("Model hasn't been updated because tuned model cannot beat original model");
        }
    } catch (Exception e) {
        LOG.warn("AutoTune throws exception due to: {}", e.getMessage());
    }
    return Response.ok(alertFilterAutotune.isUpdated()).build();
}
Also used : AnomalyFunctionManager(com.linkedin.thirdeye.datalayer.bao.AnomalyFunctionManager) AlertFilterAutoTune(com.linkedin.thirdeye.anomalydetection.alertFilterAutotune.AlertFilterAutoTune) MergedAnomalyResultDTO(com.linkedin.thirdeye.datalayer.dto.MergedAnomalyResultDTO) AlertFilter(com.linkedin.thirdeye.detector.email.filter.AlertFilter) AnomalyFunctionDTO(com.linkedin.thirdeye.datalayer.dto.AnomalyFunctionDTO) MergedAnomalyResultManager(com.linkedin.thirdeye.datalayer.bao.MergedAnomalyResultManager) AlertFilterEvaluationUtil(com.linkedin.thirdeye.detector.email.filter.AlertFilterEvaluationUtil) NullArgumentException(org.apache.commons.lang.NullArgumentException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 2 with AlertFilter

use of com.linkedin.thirdeye.detector.email.filter.AlertFilter in project pinot by linkedin.

the class DetectionJobResource method evaluateAlertFilterByFunctionId.

/**
   * The endpoint to evaluate alert filter
   * @param id: function ID
   * @param startTime: startTime of merged anomaly
   * @param endTime: endTime of merged anomaly
   * @return feedback summary, precision and recall as json object
   * @throws Exception when data has no positive label or model has no positive prediction
   */
@POST
@Path("/eval/filter/{functionId}")
public Response evaluateAlertFilterByFunctionId(@PathParam("functionId") long id, @QueryParam("startTime") long startTime, @QueryParam("endTime") long endTime) {
    // get anomalies by function id, start time and end time
    AnomalyFunctionDTO anomalyFunctionSpec = DAO_REGISTRY.getAnomalyFunctionDAO().findById(id);
    MergedAnomalyResultManager anomalyMergedResultDAO = DAO_REGISTRY.getMergedAnomalyResultDAO();
    List<MergedAnomalyResultDTO> anomalyResultDTOS = anomalyMergedResultDAO.findByStartTimeInRangeAndFunctionId(startTime, endTime, id);
    // create alert filter and evaluator
    AlertFilter alertFilter = alertFilterFactory.fromSpec(anomalyFunctionSpec.getAlertFilter());
    AlertFilterEvaluationUtil evaluator = new AlertFilterEvaluationUtil(alertFilter);
    try {
        //evaluate current alert filter (calculate current precision and recall)
        evaluator.updatePrecisionAndRecall(anomalyResultDTOS);
        LOG.info("AlertFilter of Type {}, has been evaluated with precision: {}, recall:{}", alertFilter.getClass().toString(), evaluator.getPrecision(), evaluator.getRecall());
    } catch (Exception e) {
        LOG.warn("Updating precision and recall failed because: {}", e.getMessage());
    }
    // get anomaly summary from merged anomaly results
    evaluator.updateFeedbackSummary(anomalyResultDTOS);
    return Response.ok(evaluator.toProperties().toString()).build();
}
Also used : MergedAnomalyResultDTO(com.linkedin.thirdeye.datalayer.dto.MergedAnomalyResultDTO) AlertFilter(com.linkedin.thirdeye.detector.email.filter.AlertFilter) AnomalyFunctionDTO(com.linkedin.thirdeye.datalayer.dto.AnomalyFunctionDTO) MergedAnomalyResultManager(com.linkedin.thirdeye.datalayer.bao.MergedAnomalyResultManager) AlertFilterEvaluationUtil(com.linkedin.thirdeye.detector.email.filter.AlertFilterEvaluationUtil) NullArgumentException(org.apache.commons.lang.NullArgumentException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 3 with AlertFilter

use of com.linkedin.thirdeye.detector.email.filter.AlertFilter in project pinot by linkedin.

the class AlertFilterHelper method applyFiltrationRule.

/**
   * Each function has a filtration rule which let alert module decide if an anomaly should be
   * included in the alert email. This method applies respective filtration rule on list of
   * anomalies.
   *
   * @param results
   *
   * @return
   */
public static List<MergedAnomalyResultDTO> applyFiltrationRule(List<MergedAnomalyResultDTO> results, AlertFilterFactory alertFilterFactory) {
    if (results.size() == 0) {
        return results;
    }
    // Function ID to Alert Filter
    Map<Long, AlertFilter> functionAlertFilter = new HashMap<>();
    List<MergedAnomalyResultDTO> qualifiedAnomalies = new ArrayList<>();
    for (MergedAnomalyResultDTO result : results) {
        // Lazy initiates alert filter for anomalies of the same anomaly function
        AnomalyFunctionDTO anomalyFunctionSpec = result.getFunction();
        long functionId = anomalyFunctionSpec.getId();
        AlertFilter alertFilter = functionAlertFilter.get(functionId);
        if (alertFilter == null) {
            // Get filtration rule from anomaly function configuration
            alertFilter = alertFilterFactory.fromSpec(anomalyFunctionSpec.getAlertFilter());
            functionAlertFilter.put(functionId, alertFilter);
            LOG.info("Using filter {} for anomaly function {} (dataset: {}, topic metric: {})", alertFilter, functionId, anomalyFunctionSpec.getCollection(), anomalyFunctionSpec.getTopicMetric());
        }
        if (alertFilter.isQualified(result)) {
            qualifiedAnomalies.add(result);
        }
    }
    LOG.info("Found [{}] anomalies qualified to alert after applying filtration rule on [{}] anomalies", qualifiedAnomalies.size(), results.size());
    return qualifiedAnomalies;
}
Also used : HashMap(java.util.HashMap) MergedAnomalyResultDTO(com.linkedin.thirdeye.datalayer.dto.MergedAnomalyResultDTO) AlertFilter(com.linkedin.thirdeye.detector.email.filter.AlertFilter) ArrayList(java.util.ArrayList) AnomalyFunctionDTO(com.linkedin.thirdeye.datalayer.dto.AnomalyFunctionDTO)

Aggregations

AnomalyFunctionDTO (com.linkedin.thirdeye.datalayer.dto.AnomalyFunctionDTO)3 MergedAnomalyResultDTO (com.linkedin.thirdeye.datalayer.dto.MergedAnomalyResultDTO)3 AlertFilter (com.linkedin.thirdeye.detector.email.filter.AlertFilter)3 MergedAnomalyResultManager (com.linkedin.thirdeye.datalayer.bao.MergedAnomalyResultManager)2 AlertFilterEvaluationUtil (com.linkedin.thirdeye.detector.email.filter.AlertFilterEvaluationUtil)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 NullArgumentException (org.apache.commons.lang.NullArgumentException)2 AlertFilterAutoTune (com.linkedin.thirdeye.anomalydetection.alertFilterAutotune.AlertFilterAutoTune)1 AnomalyFunctionManager (com.linkedin.thirdeye.datalayer.bao.AnomalyFunctionManager)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1