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

Replace page-based pagination with cursor-based pagination in /ras/runs #627

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/JsonError'
$ref: '#/components/schemas/APIError'
examples:
genericerror:
value:
Expand Down Expand Up @@ -1164,16 +1164,6 @@ paths:
schema:
type: string
example: dev.galasa.inttests.simbank.local.mvp.SimBankLocalJava11UbuntuMvp
- name: page
in: query
description: |
Deprecated (since 0.37.0) - Use the 'cursor' query parameter instead.
Causes a specific page in the available results to be returned.
The first page is page 1.
If omitted, then page 1 is returned.
schema:
type: integer
example: 2
- name: size
in: query
description: |
Expand All @@ -1200,16 +1190,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 @@ -2084,12 +2064,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 @@ -81,10 +79,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 @@ -101,27 +97,16 @@ private String retrieveResults(RasQueryParameters queryParams) throws InternalSe
String responseJson = null;
try {
if (runIds != null && runIds.size() > 0) {
runs = getRunsByIds(runIds);
} 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);
runs = sortResults(getRunsByIds(runIds), queryParams, sortValue);
responseJson = buildResponseBody(runs, pageSize, null);
} else {
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 @@ -178,48 +163,23 @@ private List<IRasSearchCriteria> getCriteria(RasQueryParameters queryParams) thr
return criteria ;
}

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

//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, int pageSize) throws ResultArchiveStoreException {

//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 @@ -271,37 +231,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
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