Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination system to http endpoint executions/list #1689

Merged
merged 3 commits into from
Feb 28, 2020
Merged
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
8 changes: 8 additions & 0 deletions execution/sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package execution

// ByBlockHeight implements sort.Interface for []*Execution based on the block height field.
type ByBlockHeight []*Execution

func (a ByBlockHeight) Len() int { return len(a) }
func (a ByBlockHeight) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByBlockHeight) Less(i, j int) bool { return a[i].BlockHeight < a[j].BlockHeight }
26 changes: 25 additions & 1 deletion x/execution/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package rest
import (
"fmt"
"net/http"
"sort"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"github.com/mesg-foundation/engine/execution"
"github.com/mesg-foundation/engine/x/execution/internal/types"
)

Expand Down Expand Up @@ -50,6 +53,12 @@ func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}

_, page, limit, err := rest.ParseHTTPArgs(r)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryListExecution)

res, height, err := cliCtx.QueryWithData(route, nil)
Expand All @@ -58,7 +67,22 @@ func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}

execs := make([]*execution.Execution, 0)
if err := cliCtx.Codec.UnmarshalJSON(res, &execs); err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

sort.Sort(sort.Reverse(execution.ByBlockHeight(execs)))

start, end := client.Paginate(len(execs), page, limit, limit)
if start < 0 || end < 0 {
execs = []*execution.Execution{}
} else {
execs = execs[start:end]
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
rest.PostProcessResponse(w, cliCtx, execs)
}
}