Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.

Commit

Permalink
Replace page-based pagination with cursor-based pagination in /ras/runs
Browse files Browse the repository at this point in the history
Signed-off-by: Eamonn Mansour <[email protected]>
  • Loading branch information
eamansour committed Aug 21, 2024
1 parent b101220 commit 6ef4709
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1195,16 +1195,6 @@ paths:
schema:
type: string
example: U1578

# Temporary feature flag to enable cursor-based pagination
- name: includeCursor
in: query
description: |
A boolean flag to enable cursor-based pagination and return the next page cursor
in the response. If omitted, it will default to false.
schema:
type: string
example: 'true'
- name: cursor
in: query
description: |
Expand Down Expand Up @@ -2043,12 +2033,8 @@ components:
RunResults:
type: object
properties:
pageNumber:
type: integer
pageSize:
type: integer
numPages:
type: integer
amountOfRuns:
type: integer
nextCursor:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ public String getPageCursor() throws InternalServletException {
return generalQueryParams.getSingleString("cursor", null);
}

public boolean getIncludeCursor() throws InternalServletException {
return generalQueryParams.getSingleBoolean("includeCursor", false);
}

public RasSortField getSortValue() throws InternalServletException {
return getSortValue(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*/
package dev.galasa.framework.api.ras.internal.routes;

import org.apache.commons.collections4.ListUtils;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

Expand Down Expand Up @@ -74,10 +72,8 @@ public HttpServletResponse handleGetRequest(String pathInfo, QueryParameters gen

private String retrieveResults(RasQueryParameters queryParams) throws InternalServletException {

int pageNum = queryParams.getPageNumber();
int pageSize = queryParams.getPageSize();

boolean includeCursor = queryParams.getIncludeCursor();
String pageCursor = queryParams.getPageCursor();

List<RasRunResult> runs = new ArrayList<>();
Expand All @@ -94,27 +90,16 @@ private String retrieveResults(RasQueryParameters queryParams) throws InternalSe
String responseJson = null;
try {
if (runIds != null && runIds.size() > 0) {
runs = getRunsByIds(runIds);
runs = sortResults(getRunsByIds(runIds), queryParams, sortValue);
responseJson = buildResponseBody(runs, pageSize, null);
} else {
List<IRasSearchCriteria> criteria = getCriteria(queryParams);
if (includeCursor || pageCursor != null) {
runsPage = getRunsPage(pageCursor, pageSize, formatSortField(sortValue), criteria);
} else {
runs = getRuns(criteria);
}
}

if (runsPage == null) {
runs = sortResults(runs, queryParams, sortValue);
responseJson = buildResponseBody(runs, pageNum, pageSize);
} else {
responseJson = buildResponseBody(runsPage);
runsPage = getRunsPage(pageCursor, pageSize, formatSortField(sortValue), getCriteria(queryParams));
responseJson = buildResponseBody(runsPage, pageSize);
}
} catch (ResultArchiveStoreException e) {
ServletError error = new ServletError(GAL5003_ERROR_RETRIEVING_RUNS);
throw new InternalServletException(error, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
}

return responseJson;
}

Expand Down Expand Up @@ -168,47 +153,23 @@ private List<IRasSearchCriteria> getCriteria(RasQueryParameters queryParams) thr
return criteria ;
}

private String buildResponseBody(List<RasRunResult> runs, int pageNum, int pageSize) throws InternalServletException {

//Splits up the pages based on the page size
List<List<RasRunResult>> paginatedResults = ListUtils.partition(runs, pageSize);

//Building the object to be returned by the API and splitting
JsonObject runsPage = null;
try {
if ((pageNum == 1) && paginatedResults.isEmpty()) {
// No results at all, so return one page saying that.
runsPage = pageToJson(runs, runs.size(), 1, pageSize,1);
} else {
runsPage = pageToJson(
paginatedResults.get(pageNum - 1),
runs.size(),
pageNum,
pageSize,
paginatedResults.size()
);
}
} catch (IndexOutOfBoundsException e) {
ServletError error = new ServletError(GAL5004_ERROR_RETRIEVING_PAGE);
throw new InternalServletException(error, HttpServletResponse.SC_BAD_REQUEST, e);
}
return gson.toJson(runsPage);
}

private String buildResponseBody(RasRunResultPage runsPage) throws ResultArchiveStoreException {
private String buildResponseBody(List<RasRunResult> runs, int pageSize, String nextPageCursor) {
JsonObject pageJson = new JsonObject();

//Building the object to be returned by the API and splitting
JsonObject pageJson = new JsonObject();

List<RasRunResult> runs = convertRunsToRunResults(runsPage.getRuns());
JsonElement tree = gson.toJsonTree(runs);
pageJson.addProperty("pageSize", pageSize);
pageJson.addProperty("amountOfRuns", runs.size());
pageJson.addProperty("nextCursor", runsPage.getNextCursor());
pageJson.addProperty("nextCursor", nextPageCursor);
pageJson.add("runs", tree);

return gson.toJson(pageJson);
}

private String buildResponseBody(RasRunResultPage runsPage, int pageSize) throws ResultArchiveStoreException {
List<RasRunResult> runs = convertRunsToRunResults(runsPage.getRuns());
return buildResponseBody(runs, pageSize, runsPage.getNextCursor());
}

private List<IRasSearchCriteria> getCriteria(
String requestor,
String testName,
Expand Down Expand Up @@ -260,37 +221,6 @@ private List<IRasSearchCriteria> getCriteria(
return critList;
}

private JsonObject pageToJson(List<RasRunResult> resultsInPage, int totalRuns, int pageNum, int pageSize, int numPages) {
JsonObject obj = new JsonObject();

obj.addProperty("pageNum", pageNum);
obj.addProperty("pageSize", pageSize);
obj.addProperty("numPages", numPages);
obj.addProperty("amountOfRuns", totalRuns);

JsonElement tree = gson.toJsonTree(resultsInPage);

obj.add("runs", tree);
return obj;
}

private List<RasRunResult> getRuns(List<IRasSearchCriteria> critList) throws ResultArchiveStoreException, InternalServletException {

IRasSearchCriteria[] criteria = new IRasSearchCriteria[critList.size()];

critList.toArray(criteria);

// Collect all the runs from all the RAS stores into a single list
List<IRunResult> runs = new ArrayList<>();
for (IResultArchiveStoreDirectoryService directoryService : getFramework().getResultArchiveStore().getDirectoryServices()) {
runs.addAll(directoryService.getRuns(criteria));
}

List<RasRunResult> runResults = convertRunsToRunResults(runs);

return runResults;
}

private RasRunResultPage getRunsPage(String pageCursor, int maxResults, RasSortField primarySort, List<IRasSearchCriteria> critList) throws ResultArchiveStoreException {

IRasSearchCriteria[] criteria = new IRasSearchCriteria[critList.size()];
Expand Down Expand Up @@ -413,13 +343,15 @@ private Comparator<RasRunResult> buildRunsComparator(RasQueryParameters queryPar
runsComparator = new SortByResult();
}

// Reverse the comparator if the direction is "desc"
if (runsComparator != null && !queryParams.isAscending(sortField)) {
runsComparator = runsComparator.reversed();
}
if (runsComparator != null) {
// Reverse the comparator if the direction is "desc"
if (!queryParams.isAscending(sortField)) {
runsComparator = runsComparator.reversed();
}

// Ensure null values appear last
runsComparator = Comparator.nullsLast(runsComparator);
// Ensure null values appear last
runsComparator = Comparator.nullsLast(runsComparator);
}

return runsComparator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ private void applySearchCriteria( IRasSearchCriteria searchCriteria) throws Resu

@Override
public @NotNull RasRunResultPage getRunsPage(int maxResults, RasSortField primarySort, String pageCursor, @NotNull IRasSearchCriteria... searchCriterias) throws ResultArchiveStoreException {
if (pageCursor != null && pageCursor.equals("error")) {
throw new ResultArchiveStoreException("simulating a RAS error!");
}
return new RasRunResultPage(getRuns(searchCriterias), nextCursor);
}

Expand Down
Loading

0 comments on commit 6ef4709

Please sign in to comment.