Search in sources :

Example 1 with AnomalyFunctionManager

use of com.linkedin.thirdeye.datalayer.bao.AnomalyFunctionManager 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)

Aggregations

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