<commit_before>package com.linkedin.thirdeye.client.comparison;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.joda.time.DateTime;

import com.google.common.collect.Range;
import com.linkedin.thirdeye.api.TimeGranularity;
import com.linkedin.thirdeye.client.MetricExpression;
import com.linkedin.thirdeye.client.MetricFunction;
import com.linkedin.thirdeye.client.ThirdEyeClient;
import com.linkedin.thirdeye.client.ThirdEyeRequest;
import com.linkedin.thirdeye.client.ThirdEyeRequest.ThirdEyeRequestBuilder;
import com.linkedin.thirdeye.client.ThirdEyeResponse;
import com.linkedin.thirdeye.client.TimeRangeUtils;
import com.linkedin.thirdeye.client.cache.QueryCache;
import com.linkedin.thirdeye.client.comparison.Row.Metric;
import com.linkedin.thirdeye.dashboard.Utils;

public class TimeOnTimeComparisonHandler {
  private static final String CURRENT = "current";
  private static final String BASELINE = "baseline";
  private final QueryCache queryCache;

  public TimeOnTimeComparisonHandler(QueryCache queryCache) {
    this.queryCache = queryCache;
  }

  public TimeOnTimeComparisonResponse handle(TimeOnTimeComparisonRequest comparisonRequest)
      throws Exception {
    ThirdEyeRequestBuilder builder = new ThirdEyeRequestBuilder();
    builder.setCollection(comparisonRequest.getCollectionName());
    List<Range<DateTime>> baselineTimeranges = new ArrayList<>();
    List<Range<DateTime>> currentTimeranges = new ArrayList<>();
    TimeGranularity aggregationTimeGranularity = comparisonRequest.getAggregationTimeGranularity();
    // baseline time ranges
    DateTime baselineStart = comparisonRequest.getBaselineStart();
    DateTime baselineEnd = comparisonRequest.getBaselineEnd();
    // current time ranges
    DateTime currentStart = comparisonRequest.getCurrentStart();
    DateTime currentEnd = comparisonRequest.getCurrentEnd();

    if (comparisonRequest.isEndDateInclusive()) {
      // ThirdEyeRequest is exclusive endpoint, so increment by one bucket
      long aggTimeBucketMillis = aggregationTimeGranularity.toMillis();
      currentStart = currentStart.plus(aggTimeBucketMillis);
      baselineStart = baselineStart.plus(aggTimeBucketMillis);
      currentEnd = currentEnd.plus(aggTimeBucketMillis);
      baselineEnd = baselineEnd.plus(aggTimeBucketMillis);
    }
    baselineTimeranges =
        TimeRangeUtils.computeTimeRanges(aggregationTimeGranularity, baselineStart, baselineEnd);
    currentTimeranges =
        TimeRangeUtils.computeTimeRanges(aggregationTimeGranularity, currentStart, currentEnd);

    // create baseline request<commit_msg>Adding fix for missing first datapoint on tabular and timeseries (#612)<commit_after>package com.linkedin.thirdeye.client.comparison;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.joda.time.DateTime;

import com.google.common.collect.Range;
import com.linkedin.thirdeye.api.TimeGranularity;
import com.linkedin.thirdeye.client.MetricExpression;
import com.linkedin.thirdeye.client.MetricFunction;
import com.linkedin.thirdeye.client.ThirdEyeClient;
import com.linkedin.thirdeye.client.ThirdEyeRequest;
import com.linkedin.thirdeye.client.ThirdEyeRequest.ThirdEyeRequestBuilder;
import com.linkedin.thirdeye.client.ThirdEyeResponse;
import com.linkedin.thirdeye.client.TimeRangeUtils;
import com.linkedin.thirdeye.client.cache.QueryCache;
import com.linkedin.thirdeye.client.comparison.Row.Metric;
import com.linkedin.thirdeye.dashboard.Utils;

public class TimeOnTimeComparisonHandler {
  private static final String CURRENT = "current";
  private static final String BASELINE = "baseline";
  private final QueryCache queryCache;

  public TimeOnTimeComparisonHandler(QueryCache queryCache) {
    this.queryCache = queryCache;
  }

  public TimeOnTimeComparisonResponse handle(TimeOnTimeComparisonRequest comparisonRequest)
      throws Exception {
    ThirdEyeRequestBuilder builder = new ThirdEyeRequestBuilder();
    builder.setCollection(comparisonRequest.getCollectionName());
    List<Range<DateTime>> baselineTimeranges = new ArrayList<>();
    List<Range<DateTime>> currentTimeranges = new ArrayList<>();
    TimeGranularity aggregationTimeGranularity = comparisonRequest.getAggregationTimeGranularity();
    // baseline time ranges
    DateTime baselineStart = comparisonRequest.getBaselineStart();
    DateTime baselineEnd = comparisonRequest.getBaselineEnd();
    // current time ranges
    DateTime currentStart = comparisonRequest.getCurrentStart();
    DateTime currentEnd = comparisonRequest.getCurrentEnd();

    if (comparisonRequest.isEndDateInclusive()) {
      // ThirdEyeRequest is exclusive endpoint, so increment end by one bucket
      long aggTimeBucketMillis = aggregationTimeGranularity.toMillis();
      currentEnd = currentEnd.plus(aggTimeBucketMillis);
      baselineEnd = baselineEnd.plus(aggTimeBucketMillis);
    }
    baselineTimeranges =
        TimeRangeUtils.computeTimeRanges(aggregationTimeGranularity, baselineStart, baselineEnd);
    currentTimeranges =
        TimeRangeUtils.computeTimeRanges(aggregationTimeGranularity, currentStart, currentEnd);

    // create baseline request
{'fraction_non_alphanumeric': 0.08432647165041532, 'fraction_numerical': 0.0005417118093174431, 'mean_word_length': 5.657451923076923, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4236233', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1508, 'n_words': 292}