Search in sources :

Example 1 with TabularViewRequest

use of com.linkedin.thirdeye.dashboard.views.tabular.TabularViewRequest 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)

Example 2 with TabularViewRequest

use of com.linkedin.thirdeye.dashboard.views.tabular.TabularViewRequest in project pinot by linkedin.

the class DashboardResource method getTabularData.

@GET
@Path(value = "/data/tabular")
@Produces(MediaType.APPLICATION_JSON)
public String getTabularData(@QueryParam("dataset") String collection, @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("aggTimeGranularity") String aggTimeGranularity, @QueryParam("metrics") String metricsJson) throws Exception {
    TabularViewRequest request = new TabularViewRequest();
    request.setCollection(collection);
    List<MetricExpression> metricExpressions = Utils.convertToMetricExpressions(metricsJson, MetricAggFunction.SUM, collection);
    request.setMetricExpressions(metricExpressions);
    long maxDataTime = collectionMaxDataTimeCache.get(collection);
    if (currentEnd > maxDataTime) {
        long delta = currentEnd - maxDataTime;
        currentEnd = currentEnd - delta;
        baselineEnd = baselineEnd - delta;
    }
    // See {@link #getDashboardData} for the reason that the start and end time are stored in a
    // DateTime object with data's timezone.
    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;
    try {
        TabularViewResponse response = handler.process(request);
        jsonResponse = OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(response);
        LOG.debug("Tabular response {}", jsonResponse);
    } catch (Exception e) {
        LOG.error("Exception while processing /data/tabular call", e);
    }
    return jsonResponse;
}
Also used : TabularViewHandler(com.linkedin.thirdeye.dashboard.views.tabular.TabularViewHandler) 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) TabularViewRequest(com.linkedin.thirdeye.dashboard.views.tabular.TabularViewRequest) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with TabularViewRequest

use of com.linkedin.thirdeye.dashboard.views.tabular.TabularViewRequest in project pinot by linkedin.

the class TabularTest method main.

public static void main(String[] args) throws Exception {
    TabularViewRequest request = new TabularViewRequest();
    String collection = "thirdeyeAbook";
    DateTime baselineStart = new DateTime(2016, 3, 23, 00, 00);
    List<MetricExpression> metricExpressions = new ArrayList<>();
    metricExpressions.add(new MetricExpression("__COUNT", "__COUNT"));
    request.setCollection(collection);
    request.setBaselineStart(baselineStart);
    request.setBaselineEnd(baselineStart.plusDays(1));
    request.setCurrentStart(baselineStart.plusDays(7));
    request.setCurrentEnd(baselineStart.plusDays(8));
    request.setTimeGranularity(new TimeGranularity(1, TimeUnit.HOURS));
    request.setMetricExpressions(metricExpressions);
    // TODO
    PinotThirdEyeClient pinotThirdEyeClient = PinotThirdEyeClient.getDefaultTestClient();
    // make
    // this
    // configurable;
    QueryCache queryCache = new QueryCache(pinotThirdEyeClient, Executors.newFixedThreadPool(10));
    TabularViewHandler handler = new TabularViewHandler(queryCache);
    TabularViewResponse response = handler.process(request);
    ObjectMapper mapper = new ObjectMapper();
    String jsonResponse = mapper.writeValueAsString(response);
    System.out.println(jsonResponse);
}
Also used : PinotThirdEyeClient(com.linkedin.thirdeye.client.pinot.PinotThirdEyeClient) QueryCache(com.linkedin.thirdeye.client.cache.QueryCache) TabularViewHandler(com.linkedin.thirdeye.dashboard.views.tabular.TabularViewHandler) ArrayList(java.util.ArrayList) TimeGranularity(com.linkedin.thirdeye.api.TimeGranularity) TabularViewResponse(com.linkedin.thirdeye.dashboard.views.tabular.TabularViewResponse) MetricExpression(com.linkedin.thirdeye.client.MetricExpression) DateTime(org.joda.time.DateTime) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TabularViewRequest(com.linkedin.thirdeye.dashboard.views.tabular.TabularViewRequest)

Example 4 with TabularViewRequest

use of com.linkedin.thirdeye.dashboard.views.tabular.TabularViewRequest in project pinot by linkedin.

the class DataResource method getWowSummary.

@GET
@Path("dashboard/wowsummary")
public WowSummary getWowSummary(@QueryParam("dashboard") String dashboard, @QueryParam("timeRanges") String timeRanges) {
    WowSummary wowSummary = new WowSummary();
    if (StringUtils.isBlank(dashboard)) {
        return wowSummary;
    }
    List<Long> metricIds = getMetricIdsByDashboard(dashboard);
    List<String> timeRangeLabels = Lists.newArrayList(timeRanges.split(","));
    // Sort metric's id and metric expression by collections
    Multimap<String, Long> datasetToMetrics = ArrayListMultimap.create();
    Multimap<String, MetricExpression> datasetToMetricExpressions = ArrayListMultimap.create();
    Map<Long, MetricConfigDTO> metricIdToMetricConfig = new HashMap<>();
    for (long metricId : metricIds) {
        MetricConfigDTO metricConfig = metricConfigDAO.findById(metricId);
        metricIdToMetricConfig.put(metricId, metricConfig);
        datasetToMetrics.put(metricConfig.getDataset(), metricId);
        datasetToMetricExpressions.put(metricConfig.getDataset(), ThirdEyeUtils.getMetricExpressionFromMetricConfig(metricConfig));
    }
    Multimap<String, MetricSummary> metricAliasToMetricSummariesMap = ArrayListMultimap.create();
    // Create query request for each collection
    for (String dataset : datasetToMetrics.keySet()) {
        TabularViewRequest request = new TabularViewRequest();
        request.setCollection(dataset);
        request.setMetricExpressions(new ArrayList<>(datasetToMetricExpressions.get(dataset)));
        // user and server's timezone, including daylight saving time.
        for (String timeRangeLabel : timeRangeLabels) {
            DateTimeZone timeZoneForCollection = Utils.getDataTimeZone(dataset);
            TimeRange timeRange = getTimeRangeFromLabel(dataset, timeZoneForCollection, timeRangeLabel);
            long currentEnd = timeRange.getEnd();
            long currentStart = timeRange.getStart();
            System.out.println(timeRangeLabel + "Current start end " + new DateTime(currentStart) + " " + new DateTime(currentEnd));
            TimeGranularity timeGranularity = new TimeGranularity(1, TimeUnit.HOURS);
            request.setBaselineStart(new DateTime(currentStart, timeZoneForCollection).minusDays(7));
            request.setBaselineEnd(new DateTime(currentEnd, timeZoneForCollection).minusDays(7));
            request.setCurrentStart(new DateTime(currentStart, timeZoneForCollection));
            request.setCurrentEnd(new DateTime(currentEnd, timeZoneForCollection));
            request.setTimeGranularity(timeGranularity);
            TabularViewHandler handler = new TabularViewHandler(queryCache);
            try {
                TabularViewResponse tabularViewResponse = handler.process(request);
                for (String metric : tabularViewResponse.getMetrics()) {
                    MetricDataset metricDataset = new MetricDataset(metric, dataset);
                    MetricConfigDTO metricConfig = CACHE_REGISTRY_INSTANCE.getMetricConfigCache().get(metricDataset);
                    Long metricId = metricConfig.getId();
                    String metricAlias = metricConfig.getAlias();
                    GenericResponse response = tabularViewResponse.getData().get(metric);
                    MetricSummary metricSummary = new MetricSummary();
                    metricSummary.setMetricId(metricId);
                    metricSummary.setMetricName(metricConfig.getName());
                    metricSummary.setMetricAlias(metricAlias);
                    List<String[]> data = response.getResponseData();
                    double baselineValue = 0;
                    double currentValue = 0;
                    for (String[] responseData : data) {
                        baselineValue = baselineValue + Double.valueOf(responseData[0]);
                        currentValue = currentValue + Double.valueOf(responseData[1]);
                    }
                    double percentageChange = (currentValue - baselineValue) * 100 / baselineValue;
                    metricSummary.setBaselineValue(baselineValue);
                    metricSummary.setCurrentValue(currentValue);
                    metricSummary.setWowPercentageChange(percentageChange);
                    metricAliasToMetricSummariesMap.put(metricAlias, metricSummary);
                }
            } catch (Exception e) {
                LOG.error("Exception while processing /data/tabular call", e);
            }
        }
    }
    wowSummary.setMetricAliasToMetricSummariesMap(metricAliasToMetricSummariesMap);
    return wowSummary;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TabularViewResponse(com.linkedin.thirdeye.dashboard.views.tabular.TabularViewResponse) DateTime(org.joda.time.DateTime) MetricSummary(com.linkedin.thirdeye.dashboard.resources.v2.pojo.MetricSummary) TimeGranularity(com.linkedin.thirdeye.api.TimeGranularity) MetricConfigDTO(com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO) GenericResponse(com.linkedin.thirdeye.dashboard.views.GenericResponse) TabularViewHandler(com.linkedin.thirdeye.dashboard.views.tabular.TabularViewHandler) WowSummary(com.linkedin.thirdeye.dashboard.resources.v2.pojo.WowSummary) MetricExpression(com.linkedin.thirdeye.client.MetricExpression) DateTimeZone(org.joda.time.DateTimeZone) WebApplicationException(javax.ws.rs.WebApplicationException) MetricDataset(com.linkedin.thirdeye.client.cache.MetricDataset) TimeRange(com.linkedin.thirdeye.api.TimeRange) TabularViewRequest(com.linkedin.thirdeye.dashboard.views.tabular.TabularViewRequest) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 5 with TabularViewRequest

use of com.linkedin.thirdeye.dashboard.views.tabular.TabularViewRequest in project pinot by linkedin.

the class DataResource method getMetricSummary.

/**
   * Returns percentage change between current values and baseline values. The values are
   * aggregated according to the number of buckets. If the bucket number is 1, then all values
   * between the given time ranges are sorted to the corresponding bucket and aggregated.
   *
   * Note: For current implementation, we assume the number of buckets is always 1.
   */
@GET
@Path("dashboard/metricsummary")
public List<MetricSummary> getMetricSummary(@QueryParam("dashboard") String dashboard, @QueryParam("timeRange") String timeRange) {
    List<MetricSummary> metricsSummary = new ArrayList<>();
    if (StringUtils.isBlank(dashboard)) {
        return metricsSummary;
    }
    List<Long> metricIds = getMetricIdsByDashboard(dashboard);
    // Sort metric's id and metric expression by collections
    Multimap<String, Long> datasetToMetrics = ArrayListMultimap.create();
    Multimap<String, MetricExpression> datasetToMetricExpressions = ArrayListMultimap.create();
    Map<Long, MetricConfigDTO> metricIdToMetricConfig = new HashMap<>();
    for (long metricId : metricIds) {
        MetricConfigDTO metricConfig = metricConfigDAO.findById(metricId);
        metricIdToMetricConfig.put(metricId, metricConfig);
        datasetToMetrics.put(metricConfig.getDataset(), metricId);
        datasetToMetricExpressions.put(metricConfig.getDataset(), ThirdEyeUtils.getMetricExpressionFromMetricConfig(metricConfig));
    }
    // Create query request for each collection
    for (String dataset : datasetToMetrics.keySet()) {
        TabularViewRequest request = new TabularViewRequest();
        request.setCollection(dataset);
        request.setMetricExpressions(new ArrayList<>(datasetToMetricExpressions.get(dataset)));
        // 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.
        String[] tokens = timeRange.split("_");
        TimeGranularity timeGranularity = new TimeGranularity(Integer.valueOf(tokens[0]), TimeUnit.valueOf(tokens[1]));
        long currentEnd = Utils.getMaxDataTimeForDataset(dataset);
        long currentStart = currentEnd - TimeUnit.MILLISECONDS.convert(Long.valueOf(tokens[0]), TimeUnit.valueOf(tokens[1]));
        DateTimeZone timeZoneForCollection = Utils.getDataTimeZone(dataset);
        request.setBaselineStart(new DateTime(currentStart, timeZoneForCollection).minusDays(7));
        request.setBaselineEnd(new DateTime(currentEnd, timeZoneForCollection).minusDays(7));
        request.setCurrentStart(new DateTime(currentStart, timeZoneForCollection));
        request.setCurrentEnd(new DateTime(currentEnd, timeZoneForCollection));
        request.setTimeGranularity(timeGranularity);
        TabularViewHandler handler = new TabularViewHandler(queryCache);
        try {
            TabularViewResponse tabularViewResponse = handler.process(request);
            for (String metric : tabularViewResponse.getMetrics()) {
                MetricDataset metricDataset = new MetricDataset(metric, dataset);
                MetricConfigDTO metricConfig = CACHE_REGISTRY_INSTANCE.getMetricConfigCache().get(metricDataset);
                Long metricId = metricConfig.getId();
                GenericResponse response = tabularViewResponse.getData().get(metric);
                MetricSummary metricSummary = new MetricSummary();
                metricSummary.setMetricId(metricId);
                metricSummary.setMetricName(metricConfig.getName());
                metricSummary.setMetricAlias(metricConfig.getAlias());
                String[] responseData = response.getResponseData().get(0);
                double baselineValue = Double.valueOf(responseData[0]);
                double curentvalue = Double.valueOf(responseData[1]);
                double percentageChange = (curentvalue - baselineValue) * 100 / baselineValue;
                metricSummary.setBaselineValue(baselineValue);
                metricSummary.setCurrentValue(curentvalue);
                metricSummary.setWowPercentageChange(percentageChange);
                AnomaliesSummary anomaliesSummary = anomaliesResoure.getAnomalyCountForMetricInRange(metricId, currentStart, currentEnd);
                metricSummary.setAnomaliesSummary(anomaliesSummary);
                metricsSummary.add(metricSummary);
            }
        } catch (Exception e) {
            LOG.error("Exception while processing /data/tabular call", e);
        }
    }
    return metricsSummary;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) TabularViewResponse(com.linkedin.thirdeye.dashboard.views.tabular.TabularViewResponse) DateTime(org.joda.time.DateTime) MetricSummary(com.linkedin.thirdeye.dashboard.resources.v2.pojo.MetricSummary) TimeGranularity(com.linkedin.thirdeye.api.TimeGranularity) MetricConfigDTO(com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO) GenericResponse(com.linkedin.thirdeye.dashboard.views.GenericResponse) TabularViewHandler(com.linkedin.thirdeye.dashboard.views.tabular.TabularViewHandler) MetricExpression(com.linkedin.thirdeye.client.MetricExpression) DateTimeZone(org.joda.time.DateTimeZone) WebApplicationException(javax.ws.rs.WebApplicationException) MetricDataset(com.linkedin.thirdeye.client.cache.MetricDataset) AnomaliesSummary(com.linkedin.thirdeye.dashboard.resources.v2.pojo.AnomaliesSummary) TabularViewRequest(com.linkedin.thirdeye.dashboard.views.tabular.TabularViewRequest) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

MetricExpression (com.linkedin.thirdeye.client.MetricExpression)6 TabularViewHandler (com.linkedin.thirdeye.dashboard.views.tabular.TabularViewHandler)6 TabularViewRequest (com.linkedin.thirdeye.dashboard.views.tabular.TabularViewRequest)6 TabularViewResponse (com.linkedin.thirdeye.dashboard.views.tabular.TabularViewResponse)6 DateTime (org.joda.time.DateTime)6 DateTimeZone (org.joda.time.DateTimeZone)5 MetricConfigDTO (com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO)4 ArrayList (java.util.ArrayList)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 TimeGranularity (com.linkedin.thirdeye.api.TimeGranularity)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 MetricDataset (com.linkedin.thirdeye.client.cache.MetricDataset)2 MetricSummary (com.linkedin.thirdeye.dashboard.resources.v2.pojo.MetricSummary)2 GenericResponse (com.linkedin.thirdeye.dashboard.views.GenericResponse)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 Produces (javax.ws.rs.Produces)2