package com.hubspot.singularity.data.history;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.hubspot.mesos.JavaUtils;
import com.hubspot.singularity.OrderDirection;
import com.hubspot.singularity.SingularityRequestHistory;
import com.hubspot.singularity.data.RequestManager;

@Singleton
public class RequestHistoryHelper extends BlendedHistoryHelper<SingularityRequestHistory, String> {

  private final RequestManager requestManager;
  private final HistoryManager historyManager;

  @Inject
  public RequestHistoryHelper(RequestManager requestManager, HistoryManager historyManager) {
    this.requestManager = requestManager;
    this.historyManager = historyManager;
  }

  @Override
  protected List<SingularityRequestHistory> getFromZk(String requestId) {
    List<SingularityRequestHistory> requestHistory = requestManager.getRequestHistory(requestId);

    Collections.sort(requestHistory);

    return requestHistory;
  }

  @Override
  protected boolean queryUsesZkFirst(String id) {
    return false;
  }

  @Override
  protected Comparator<SingularityRequestHistory> getComparator(String id) {
    return new Comparator<SingularityRequestHistory>() {
      @Override public int compare(SingularityRequestHistory o1, SingularityRequestHistory o2) {
        return Long.compare(o2.getCreatedAt(), o1.getCreatedAt());
      }
    };
  }

  @Override
  protected List<SingularityRequestHistory> getFromHistory(String requestId, int historyStart, int numFromHistory) {
    return historyManager.getRequestHistory(requestId, Optional.of(OrderDirection.DESC), historyStart, numFromHistory);
  }

  public Optional<SingularityRequestHistory> getFirstHistory(String requestId) {
    Optional<SingularityRequestHistory> firstHistory = JavaUtils.getFirst(historyManager.getRequestHistory(requestId, Optional.of(OrderDirection.ASC), 0, 1));

    if (firstHistory.isPresent()) {
      return firstHistory;
    }

    return JavaUtils.getLast(getFromZk(requestId));
  }

  public Optional<SingularityRequestHistory> getLastHistory(String requestId) {
    Optional<SingularityRequestHistory> lastHistory = JavaUtils.getFirst(getFromZk(requestId));

    if (lastHistory.isPresent()) {
      return lastHistory;
    }

    return JavaUtils.getFirst(historyManager.getRequestHistory(requestId, Optional.of(OrderDirection.DESC), 0, 1));
  }

}

package com.hubspot.singularity.data.history;

import java.util.Collections;
import java.util.List;

import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.hubspot.mesos.JavaUtils;
import com.hubspot.singularity.OrderDirection;
import com.hubspot.singularity.SingularityRequestHistory;
import com.hubspot.singularity.data.RequestManager;

@Singleton
public class RequestHistoryHelper extends BlendedHistoryHelper<SingularityRequestHistory, String> {

  private final RequestManager requestManager;
  private final HistoryManager historyManager;

  @Inject
  public RequestHistoryHelper(RequestManager requestManager, HistoryManager historyManager) {
    this.requestManager = requestManager;
    this.historyManager = historyManager;
  }

  @Override
  protected List<SingularityRequestHistory> getFromZk(String requestId) {
    List<SingularityRequestHistory> requestHistory = requestManager.getRequestHistory(requestId);

    Collections.sort(requestHistory);

    return requestHistory;
  }

  @Override
  protected List<SingularityRequestHistory> getFromHistory(String requestId, int historyStart, int numFromHistory) {
    return historyManager.getRequestHistory(requestId, Optional.of(OrderDirection.DESC), historyStart, numFromHistory);
  }

  public Optional<SingularityRequestHistory> getFirstHistory(String requestId) {
    Optional<SingularityRequestHistory> firstHistory = JavaUtils.getFirst(historyManager.getRequestHistory(requestId, Optional.of(OrderDirection.ASC), 0, 1));

    if (firstHistory.isPresent()) {
      return firstHistory;
    }

    return JavaUtils.getLast(getFromZk(requestId));
  }

  public Optional<SingularityRequestHistory> getLastHistory(String requestId) {
    Optional<SingularityRequestHistory> lastHistory = JavaUtils.getFirst(getFromZk(requestId));

    if (lastHistory.isPresent()) {
      return lastHistory;
    }

    return JavaUtils.getFirst(historyManager.getRequestHistory(requestId, Optional.of(OrderDirection.DESC), 0, 1));
  }

}

revert sorting changes for request history