@Test public void testGenerateDetectionConfig() throws Exception { String payload = IOUtils.toString(this.getClass().getResourceAsStream("payload-good.json")); JsonNode payloadNode = this.objectMapper.readTree(payload); DatasetConfigDTO datasetConfigDTO = this.anomalyDetectionResource.generateDatasetConfig(payloadNode, this.suffix); MetricConfigDTO metricConfigDTO = this.anomalyDetectionResource.generateMetricConfig(payloadNode, this.suffix); DetectionConfigDTO detectionConfigDTO = this.anomalyDetectionResource .generateDetectionConfig(payloadNode, this.suffix, datasetConfigDTO, metricConfigDTO, 0, 0); Assert.assertEquals(detectionConfigDTO.getName(), DEFAULT_DETECTION_NAME + this.suffix); }
DetectionConfigDTO generateDetectionConfig(JsonNode payloadNode, String suffix, DatasetConfigDTO datasetConfigDTO, MetricConfigDTO metricConfigDTO, long start, long end) throws ConfigValidationException { DetectionConfigDTO detectionConfigDTO; Map<String, Object> detectionYaml; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (payloadNode.has(DETECTION_FIELD)) { detectionYaml = ConfigUtils.getMap(yaml.load(payloadNode.get(DETECTION_FIELD).textValue())); } else { detectionYaml = ConfigUtils.getMap(yaml.load(classLoader.getResourceAsStream(TEMPLATE_DETECTION_PATH))); } detectionYaml.put("detectionName", DEFAULT_DETECTION_NAME + suffix); detectionYaml.put("dataset", datasetConfigDTO.getName()); detectionYaml.put("metric", metricConfigDTO.getName()); detectionConfigDTO = new DetectionConfigTranslator(this.yaml.dump(detectionYaml), this.provider).translate(); detectionConfigDTO.setCron("0 0 0 1 1 ? 2200"); DetectionConfigTuner detectionTuner = new DetectionConfigTuner(detectionConfigDTO, provider); detectionConfigDTO = detectionTuner.tune(start, end); detectionValidator.semanticValidation(detectionConfigDTO); LOG.info("Created detection with config {}", detectionConfigDTO); return detectionConfigDTO; }
AnomalyDetectionResource { DetectionConfigDTO generateDetectionConfig(JsonNode payloadNode, String suffix, DatasetConfigDTO datasetConfigDTO, MetricConfigDTO metricConfigDTO, long start, long end) throws ConfigValidationException { DetectionConfigDTO detectionConfigDTO; Map<String, Object> detectionYaml; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (payloadNode.has(DETECTION_FIELD)) { detectionYaml = ConfigUtils.getMap(yaml.load(payloadNode.get(DETECTION_FIELD).textValue())); } else { detectionYaml = ConfigUtils.getMap(yaml.load(classLoader.getResourceAsStream(TEMPLATE_DETECTION_PATH))); } detectionYaml.put("detectionName", DEFAULT_DETECTION_NAME + suffix); detectionYaml.put("dataset", datasetConfigDTO.getName()); detectionYaml.put("metric", metricConfigDTO.getName()); detectionConfigDTO = new DetectionConfigTranslator(this.yaml.dump(detectionYaml), this.provider).translate(); detectionConfigDTO.setCron("0 0 0 1 1 ? 2200"); DetectionConfigTuner detectionTuner = new DetectionConfigTuner(detectionConfigDTO, provider); detectionConfigDTO = detectionTuner.tune(start, end); detectionValidator.semanticValidation(detectionConfigDTO); LOG.info("Created detection with config {}", detectionConfigDTO); return detectionConfigDTO; } }
AnomalyDetectionResource { DetectionConfigDTO generateDetectionConfig(JsonNode payloadNode, String suffix, DatasetConfigDTO datasetConfigDTO, MetricConfigDTO metricConfigDTO, long start, long end) throws ConfigValidationException { DetectionConfigDTO detectionConfigDTO; Map<String, Object> detectionYaml; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (payloadNode.has(DETECTION_FIELD)) { detectionYaml = ConfigUtils.getMap(yaml.load(payloadNode.get(DETECTION_FIELD).textValue())); } else { detectionYaml = ConfigUtils.getMap(yaml.load(classLoader.getResourceAsStream(TEMPLATE_DETECTION_PATH))); } detectionYaml.put("detectionName", DEFAULT_DETECTION_NAME + suffix); detectionYaml.put("dataset", datasetConfigDTO.getName()); detectionYaml.put("metric", metricConfigDTO.getName()); detectionConfigDTO = new DetectionConfigTranslator(this.yaml.dump(detectionYaml), this.provider).translate(); detectionConfigDTO.setCron("0 0 0 1 1 ? 2200"); DetectionConfigTuner detectionTuner = new DetectionConfigTuner(detectionConfigDTO, provider); detectionConfigDTO = detectionTuner.tune(start, end); detectionValidator.semanticValidation(detectionConfigDTO); LOG.info("Created detection with config {}", detectionConfigDTO); return detectionConfigDTO; } @Inject  AnomalyDetectionResource(UserDashboardResource userDashboardResource); }
AnomalyDetectionResource { DetectionConfigDTO generateDetectionConfig(JsonNode payloadNode, String suffix, DatasetConfigDTO datasetConfigDTO, MetricConfigDTO metricConfigDTO, long start, long end) throws ConfigValidationException { DetectionConfigDTO detectionConfigDTO; Map<String, Object> detectionYaml; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (payloadNode.has(DETECTION_FIELD)) { detectionYaml = ConfigUtils.getMap(yaml.load(payloadNode.get(DETECTION_FIELD).textValue())); } else { detectionYaml = ConfigUtils.getMap(yaml.load(classLoader.getResourceAsStream(TEMPLATE_DETECTION_PATH))); } detectionYaml.put("detectionName", DEFAULT_DETECTION_NAME + suffix); detectionYaml.put("dataset", datasetConfigDTO.getName()); detectionYaml.put("metric", metricConfigDTO.getName()); detectionConfigDTO = new DetectionConfigTranslator(this.yaml.dump(detectionYaml), this.provider).translate(); detectionConfigDTO.setCron("0 0 0 1 1 ? 2200"); DetectionConfigTuner detectionTuner = new DetectionConfigTuner(detectionConfigDTO, provider); detectionConfigDTO = detectionTuner.tune(start, end); detectionValidator.semanticValidation(detectionConfigDTO); LOG.info("Created detection with config {}", detectionConfigDTO); return detectionConfigDTO; } @Inject  AnomalyDetectionResource(UserDashboardResource userDashboardResource); @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation("Request an anomaly detection online task") Response onlineApi(
          @QueryParam("start") Long start,
          @QueryParam("end") Long end,
          @QueryParam("data-id") Long dataId,
          @ApiParam("jsonPayload") String payload,
          @Auth ThirdEyePrincipal principal); @POST @Path("/data") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation("Register ad hoc data used in online service and created dataset/metric configs") Response createOnlineData(@ApiParam("jsonPayload") String payload,
      @Auth ThirdEyePrincipal principal); @PUT @Path("/data/{data-id}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation("Update ad hoc data used in online service and corresponding dataset/metric configs") Response updateOnlineData(
      @ApiParam("jsonPayload") String payload,
      @PathParam("data-id") Long dataId,
      @Auth ThirdEyePrincipal principal); @DELETE @Path("/data/{data-id}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation("Delete ad hoc data used in online service and corresponding dataset/metric configs") Response deleteOnlineData(@PathParam("data-id") Long dataId); @GET @Path("/data/{data-id}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation("Get ad hoc data used in online service and corresponding dataset/metric configs") Response getOnlineData(@PathParam("data-id") Long dataId); @POST @Path("/tasks") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.TEXT_PLAIN) @ApiOperation("Submit an anomaly detection task") Response taskSubmitApi(
      @QueryParam("start") long start,
      @QueryParam("end") long end,
      @QueryParam("detection-name") String detectionName); @GET @Path("/task/{task-id}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.TEXT_PLAIN) @ApiOperation("Query a task status") Response taskStatusApi(@PathParam("task-id") long taskId); }
AnomalyDetectionResource { DetectionConfigDTO generateDetectionConfig(JsonNode payloadNode, String suffix, DatasetConfigDTO datasetConfigDTO, MetricConfigDTO metricConfigDTO, long start, long end) throws ConfigValidationException { DetectionConfigDTO detectionConfigDTO; Map<String, Object> detectionYaml; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (payloadNode.has(DETECTION_FIELD)) { detectionYaml = ConfigUtils.getMap(yaml.load(payloadNode.get(DETECTION_FIELD).textValue())); } else { detectionYaml = ConfigUtils.getMap(yaml.load(classLoader.getResourceAsStream(TEMPLATE_DETECTION_PATH))); } detectionYaml.put("detectionName", DEFAULT_DETECTION_NAME + suffix); detectionYaml.put("dataset", datasetConfigDTO.getName()); detectionYaml.put("metric", metricConfigDTO.getName()); detectionConfigDTO = new DetectionConfigTranslator(this.yaml.dump(detectionYaml), this.provider).translate(); detectionConfigDTO.setCron("0 0 0 1 1 ? 2200"); DetectionConfigTuner detectionTuner = new DetectionConfigTuner(detectionConfigDTO, provider); detectionConfigDTO = detectionTuner.tune(start, end); detectionValidator.semanticValidation(detectionConfigDTO); LOG.info("Created detection with config {}", detectionConfigDTO); return detectionConfigDTO; } @Inject  AnomalyDetectionResource(UserDashboardResource userDashboardResource); @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation("Request an anomaly detection online task") Response onlineApi(
          @QueryParam("start") Long start,
          @QueryParam("end") Long end,
          @QueryParam("data-id") Long dataId,
          @ApiParam("jsonPayload") String payload,
          @Auth ThirdEyePrincipal principal); @POST @Path("/data") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation("Register ad hoc data used in online service and created dataset/metric configs") Response createOnlineData(@ApiParam("jsonPayload") String payload,
      @Auth ThirdEyePrincipal principal); @PUT @Path("/data/{data-id}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation("Update ad hoc data used in online service and corresponding dataset/metric configs") Response updateOnlineData(
      @ApiParam("jsonPayload") String payload,
      @PathParam("data-id") Long dataId,
      @Auth ThirdEyePrincipal principal); @DELETE @Path("/data/{data-id}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation("Delete ad hoc data used in online service and corresponding dataset/metric configs") Response deleteOnlineData(@PathParam("data-id") Long dataId); @GET @Path("/data/{data-id}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation("Get ad hoc data used in online service and corresponding dataset/metric configs") Response getOnlineData(@PathParam("data-id") Long dataId); @POST @Path("/tasks") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.TEXT_PLAIN) @ApiOperation("Submit an anomaly detection task") Response taskSubmitApi(
      @QueryParam("start") long start,
      @QueryParam("end") long end,
      @QueryParam("detection-name") String detectionName); @GET @Path("/task/{task-id}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.TEXT_PLAIN) @ApiOperation("Query a task status") Response taskStatusApi(@PathParam("task-id") long taskId);  }