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

Support query txs' TotalCount in GET /txs #3942

Closed
wangjj9219 opened this issue Mar 20, 2019 · 10 comments · Fixed by #4214
Closed

Support query txs' TotalCount in GET /txs #3942

wangjj9219 opened this issue Mar 20, 2019 · 10 comments · Fixed by #4214

Comments

@wangjj9219
Copy link
Contributor

Use GET /txs to query txs that match the tags, it just returns the array of txs.

I see the return of following function contains the count of all target txs, why don't put the TotalCount field in the response, it will help to calculate pagination:

// client/tx/search.go

res, err := node.TxSearch(query, prove, page, limit)
@alexanderbez
Copy link
Contributor

Agreed the result should include a total in the response (will need to update FormatTxResults). But to get the total, is this something we can do easily? Is this data available to us? Do we have to break any TM interfaces?

@alexanderbez
Copy link
Contributor

/cc @jackzampolin @alessio

@jackzampolin
Copy link
Member

What about a /txs/count endpoint?

@alexanderbez
Copy link
Contributor

Ohhh wait, ResultTxSearch already includes the total count! We just need to update FormatTxResults here 👍

@yangyanqing
Copy link
Contributor

We have got the solution already. @jackzampolin @alexanderbez
Please commit the PR directly. @wangjj9219

@yangyanqing
Copy link
Contributor

The standard result of REST paging query should contain several fields:

  • limit - The maximum count in one page.
  • number - The count in current page.
  • page - Current page offset in all page.
  • total_count - The count of all record.
  • data - array of record.

So, I want to add a type to hold these information.

type SearchTxsPageableResult struct {
	TotalCount int          `json:"total_count"`
	Number     int          `json:"number"`
	Page       int          `json:"page"`
	Limit      int          `json:"limit"`
	Data       []TxResponse `json:"data"`
}

func SearchTxs(cliCtx context.CLIContext, cdc *codec.Codec, 
	tags []string, page, limit int) (*sdk.SearchTxsPageableResult, error) {
	if len(tags) == 0 {
		return nil, errors.New("must declare at least one tag to search")
	}

	if page <= 0 {
		return nil, errors.New("page must greater than 0")
	}

	if limit <= 0 {
		return nil, errors.New("limit must greater than 0")
	}

	// XXX: implement ANY
	query := strings.Join(tags, " AND ")

	// get the node
	node, err := cliCtx.GetNode()
	if err != nil {
		return nil, err
	}

	prove := !cliCtx.TrustNode

	res, err := node.TxSearch(query, prove, page, limit)
	if err != nil {
		return nil, err
	}

	if prove {
		for _, tx := range res.Txs {
			err := ValidateTxResult(cliCtx, tx)
			if err != nil {
				return nil, err
			}
		}
	}

	info, err := FormatTxResults(cdc, res.Txs)
	if err != nil {
		return nil, err
	}

	result := sdk.NewSearchTxsPageableResult(
		res.TotalCount, len(info), page, limit, info)

	return &result, nil
}

@alexanderbez @jackzampolin

@alessio
Copy link
Contributor

alessio commented Mar 28, 2019

@yangyanqing I agree, though I'd prefer count instead of number and total instead of total_count. Furthermore data should be either items or results. Just my personal preference anyway, so not a big one.

@yangyanqing
Copy link
Contributor

Parameter in spring-boot named such as totalPages, totalElements, numberOfElements, size, pageNumber.
Naming is the biggest one. ╮(╯▽╰)╭

Thanks for your suggestion ! @alessio

@alessio
Copy link
Contributor

alessio commented Mar 28, 2019

Oh yeah, total pages is quite important. Can we get that too?

@yangyanqing
Copy link
Contributor

Great idea ! I had commit it. @alessio

type SearchTxsResult struct {
	TotalCount int          `json:"total_count"`
	Count      int          `json:"count"`
	PageNumber int          `json:"page_number"`
	PageCount  int          `json:"page_count"`
	Limit      int          `json:"limit"`
	Txs        []TxResponse `json:"txs"`
}

func NewSearchTxsResult(totalCount, count, page, limit int, txs []TxResponse) SearchTxsResult {
	return SearchTxsResult{
		TotalCount: totalCount,
		Count:      count,
		PageNumber: page,
		PageCount:  int(math.Ceil(float64(totalCount) / float64(limit))),
		Limit:      limit,
		Txs:        txs,
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants