Search in sources :

Example 6 with ThirdEyeResponseRow

use of com.linkedin.thirdeye.client.ThirdEyeResponseRow in project pinot by linkedin.

the class TimeOnTimeResponseParser method parseGroupByTimeDimensionResponse.

private void parseGroupByTimeDimensionResponse() {
    baselineResponseMap = ResponseParserUtils.createResponseMapByTimeAndDimension(baselineResponse);
    currentResponseMap = ResponseParserUtils.createResponseMapByTimeAndDimension(currentResponse);
    Map<Integer, List<Double>> baselineMetricSums = ResponseParserUtils.getMetricSumsByTime(baselineResponse);
    Map<Integer, List<Double>> currentMetricSums = ResponseParserUtils.getMetricSumsByTime(currentResponse);
    // group by time and dimension values
    Set<String> timeDimensionValues = new HashSet<>();
    timeDimensionValues.addAll(baselineResponseMap.keySet());
    timeDimensionValues.addAll(currentResponseMap.keySet());
    Set<String> dimensionValues = new HashSet<>();
    for (String timeDimensionValue : timeDimensionValues) {
        String dimensionValue = ResponseParserUtils.extractFirstDimensionValue(timeDimensionValue);
        dimensionValues.add(dimensionValue);
    }
    // group by dimension name
    String dimensionName = baselineResponse.getGroupKeyColumns().get(1);
    // other row
    List<Row.Builder> otherBuilders = new ArrayList<>();
    List<Double[]> otherBaselineMetrics = new ArrayList<>();
    List<Double[]> otherCurrentMetrics = new ArrayList<>();
    boolean includeOther = false;
    // constructing an OTHER rows, 1 for each time bucket
    for (int timeBucketId = 0; timeBucketId < numTimeBuckets; timeBucketId++) {
        Range<DateTime> baselineTimeRange = baselineRanges.get(timeBucketId);
        Range<DateTime> currentTimeRange = currentRanges.get(timeBucketId);
        Row.Builder builder = new Row.Builder();
        builder.setBaselineStart(baselineTimeRange.lowerEndpoint());
        builder.setBaselineEnd(baselineTimeRange.upperEndpoint());
        builder.setCurrentStart(currentTimeRange.lowerEndpoint());
        builder.setCurrentEnd(currentTimeRange.upperEndpoint());
        builder.setDimensionName(dimensionName);
        builder.setDimensionValue(OTHER);
        otherBuilders.add(builder);
        Double[] otherBaseline = new Double[numMetrics];
        Arrays.fill(otherBaseline, 0.0);
        Double[] otherCurrent = new Double[numMetrics];
        Arrays.fill(otherCurrent, 0.0);
        otherBaselineMetrics.add(otherBaseline);
        otherCurrentMetrics.add(otherCurrent);
    }
    // else, we add the metric values to the OTHER row
    for (String dimensionValue : dimensionValues) {
        List<Row> thresholdRows = new ArrayList<>();
        for (int timeBucketId = 0; timeBucketId < numTimeBuckets; timeBucketId++) {
            Range<DateTime> baselineTimeRange = baselineRanges.get(timeBucketId);
            Range<DateTime> currentTimeRange = currentRanges.get(timeBucketId);
            // compute the time|dimension key
            String baselineTimeDimensionValue = ResponseParserUtils.computeTimeDimensionValue(timeBucketId, dimensionValue);
            String currentTimeDimensionValue = baselineTimeDimensionValue;
            ThirdEyeResponseRow baselineRow = baselineResponseMap.get(baselineTimeDimensionValue);
            ThirdEyeResponseRow currentRow = currentResponseMap.get(currentTimeDimensionValue);
            Row.Builder builder = new Row.Builder();
            builder.setBaselineStart(baselineTimeRange.lowerEndpoint());
            builder.setBaselineEnd(baselineTimeRange.upperEndpoint());
            builder.setCurrentStart(currentTimeRange.lowerEndpoint());
            builder.setCurrentEnd(currentTimeRange.upperEndpoint());
            builder.setDimensionName(dimensionName);
            builder.setDimensionValue(dimensionValue);
            addMetric(baselineRow, currentRow, builder);
            Row row = builder.build();
            thresholdRows.add(row);
        }
        // check if rows pass threshold
        boolean passedThreshold = false;
        for (int timeBucketId = 0; timeBucketId < numTimeBuckets; timeBucketId++) {
            if (checkMetricSums(thresholdRows.get(timeBucketId), baselineMetricSums.get(timeBucketId), currentMetricSums.get(timeBucketId))) {
                passedThreshold = true;
                break;
            }
        }
        if (passedThreshold) {
            // if any of the cells of a contributor row passes threshold, add all
            // those cells
            rows.addAll(thresholdRows);
        } else {
            // else that row of cells goes into OTHER
            includeOther = true;
            for (int timeBucketId = 0; timeBucketId < numTimeBuckets; timeBucketId++) {
                Row row = thresholdRows.get(timeBucketId);
                List<Metric> metrics = row.getMetrics();
                for (int i = 0; i < metrics.size(); i++) {
                    Metric metricToAdd = metrics.get(i);
                    otherBaselineMetrics.get(timeBucketId)[i] += metricToAdd.getBaselineValue();
                    otherCurrentMetrics.get(timeBucketId)[i] += metricToAdd.getCurrentValue();
                }
            }
        }
    }
    // create other row using the other baseline and current sums
    if (includeOther) {
        for (int timeBucketId = 0; timeBucketId < numTimeBuckets; timeBucketId++) {
            Builder otherBuilder = otherBuilders.get(timeBucketId);
            Double[] otherBaseline = otherBaselineMetrics.get(timeBucketId);
            Double[] otherCurrent = otherCurrentMetrics.get(timeBucketId);
            for (int i = 0; i < numMetrics; i++) {
                otherBuilder.addMetric(metricFunctions.get(i).getMetricName(), otherBaseline[i], otherCurrent[i]);
            }
            Row row = otherBuilder.build();
            if (isValidMetric(row, Arrays.asList(otherBaseline), Arrays.asList(otherCurrent))) {
                rows.add(row);
            }
        }
    }
}
Also used : Builder(com.linkedin.thirdeye.client.comparison.Row.Builder) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) ThirdEyeResponseRow(com.linkedin.thirdeye.client.ThirdEyeResponseRow) ArrayList(java.util.ArrayList) List(java.util.List) Metric(com.linkedin.thirdeye.client.comparison.Row.Metric) ThirdEyeResponseRow(com.linkedin.thirdeye.client.ThirdEyeResponseRow) Builder(com.linkedin.thirdeye.client.comparison.Row.Builder) HashSet(java.util.HashSet)

Aggregations

ThirdEyeResponseRow (com.linkedin.thirdeye.client.ThirdEyeResponseRow)6 DateTime (org.joda.time.DateTime)4 Builder (com.linkedin.thirdeye.client.comparison.Row.Builder)3 HashSet (java.util.HashSet)3 Metric (com.linkedin.thirdeye.client.comparison.Row.Metric)2 Builder (com.linkedin.thirdeye.client.timeseries.TimeSeriesRow.Builder)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ThirdEyeResponse (com.linkedin.thirdeye.client.ThirdEyeResponse)1 TimeSeriesMetric (com.linkedin.thirdeye.client.timeseries.TimeSeriesRow.TimeSeriesMetric)1