com.hubspot.singularity.data.history.RequestHistoryHelper Maven / Gradle / Ivy
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 {
private final RequestManager requestManager;
private final HistoryManager historyManager;
@Inject
public RequestHistoryHelper(RequestManager requestManager, HistoryManager historyManager) {
this.requestManager = requestManager;
this.historyManager = historyManager;
}
@Override
protected List getFromZk(String requestId) {
List requestHistory = requestManager.getRequestHistory(requestId);
Collections.sort(requestHistory);
return requestHistory;
}
@Override
protected List getFromHistory(String requestId, int historyStart, int numFromHistory) {
return historyManager.getRequestHistory(requestId, Optional.of(OrderDirection.DESC), historyStart, numFromHistory);
}
public Optional getFirstHistory(String requestId) {
Optional firstHistory = JavaUtils.getFirst(historyManager.getRequestHistory(requestId, Optional.of(OrderDirection.ASC), 0, 1));
if (firstHistory.isPresent()) {
return firstHistory;
}
return JavaUtils.getLast(getFromZk(requestId));
}
public Optional getLastHistory(String requestId) {
Optional lastHistory = JavaUtils.getFirst(getFromZk(requestId));
if (lastHistory.isPresent()) {
return lastHistory;
}
return JavaUtils.getFirst(historyManager.getRequestHistory(requestId, Optional.of(OrderDirection.DESC), 0, 1));
}
@Override
protected Optional getTotalCount(String requestId) {
int numFromZk = requestManager.getRequestHistory(requestId).size();
int numFromHistory = historyManager.getRequestHistoryCount(requestId);
return Optional.of(numFromZk + numFromHistory);
}
}