Skip to content

Commit

Permalink
Use cursor-based pagination when getting runs from the API server
Browse files Browse the repository at this point in the history
Signed-off-by: Eamonn Mansour <47121388+eamansour@users.noreply.github.com>
  • Loading branch information
eamansour committed Aug 21, 2024
1 parent 44e2194 commit 37cf313
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 128 deletions.
7 changes: 1 addition & 6 deletions pkg/runs/runsDownload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"net/http"
"net/http/httptest"
"os"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -237,10 +236,7 @@ func WriteMockRasRunsResponse(
writer.Header().Set("Content-Type", "application/json")

values := req.URL.Query()
pageRequestedStr := values.Get("page")
runNameQueryParameter := values.Get("runname")
pageRequested, _ := strconv.Atoi(pageRequestedStr)
assert.Equal(t, pageRequested, 1)

assert.Equal(t, runNameQueryParameter, runName)

Expand All @@ -254,9 +250,8 @@ func WriteMockRasRunsResponse(

writer.Write([]byte(fmt.Sprintf(`
{
"pageNumber": 1,
"nextCursor": "",
"pageSize": 1,
"numPages": 1,
"amountOfRuns": %d,
"runs":[ %s ]
}`, len(runResultStrings), combinedRunResultStrings)))
Expand Down
12 changes: 8 additions & 4 deletions pkg/runs/runsGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ func GetRunsFromRestApi(
var pageNumberWanted int32 = 1
gotAllResults := false
var restApiVersion string
var pageCursor string = ""

restApiVersion, err = embedded.GetGalasactlRestApiVersion()

Expand All @@ -232,7 +233,7 @@ func GetRunsFromRestApi(
var runData *galasaapi.RunResults
var httpResponse *http.Response
log.Printf("Requesting page '%d' ", pageNumberWanted)
apicall := apiClient.ResultArchiveStoreAPIApi.GetRasSearchRuns(context).ClientApiVersion(restApiVersion)
apicall := apiClient.ResultArchiveStoreAPIApi.GetRasSearchRuns(context).ClientApiVersion(restApiVersion).IncludeCursor("true")
if fromAgeMins != 0 {
apicall = apicall.From(fromTime)
}
Expand All @@ -251,7 +252,9 @@ func GetRunsFromRestApi(
if shouldGetActive {
apicall = apicall.Status(activeStatusNames)
}
apicall = apicall.Page(pageNumberWanted)
if pageCursor != "" {
apicall = apicall.Cursor(pageCursor)
}
apicall = apicall.Sort("to:desc")
runData, httpResponse, err = apicall.Execute()

Expand All @@ -270,10 +273,11 @@ func GetRunsFromRestApi(
// Note: The ... syntax means 'all of the array', so they all get appended at once.
results = append(results, runsOnThisPage...)

// Have we processed the last page ?
if pageNumberWanted == runData.GetNumPages() {
// If the page cursor is the same, then we've gone through all pages
if pageCursor == runData.GetNextCursor() || runData.GetNextCursor() == "" {
gotAllResults = true
} else {
pageCursor = runData.GetNextCursor()
pageNumberWanted++
}
}
Expand Down
Loading

0 comments on commit 37cf313

Please sign in to comment.