diff --git a/SingularityClient/src/main/java/com/hubspot/singularity/client/SingularityClient.java b/SingularityClient/src/main/java/com/hubspot/singularity/client/SingularityClient.java index 0165712a33..e4989b03d9 100644 --- a/SingularityClient/src/main/java/com/hubspot/singularity/client/SingularityClient.java +++ b/SingularityClient/src/main/java/com/hubspot/singularity/client/SingularityClient.java @@ -98,6 +98,8 @@ import com.hubspot.singularity.api.SingularityPauseRequest; import com.hubspot.singularity.api.SingularityPriorityFreeze; import com.hubspot.singularity.api.SingularityRunNowRequest; +import com.hubspot.singularity.api.SingularityS3SearchRequest; +import com.hubspot.singularity.api.SingularityS3SearchResult; import com.hubspot.singularity.api.SingularityScaleRequest; import com.hubspot.singularity.api.SingularityUnpauseRequest; import com.hubspot.singularity.api.SingularityUpdateGroupsRequest; @@ -200,6 +202,7 @@ public class SingularityClient { private static final String SANDBOX_READ_FILE_FORMAT = SANDBOX_FORMAT + "/%s/read"; private static final String S3_LOG_FORMAT = "%s/logs"; + private static final String S3_LOG_SEARCH_LOGS = S3_LOG_FORMAT + "/search"; private static final String S3_LOG_GET_TASK_LOGS = S3_LOG_FORMAT + "/task/%s"; private static final String S3_LOG_GET_REQUEST_LOGS = S3_LOG_FORMAT + "/request/%s"; private static final String S3_LOG_GET_DEPLOY_LOGS = S3_LOG_FORMAT + "/request/%s/deploy/%s"; @@ -1371,6 +1374,26 @@ public Optional readSandBoxFile(String taskId, String path // S3 LOGS // + /** + * Retrieve the list of logs stored in S3 based on a specified search request + * + * @param searchRequest + * The parameters upon which to base the search + * @return + * A result in the form of a {@link SingularityS3SearchResult} + */ + public SingularityS3SearchResult getLogs(SingularityS3SearchRequest searchRequest) { + final Function requestUri = (host) -> String.format(S3_LOG_SEARCH_LOGS, getApiBase(host)); + + final Optional maybeResult = post(requestUri, "S3 log search", Optional.of(searchRequest), Optional.of(SingularityS3SearchResult.class)); + + if (!maybeResult.isPresent()) { + throw new SingularityClientException("Singularity url not found", 404); + } else { + return maybeResult.get(); + } + } + /** * Retrieve the list of logs stored in S3 for a specific task * @@ -1398,11 +1421,24 @@ public Collection getTaskLogs(String taskId) { * A collection of {@link SingularityS3Log} */ public Collection getRequestLogs(String requestId) { + return getRequestLogs(requestId, Optional.absent(), Optional.absent()); + } + + public Collection getRequestLogs(String requestId, Optional start, Optional end) { final Function requestUri = (host) -> String.format(S3_LOG_GET_REQUEST_LOGS, getApiBase(host), requestId); final String type = String.format("S3 logs for request %s", requestId); - return getCollection(requestUri, type, S3_LOG_COLLECTION); + Map dateRange = new HashMap<>(); + if (start.isPresent()) { + dateRange.put("start", start.get()); + } + + if (end.isPresent()) { + dateRange.put("end", end.get()); + } + + return getCollectionWithParams(requestUri, type, Optional.of(dateRange), S3_LOG_COLLECTION); } /** diff --git a/SingularityService/src/main/java/com/hubspot/singularity/resources/S3LogResource.java b/SingularityService/src/main/java/com/hubspot/singularity/resources/S3LogResource.java index 956e65f1a4..4f26555d3b 100644 --- a/SingularityService/src/main/java/com/hubspot/singularity/resources/S3LogResource.java +++ b/SingularityService/src/main/java/com/hubspot/singularity/resources/S3LogResource.java @@ -185,13 +185,12 @@ private boolean isCurrentDeploy(String requestId, String deployId) { private Collection getS3PrefixesForRequest(S3Configuration s3Configuration, String requestId, Optional startArg, Optional endArg, String group) { Optional firstHistory = requestHistoryHelper.getFirstHistory(requestId); - checkNotFound(firstHistory.isPresent(), "No request history found for %s", requestId); - - long start = firstHistory.get().getCreatedAt(); - if (startArg.isPresent()) { - start = Math.max(startArg.get(), start); + if (!startArg.isPresent()) { + checkBadRequest(firstHistory.isPresent(), "No request history found for %s. A start time must be specified.", requestId); } + long start = Math.max(startArg.or(0L), firstHistory.get().getCreatedAt()); + Optional lastHistory = requestHistoryHelper.getLastHistory(requestId); long end = System.currentTimeMillis();