Search in sources :

Example 11 with MetricConfigDTO

use of com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO in project pinot by linkedin.

the class DataResource method getAnomalySummary.

@GET
@Path("dashboard/anomalysummary")
public Map<String, List<AnomaliesSummary>> getAnomalySummary(@QueryParam("dashboard") String dashboard, @QueryParam("timeRanges") String timeRanges) {
    List<Long> metricIds = getMetricIdsByDashboard(dashboard);
    List<String> timeRangesList = Lists.newArrayList(timeRanges.split(","));
    Map<String, Long> timeRangeToDurationMap = new HashMap<>();
    for (String timeRange : timeRangesList) {
        String[] tokens = timeRange.split("_");
        long duration = TimeUnit.MILLISECONDS.convert(Long.valueOf(tokens[0]), TimeUnit.valueOf(tokens[1]));
        timeRangeToDurationMap.put(timeRange, duration);
    }
    Map<String, List<AnomaliesSummary>> metricAliasToAnomaliesSummariesMap = new HashMap<>();
    for (Long metricId : metricIds) {
        List<AnomaliesSummary> summaries = new ArrayList<>();
        MetricConfigDTO metricConfig = metricConfigDAO.findById(metricId);
        String metricAlias = metricConfig.getAlias();
        String dataset = metricConfig.getDataset();
        long endTime = Utils.getMaxDataTimeForDataset(dataset);
        for (String timeRange : timeRangesList) {
            long startTime = endTime - timeRangeToDurationMap.get(timeRange);
            AnomaliesSummary summary = anomaliesResoure.getAnomalyCountForMetricInRange(metricId, startTime, endTime);
            summaries.add(summary);
        }
        metricAliasToAnomaliesSummariesMap.put(metricAlias, summaries);
    }
    return metricAliasToAnomaliesSummariesMap;
}
Also used : MetricConfigDTO(com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) AnomaliesSummary(com.linkedin.thirdeye.dashboard.resources.v2.pojo.AnomaliesSummary) List(java.util.List) ArrayList(java.util.ArrayList) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 12 with MetricConfigDTO

use of com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO in project pinot by linkedin.

the class MetricConfigResource method populateMetricConfig.

private void populateMetricConfig(MetricConfigDTO metricConfigDTO, String dataset, String name, String metricType, boolean active, boolean derived, String derivedFunctionType, String numerator, String denominator, String derivedMetricExpression, boolean inverseMetric, String cellSizeExpression, Double rollupThreshold) {
    metricConfigDTO.setDataset(dataset);
    metricConfigDTO.setName(name);
    metricConfigDTO.setAlias(ThirdEyeUtils.constructMetricAlias(dataset, name));
    metricConfigDTO.setDatatype(MetricType.valueOf(metricType));
    metricConfigDTO.setActive(active);
    // optional ones
    metricConfigDTO.setCellSizeExpression(cellSizeExpression);
    metricConfigDTO.setInverseMetric(inverseMetric);
    if (rollupThreshold != null) {
        metricConfigDTO.setRollupThreshold(rollupThreshold);
    }
    // handle derived
    if (derived) {
        if (StringUtils.isEmpty(derivedMetricExpression) && numerator != null && denominator != null) {
            MetricConfigDTO numMetricConfigDTO = metricConfigDao.findByAliasAndDataset(numerator, dataset);
            MetricConfigDTO denMetricConfigDTO = metricConfigDao.findByAliasAndDataset(denominator, dataset);
            if ("RATIO".equals(derivedFunctionType)) {
                derivedMetricExpression = String.format("id%s/id%s", numMetricConfigDTO.getId(), denMetricConfigDTO.getId());
            } else if ("PERCENT".equals(derivedFunctionType)) {
                derivedMetricExpression = String.format("id%s*100/id%s", numMetricConfigDTO.getId(), denMetricConfigDTO.getId());
            }
        }
        metricConfigDTO.setDerived(derived);
        metricConfigDTO.setDerivedMetricExpression(derivedMetricExpression);
    }
}
Also used : MetricConfigDTO(com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO)

Example 13 with MetricConfigDTO

use of com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO in project pinot by linkedin.

the class MetricConfigResource method updateMetricConfig.

@GET
@Path("/update")
public String updateMetricConfig(@NotNull @QueryParam("id") long metricConfigId, @QueryParam("dataset") String dataset, @QueryParam("name") String name, @QueryParam("datatype") String metricType, @QueryParam("active") boolean active, @QueryParam("derived") boolean derived, @QueryParam("derivedFunctionType") String derivedFunctionType, @QueryParam("numerator") String numerator, @QueryParam("denominator") String denominator, @QueryParam("derivedMetricExpression") String derivedMetricExpression, @QueryParam("inverseMetric") boolean inverseMetric, @QueryParam("cellSizeExpression") String cellSizeExpression, @QueryParam("rollupThreshold") Double rollupThreshold) {
    try {
        MetricConfigDTO metricConfigDTO = metricConfigDao.findById(metricConfigId);
        populateMetricConfig(metricConfigDTO, dataset, name, metricType, active, derived, derivedFunctionType, numerator, denominator, derivedMetricExpression, inverseMetric, cellSizeExpression, rollupThreshold);
        int numRowsUpdated = metricConfigDao.update(metricConfigDTO);
        if (numRowsUpdated == 1) {
            return JsonResponseUtil.buildResponseJSON(metricConfigDTO).toString();
        } else {
            return JsonResponseUtil.buildErrorResponseJSON("Failed to update metric id:" + metricConfigId).toString();
        }
    } catch (Exception e) {
        return JsonResponseUtil.buildErrorResponseJSON("Failed to update metric id:" + metricConfigId + ". Exception:" + e.getMessage()).toString();
    }
}
Also used : MetricConfigDTO(com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 14 with MetricConfigDTO

use of com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO in project pinot by linkedin.

the class MetricConfigResource method createMetricConfig.

@GET
@Path("/create")
public String createMetricConfig(@QueryParam("dataset") String dataset, @QueryParam("name") String name, @QueryParam("datatype") String metricType, @QueryParam("active") boolean active, @QueryParam("derived") boolean derived, @QueryParam("derivedFunctionType") String derivedFunctionType, @QueryParam("numerator") String numerator, @QueryParam("denominator") String denominator, @QueryParam("derivedMetricExpression") String derivedMetricExpression, @QueryParam("inverseMetric") boolean inverseMetric, @QueryParam("cellSizeExpression") String cellSizeExpression, @QueryParam("rollupThreshold") Double rollupThreshold) {
    try {
        MetricConfigDTO metricConfigDTO = new MetricConfigDTO();
        populateMetricConfig(metricConfigDTO, dataset, name, metricType, active, derived, derivedFunctionType, numerator, denominator, derivedMetricExpression, inverseMetric, cellSizeExpression, rollupThreshold);
        Long id = metricConfigDao.save(metricConfigDTO);
        metricConfigDTO.setId(id);
        return JsonResponseUtil.buildResponseJSON(metricConfigDTO).toString();
    } catch (Exception e) {
        LOG.warn("Failed to create metric:{}", name, e);
        return JsonResponseUtil.buildErrorResponseJSON("Failed to create metric:" + name + " Message:" + e.getMessage()).toString();
    }
}
Also used : MetricConfigDTO(com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 15 with MetricConfigDTO

use of com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO in project pinot by linkedin.

the class DashboardResource method getDashboardData.

@GET
@Path(value = "/data/customDashboard")
@Produces(MediaType.APPLICATION_JSON)
public String getDashboardData(@QueryParam("dataset") String collection, @QueryParam("dashboard") String dashboardName, @QueryParam("filters") String filterJson, @QueryParam("timeZone") @DefaultValue(DEFAULT_TIMEZONE_ID) String timeZone, @QueryParam("baselineStart") Long baselineStart, @QueryParam("baselineEnd") Long baselineEnd, @QueryParam("currentStart") Long currentStart, @QueryParam("currentEnd") Long currentEnd, @QueryParam("compareMode") String compareMode, @QueryParam("aggTimeGranularity") String aggTimeGranularity) {
    try {
        TabularViewRequest request = new TabularViewRequest();
        request.setCollection(collection);
        List<MetricExpression> metricExpressions = new ArrayList<>();
        DashboardConfigDTO dashboardConfig = dashboardConfigDAO.findByName(dashboardName);
        List<Long> metricIds = dashboardConfig.getMetricIds();
        for (Long metricId : metricIds) {
            MetricConfigDTO metricConfig = metricConfigDAO.findById(metricId);
            MetricExpression metricExpression = ThirdEyeUtils.getMetricExpressionFromMetricConfig(metricConfig);
            metricExpressions.add(metricExpression);
        }
        request.setMetricExpressions(metricExpressions);
        long maxDataTime = collectionMaxDataTimeCache.get(collection);
        if (currentEnd > maxDataTime) {
            long delta = currentEnd - maxDataTime;
            currentEnd = currentEnd - delta;
            baselineEnd = baselineEnd - delta;
        }
        // The input start and end time (i.e., currentStart, currentEnd, baselineStart, and
        // baselineEnd) are given in millisecond since epoch, which is timezone insensitive. On the
        // other hand, the start and end time of the request to be sent to backend database (e.g.,
        // Pinot) could be converted to SimpleDateFormat, which is timezone sensitive. Therefore,
        // we need to store user's start and end time in DateTime objects with data's timezone
        // in order to ensure that the conversion to SimpleDateFormat is always correct regardless
        // user and server's timezone, including daylight saving time.
        DateTimeZone timeZoneForCollection = Utils.getDataTimeZone(collection);
        request.setBaselineStart(new DateTime(baselineStart, timeZoneForCollection));
        request.setBaselineEnd(new DateTime(baselineEnd, timeZoneForCollection));
        request.setCurrentStart(new DateTime(currentStart, timeZoneForCollection));
        request.setCurrentEnd(new DateTime(currentEnd, timeZoneForCollection));
        if (filterJson != null && !filterJson.isEmpty()) {
            filterJson = URLDecoder.decode(filterJson, "UTF-8");
            request.setFilters(ThirdEyeUtils.convertToMultiMap(filterJson));
        }
        request.setTimeGranularity(Utils.getAggregationTimeGranularity(aggTimeGranularity, collection));
        TabularViewHandler handler = new TabularViewHandler(queryCache);
        String jsonResponse = null;
        TabularViewResponse response = handler.process(request);
        jsonResponse = OBJECT_MAPPER.enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(response);
        LOG.debug("customDashboard response {}", jsonResponse);
        return jsonResponse;
    } catch (Exception e) {
        LOG.error("Exception while processing /data/tabular call", e);
        return "{\"ERROR\": + " + e.getMessage() + "}";
    }
}
Also used : MetricConfigDTO(com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO) TabularViewHandler(com.linkedin.thirdeye.dashboard.views.tabular.TabularViewHandler) ArrayList(java.util.ArrayList) TabularViewResponse(com.linkedin.thirdeye.dashboard.views.tabular.TabularViewResponse) MetricExpression(com.linkedin.thirdeye.client.MetricExpression) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) JSONException(org.json.JSONException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DashboardConfigDTO(com.linkedin.thirdeye.datalayer.dto.DashboardConfigDTO) TabularViewRequest(com.linkedin.thirdeye.dashboard.views.tabular.TabularViewRequest) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

MetricConfigDTO (com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO)54 ArrayList (java.util.ArrayList)22 Path (javax.ws.rs.Path)18 GET (javax.ws.rs.GET)17 HashMap (java.util.HashMap)13 DatasetConfigDTO (com.linkedin.thirdeye.datalayer.dto.DatasetConfigDTO)9 DashboardConfigDTO (com.linkedin.thirdeye.datalayer.dto.DashboardConfigDTO)8 WebApplicationException (javax.ws.rs.WebApplicationException)8 MetricExpression (com.linkedin.thirdeye.client.MetricExpression)7 MetricDataset (com.linkedin.thirdeye.client.cache.MetricDataset)7 DateTime (org.joda.time.DateTime)7 DateTimeZone (org.joda.time.DateTimeZone)7 MetricConfigBean (com.linkedin.thirdeye.datalayer.pojo.MetricConfigBean)6 Predicate (com.linkedin.thirdeye.datalayer.util.Predicate)5 IOException (java.io.IOException)5 LinkedHashMap (java.util.LinkedHashMap)5 Test (org.testng.annotations.Test)5 MetricFieldSpec (com.linkedin.pinot.common.data.MetricFieldSpec)4 TimeGranularity (com.linkedin.thirdeye.api.TimeGranularity)4 ExecutionException (java.util.concurrent.ExecutionException)4