Skip to content

Commit

Permalink
Make PaginatedResponse fields optional, compute page count seperately
Browse files Browse the repository at this point in the history
  • Loading branch information
kwm4385 committed Aug 18, 2016
1 parent e1c25c6 commit d20640e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
@@ -1,30 +1,32 @@
package com.hubspot.singularity;

import com.google.common.base.Optional;

import java.util.List;

public class SingularityPaginatedResponse<Q> {

private final int dataCount;
private final int pageCount;
private final int page;
private final Optional<Integer> dataCount;
private final Optional<Integer> pageCount;
private final Optional<Integer> page;
private final List<Q> data;

public SingularityPaginatedResponse(final int dataCount, final int pageCount, final int page, final List<Q> data) {
public SingularityPaginatedResponse(final Optional<Integer> dataCount, final Optional<Integer> pageCount, final Optional<Integer> page, final List<Q> data) {
this.dataCount = dataCount;
this.pageCount = pageCount;
this.page = page;
this.data = data;
}

public int getDataCount() {
public Optional<Integer> getDataCount() {
return dataCount;
}

public int getPageCount() {
public Optional<Integer> getPageCount() {
return pageCount;
}

public int getPage() {
public Optional<Integer> getPage() {
return page;
}

Expand Down
Expand Up @@ -93,6 +93,13 @@ private Integer getLimitStart(Integer limitCount, Integer pageParam) {
return limitCount * (pageParam - 1);
}

private Optional<Integer> getPageCount(Optional<Integer> dataCount, Integer count) {
if (!dataCount.isPresent()) {
return Optional.absent();
}
return Optional.fromNullable((int) Math.ceil((double) dataCount.get() / count));
}

@GET
@Path("/request/{requestId}/tasks/active")
@ApiOperation("Retrieve the history for all active tasks of a specific request.")
Expand Down Expand Up @@ -182,11 +189,12 @@ public SingularityPaginatedResponse<SingularityTaskIdHistory> getTaskHistoryForR
@ApiParam("Which page of items to view") @QueryParam("page") Integer page) {
authorizationHelper.checkForAuthorizationByRequestId(requestId, user, SingularityAuthorizationScope.READ);

final int dataCount = taskHistoryHelper.getBlendedHistoryCount(new SingularityTaskHistoryQuery(Optional.of(requestId), deployId, host, lastTaskStatus, startedBefore, startedAfter, orderDirection)).get();
final Optional dataCount = taskHistoryHelper.getBlendedHistoryCount(new SingularityTaskHistoryQuery(Optional.of(requestId), deployId, host, lastTaskStatus, startedBefore, startedAfter, orderDirection));
final int limitCount = getLimitCount(count);
final List<SingularityTaskIdHistory> data = this.getTaskHistoryForRequest(requestId, deployId, host, lastTaskStatus, startedAfter, startedBefore, orderDirection, count, page);
final Optional<Integer> pageCount = getPageCount(dataCount, limitCount);

return new SingularityPaginatedResponse<>(dataCount, (int) Math.ceil((double) dataCount / limitCount), page, data);
return new SingularityPaginatedResponse<>(dataCount, pageCount, Optional.fromNullable(page), data);
}

@GET
Expand Down Expand Up @@ -246,11 +254,12 @@ public SingularityPaginatedResponse<SingularityDeployHistory> getDeploysCount(
@ApiParam("Which page of items to view") @QueryParam("page") Integer page) {
authorizationHelper.checkForAuthorizationByRequestId(requestId, user, SingularityAuthorizationScope.READ);

final int dataCount = deployHistoryHelper.getBlendedHistoryCount(requestId).get();
final Optional dataCount = deployHistoryHelper.getBlendedHistoryCount(requestId);
final int limitCount = getLimitCount(count);
final List<SingularityDeployHistory> data = this.getDeploys(requestId, count, page);
final Optional<Integer> pageCount = getPageCount(dataCount, limitCount);

return new SingularityPaginatedResponse<>(dataCount, (int) Math.ceil((double) dataCount / limitCount), page, data);
return new SingularityPaginatedResponse<>(dataCount, pageCount, Optional.fromNullable(page), data);
}

@GET
Expand Down

0 comments on commit d20640e

Please sign in to comment.