Unify paging in api.worker
This commit is contained in:
parent
bebddbcd11
commit
0147df957c
16
api/types.go
16
api/types.go
@ -77,7 +77,14 @@ type Tx struct {
|
|||||||
WithSpends bool `json:"withSpends,omitempty"`
|
WithSpends bool `json:"withSpends,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Paging struct {
|
||||||
|
Page int `json:"page"`
|
||||||
|
TotalPages int `json:"totalPages"`
|
||||||
|
ItemsOnPage int `json:"itemsOnPage"`
|
||||||
|
}
|
||||||
|
|
||||||
type Address struct {
|
type Address struct {
|
||||||
|
Paging
|
||||||
AddrStr string `json:"addrStr"`
|
AddrStr string `json:"addrStr"`
|
||||||
Balance string `json:"balance"`
|
Balance string `json:"balance"`
|
||||||
TotalReceived string `json:"totalReceived"`
|
TotalReceived string `json:"totalReceived"`
|
||||||
@ -87,14 +94,9 @@ type Address struct {
|
|||||||
TxApperances int `json:"txApperances"`
|
TxApperances int `json:"txApperances"`
|
||||||
Transactions []*Tx `json:"txs,omitempty"`
|
Transactions []*Tx `json:"txs,omitempty"`
|
||||||
Txids []string `json:"transactions,omitempty"`
|
Txids []string `json:"transactions,omitempty"`
|
||||||
Page int `json:"page"`
|
|
||||||
TotalPages int `json:"totalPages"`
|
|
||||||
TxsOnPage int `json:"txsOnPage"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Blocks struct {
|
type Blocks struct {
|
||||||
Blocks []db.BlockInfo `json:"blocks"`
|
Paging
|
||||||
Page int `json:"page"`
|
Blocks []db.BlockInfo `json:"blocks"`
|
||||||
TotalPages int `json:"totalPages"`
|
|
||||||
BlocksOnPage int `json:"blocksOnPage"`
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -296,6 +296,27 @@ func (w *Worker) txFromTxAddress(txid string, ta *db.TxAddresses, bi *db.BlockIn
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func computePaging(count, page, itemsOnPage int) (Paging, int, int, int) {
|
||||||
|
from := page * itemsOnPage
|
||||||
|
totalPages := (count - 1) / itemsOnPage
|
||||||
|
if totalPages < 0 {
|
||||||
|
totalPages = 0
|
||||||
|
}
|
||||||
|
if from >= count {
|
||||||
|
page = totalPages
|
||||||
|
}
|
||||||
|
from = page * itemsOnPage
|
||||||
|
to := (page + 1) * itemsOnPage
|
||||||
|
if to > count {
|
||||||
|
to = count
|
||||||
|
}
|
||||||
|
return Paging{
|
||||||
|
ItemsOnPage: itemsOnPage,
|
||||||
|
Page: page + 1,
|
||||||
|
TotalPages: totalPages + 1,
|
||||||
|
}, from, to, page
|
||||||
|
}
|
||||||
|
|
||||||
// GetAddress computes address value and gets transactions for given address
|
// GetAddress computes address value and gets transactions for given address
|
||||||
func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids bool) (*Address, error) {
|
func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids bool) (*Address, error) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
@ -341,23 +362,7 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids b
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotatef(err, "GetBestBlock")
|
return nil, errors.Annotatef(err, "GetBestBlock")
|
||||||
}
|
}
|
||||||
// paging
|
pg, from, to, page := computePaging(len(txc), page, txsOnPage)
|
||||||
from := page * txsOnPage
|
|
||||||
totalPages := (len(txc) - 1) / txsOnPage
|
|
||||||
if totalPages < 0 {
|
|
||||||
totalPages = 0
|
|
||||||
}
|
|
||||||
if from >= len(txc) {
|
|
||||||
page = totalPages - 1
|
|
||||||
if page < 0 {
|
|
||||||
page = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
from = page * txsOnPage
|
|
||||||
to := (page + 1) * txsOnPage
|
|
||||||
if to > len(txc) {
|
|
||||||
to = len(txc)
|
|
||||||
}
|
|
||||||
var txs []*Tx
|
var txs []*Tx
|
||||||
var txids []string
|
var txids []string
|
||||||
if onlyTxids {
|
if onlyTxids {
|
||||||
@ -420,6 +425,7 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids b
|
|||||||
txs = txs[:txi]
|
txs = txs[:txi]
|
||||||
}
|
}
|
||||||
r := &Address{
|
r := &Address{
|
||||||
|
Paging: pg,
|
||||||
AddrStr: address,
|
AddrStr: address,
|
||||||
Balance: w.chainParser.AmountToDecimalString(&ba.BalanceSat),
|
Balance: w.chainParser.AmountToDecimalString(&ba.BalanceSat),
|
||||||
TotalReceived: w.chainParser.AmountToDecimalString(ba.ReceivedSat()),
|
TotalReceived: w.chainParser.AmountToDecimalString(ba.ReceivedSat()),
|
||||||
@ -429,9 +435,6 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, onlyTxids b
|
|||||||
UnconfirmedTxApperances: len(txm),
|
UnconfirmedTxApperances: len(txm),
|
||||||
Transactions: txs,
|
Transactions: txs,
|
||||||
Txids: txids,
|
Txids: txids,
|
||||||
Page: page + 1,
|
|
||||||
TotalPages: totalPages + 1,
|
|
||||||
TxsOnPage: txsOnPage,
|
|
||||||
}
|
}
|
||||||
glog.Info("GetAddress ", address, " finished in ", time.Since(start))
|
glog.Info("GetAddress ", address, " finished in ", time.Since(start))
|
||||||
return r, nil
|
return r, nil
|
||||||
@ -449,28 +452,8 @@ func (w *Worker) GetBlocks(page int, blocksOnPage int) (*Blocks, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotatef(err, "GetBestBlock")
|
return nil, errors.Annotatef(err, "GetBestBlock")
|
||||||
}
|
}
|
||||||
// paging
|
pg, from, to, page := computePaging(bestheight, page, blocksOnPage)
|
||||||
from := page * blocksOnPage
|
r := &Blocks{Paging: pg}
|
||||||
totalPages := (bestheight - 1) / blocksOnPage
|
|
||||||
if totalPages < 0 {
|
|
||||||
totalPages = 0
|
|
||||||
}
|
|
||||||
if from >= bestheight {
|
|
||||||
page = totalPages - 1
|
|
||||||
if page < 0 {
|
|
||||||
page = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
from = page * blocksOnPage
|
|
||||||
to := (page + 1) * blocksOnPage
|
|
||||||
if to > bestheight {
|
|
||||||
to = bestheight
|
|
||||||
}
|
|
||||||
r := &Blocks{
|
|
||||||
Page: page + 1,
|
|
||||||
TotalPages: totalPages + 1,
|
|
||||||
BlocksOnPage: blocksOnPage,
|
|
||||||
}
|
|
||||||
r.Blocks = make([]db.BlockInfo, to-from)
|
r.Blocks = make([]db.BlockInfo, to-from)
|
||||||
for i := from; i < to; i++ {
|
for i := from; i < to; i++ {
|
||||||
bi, err := w.db.GetBlockInfo(uint32(bestheight - i))
|
bi, err := w.db.GetBlockInfo(uint32(bestheight - i))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user