From 2c6bc4a2805bdd6bd1694434fbb775d14095c335 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 27 Feb 2020 18:46:00 +0700 Subject: [PATCH 1/3] Add pagination system on http handler of executions/list --- execution/sort.go | 8 ++++++++ x/execution/client/rest/query.go | 32 +++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 execution/sort.go diff --git a/execution/sort.go b/execution/sort.go new file mode 100644 index 000000000..d3938b433 --- /dev/null +++ b/execution/sort.go @@ -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 } diff --git a/x/execution/client/rest/query.go b/x/execution/client/rest/query.go index 9c4bc4127..234ea20d3 100644 --- a/x/execution/client/rest/query.go +++ b/x/execution/client/rest/query.go @@ -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" ) @@ -50,6 +53,12 @@ func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return } + tags, 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) @@ -58,7 +67,28 @@ 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 + } + + sortedExecs := sort.Interface(execution.ByBlockHeight(execs)) + for _, tag := range tags { + if tag == "sort='reverse'" { + sortedExecs = sort.Reverse(sortedExecs) + } + } + sort.Sort(sortedExecs) + + start, end := client.Paginate(len(execs), page, limit, limit) + if start < 0 || end < 0 { + execs = make([]*execution.Execution, 0) + } else { + execs = execs[start:end] + } + cliCtx = cliCtx.WithHeight(height) - rest.PostProcessResponse(w, cliCtx, res) + rest.PostProcessResponse(w, cliCtx, execs) } } From 81ae3be7453a8eae128559012f53c4a74b69e596 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 27 Feb 2020 18:50:08 +0700 Subject: [PATCH 2/3] rename a variable for clarity --- x/execution/client/rest/query.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/execution/client/rest/query.go b/x/execution/client/rest/query.go index 234ea20d3..11a781d9e 100644 --- a/x/execution/client/rest/query.go +++ b/x/execution/client/rest/query.go @@ -73,13 +73,13 @@ func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return } - sortedExecs := sort.Interface(execution.ByBlockHeight(execs)) + sortFunc := sort.Interface(execution.ByBlockHeight(execs)) for _, tag := range tags { if tag == "sort='reverse'" { - sortedExecs = sort.Reverse(sortedExecs) + sortFunc = sort.Reverse(sortFunc) } } - sort.Sort(sortedExecs) + sort.Sort(sortFunc) start, end := client.Paginate(len(execs), page, limit, limit) if start < 0 || end < 0 { From 76b517f7291d812d699c4d6835bb868f21d83dd2 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Fri, 28 Feb 2020 10:33:14 +0700 Subject: [PATCH 3/3] remove tag sort=reverse in executions/list http endpoint --- x/execution/client/rest/query.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/x/execution/client/rest/query.go b/x/execution/client/rest/query.go index 11a781d9e..fb09b27e3 100644 --- a/x/execution/client/rest/query.go +++ b/x/execution/client/rest/query.go @@ -53,7 +53,7 @@ func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return } - tags, page, limit, err := rest.ParseHTTPArgs(r) + _, page, limit, err := rest.ParseHTTPArgs(r) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return @@ -73,17 +73,11 @@ func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return } - sortFunc := sort.Interface(execution.ByBlockHeight(execs)) - for _, tag := range tags { - if tag == "sort='reverse'" { - sortFunc = sort.Reverse(sortFunc) - } - } - sort.Sort(sortFunc) + sort.Sort(sort.Reverse(execution.ByBlockHeight(execs))) start, end := client.Paginate(len(execs), page, limit, limit) if start < 0 || end < 0 { - execs = make([]*execution.Execution, 0) + execs = []*execution.Execution{} } else { execs = execs[start:end] }