use of com.linkedin.thirdeye.dashboard.resources.v2.pojo.TimeSeriesCompareMetricView in project pinot by linkedin.
the class TimeSeriesResource method getContributorDataForDimension.
private TimeSeriesCompareMetricView getContributorDataForDimension(long metricId, long currentStart, long currentEnd, long baselineStart, long baselineEnd, String dimension, String filters, String granularity) {
MetricConfigDTO metricConfigDTO = metricConfigDAO.findById(metricId);
TimeSeriesCompareMetricView timeSeriesCompareMetricView = new TimeSeriesCompareMetricView(metricConfigDTO.getName(), metricId, currentStart, currentEnd);
try {
String dataset = metricConfigDTO.getDataset();
ContributorViewRequest request = new ContributorViewRequest();
request.setCollection(dataset);
MetricExpression metricExpression = ThirdEyeUtils.getMetricExpressionFromMetricConfig(metricConfigDTO);
request.setMetricExpressions(Arrays.asList(metricExpression));
DateTimeZone timeZoneForCollection = Utils.getDataTimeZone(dataset);
request.setBaselineStart(new DateTime(baselineStart, timeZoneForCollection));
request.setBaselineEnd(new DateTime(baselineEnd, timeZoneForCollection));
request.setCurrentStart(new DateTime(currentStart, timeZoneForCollection));
request.setCurrentEnd(new DateTime(currentEnd, timeZoneForCollection));
request.setTimeGranularity(Utils.getAggregationTimeGranularity(granularity, dataset));
if (filters != null && !filters.isEmpty()) {
filters = URLDecoder.decode(filters, "UTF-8");
request.setFilters(ThirdEyeUtils.convertToMultiMap(filters));
}
request.setGroupByDimensions(Arrays.asList(dimension));
ContributorViewHandler handler = new ContributorViewHandler(queryCache);
ContributorViewResponse response = handler.process(request);
// Assign the time buckets
List<Long> timeBucketsCurrent = new ArrayList<>();
List<Long> timeBucketsBaseline = new ArrayList<>();
timeSeriesCompareMetricView.setTimeBucketsCurrent(timeBucketsCurrent);
timeSeriesCompareMetricView.setTimeBucketsBaseline(timeBucketsBaseline);
Map<String, ValuesContainer> subDimensionValuesMap = new LinkedHashMap<>();
timeSeriesCompareMetricView.setSubDimensionContributionMap(subDimensionValuesMap);
int timeBuckets = response.getTimeBuckets().size();
// this is for over all values
ValuesContainer vw = new ValuesContainer();
subDimensionValuesMap.put(ALL, vw);
vw.setCurrentValues(new double[timeBuckets]);
vw.setBaselineValues(new double[timeBuckets]);
vw.setPercentageChange(new String[timeBuckets]);
vw.setCumulativeCurrentValues(new double[timeBuckets]);
vw.setCumulativeBaselineValues(new double[timeBuckets]);
vw.setCumulativePercentageChange(new String[timeBuckets]);
// lets find the indices
int subDimensionIndex = response.getResponseData().getSchema().getColumnsToIndexMapping().get("dimensionValue");
int currentValueIndex = response.getResponseData().getSchema().getColumnsToIndexMapping().get("currentValue");
int baselineValueIndex = response.getResponseData().getSchema().getColumnsToIndexMapping().get("baselineValue");
int percentageChangeIndex = response.getResponseData().getSchema().getColumnsToIndexMapping().get("percentageChange");
int cumCurrentValueIndex = response.getResponseData().getSchema().getColumnsToIndexMapping().get("cumulativeCurrentValue");
int cumBaselineValueIndex = response.getResponseData().getSchema().getColumnsToIndexMapping().get("cumulativeBaselineValue");
int cumPercentageChangeIndex = response.getResponseData().getSchema().getColumnsToIndexMapping().get("cumulativePercentageChange");
// populate current and baseline time buckets
for (int i = 0; i < timeBuckets; i++) {
TimeBucket tb = response.getTimeBuckets().get(i);
timeBucketsCurrent.add(tb.getCurrentStart());
timeBucketsBaseline.add(tb.getBaselineStart());
}
// set current and baseline values for sub dimensions
for (int i = 0; i < response.getResponseData().getResponseData().size(); i++) {
String[] data = response.getResponseData().getResponseData().get(i);
String subDimension = data[subDimensionIndex];
Double currentVal = Double.valueOf(data[currentValueIndex]);
Double baselineVal = Double.valueOf(data[baselineValueIndex]);
Double percentageChangeVal = Double.valueOf(data[percentageChangeIndex]);
Double cumCurrentVal = Double.valueOf(data[cumCurrentValueIndex]);
Double cumBaselineVal = Double.valueOf(data[cumBaselineValueIndex]);
Double cumPercentageChangeVal = Double.valueOf(data[cumPercentageChangeIndex]);
int index = i % timeBuckets;
// set overAll values
vw.getCurrentValues()[index] += currentVal;
vw.getBaselineValues()[index] += baselineVal;
vw.getCumulativeCurrentValues()[index] += cumCurrentVal;
vw.getCumulativeBaselineValues()[index] += cumBaselineVal;
// set individual sub-dimension values
if (!subDimensionValuesMap.containsKey(subDimension)) {
ValuesContainer subDimVals = new ValuesContainer();
subDimVals.setCurrentValues(new double[timeBuckets]);
subDimVals.setBaselineValues(new double[timeBuckets]);
subDimVals.setPercentageChange(new String[timeBuckets]);
subDimVals.setCumulativeCurrentValues(new double[timeBuckets]);
subDimVals.setCumulativeBaselineValues(new double[timeBuckets]);
subDimVals.setCumulativePercentageChange(new String[timeBuckets]);
subDimensionValuesMap.put(subDimension, subDimVals);
}
subDimensionValuesMap.get(subDimension).getCurrentValues()[index] = currentVal;
subDimensionValuesMap.get(subDimension).getBaselineValues()[index] = baselineVal;
subDimensionValuesMap.get(subDimension).getPercentageChange()[index] = String.format(DECIMAL_FORMAT, percentageChangeVal);
subDimensionValuesMap.get(subDimension).getCumulativeCurrentValues()[index] = cumCurrentVal;
subDimensionValuesMap.get(subDimension).getCumulativeBaselineValues()[index] = cumBaselineVal;
subDimensionValuesMap.get(subDimension).getCumulativePercentageChange()[index] = String.format(DECIMAL_FORMAT, cumPercentageChangeVal);
}
// TODO : compute cumulative values for all
for (int i = 0; i < vw.getCurrentValues().length; i++) {
vw.getPercentageChange()[i] = String.format(DECIMAL_FORMAT, getPercentageChange(vw.getCurrentValues()[i], vw.getBaselineValues()[i]));
vw.getCumulativePercentageChange()[i] = String.format(DECIMAL_FORMAT, getPercentageChange(vw.getCumulativeCurrentValues()[i], vw.getCumulativeBaselineValues()[i]));
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new WebApplicationException(e);
}
return timeSeriesCompareMetricView;
}
use of com.linkedin.thirdeye.dashboard.resources.v2.pojo.TimeSeriesCompareMetricView in project pinot by linkedin.
the class TimeSeriesResource method getTabularData.
/**
* used when dimension is not passed, i.e. data is requested for all dimensions.
* @param metricId
* @param currentStart
* @param currentEnd
* @param baselineStart
* @param baselineEnd
* @param filters
* @param granularity
* @return
*/
private TimeSeriesCompareMetricView getTabularData(long metricId, long currentStart, long currentEnd, long baselineStart, long baselineEnd, String filters, String granularity) {
TimeSeriesCompareMetricView timeSeriesCompareView = new TimeSeriesCompareMetricView();
try {
MetricConfigDTO metricConfigDTO = metricConfigDAO.findById(metricId);
if (metricConfigDTO != null) {
String dataset = metricConfigDTO.getDataset();
TabularViewRequest request = new TabularViewRequest();
request.setCollection(dataset);
MetricExpression metricExpression = ThirdEyeUtils.getMetricExpressionFromMetricConfig(metricConfigDTO);
request.setMetricExpressions(Arrays.asList(metricExpression));
DateTimeZone timeZoneForCollection = Utils.getDataTimeZone(dataset);
request.setBaselineStart(new DateTime(baselineStart, timeZoneForCollection));
request.setBaselineEnd(new DateTime(baselineEnd, timeZoneForCollection));
request.setCurrentStart(new DateTime(currentStart, timeZoneForCollection));
request.setCurrentEnd(new DateTime(currentEnd, timeZoneForCollection));
request.setTimeGranularity(Utils.getAggregationTimeGranularity(granularity, dataset));
if (filters != null && !filters.isEmpty()) {
filters = URLDecoder.decode(filters, "UTF-8");
request.setFilters(ThirdEyeUtils.convertToMultiMap(filters));
}
TabularViewHandler handler = new TabularViewHandler(queryCache);
TabularViewResponse response = handler.process(request);
timeSeriesCompareView.setStart(currentStart);
timeSeriesCompareView.setEnd(currentEnd);
timeSeriesCompareView.setMetricId(metricConfigDTO.getId());
timeSeriesCompareView.setMetricName(metricConfigDTO.getName());
List<Long> timeBucketsCurrent = new ArrayList<>();
List<Long> timeBucketsBaseline = new ArrayList<>();
int numTimeBuckets = response.getTimeBuckets().size();
double[] currentValues = new double[numTimeBuckets];
double[] baselineValues = new double[numTimeBuckets];
String[] percentageChangeValues = new String[numTimeBuckets];
double[] cumCurrentValues = new double[numTimeBuckets];
double[] cumBaselineValues = new double[numTimeBuckets];
String[] cumPercentageChangeValues = new String[numTimeBuckets];
int currentValIndex = response.getData().get(metricConfigDTO.getName()).getSchema().getColumnsToIndexMapping().get("currentValue");
int baselineValIndex = response.getData().get(metricConfigDTO.getName()).getSchema().getColumnsToIndexMapping().get("baselineValue");
int percentageChangeIndex = response.getData().get(metricConfigDTO.getName()).getSchema().getColumnsToIndexMapping().get("ratio");
int cumCurrentValIndex = response.getData().get(metricConfigDTO.getName()).getSchema().getColumnsToIndexMapping().get("cumulativeCurrentValue");
int cumBaselineValIndex = response.getData().get(metricConfigDTO.getName()).getSchema().getColumnsToIndexMapping().get("cumulativeBaselineValue");
int cumPercentageChangeIndex = response.getData().get(metricConfigDTO.getName()).getSchema().getColumnsToIndexMapping().get("cumulativeRatio");
for (int i = 0; i < numTimeBuckets; i++) {
TimeBucket tb = response.getTimeBuckets().get(i);
timeBucketsCurrent.add(tb.getCurrentStart());
timeBucketsBaseline.add(tb.getBaselineStart());
currentValues[i] = Double.valueOf(response.getData().get(metricConfigDTO.getName()).getResponseData().get(i)[currentValIndex]);
baselineValues[i] = Double.valueOf(response.getData().get(metricConfigDTO.getName()).getResponseData().get(i)[baselineValIndex]);
percentageChangeValues[i] = response.getData().get(metricConfigDTO.getName()).getResponseData().get(i)[percentageChangeIndex];
cumCurrentValues[i] = Double.valueOf(response.getData().get(metricConfigDTO.getName()).getResponseData().get(i)[cumCurrentValIndex]);
cumBaselineValues[i] = Double.valueOf(response.getData().get(metricConfigDTO.getName()).getResponseData().get(i)[cumBaselineValIndex]);
cumPercentageChangeValues[i] = response.getData().get(metricConfigDTO.getName()).getResponseData().get(i)[cumPercentageChangeIndex];
}
timeSeriesCompareView.setTimeBucketsCurrent(timeBucketsCurrent);
timeSeriesCompareView.setTimeBucketsBaseline(timeBucketsBaseline);
ValuesContainer values = new ValuesContainer();
values.setCurrentValues(currentValues);
values.setBaselineValues(baselineValues);
values.setPercentageChange(percentageChangeValues);
values.setCumulativeCurrentValues(cumCurrentValues);
values.setCumulativeBaselineValues(cumBaselineValues);
values.setCumulativePercentageChange(cumPercentageChangeValues);
timeSeriesCompareView.setSubDimensionContributionMap(new LinkedHashMap<>());
timeSeriesCompareView.getSubDimensionContributionMap().put(ALL, values);
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new WebApplicationException(e);
}
return timeSeriesCompareView;
}
Aggregations