diff options
author | Young Gyu Park <younggyu@google.com> | 2018-04-12 17:47:04 +0900 |
---|---|---|
committer | Young Gyu Park <younggyu@google.com> | 2018-04-16 09:41:16 +0900 |
commit | a0698d6c8f7e91e5067a82a1c16884fcc534e681 (patch) | |
tree | 3f57cefdd7030e22fd1b0eea2786769f4ad2be19 /src/main/java/com/android | |
parent | d3fe7f44e6d49e3a423b423c44b1625c771fe995 (diff) | |
download | dashboard-a0698d6c8f7e91e5067a82a1c16884fcc534e681.tar.gz |
Implementing pagination
Test: go/vts-web-staging/show_plan_release?plan=vts&type=suite&page=1
Bug: 77456536
Change-Id: I0e9c7ad04c8c832f4229d77c2840be813869d293
Diffstat (limited to 'src/main/java/com/android')
-rw-r--r-- | src/main/java/com/android/vts/servlet/ShowPlanReleaseServlet.java | 52 | ||||
-rw-r--r-- | src/main/java/com/android/vts/util/Pagination.java | 108 |
2 files changed, 121 insertions, 39 deletions
diff --git a/src/main/java/com/android/vts/servlet/ShowPlanReleaseServlet.java b/src/main/java/com/android/vts/servlet/ShowPlanReleaseServlet.java index ad3c0d8..f7b3d58 100644 --- a/src/main/java/com/android/vts/servlet/ShowPlanReleaseServlet.java +++ b/src/main/java/com/android/vts/servlet/ShowPlanReleaseServlet.java @@ -38,12 +38,16 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.*; import java.util.logging.Level; +import java.util.stream.Collectors; import static com.googlecode.objectify.ObjectifyService.ofy; public class ShowPlanReleaseServlet extends BaseServlet { private static final int MAX_RUNS_PER_PAGE = 90; + /** the previous cursor string token list where to start */ + private static final LinkedHashSet<String> pageCountTokenSet = new LinkedHashSet<>(); + @Override public PageType getNavParentType() { return PageType.RELEASE; @@ -273,21 +277,51 @@ public class ShowPlanReleaseServlet extends BaseServlet { String PLAN_RELEASE_JSP = "WEB-INF/jsp/show_suite_release.jsp"; String testPlan = request.getParameter("plan"); - int page = request.getParameter("page") == null ? 1 : Integer.valueOf(request.getParameter("page")); - String nextPageToken = request.getParameter("nextPageToken") == null ? "" : request.getParameter("nextPageToken"); - - com.googlecode.objectify.cmd.Query<TestSuiteResultEntity> testSuiteResultEntityQuery = ofy().load().type(TestSuiteResultEntity.class).filter("suitePlan", testPlan).limit(105); + int page = + Objects.isNull(request.getParameter("page")) + ? 1 + : Integer.valueOf(request.getParameter("page")); + String nextPageToken = + Objects.isNull(request.getParameter("nextPageToken")) + ? "" + : request.getParameter("nextPageToken"); + + com.googlecode.objectify.cmd.Query<TestSuiteResultEntity> testSuiteResultEntityQuery = + ofy().load().type(TestSuiteResultEntity.class).filter("suitePlan", testPlan).orderKey(true); + + Pagination<TestSuiteResultEntity> testSuiteResultEntityPagination = + new Pagination( + testSuiteResultEntityQuery, + page, + Pagination.DEFAULT_PAGE_SIZE, + nextPageToken, + pageCountTokenSet); + + String nextPageTokenPagination = testSuiteResultEntityPagination.getNextPageCountToken(); + if (nextPageTokenPagination == "") { - Pagination<TestSuiteResultEntity> testSuiteResultEntityPagination = new Pagination(testSuiteResultEntityQuery, page, Pagination.DEFAULT_PAGE_SIZE, nextPageToken); + } else { + this.pageCountTokenSet.add(nextPageTokenPagination); + } + logger.log(Level.INFO, "pageCountTokenSet => " + pageCountTokenSet); logger.log(Level.INFO, "list => " + testSuiteResultEntityPagination.getList()); - logger.log(Level.INFO, "next page count token => " + testSuiteResultEntityPagination.getNextPageCountToken()); - logger.log(Level.INFO, "page min range => " + testSuiteResultEntityPagination.getMinPageRange()); - logger.log(Level.INFO, "page max range => " + testSuiteResultEntityPagination.getMaxPageRange()); + logger.log( + Level.INFO, + "next page count token => " + + testSuiteResultEntityPagination.getNextPageCountToken()); + logger.log( + Level.INFO, + "page min range => " + testSuiteResultEntityPagination.getMinPageRange()); + logger.log( + Level.INFO, + "page max range => " + testSuiteResultEntityPagination.getMaxPageRange()); logger.log(Level.INFO, "page size => " + testSuiteResultEntityPagination.getPageSize()); logger.log(Level.INFO, "total count => " + testSuiteResultEntityPagination.getTotalCount()); - request.setAttribute("plan", request.getParameter("plan")); + request.setAttribute("page", page); + request.setAttribute("testType", "suite"); + request.setAttribute("plan", testPlan); request.setAttribute("testSuiteResultEntityPagination", testSuiteResultEntityPagination); RequestDispatcher dispatcher = request.getRequestDispatcher(PLAN_RELEASE_JSP); return dispatcher; diff --git a/src/main/java/com/android/vts/util/Pagination.java b/src/main/java/com/android/vts/util/Pagination.java index c8bec0f..6223c35 100644 --- a/src/main/java/com/android/vts/util/Pagination.java +++ b/src/main/java/com/android/vts/util/Pagination.java @@ -22,7 +22,9 @@ import com.googlecode.objectify.cmd.Query; import java.util.ArrayList; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; +import java.util.stream.Collectors; /** Helper class for pagination. */ public class Pagination<T> implements Iterable<T> { @@ -41,9 +43,18 @@ public class Pagination<T> implements Iterable<T> { /** the total number of found entities */ private int totalCount; - /** the cursor string token where to start */ + /** the previous cursor string token where to start */ + private String previousPageCountToken = ""; + + /** the previous cursor string token where to start */ + private String currentPageCountToken = ""; + + /** the next cursor string token where to start */ private String nextPageCountToken = ""; + /** the previous cursor string token list where to start */ + private LinkedHashSet<String> pageCountTokenSet; + /** the maximum number of pages */ private int maxPages; @@ -57,33 +68,53 @@ public class Pagination<T> implements Iterable<T> { this.totalCount = totalCount; } - public Pagination(Query<T> query, int page, int pageSize, String nextPageToken) { + public Pagination( + Query<T> query, + int page, + int pageSize, + String startPageToken, + LinkedHashSet<String> pageCountTokenSet) { this.page = page; this.pageSize = pageSize; + this.pageCountTokenSet = pageCountTokenSet; + this.currentPageCountToken = startPageToken; + + List<String> pageCountTokenList = + this.pageCountTokenSet.stream().collect(Collectors.toList()); + if (pageCountTokenList.size() > 1) { + int foundIndex = pageCountTokenList.indexOf(startPageToken); + if (foundIndex <= 0) { + this.previousPageCountToken = ""; + } else { + this.previousPageCountToken = pageCountTokenList.get(foundIndex - 1); + } + } - int limitValue = pageSize * DEFAULT_PAGE_WINDOW + pageSize; + int limitValue = pageSize * DEFAULT_PAGE_WINDOW + pageSize / 2; query = query.limit(limitValue); - if (nextPageToken.equals("")) { + if (startPageToken.equals("")) { this.totalCount = query.count(); } else { - query = query.startAt(Cursor.fromWebSafeString(nextPageToken)); + query = query.startAt(Cursor.fromWebSafeString(startPageToken)); this.totalCount = query.count(); } - this.maxPages = this.totalCount / this.pageSize + (this.totalCount % this.pageSize == 0 ? 0 : 1); + this.maxPages = + this.totalCount / this.pageSize + (this.totalCount % this.pageSize == 0 ? 0 : 1); + + int iteratorIndex = 0; + int startIndex = (page % pageSize == 0 ? 1 : page % pageSize - 1) * pageSize; QueryResultIterator<T> resultIterator = query.iterator(); - int iteratorIndex = 1; - int startIndex = page / pageSize * pageSize; while (resultIterator.hasNext()) { if (startIndex <= iteratorIndex && iteratorIndex < startIndex + this.pageSize) this.list.add(resultIterator.next()); - else - resultIterator.next(); + else resultIterator.next(); + if (iteratorIndex == DEFAULT_PAGE_SIZE * this.pageSize) { + this.nextPageCountToken = resultIterator.getCursor().toWebSafeString(); + } iteratorIndex++; } - - this.nextPageCountToken = resultIterator.getCursor().toWebSafeString(); } public Iterator<T> iterator() { @@ -93,7 +124,7 @@ public class Pagination<T> implements Iterable<T> { /** * Gets the total number of objects. * - * @return the total number of objects as an int + * @return the total number of objects as an int */ public int getTotalCount() { return totalCount; @@ -102,7 +133,7 @@ public class Pagination<T> implements Iterable<T> { /** * Gets the number of page window. * - * @return the number of page window as an int + * @return the number of page window as an int */ public int getPageSize() { return pageSize; @@ -111,7 +142,7 @@ public class Pagination<T> implements Iterable<T> { /** * Gets the maximum number of pages. * - * @return the maximum number of pages as an int + * @return the maximum number of pages as an int */ public int getMaxPages() { return this.maxPages; @@ -120,7 +151,7 @@ public class Pagination<T> implements Iterable<T> { /** * Gets the page. * - * @return the page as an int + * @return the page as an int */ public int getPage() { return page; @@ -129,49 +160,66 @@ public class Pagination<T> implements Iterable<T> { /** * Gets the minimum page in the window. * - * @return the page number + * @return the page number */ public int getMinPageRange() { - if (this.getPage() < DEFAULT_PAGE_WINDOW) { + if (this.getPage() <= this.getPageSize()) { return 1; } else { - return this.getPage() / DEFAULT_PAGE_WINDOW * DEFAULT_PAGE_WINDOW; + if ((this.getPage() % this.getPageSize()) == 0) { + return (this.getPage() / this.getPageSize() - 1) * this.getPageSize() + 1; + } else { + return this.getPage() / this.getPageSize() * this.getPageSize() + 1; + } } } /** * Gets the maximum page in the window. * - * @return the page number + * @return the page number */ public int getMaxPageRange() { - if (this.getPage() < DEFAULT_PAGE_WINDOW) { - return DEFAULT_PAGE_WINDOW; + if (this.getMaxPages() > this.getPageSize()) { + return getMinPageRange() + this.getPageSize() - 1; } else { - if (this.getMaxPages() > DEFAULT_PAGE_WINDOW) { - return this.getPage() / DEFAULT_PAGE_WINDOW * DEFAULT_PAGE_WINDOW + this.pageSize; - } else { - return this.getMinPageRange() + this.getMaxPages(); - } + return this.getMinPageRange() + this.getMaxPages() - 1; } } /** * Gets the subset of the list for the current page. * - * @return a List + * @return a List */ public List<T> getList() { return this.list; } /** + * Gets the cursor token for the previous page starting point. + * + * @return a string of cursor of previous starting point + */ + public String getPreviousPageCountToken() { + return this.previousPageCountToken; + } + + /** + * Gets the cursor token for the current page starting point. + * + * @return a string of cursor of current starting point + */ + public String getCurrentPageCountToken() { + return this.currentPageCountToken; + } + + /** * Gets the cursor token for the next page starting point. * - * @return a string of cursor of next starting point + * @return a string of cursor of next starting point */ public String getNextPageCountToken() { return this.nextPageCountToken; } - } |